Use new categories correctly in existing components and handle category names with commas
This commit is contained in:
@@ -65,7 +65,7 @@ router.get('/profit', async (req, res) => {
|
||||
// Get profit margins by category
|
||||
const [byCategory] = await pool.query(`
|
||||
SELECT
|
||||
COALESCE(p.categories, 'Uncategorized') as category,
|
||||
c.name as category,
|
||||
ROUND(
|
||||
(SUM(o.price * o.quantity - p.cost_price * o.quantity) /
|
||||
NULLIF(SUM(o.price * o.quantity), 0)) * 100, 1
|
||||
@@ -74,8 +74,10 @@ router.get('/profit', async (req, res) => {
|
||||
SUM(p.cost_price * o.quantity) as cost
|
||||
FROM products p
|
||||
LEFT JOIN orders o ON p.product_id = o.product_id
|
||||
JOIN product_categories pc ON p.product_id = pc.product_id
|
||||
JOIN categories c ON pc.category_id = c.id
|
||||
WHERE o.date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
|
||||
GROUP BY p.categories
|
||||
GROUP BY c.name
|
||||
ORDER BY profitMargin DESC
|
||||
LIMIT 10
|
||||
`);
|
||||
@@ -190,14 +192,16 @@ router.get('/stock', async (req, res) => {
|
||||
// Get turnover by category
|
||||
const [turnoverByCategory] = await pool.query(`
|
||||
SELECT
|
||||
COALESCE(p.categories, 'Uncategorized') as category,
|
||||
c.name as category,
|
||||
ROUND(SUM(o.quantity) / NULLIF(AVG(p.stock_quantity), 0), 1) as turnoverRate,
|
||||
ROUND(AVG(p.stock_quantity), 0) as averageStock,
|
||||
SUM(o.quantity) as totalSales
|
||||
FROM products p
|
||||
LEFT JOIN orders o ON p.product_id = o.product_id
|
||||
JOIN product_categories pc ON p.product_id = pc.product_id
|
||||
JOIN categories c ON pc.category_id = c.id
|
||||
WHERE o.date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
|
||||
GROUP BY p.categories
|
||||
GROUP BY c.name
|
||||
HAVING turnoverRate > 0
|
||||
ORDER BY turnoverRate DESC
|
||||
LIMIT 10
|
||||
@@ -328,7 +332,7 @@ router.get('/categories', async (req, res) => {
|
||||
// Get category performance metrics
|
||||
const [performance] = await pool.query(`
|
||||
SELECT
|
||||
COALESCE(p.categories, 'Uncategorized') as category,
|
||||
c.name as category,
|
||||
SUM(o.price * o.quantity) as revenue,
|
||||
SUM(o.price * o.quantity - p.cost_price * o.quantity) as profit,
|
||||
ROUND(
|
||||
@@ -348,8 +352,10 @@ router.get('/categories', async (req, res) => {
|
||||
COUNT(DISTINCT p.product_id) as productCount
|
||||
FROM products p
|
||||
LEFT JOIN orders o ON p.product_id = o.product_id
|
||||
JOIN product_categories pc ON p.product_id = pc.product_id
|
||||
JOIN categories c ON pc.category_id = c.id
|
||||
WHERE o.date >= DATE_SUB(CURDATE(), INTERVAL 60 DAY)
|
||||
GROUP BY p.categories
|
||||
GROUP BY c.name
|
||||
HAVING revenue > 0
|
||||
ORDER BY revenue DESC
|
||||
LIMIT 10
|
||||
@@ -358,12 +364,14 @@ router.get('/categories', async (req, res) => {
|
||||
// Get category revenue distribution
|
||||
const [distribution] = await pool.query(`
|
||||
SELECT
|
||||
COALESCE(p.categories, 'Uncategorized') as category,
|
||||
c.name as category,
|
||||
SUM(o.price * o.quantity) as value
|
||||
FROM products p
|
||||
LEFT JOIN orders o ON p.product_id = o.product_id
|
||||
JOIN product_categories pc ON p.product_id = pc.product_id
|
||||
JOIN categories c ON pc.category_id = c.id
|
||||
WHERE o.date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
|
||||
GROUP BY p.categories
|
||||
GROUP BY c.name
|
||||
HAVING value > 0
|
||||
ORDER BY value DESC
|
||||
LIMIT 6
|
||||
|
||||
Reference in New Issue
Block a user