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

100 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 08:00:21 +00:00
vItemFk INT, vWarehouse 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-04 07:11:06 +00:00
* @param vWarehouse Almacén
* @param vDate Fecha
*/
DECLARE vCalculatedInventory INT;
DECLARE vToday DATETIME;
SET vToday = util.VN_CURDATE();
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`,
(tr.isReceived != FALSE) 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.landed >= '2001-01-01'
AND s.id <> 4
AND vWarehouse 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'
AND vWarehouse = tr.warehouseOutFk
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`,
(m.isPicked <> 0 OR t.isLabeled <> 0 OR t.refFk IS NOT NULL) ok,
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
AND vWarehouse 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-05 12:43:03 +00:00
SELECT sum(input) - sum(`output`) INTO vCalculatedInventory
FROM tHistoricalPast
WHERE `date` < vDate;
2024-03-04 07:11:06 +00:00
SELECT p1.*, NULL v_virtual
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 ;