Use new categories correctly in existing components and handle category names with commas
This commit is contained in:
@@ -123,11 +123,13 @@ router.get('/category-stats', async (req, res) => {
|
||||
try {
|
||||
const [rows] = await pool.query(`
|
||||
SELECT
|
||||
categories,
|
||||
COUNT(*) as count
|
||||
FROM products
|
||||
WHERE visible = true
|
||||
GROUP BY categories
|
||||
c.name as category,
|
||||
COUNT(DISTINCT pc.product_id) as count
|
||||
FROM categories c
|
||||
LEFT JOIN product_categories pc ON c.id = pc.category_id
|
||||
LEFT JOIN products p ON pc.product_id = p.product_id
|
||||
WHERE p.visible = true
|
||||
GROUP BY c.name
|
||||
ORDER BY count DESC
|
||||
LIMIT 10
|
||||
`);
|
||||
@@ -164,13 +166,15 @@ router.get('/sales-by-category', async (req, res) => {
|
||||
try {
|
||||
const [rows] = await pool.query(`
|
||||
SELECT
|
||||
p.categories as category,
|
||||
c.name as category,
|
||||
SUM(o.price * o.quantity) as total
|
||||
FROM orders o
|
||||
JOIN products p ON o.product_id = p.product_id
|
||||
JOIN product_categories pc ON p.product_id = pc.product_id
|
||||
JOIN categories c ON pc.category_id = c.id
|
||||
WHERE o.canceled = false
|
||||
AND DATE(o.date) >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
|
||||
GROUP BY p.categories
|
||||
GROUP BY c.name
|
||||
ORDER BY total DESC
|
||||
LIMIT 6
|
||||
`);
|
||||
@@ -266,14 +270,16 @@ router.get('/inventory-metrics', async (req, res) => {
|
||||
// Get stock levels by category
|
||||
const [stockLevels] = await pool.query(`
|
||||
SELECT
|
||||
categories as category,
|
||||
c.name as category,
|
||||
SUM(CASE WHEN stock_quantity > 5 THEN 1 ELSE 0 END) as inStock,
|
||||
SUM(CASE WHEN stock_quantity > 0 AND stock_quantity <= 5 THEN 1 ELSE 0 END) as lowStock,
|
||||
SUM(CASE WHEN stock_quantity = 0 THEN 1 ELSE 0 END) as outOfStock
|
||||
FROM products
|
||||
FROM products p
|
||||
JOIN product_categories pc ON p.product_id = pc.product_id
|
||||
JOIN categories c ON pc.category_id = c.id
|
||||
WHERE visible = true
|
||||
GROUP BY categories
|
||||
ORDER BY categories ASC
|
||||
GROUP BY c.name
|
||||
ORDER BY c.name ASC
|
||||
`);
|
||||
|
||||
// Get top vendors with product counts and average stock
|
||||
@@ -296,21 +302,25 @@ router.get('/inventory-metrics', async (req, res) => {
|
||||
const [stockTurnover] = await pool.query(`
|
||||
WITH CategorySales AS (
|
||||
SELECT
|
||||
p.categories as category,
|
||||
c.name as category,
|
||||
SUM(o.quantity) as units_sold
|
||||
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.canceled = false
|
||||
AND DATE(o.date) >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
|
||||
GROUP BY p.categories
|
||||
GROUP BY c.name
|
||||
),
|
||||
CategoryStock AS (
|
||||
SELECT
|
||||
categories as category,
|
||||
AVG(stock_quantity) as avg_stock
|
||||
FROM products
|
||||
WHERE visible = true
|
||||
GROUP BY categories
|
||||
c.name as category,
|
||||
AVG(p.stock_quantity) as avg_stock
|
||||
FROM products p
|
||||
JOIN product_categories pc ON p.product_id = pc.product_id
|
||||
JOIN categories c ON pc.category_id = c.id
|
||||
WHERE p.visible = true
|
||||
GROUP BY c.name
|
||||
)
|
||||
SELECT
|
||||
cs.category,
|
||||
|
||||
Reference in New Issue
Block a user