salix/db/changes/10420-valentines/00-ticket_getMovable.sql

47 lines
1.4 KiB
MySQL
Raw Normal View History

2022-01-26 07:54:48 +00:00
DROP PROCEDURE IF EXISTS `vn`.`ticket_getMovable`;
2022-01-17 06:14:29 +00:00
DELIMITER $$
$$
2022-01-26 07:54:48 +00:00
CREATE DEFINER=`root`@`%` PROCEDURE `vn`.`ticket_getMovable`(vTicketFk INT, vDatedNew DATETIME, vWarehouseFk INT)
2022-01-17 06:14:29 +00:00
BEGIN
/**
2022-01-26 07:54:48 +00:00
* Cálcula el stock movible para los artículos de un ticket
2022-02-10 08:13:48 +00:00
* vDatedNew debe ser menor que vDatedOld, en los otros casos se
* asume que siempre es posible
2022-01-17 06:14:29 +00:00
*
* @param vTicketFk -> Ticket
* @param vDatedNew -> Nueva fecha
2022-01-26 07:54:48 +00:00
* @return Sales con Movible
2022-01-17 06:14:29 +00:00
*/
DECLARE vDatedOld DATETIME;
2022-02-10 08:13:48 +00:00
SET vDatedNew = DATE_ADD(vDatedNew, INTERVAL 1 DAY);
2022-01-18 11:48:39 +00:00
SELECT t.shipped INTO vDatedOld
2022-01-17 06:14:29 +00:00
FROM ticket t
WHERE t.id = vTicketFk;
2022-02-10 08:13:48 +00:00
CALL itemStock(vWarehouseFk, vDatedNew, NULL);
CALL item_getMinacum(vWarehouseFk, vDatedNew, DATEDIFF(DATE_SUB(vDatedOld, INTERVAL 1 DAY), vDatedNew), NULL);
2022-01-17 06:14:29 +00:00
SELECT s.id,
s.itemFk,
s.quantity,
s.concept,
s.price,
s.reserved,
s.discount,
i.image,
i.subName,
2022-01-26 07:54:48 +00:00
il.stock + IFNULL(im.amount, 0) AS movable
2022-01-17 06:14:29 +00:00
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 ;