Fix forecasting brand list and filter on products received during period selected
This commit is contained in:
@@ -547,10 +547,12 @@ router.get('/forecast', async (req, res) => {
|
||||
FROM categories c
|
||||
JOIN product_categories pc ON c.id = pc.category_id
|
||||
JOIN products p ON pc.product_id = p.product_id
|
||||
LEFT JOIN product_metrics pm ON p.product_id = pm.product_id
|
||||
LEFT JOIN orders o ON p.product_id = o.product_id
|
||||
AND o.date BETWEEN ? AND ?
|
||||
AND o.canceled = false
|
||||
WHERE p.brand = ?
|
||||
AND pm.first_received_date BETWEEN ? AND ?
|
||||
GROUP BY c.id, c.name, p.brand
|
||||
),
|
||||
product_metrics AS (
|
||||
@@ -560,15 +562,18 @@ router.get('/forecast', async (req, res) => {
|
||||
p.sku,
|
||||
p.stock_quantity,
|
||||
pc.category_id,
|
||||
pm.first_received_date,
|
||||
COALESCE(SUM(o.quantity), 0) as total_sold,
|
||||
COALESCE(ROUND(AVG(o.price), 2), 0) as avg_price
|
||||
FROM products p
|
||||
JOIN product_categories pc ON p.product_id = pc.product_id
|
||||
JOIN product_metrics pm ON p.product_id = pm.product_id
|
||||
LEFT JOIN orders o ON p.product_id = o.product_id
|
||||
AND o.date BETWEEN ? AND ?
|
||||
AND o.canceled = false
|
||||
WHERE p.brand = ?
|
||||
GROUP BY p.product_id, p.title, p.sku, p.stock_quantity, pc.category_id
|
||||
AND pm.first_received_date BETWEEN ? AND ?
|
||||
GROUP BY p.product_id, p.title, p.sku, p.stock_quantity, pc.category_id, pm.first_received_date
|
||||
)
|
||||
SELECT
|
||||
cm.*,
|
||||
@@ -579,14 +584,15 @@ router.get('/forecast', async (req, res) => {
|
||||
'sku', pm.sku,
|
||||
'stock_quantity', pm.stock_quantity,
|
||||
'total_sold', pm.total_sold,
|
||||
'avg_price', pm.avg_price
|
||||
'avg_price', pm.avg_price,
|
||||
'first_received_date', DATE_FORMAT(pm.first_received_date, '%Y-%m-%d')
|
||||
)
|
||||
) as products
|
||||
FROM category_metrics cm
|
||||
JOIN product_metrics pm ON cm.category_id = pm.category_id
|
||||
GROUP BY cm.category_id, cm.category_name, cm.brand, cm.num_products, cm.avg_daily_sales, cm.total_sold, cm.avgTotalSold, cm.avg_price
|
||||
ORDER BY cm.total_sold DESC
|
||||
`, [startDate, endDate, startDate, endDate, brand, startDate, endDate, brand]);
|
||||
`, [startDate, endDate, startDate, endDate, brand, startDate, endDate, startDate, endDate, brand, startDate, endDate]);
|
||||
|
||||
res.json(results);
|
||||
} catch (error) {
|
||||
|
||||
@@ -20,12 +20,15 @@ router.get('/brands', async (req, res) => {
|
||||
console.log('Fetching brands from database...');
|
||||
|
||||
const [results] = await pool.query(`
|
||||
SELECT DISTINCT brand
|
||||
FROM products
|
||||
WHERE brand IS NOT NULL
|
||||
AND brand != ''
|
||||
AND visible = true
|
||||
ORDER BY brand
|
||||
SELECT DISTINCT p.brand
|
||||
FROM products p
|
||||
JOIN purchase_orders po ON p.product_id = po.product_id
|
||||
WHERE p.brand IS NOT NULL
|
||||
AND p.brand != ''
|
||||
AND p.visible = true
|
||||
GROUP BY p.brand
|
||||
HAVING SUM(po.cost_price * po.received) >= 500
|
||||
ORDER BY p.brand
|
||||
`);
|
||||
|
||||
console.log(`Found ${results.length} brands:`, results.slice(0, 3));
|
||||
|
||||
Reference in New Issue
Block a user