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

161 lines
5.3 KiB
MySQL
Raw Normal View History

2024-03-05 12:43:03 +00:00
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`multipleInventoryHistory`(
2024-03-06 08:00:21 +00:00
vItemFk INT)
2024-03-05 12:43:03 +00:00
BEGIN
/**
* Calcula y proporciona un historial de inventario detallado
* para un artículo dividiendo la información
* por almacén.
*
* @param vItemFk Artículo
*/
DECLARE vDateInventory DATETIME;
SELECT inventoried INTO vDateInventory FROM config;
DROP TEMPORARY TABLE IF EXISTS tMultipleHistory1;
CREATE TEMPORARY TABLE tMultipleHistory1
SELECT DATE(`date`) `date`,
2024-03-06 08:00:21 +00:00
input,
2024-03-05 12:43:03 +00:00
`output`,
ok,
reference,
history.id,
warehouse,
`name` warehouseName
2024-03-06 08:00:21 +00:00
FROM (
SELECT tr.landed `date`,
2024-03-05 12:43:03 +00:00
c.quantity input,
NULL `output`,
IF(warehouseInFk = 44, 1, warehouseInFk) warehouse,
(tr.isReceived <> FALSE) ok,
e.invoiceNumber reference,
2024-03-06 08:00:21 +00:00
e.id id
FROM buy c
JOIN `entry` e ON e.id = c.entryFk
JOIN travel tr ON tr.id = e.travelFk
WHERE tr.landed >= vDateInventory
AND c.itemFk = vItemFk
AND NOT e.isRaid
AND c.quantity <> 0
UNION ALL
SELECT tr.shipped `date`,
2024-03-05 12:43:03 +00:00
NULL input,
c.quantity `output`,
2024-03-06 08:00:21 +00:00
tr.warehouseOutFk warehouse,
2024-03-05 12:43:03 +00:00
tr.isDelivered ok,
e.invoiceNumber reference,
e.id id
2024-03-06 08:00:21 +00:00
FROM buy c
JOIN `entry` e ON e.id = c.entryFk
JOIN travel tr ON tr.id = e.travelFk
WHERE tr.shipped >= vDateInventory
AND c.itemFk = vItemFk
AND NOT e.isRaid
AND c.quantity <> 0
UNION ALL
SELECT t.shipped `date`,
2024-03-05 12:43:03 +00:00
NULL input,
m.quantity `output`,
2024-03-06 08:00:21 +00:00
tr.warehouseInFk warehouse,
2024-03-05 12:43:03 +00:00
(m.isPicked <> 0 OR t.isLabeled <> 0 OR t.refFk IS NOT NULL) ok,
t.refFk reference,
t.id id
2024-03-06 08:00:21 +00:00
FROM sale m
JOIN ticket t ON t.id = m.ticketFk
WHERE t.shipped >= vDateInventory
AND m.itemFk = vItemFk
) history
JOIN warehouse ON warehouse.id = history.warehouse
ORDER BY `date`, input DESC, ok DESC;
2024-03-05 12:43:03 +00:00
DROP TEMPORARY TABLE IF EXISTS tMultipleHistory2;
DROP TEMPORARY TABLE IF EXISTS tMultipleHistory3;
DROP TEMPORARY TABLE IF EXISTS tMultipleHistory4;
DROP TEMPORARY TABLE IF EXISTS tMultipleHistory5;
DROP TEMPORARY TABLE IF EXISTS tMultipleHistory6;
DROP TEMPORARY TABLE IF EXISTS tMultipleHistory7;
DROP TEMPORARY TABLE IF EXISTS tMultipleHistory8;
CREATE TEMPORARY TABLE tMultipleHistory2 SELECT * FROM tMultipleHistory1 WHERE warehouse = 19;
CREATE TEMPORARY TABLE tMultipleHistory3 SELECT * FROM tMultipleHistory1 WHERE warehouse = 7;
CREATE TEMPORARY TABLE tMultipleHistory4 SELECT * FROM tMultipleHistory1 WHERE warehouse = 60;
CREATE TEMPORARY TABLE tMultipleHistory5 SELECT * FROM tMultipleHistory1 WHERE warehouse = 5;
CREATE TEMPORARY TABLE tMultipleHistory6 SELECT * FROM tMultipleHistory1 WHERE warehouse = 17;
CREATE TEMPORARY TABLE tMultipleHistory7 SELECT * FROM tMultipleHistory1 WHERE warehouse = 37;
CREATE TEMPORARY TABLE tMultipleHistory8 SELECT * FROM tMultipleHistory1 WHERE warehouse = 55;
SELECT *
2024-03-06 08:00:21 +00:00
FROM (
SELECT `date`, input BOGinput, `output` BOGoutput, ok BOGok, reference BOGreference, id BOGid,
NULL VNHinput, NULL VNHoutput, NULL VNHok, NULL VNHreference, NULL VNHid,
NULL ALGinput, NULL ALGoutput, NULL ALGok, NULL ALGreference, NULL ALGid,
NULL MADinput, NULL MADoutput, NULL MADok, NULL MADreference, NULL MADid,
NULL MCFinput, NULL MCFoutput, NULL MCFok, NULL MCFreference, NULL MCFid,
NULL VILinput, NULL VILoutput, NULL VILok, NULL VILreference, NULL VILid,
NULL BARinput, NULL BARoutput, NULL BARok, NULL BARreference, NULL BARid
FROM tMultipleHistory2
UNION ALL
SELECT `date`,
NULL, NULL, NULL, NULL, NULL,
input, `output`, ok, reference, id,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL
FROM tMultipleHistory3
UNION ALL
SELECT `date`,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
input, `output`, ok, reference, id,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL
FROM tMultipleHistory4
UNION ALL
SELECT `date`,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
input, `output`, ok, reference, id,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL
FROM tMultipleHistory5
UNION ALL
SELECT `date`,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
input, `output`, ok, reference, id,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL
FROM tMultipleHistory6
UNION ALL
SELECT `date`,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
input, `output`, ok, reference, id,
NULL, NULL, NULL, NULL, NULL
FROM tMultipleHistory7
UNION ALL
SELECT `date`,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
input, `output`, ok, reference, id
FROM tMultipleHistory8
) sub
ORDER BY `date`, BOGinput IS NULL, VNHinput IS NULL, ALGinput IS NULL, MADinput IS NULL, MCFinput IS NULL, VILinput IS NULL, BARinput IS NULL;
2024-03-05 12:43:03 +00:00
END$$
DELIMITER ;