salix/db/routines/vn/procedures/inventoryFailureAdd.sql

49 lines
1.0 KiB
MySQL
Raw Normal View History

DELIMITER $$
2024-08-20 08:06:10 +00:00
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`inventoryFailureAdd`()
BEGIN
DECLARE done BOOL DEFAULT FALSE;
DECLARE vTicketFk INT;
DECLARE rs CURSOR FOR
SELECT id FROM vn.ticket
WHERE shipped = util.yesterday()
AND clientFk = 400
AND warehouseFk IN (1,44);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN rs;
FETCH rs INTO vTicketFk;
WHILE NOT done DO
INSERT INTO vn.inventoryFailure(dated, itemFk, quantity, value, warehouseFk, throwerFk)
SELECT t.shipped,
s.itemFk,
s.quantity,
b.buyingValue + b.freightValue + b.packageValue + b.comissionValue,
t.warehouseFk,
w.id
FROM vn.ticket t
JOIN vn.sale s ON s.ticketFk = t.id
LEFT JOIN cache.last_buy lb ON lb.warehouse_id = t.warehouseFk AND item_id = s.itemFk
LEFT JOIN vn.buy b ON b.id = lb.buy_id
LEFT JOIN vn.worker w ON w.code = LEFT(s.concept, 3)
WHERE t.id = vTicketFk
AND s.quantity > 0;
FETCH rs INTO vTicketFk;
END WHILE;
CLOSE rs;
END$$
DELIMITER ;