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

35 lines
1003 B
MySQL
Raw Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`copyComponentsFromSaleList`(vTargetTicketFk INT)
BEGIN
/* Copy sales and components to the target ticket
*
* ¡¡¡¡ Requires tmp.saleList(saleFk, itemFk, quantity, concept, price, discount, orden) !!!!!
*
* param VTargetTicketFk id del ticket a rellenar
*/
INSERT INTO vn.sale(ticketFk, itemFk, quantity,concept,price,discount)
SELECT vTargetTicketFk, itemFk, quantity,concept,price,discount
FROM tmp.saleList
ORDER BY orden;
SET @order = 0;
DROP TEMPORARY TABLE IF EXISTS tmp.newSaleList;
CREATE TEMPORARY TABLE tmp.newSaleList
SELECT id as saleFk, @order := @order + 1 as orden
FROM vn.sale
WHERE ticketFk = vTargetTicketFk
ORDER BY saleFk;
INSERT INTO vn.saleComponent(saleFk,componentFk,value)
SELECT ns.saleFk, sc.componentFk, sc.value
FROM vn.saleComponent sc
JOIN tmp.saleList s ON s.saleFk = sc.saleFk
JOIN tmp.newSaleList ns ON ns.orden = s.orden;
END$$
DELIMITER ;