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

44 lines
1.1 KiB
MySQL
Raw Normal View History

DELIMITER $$
2024-08-20 08:06:10 +00:00
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`saleSplit`(vSaleFk INT, vQuantity INT)
BEGIN
DECLARE vNewSaleFk INT;
INSERT INTO vn.sale(itemFk,
concept,
price,
discount,
quantity,
ticketFk,
isPriceFixed)
SELECT s.itemFk,
i.longName,
s.price,
s.discount,
vQuantity,
s.ticketFk,
s.isPriceFixed
FROM vn.sale s
JOIN vn.item i ON i.id = s.itemFk
WHERE s.id = vSaleFk;
UPDATE vn.sale s
JOIN vn.item i ON i.id = s.itemFk
SET s.quantity = s.quantity - vQuantity, s.concept = i.longName
WHERE s.id = vSaleFk;
SELECT LAST_INSERT_ID() INTO vNewSaleFk;
INSERT INTO vn.saleComponent( saleFk,
componentFk,
value)
SELECT vNewSaleFk,
componentFk,
value
FROM vn.saleComponent
WHERE saleFk = vSaleFk;
END$$
DELIMITER ;