salix/db/routines/bs/procedures/waste_addSales.sql

49 lines
1.5 KiB
MySQL
Raw Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bs`.`waste_addSales`()
BEGIN
2024-07-09 09:56:32 +00:00
CALL cache.last_buy_refresh (FALSE);
REPLACE bs.waste
SELECT YEAR(t.shipped),
WEEK(t.shipped, 4),
it.workerFk,
it.id,
s.itemFk,
SUM(s.quantity),
2024-07-09 10:00:55 +00:00
SUM((b.buyingValue + b.freightValue + b.comissionValue + b.packageValue) * s.quantity) `value`,
SUM (
IF(
2024-07-09 09:56:32 +00:00
aw.`type` = 'internal',
(b.buyingValue + b.freightValue + b.comissionValue + b.packageValue) * s.quantity,
0
)
) internalWaste,
SUM (
IF(
2024-07-09 09:56:32 +00:00
aw.`type` = 'external',
(b.buyingValue + b.freightValue + b.comissionValue + b.packageValue) * s.quantity,
2024-05-10 11:20:15 +00:00
IF(c.code = 'manaClaim',
sc.value * s.quantity,
0
)
)
) externalWaste
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
LEFT JOIN vn.addressWaste aw ON aw.addressFk = a.id
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-09 09:43:15 +00:00
LEFT JOIN vn.saleComponent sc ON sc.saleFk = s.id
LEFT JOIN vn.component c ON c.id = sc.componentFk
WHERE w.isManaged
AND YEAR(t.shipped) = YEAR(util.VN_CURDATE())
AND WEEK(t.shipped, 4) = WEEK(util.VN_CURDATE(), 4)
2024-05-10 11:20:15 +00:00
GROUP BY it.id, i.id;
END$$
DELIMITER ;