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

56 lines
1.4 KiB
MySQL
Raw Normal View History

DELIMITER $$
2024-02-15 09:04:15 +00:00
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_getMovable`(
vTicketFk INT,
vNewShipped DATETIME,
vWarehouseFk INT
)
BEGIN
/**
* Cálcula el stock movible para los artículos de un ticket
2024-02-15 09:04:15 +00:00
* vNewShipped debe ser menor que vOldShipped, en los otros casos se
* asume que siempre es posible
*
* @param vTicketFk -> Ticket
2024-02-15 09:04:15 +00:00
* @param vNewShipped -> Nueva fecha
* @return Sales con Movible
2024-02-15 09:04:15 +00:00
*/
DECLARE vOldShipped DATETIME;
2024-02-15 09:04:15 +00:00
SELECT t.shipped INTO vOldShipped
FROM ticket t
WHERE t.id = vTicketFk;
2024-02-15 09:04:15 +00:00
-- Añadimos un dia más para calcular el stock hasta vNewShipped inclusive
CALL item_getStock(vWarehouseFk, vNewShipped, NULL);
2024-02-15 09:04:15 +00:00
CALL item_getMinacum(
vWarehouseFk,
vNewShipped,
DATEDIFF(DATE_SUB(vOldShipped, INTERVAL 1 DAY), vNewShipped),
NULL
);
SELECT s.id,
s.itemFk,
s.quantity,
s.concept,
s.price,
s.reserved,
2024-02-15 09:04:15 +00:00
s.discount,
i.image,
i.subName,
il.stock + IFNULL(im.amount, 0) AS movable
FROM ticket t
JOIN sale s ON s.ticketFk = t.id
2024-02-15 09:04:15 +00:00
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;
2024-02-15 09:04:15 +00:00
DROP TEMPORARY TABLE IF EXISTS
tmp.itemList,
tmp.itemMinacum;
END$$
DELIMITER ;