2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-09-16 11:33:53 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bs`.`waste_addSales`(
|
|
|
|
vDateFrom DATE,
|
|
|
|
vDateTo DATE
|
|
|
|
)
|
2024-01-15 11:31:03 +00:00
|
|
|
BEGIN
|
2024-09-16 11:33:53 +00:00
|
|
|
IF vDateFrom IS NULL THEN
|
|
|
|
SET vDateFrom = util.VN_CURDATE() - INTERVAL WEEKDAY(util.VN_CURDATE()) DAY;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
IF vDateTo IS NULL THEN
|
|
|
|
SET vDateTo = vDateFrom + INTERVAL 6 DAY;
|
|
|
|
END IF;
|
2024-07-10 10:00:54 +00:00
|
|
|
|
|
|
|
CALL cache.last_buy_refresh(FALSE);
|
2024-07-09 09:56:32 +00:00
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
REPLACE bs.waste
|
2024-05-10 08:01:46 +00:00
|
|
|
SELECT YEAR(t.shipped),
|
|
|
|
WEEK(t.shipped, 4),
|
|
|
|
it.workerFk,
|
|
|
|
it.id,
|
|
|
|
s.itemFk,
|
2024-08-09 06:18:30 +00:00
|
|
|
SUM((b.buyingValue + b.freightValue + b.comissionValue + b.packageValue) * s.quantity),
|
|
|
|
SUM(IF(aw.`type`, s.quantity, 0)),
|
2024-09-16 10:32:52 +00:00
|
|
|
SUM(IF(
|
|
|
|
aw.`type` = 'external',
|
2024-05-10 08:01:46 +00:00
|
|
|
(b.buyingValue + b.freightValue + b.comissionValue + b.packageValue) * s.quantity,
|
|
|
|
0
|
|
|
|
)
|
2024-09-16 10:32:52 +00:00
|
|
|
),
|
|
|
|
SUM(IF(
|
|
|
|
aw.`type` = 'fault',
|
|
|
|
(b.buyingValue + b.freightValue + b.comissionValue + b.packageValue) * s.quantity,
|
|
|
|
0
|
|
|
|
)
|
|
|
|
),
|
|
|
|
SUM(IF(
|
|
|
|
aw.`type` = 'container',
|
|
|
|
(b.buyingValue + b.freightValue + b.comissionValue + b.packageValue) * s.quantity,
|
|
|
|
0
|
|
|
|
)
|
|
|
|
),
|
|
|
|
SUM(IF(
|
|
|
|
aw.`type` = 'break',
|
|
|
|
(b.buyingValue + b.freightValue + b.comissionValue + b.packageValue) * s.quantity,
|
|
|
|
0
|
|
|
|
)
|
|
|
|
),
|
|
|
|
SUM(IF(
|
|
|
|
aw.`type` = 'other',
|
2024-05-10 08:01:46 +00:00
|
|
|
(b.buyingValue + b.freightValue + b.comissionValue + b.packageValue) * s.quantity,
|
2024-08-09 11:53:28 +00:00
|
|
|
0
|
2024-05-10 08:01:46 +00:00
|
|
|
)
|
2024-08-09 06:18:30 +00:00
|
|
|
)
|
2024-05-10 08:01:46 +00:00
|
|
|
FROM vn.sale s
|
|
|
|
JOIN vn.item i ON i.id = s.itemFk
|
|
|
|
JOIN vn.itemType it ON it.id = i.typeFk
|
|
|
|
JOIN vn.ticket t ON t.id = s.ticketFk
|
2024-07-09 09:56:32 +00:00
|
|
|
JOIN vn.address a FORCE INDEX (PRIMARY) ON a.id = t.addressFk
|
2024-07-09 09:40:24 +00:00
|
|
|
LEFT JOIN vn.addressWaste aw ON aw.addressFk = a.id
|
2024-05-10 08:01:46 +00:00
|
|
|
JOIN vn.warehouse w ON w.id = t.warehouseFk
|
|
|
|
JOIN cache.last_buy lb ON lb.item_id = i.id
|
|
|
|
AND lb.warehouse_id = w.id
|
|
|
|
JOIN vn.buy b ON b.id = lb.buy_id
|
2024-07-10 10:00:54 +00:00
|
|
|
WHERE t.shipped BETWEEN vDateFrom AND vDateTo
|
|
|
|
AND w.isManaged
|
2024-09-16 11:33:53 +00:00
|
|
|
GROUP BY YEAR(t.shipped), WEEK(t.shipped, 4), i.id;
|
2024-01-15 11:31:03 +00:00
|
|
|
END$$
|
|
|
|
DELIMITER ;
|