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

101 lines
2.5 KiB
MySQL
Raw Normal View History

2024-03-04 07:11:06 +00:00
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`absoluteInventoryHistory`(
2024-03-06 11:44:59 +00:00
vItemFk INT,
vWarehouseFk INT,
vDate DATETIME
)
2024-03-04 07:11:06 +00:00
BEGIN
/**
* Calcula y proporciona un historial de inventario absoluto
* para un artículo específico en un almacén dado
* hasta una fecha determinada.
*
2024-03-05 12:43:03 +00:00
* @param vItemFk Id de artículo
2024-03-06 11:44:59 +00:00
* @param vWarehouseFk Id de almacén
2024-03-04 07:11:06 +00:00
* @param vDate Fecha
*/
DECLARE vCalculatedInventory INT;
2024-03-06 11:44:59 +00:00
DECLARE vToday DATETIME DEFAULT util.VN_CURDATE();
2024-03-04 07:11:06 +00:00
2024-03-05 12:43:03 +00:00
CREATE OR REPLACE TEMPORARY TABLE tHistoricalPast
ENGINE = MEMORY
2024-03-04 07:11:06 +00:00
SELECT *
FROM (
2024-03-05 12:43:03 +00:00
SELECT tr.landed `date`,
b.quantity input,
NULL `output`,
2024-03-06 11:44:59 +00:00
tr.isReceived ok,
2024-03-05 12:43:03 +00:00
s.name alias,
e.invoiceNumber reference,
2024-03-04 07:11:06 +00:00
e.id id,
2024-03-05 12:43:03 +00:00
tr.isDelivered f5
2024-03-04 07:11:06 +00:00
FROM buy b
2024-03-05 12:43:03 +00:00
JOIN `entry` e ON e.id = b.entryFk
2024-03-04 07:11:06 +00:00
JOIN travel tr ON tr.id = e.travelFk
2024-03-05 12:43:03 +00:00
JOIN supplier s ON s.id = e.supplierFk
2024-03-04 07:11:06 +00:00
WHERE tr.landed >= '2001-01-01'
AND s.id <> 4
2024-03-06 11:44:59 +00:00
AND vWarehouseFk IN (tr.warehouseInFk, 0)
2024-03-05 12:43:03 +00:00
AND b.itemFk = vItemFk
AND NOT e.isExcludedFromAvailable
AND NOT e.isRaid
2024-03-04 07:11:06 +00:00
UNION ALL
2024-03-05 12:43:03 +00:00
SELECT tr.shipped `date`,
NULL input,
b.quantity `output`,
tr.isDelivered ok,
s.name alias,
e.invoiceNumber reference,
2024-03-04 07:11:06 +00:00
e.id id,
2024-03-05 12:43:03 +00:00
tr.isDelivered f5
2024-03-04 07:11:06 +00:00
FROM buy b
2024-03-05 12:43:03 +00:00
JOIN `entry` e ON e.id = b.entryFk
2024-03-04 07:11:06 +00:00
JOIN travel tr ON tr.id = e.travelFk
2024-03-05 12:43:03 +00:00
JOIN supplier s ON s.id = e.supplierFk
2024-03-04 07:11:06 +00:00
WHERE tr.shipped >= '2001-01-01'
2024-03-06 11:44:59 +00:00
AND vWarehouseFk = tr.warehouseOutFk
2024-03-04 07:11:06 +00:00
AND s.id <> 4
2024-03-05 12:43:03 +00:00
AND b.itemFk = vItemFk
AND NOT e.isExcludedFromAvailable
AND NOT e.isRaid
2024-03-04 07:11:06 +00:00
UNION ALL
2024-03-05 12:43:03 +00:00
SELECT t.shipped `date`,
NULL input,
m.quantity `output`,
2024-03-06 11:44:59 +00:00
(m.isPicked OR t.isLabeled OR t.refFk IS NOT NULL) ok,
2024-03-05 12:43:03 +00:00
t.nickname alias,
t.refFk reference,
2024-03-04 07:11:06 +00:00
t.id,
t.isPrinted
FROM sale m
2024-03-05 12:43:03 +00:00
JOIN ticket t ON t.id = m.ticketFk
JOIN client c ON c.id = t.clientFk
WHERE t.shipped >= '2001-01-01'
AND m.itemFk = vItemFk
2024-03-06 11:44:59 +00:00
AND vWarehouseFk IN (t.warehouseFk , 0)
2024-03-06 08:00:21 +00:00
) t1
2024-03-05 12:43:03 +00:00
ORDER BY `date`, input DESC, ok DESC;
2024-03-04 07:11:06 +00:00
2024-03-06 11:44:59 +00:00
SELECT SUM(input) - SUM(`output`) INTO vCalculatedInventory
2024-03-05 12:43:03 +00:00
FROM tHistoricalPast
WHERE `date` < vDate;
2024-03-04 07:11:06 +00:00
SELECT p1.*, NULL v_virtual
2024-03-06 11:44:59 +00:00
FROM (
2024-03-05 12:43:03 +00:00
SELECT vDate `date`,
vCalculatedInventory input,
NULL `output`,
1 ok,
'Inventario calculado' alias,
'' reference,
0 id,
1 f5
2024-03-04 07:11:06 +00:00
UNION ALL
2024-03-05 12:43:03 +00:00
SELECT *
FROM tHistoricalPast
WHERE `date` >= vDate
2024-03-04 07:11:06 +00:00
) p1;
2024-03-05 12:43:03 +00:00
DROP TEMPORARY TABLE tHistoricalPast;
2024-03-04 07:11:06 +00:00
END$$
DELIMITER ;