salix/db/changes/10410-january/00-ticket_getAdvancable.sql

45 lines
1.3 KiB
MySQL
Raw Normal View History

2022-01-17 06:14:29 +00:00
DROP PROCEDURE IF EXISTS `vn`.`ticket_getAdvanceable`;
DELIMITER $$
$$
CREATE DEFINER=`root`@`%` PROCEDURE `vn`.`ticket_getAdvanceable`(vTicketFk INT, vDatedNew DATETIME)
BEGIN
/**
* Cálcula el stock avanzable para los artículos de un ticket
*
* @param vTicketFk -> Ticket
* @param vDatedNew -> Nueva fecha
* @return Sales con Avanzable
*/
DECLARE vWarehouseFk INT;
DECLARE vDatedOld DATETIME;
SELECT t.warehouseFk, t.shipped INTO vWarehouseFk, vDatedOld
FROM ticket t
WHERE t.id = vTicketFk;
CALL itemStock(vWarehouseFk, DATE_SUB(vDatedNew, INTERVAL 1 DAY), NULL);
CALL item_getMinacum(vWarehouseFk, vDatedNew, DATEDIFF(vDatedOld, vDatedNew), NULL);
SELECT s.id,
s.itemFk,
s.quantity,
s.concept,
s.price,
s.reserved,
s.discount,
i.image,
i.subName,
il.stock + IFNULL(im.amount, 0) AS advanceable
FROM ticket t
JOIN sale s ON s.ticketFk = t.id
JOIN item i ON i.id = s.itemFk
LEFT JOIN tmp.itemMinacum im ON im.itemFk = s.itemFk AND im.warehouseFk = vWarehouseFk
LEFT JOIN tmp.itemList il ON il.itemFk = s.itemFk
WHERE t.id = vTicketFk;
DROP TEMPORARY TABLE IF EXISTS tmp.itemList;
DROP TEMPORARY TABLE IF EXISTS tmp.itemMinacum;
END$$
DELIMITER ;