refs #6499 Refactor historico_multiple
gitea/salix/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Ivan Mas 2024-03-05 13:43:03 +01:00
parent 1ccec1e0fb
commit 68846d3018
2 changed files with 216 additions and 54 deletions

View File

@ -1,13 +1,13 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`absoluteInventoryHistory`(
vIdArticle INT, vWarehouse INT, vDate DATETIME)
vItemFk INT, vWarehouse INT, vDate DATETIME)
BEGIN
/**
* Calcula y proporciona un historial de inventario absoluto
* para un artículo específico en un almacén dado
* hasta una fecha determinada.
*
* @param vIdArticle Id de artículo
* @param vItemFk Id de artículo
* @param vWarehouse Almacén
* @param vDate Fecha
*/
@ -16,84 +16,85 @@ BEGIN
SET vToday = util.VN_CURDATE();
CREATE OR REPLACE TEMPORARY TABLE historicalPast
ENGINE = MEMORY
CREATE OR REPLACE TEMPORARY TABLE tHistoricalPast
ENGINE = MEMORY
SELECT *
FROM (
SELECT tr.landed Fecha,
b.quantity Entrada,
NULL Salida,
(tr.isReceived != FALSE) OK,
s.name Alias,
e.invoiceNumber Referencia,
SELECT tr.landed `date`,
b.quantity input,
NULL `output`,
(tr.isReceived != FALSE) ok,
s.name alias,
e.invoiceNumber reference,
e.id id,
tr.isDelivered F5
tr.isDelivered f5
FROM buy b
JOIN entry e ON e.id = b.entryFk
JOIN `entry` e ON e.id = b.entryFk
JOIN travel tr ON tr.id = e.travelFk
JOIN supplier s ON s.id = e.supplierFk
JOIN supplier s ON s.id = e.supplierFk
WHERE tr.landed >= '2001-01-01'
AND s.id <> 4
AND vWarehouse IN (tr.warehouseInFk, 0)
AND b.itemFk = vIdArticle
AND e.isExcludedFromAvailable = 0
AND e.isRaid = 0
AND b.itemFk = vItemFk
AND NOT e.isExcludedFromAvailable
AND NOT e.isRaid
UNION ALL
SELECT tr.shipped Fecha,
NULL Entrada,
b.quantity Salida,
tr.isDelivered OK,
s.name Alias,
e.invoiceNumber Referencia,
SELECT tr.shipped `date`,
NULL input,
b.quantity `output`,
tr.isDelivered ok,
s.name alias,
e.invoiceNumber reference,
e.id id,
tr.isDelivered F5
tr.isDelivered f5
FROM buy b
JOIN entry e ON e.id = b.entryFk
JOIN `entry` e ON e.id = b.entryFk
JOIN travel tr ON tr.id = e.travelFk
JOIN supplier s ON s.id = e.supplierFk
JOIN supplier s ON s.id = e.supplierFk
WHERE tr.shipped >= '2001-01-01'
AND vWarehouse = tr.warehouseOutFk
AND s.id <> 4
AND b.itemFk = vIdArticle
AND e.isExcludedFromAvailable = 0
AND e.isRaid = 0
AND b.itemFk = vItemFk
AND NOT e.isExcludedFromAvailable
AND NOT e.isRaid
UNION ALL
SELECT t.shipped Fecha,
NULL Entrada,
m.quantity Salida,
(m.isPicked <> 0 OR t.isLabeled <> 0 OR t.refFk IS NOT NULL) OK,
t.nickname Alias,
t.refFk Referencia,
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,
t.id,
t.isPrinted
FROM sale m
JOIN Tickets T USING (Id_Ticket)
JOIN Clientes C ON C.Id_Cliente = T.Id_Cliente
WHERE T.Fecha >= '2001-01-01'
AND M.Id_Article = vIdArticle
AND vWarehouse IN (T.warehouse_id , 0)
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)
) t1
ORDER BY Fecha, Entrada DESC, OK DESC;
ORDER BY `date`, input DESC, ok DESC;
SELECT sum(Entrada) - sum(Salida) INTO vCalculatedInventory
FROM historicalPast
WHERE Fecha < vDate;
SELECT sum(input) - sum(`output`) INTO vCalculatedInventory
FROM tHistoricalPast
WHERE `date` < vDate;
SELECT p1.*, NULL v_virtual
FROM(
SELECT vDate Fecha,
vCalculatedInventory Entrada,
NULL Salida,
1 OK,
'Inventario calculado' Alias,
'' Referencia, 0 id,
1 F5
SELECT vDate `date`,
vCalculatedInventory input,
NULL `output`,
1 ok,
'Inventario calculado' alias,
'' reference,
0 id,
1 f5
UNION ALL
SELECT *
FROM historicalPast
WHERE Fecha >= vDate
SELECT *
FROM tHistoricalPast
WHERE `date` >= vDate
) p1;
DROP TEMPORARY TABLE historicalPast;
DROP TEMPORARY TABLE tHistoricalPast;
END$$
DELIMITER ;

View File

@ -0,0 +1,161 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`multipleInventoryHistory`(
vItemFk INT)
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`,
input,
`output`,
ok,
reference,
history.id,
warehouse,
`name` warehouseName
FROM (
SELECT tr.landed `date`,
c.quantity input,
NULL `output`,
IF(warehouseInFk = 44, 1, warehouseInFk) warehouse,
(tr.isReceived <> FALSE) ok,
e.invoiceNumber reference,
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`,
NULL input,
c.quantity `output`,
warehouseOutFk warehouse,
tr.isDelivered ok,
e.invoiceNumber reference,
e.id id
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`,
NULL input,
m.quantity `output`,
warehouseInFk warehouse,
(m.isPicked <> 0 OR t.isLabeled <> 0 OR t.refFk IS NOT NULL) ok,
t.refFk reference,
t.id id
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;
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 *
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;
END$$
DELIMITER ;