refs #6498 procRefactor6 #2134

Merged
ivanm merged 8 commits from 6499-procRefactor6 into dev 2024-03-21 11:03:33 +00:00
5 changed files with 282 additions and 297 deletions

View File

@ -0,0 +1,102 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`absoluteInventoryHistory`(
vItemFk INT,

Esto separalo, tipo:

   vItemFk INT,
   vWarehouse INT,
   vDate DATETIME
)
Esto separalo, tipo: ``` vItemFk INT, vWarehouse INT, vDate DATETIME ) ```
vWarehouseFk 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.
guillermo marked this conversation as resolved
Review

Cambiar nombre de variable a vWarehouseFk y comentario a Id de almacén

Cambiar nombre de variable a vWarehouseFk y comentario a `Id de almacén`
*
* @param vItemFk Id de artículo
* @param vWarehouseFk Id de almacén
* @param vDate Fecha
guillermo marked this conversation as resolved
Review
DECLARE vToday DATETIME DEFAULT util.VN_CURDATE();
``` DECLARE vToday DATETIME DEFAULT util.VN_CURDATE(); ```
*/
DECLARE vCalculatedInventory INT;
guillermo marked this conversation as resolved
Review

Quitar linea

Quitar linea
DECLARE vToday DATETIME DEFAULT util.VN_CURDATE();
DECLARE vStartDate DATE DEFAULT '2001-01-01';
CREATE OR REPLACE TEMPORARY TABLE tHistoricalPast
ENGINE = MEMORY
SELECT *
FROM (
SELECT tr.landed `date`,
b.quantity input,
guillermo marked this conversation as resolved
Review
tr.isReceived ok,
``` tr.isReceived ok, ```
NULL `output`,
tr.isReceived ok,
s.name alias,
e.invoiceNumber reference,
e.id id,
tr.isDelivered f5
FROM buy b
JOIN `entry` e ON e.id = b.entryFk
JOIN travel tr ON tr.id = e.travelFk
JOIN supplier s ON s.id = e.supplierFk

No poner números a pelo en el código, para ello:

AND s.id <> (SELECT supplierFk FROM inventoryConfig)
No poner números a pelo en el código, para ello: ``` AND s.id <> (SELECT supplierFk FROM inventoryConfig) ```
WHERE tr.landed >= vStartDate
AND s.id <> (SELECT supplierFk FROM inventoryConfig)
AND vWarehouseFk IN (tr.warehouseInFk, 0)
AND b.itemFk = vItemFk
AND NOT e.isExcludedFromAvailable
AND NOT e.isRaid
UNION ALL
SELECT tr.shipped,
NULL,
b.quantity,
tr.isDelivered,
s.name,
e.invoiceNumber,
e.id,
tr.isDelivered
FROM buy b
JOIN `entry` e ON e.id = b.entryFk
JOIN travel tr ON tr.id = e.travelFk
JOIN supplier s ON s.id = e.supplierFk
WHERE tr.shipped >= vStartDate
AND vWarehouseFk = tr.warehouseOutFk
AND s.id <> (SELECT supplierFk FROM inventoryConfig)
AND b.itemFk = vItemFk
AND NOT e.isExcludedFromAvailable
AND NOT e.isRaid
UNION ALL
SELECT t.shipped,
NULL,
(m.isPicked OR t.isLabeled OR t.refFk IS NOT NULL)
``` (m.isPicked OR t.isLabeled OR t.refFk IS NOT NULL) ```
m.quantity,
(m.isPicked OR t.isLabeled OR t.refFk IS NOT NULL),
t.nickname,
t.refFk,
t.id,
t.isPrinted
FROM sale m
JOIN ticket t ON t.id = m.ticketFk
Review

'2001-01-01' repetido en el código, crear variable con DEFAULT '2001-01-01'

'2001-01-01' repetido en el código, crear variable con DEFAULT '2001-01-01'
JOIN client c ON c.id = t.clientFk
WHERE t.shipped >= vStartDate
AND m.itemFk = vItemFk
AND vWarehouseFk IN (t.warehouseFk, 0)
) t1
ORDER BY `date`, input DESC, ok DESC;

sum en mayuscula

sum en mayuscula
SELECT SUM(input) - SUM(`output`) INTO vCalculatedInventory
FROM tHistoricalPast
WHERE `date` < vDate;
guillermo marked this conversation as resolved
Review

Espacio entre el FROM y el (

Espacio entre el FROM y el (
SELECT p1.*, NULL v_virtual
FROM (
SELECT vDate `date`,
vCalculatedInventory input,
NULL `output`,
1 ok,
'Inventario calculado' alias,
'' reference,
0 id,
1 f5
UNION ALL
SELECT *
FROM tHistoricalPast
WHERE `date` >= vDate
) p1;
DROP TEMPORARY TABLE tHistoricalPast;
END$$
DELIMITER ;

View File

@ -0,0 +1,158 @@
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`,
guillermo marked this conversation as resolved
Review

warehouseInFk warehouse

warehouseInFk warehouse
tr.warehouseInFk warehouse,

tr.isReceived

`tr.isReceived`
tr.isReceived ok,
e.invoiceNumber reference,

e.id

e.id
e.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

`AND c.quantity`
AND c.quantity
UNION ALL
SELECT tr.shipped,
NULL,
c.quantity,
tr.warehouseOutFk,
tr.isDelivered,
e.invoiceNumber,

Quitar alias siempre a partir del segundo select cuando se haga un UNION

Quitar alias siempre a partir del segundo select cuando se haga un UNION
e.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

c.quantity

`c.quantity`
AND c.quantity
UNION ALL
SELECT t.shipped,
NULL,
m.quantity,
t.warehouseFk,
(m.isPicked OR t.isLabeled OR t.refFk IS NOT NULL),
t.refFk,
t.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;

Poner todo en un solo drop

Poner todo en un solo drop
DROP TEMPORARY TABLE IF EXISTS tMultipleHistory2, tMultipleHistory3,
tMultipleHistory4, tMultipleHistory5, tMultipleHistory6, tMultipleHistory7, 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 ;

View File

@ -1,91 +0,0 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`historico_absoluto`(IN idART INT, IN wh INT, IN datfecha DATETIME)
BEGIN
DECLARE inv_calculado INT;
DECLARE inv INT;
DECLARE today DATETIME;
DECLARE fecha_inv DATETIME;
SET today = util.VN_CURDATE();
CREATE OR REPLACE TEMPORARY TABLE historico_pasado
SELECT *
FROM (
SELECT TR.landing Fecha,
C.Cantidad Entrada,
NULL Salida,
(TR.received != FALSE) OK,
P.Proveedor Alias,
E.Referencia Referencia,
E.Id_Entrada id,
TR.delivered F5
FROM Compres C -- mirar perque no entra en received
INNER JOIN Entradas E USING (Id_Entrada)
INNER JOIN travel TR ON TR.id = E.travel_id
INNER JOIN Proveedores P USING (Id_Proveedor)
WHERE TR.landing >= '2001-01-01'
AND Id_proveedor <> 4
AND wh IN (TR.warehouse_id , 0)
AND C.Id_Article = idART
AND E.Inventario = 0
AND E.Redada = 0
UNION ALL
SELECT TR.shipment Fecha,
NULL Entrada,
C.Cantidad Salida,
TR.delivered OK,
P.Proveedor Alias,
E.Referencia Referencia,
E.Id_Entrada id,
TR.delivered F5
FROM Compres C
INNER JOIN Entradas E USING (Id_Entrada)
INNER JOIN travel TR ON TR.id = E.travel_id
INNER JOIN Proveedores P USING (Id_Proveedor)
WHERE TR.shipment >= '2001-01-01'
AND wh = TR.warehouse_id_out
AND Id_Proveedor <> 4
AND C.Id_Article = idART
AND E.Inventario = 0
AND E.Redada = 0
UNION ALL
SELECT T.Fecha Fecha,
NULL Entrada,
M.Cantidad Salida,
(M.OK <> 0 OR T.Etiquetasemitidas <> 0 OR T.Factura IS NOT NULL) OK,
T.Alias Alias,
T.Factura Referencia,
T.Id_Ticket,
T.PedidoImpreso
FROM Movimientos M
INNER 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 = idART
AND wh IN (T.warehouse_id , 0)
) t1
ORDER BY Fecha, Entrada DESC, OK DESC;
SELECT sum(Entrada) - sum(Salida) INTO inv_calculado
FROM historico_pasado
WHERE Fecha < datfecha;
SELECT p1.*, NULL v_virtual
FROM(
SELECT datfecha Fecha,
inv_calculado Entrada,
NULL Salida,
1 OK,
'Inventario calculado' Alias,
'' Referencia, 0 id,
1 F5
UNION ALL
SELECT *
FROM historico_pasado
WHERE Fecha >= datfecha
) p1;
DROP TEMPORARY TABLE historico_pasado;
END$$
DELIMITER ;

View File

@ -1,206 +0,0 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`historico_multiple`(IN vItemFk INT)
BEGIN
DECLARE vDateInventory DATETIME;
SELECT Fechainventario INTO vDateInventory FROM tblContadores;
SET @a = 0;
DROP TEMPORARY TABLE IF EXISTS hm1;
CREATE TEMPORARY TABLE hm1
SELECT DATE(Fecha) as Fecha,
Entrada,
Salida,
OK,
Referencia,
Historia.id,
wh,
`name` as wh_name
FROM
( SELECT TR.landing as Fecha,
C.Cantidad as Entrada,
NULL as Salida,
IF(warehouse_id = 44, 1, warehouse_id) as wh,
(TR.received != FALSE) as OK,
E.Referencia as Referencia,
E.Id_Entrada as id
FROM Compres C
INNER JOIN Entradas E USING (Id_Entrada)
INNER JOIN travel TR ON TR.id = E.travel_id
WHERE TR.landing >= vDateInventory
AND C.Id_Article = vItemFk
AND E.Redada = 0
AND C.Cantidad <> 0
UNION ALL
SELECT TR.shipment as Fecha,
NULL as Entrada,
C.Cantidad as Salida,
warehouse_id_out as wh,
TR.delivered as OK,
E.Referencia as Referencia,
E.Id_Entrada as id
FROM Compres C
INNER JOIN Entradas E USING (Id_Entrada)
INNER JOIN travel TR ON TR.id = E.travel_id
WHERE TR.shipment >= vDateInventory
AND C.Id_Article = vItemFk
AND E.Redada = 0
AND C.Cantidad <> 0
UNION ALL
SELECT T.Fecha as Fecha,
NULL as Entrada,
M.Cantidad as Salida,
warehouse_id as wh,
(M.OK <> 0 OR T.Etiquetasemitidas <> 0 OR T.Factura IS NOT NULL) as OK,
T.Factura as Referencia,
T.Id_Ticket as id
FROM Movimientos M
INNER JOIN Tickets T USING (Id_Ticket)
WHERE T.Fecha >= vDateInventory
AND M.Id_Article = vItemFk
) AS Historia
INNER JOIN warehouse ON warehouse.id = Historia.wh
ORDER BY Fecha, Entrada DESC, OK DESC;
DROP TEMPORARY TABLE IF EXISTS hm2;
DROP TEMPORARY TABLE IF EXISTS hm3;
DROP TEMPORARY TABLE IF EXISTS hm4;
DROP TEMPORARY TABLE IF EXISTS hm5;
DROP TEMPORARY TABLE IF EXISTS hm6;
DROP TEMPORARY TABLE IF EXISTS hm7;
DROP TEMPORARY TABLE IF EXISTS hm8;
CREATE TEMPORARY TABLE hm2 SELECT * FROM hm1 WHERE wh = 19;
CREATE TEMPORARY TABLE hm3 SELECT * FROM hm1 WHERE wh = 7;
CREATE TEMPORARY TABLE hm4 SELECT * FROM hm1 WHERE wh = 60;
CREATE TEMPORARY TABLE hm5 SELECT * FROM hm1 WHERE wh = 5;
CREATE TEMPORARY TABLE hm6 SELECT * FROM hm1 WHERE wh = 17;
CREATE TEMPORARY TABLE hm7 SELECT * FROM hm1 WHERE wh = 37;
CREATE TEMPORARY TABLE hm8 SELECT * FROM hm1 WHERE wh = 55;
SELECT * FROM
(
SELECT Fecha, Entrada as BOGEntrada, Salida as BOGSalida, OK as BOGOK, Referencia as BOGReferencia, id as BOGid,
NULL AS VNHEntrada, NULL AS VNHSalida, NULL AS VNHOK, NULL AS VNHReferencia, NULL AS VNHid,
NULL AS ALGEntrada, NULL AS ALGSalida, NULL AS ALGOK, NULL AS ALGReferencia, NULL AS ALGid,
NULL AS MADEntrada, NULL AS MADSalida, NULL AS MADOK, NULL AS MADReferencia, NULL AS MADid,
NULL AS MCFEntrada, NULL AS MCFSalida, NULL AS MCFOK, NULL AS MCFReferencia, NULL AS MCFid,
NULL AS VILEntrada, NULL AS VILSalida, NULL AS VILOK, NULL AS VILReferencia, NULL AS VILid,
NULL AS BAREntrada, NULL AS BARSalida, NULL AS BAROK, NULL AS BARReferencia, NULL AS BARid
FROM hm2
UNION ALL
SELECT Fecha
, NULL, NULL, NULL, NULL, NULL
,Entrada, Salida, OK, Referencia, 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 hm3
UNION ALL
SELECT Fecha
, NULL, NULL, NULL, NULL, NULL
, NULL, NULL, NULL, NULL, NULL
, Entrada, Salida, OK, Referencia, id
, NULL, NULL, NULL, NULL, NULL
, NULL, NULL, NULL, NULL, NULL
, NULL, NULL, NULL, NULL, NULL
, NULL, NULL, NULL, NULL, NULL
FROM hm4
UNION ALL
SELECT Fecha
, NULL, NULL, NULL, NULL, NULL
, NULL, NULL, NULL, NULL, NULL
, NULL, NULL, NULL, NULL, NULL
, Entrada, Salida, OK, Referencia, id
, NULL, NULL, NULL, NULL, NULL
, NULL, NULL, NULL, NULL, NULL
, NULL, NULL, NULL, NULL, NULL
FROM hm5
UNION ALL
SELECT Fecha
, NULL, NULL, NULL, NULL, NULL
, NULL, NULL, NULL, NULL, NULL
, NULL, NULL, NULL, NULL, NULL
, NULL, NULL, NULL, NULL, NULL
, Entrada, Salida, OK, Referencia, id
, NULL, NULL, NULL, NULL, NULL
, NULL, NULL, NULL, NULL, NULL
FROM hm6
UNION ALL
SELECT Fecha
, 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
, Entrada, Salida, OK, Referencia, id
, NULL, NULL, NULL, NULL, NULL
FROM hm7
UNION ALL
SELECT Fecha
, 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
, Entrada, Salida, OK, Referencia, id
FROM hm8
) sub
ORDER BY Fecha, BOGEntrada IS NULL, VNHEntrada IS NULL, ALGEntrada IS NULL, MADEntrada IS NULL, MCFEntrada IS NULL, VILEntrada IS NULL, BAREntrada IS NULL;
END$$
DELIMITER ;

View File

@ -0,0 +1,22 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`multipleInventoryHistory`(
vItemFk INT)
BEGIN
DECLARE vDateInventory DATETIME;
SELECT inventoried INTO vDateInventory FROM config;
END$$
DELIMITER ;
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`absoluteInventoryHistory`(
vItemFk INT, vWarehouse INT, vDate DATETIME)
BEGIN
DECLARE vCalculatedInventory INT;
SET vCalculatedInventory = 0;
END$$
DELIMITER ;
GRANT EXECUTE ON PROCEDURE vn.absoluteInventoryHistory TO buyer;
GRANT EXECUTE ON PROCEDURE vn.multipleInventoryHistory TO buyer;