diff --git a/db/routines/vn/procedures/absoluteInventoryHistory.sql b/db/routines/vn/procedures/absoluteInventoryHistory.sql new file mode 100644 index 000000000..427b9ba51 --- /dev/null +++ b/db/routines/vn/procedures/absoluteInventoryHistory.sql @@ -0,0 +1,99 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`absoluteInventoryHistory`( + vIdArticle 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 vWarehouse Almacén +* @param vDate Fecha +*/ + DECLARE vCalculatedInventory INT; + DECLARE vToday DATETIME; + + SET vToday = util.VN_CURDATE(); + + CREATE OR REPLACE TEMPORARY TABLE historicalPast + ENGINE = MEMORY + SELECT * + FROM ( + SELECT tr.landed Fecha, + b.quantity Entrada, + NULL Salida, + (tr.isReceived != FALSE) OK, + s.name Alias, + e.invoiceNumber Referencia, + 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 + 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 + UNION ALL + SELECT tr.shipped Fecha, + NULL Entrada, + b.quantity Salida, + tr.isDelivered OK, + s.name Alias, + e.invoiceNumber Referencia, + 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 + 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 + 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, + 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) + ) t1 + ORDER BY Fecha, Entrada DESC, OK DESC; + + SELECT sum(Entrada) - sum(Salida) INTO vCalculatedInventory + FROM historicalPast + WHERE Fecha < vDate; + + SELECT p1.*, NULL v_virtual + FROM( + SELECT vDate Fecha, + vCalculatedInventory Entrada, + NULL Salida, + 1 OK, + 'Inventario calculado' Alias, + '' Referencia, 0 id, + 1 F5 + UNION ALL + SELECT * + FROM historicalPast + WHERE Fecha >= vDate + ) p1; + + DROP TEMPORARY TABLE historicalPast; +END$$ +DELIMITER ; \ No newline at end of file