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

309 lines
7.7 KiB
MySQL
Raw Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`item_getBalance`(
vItemFk INT,
vWarehouseFk INT,
2024-07-04 07:17:22 +00:00
vDated DATETIME
)
BEGIN
/**
2024-07-04 07:17:22 +00:00
* Calcula el disponible.
*
* @vItemFk Id de artículo
* @vWarehouseFk Id de almacén
* @vDated Fecha
* Si la fecha es NULL, muestra el histórico desde el inventario.
* Si la fecha no es NULL, muestra histórico desde la fecha de vDated.
*/
DECLARE vDateInventory DATETIME;
2024-07-04 07:17:22 +00:00
DECLARE vLifeScope DATE;
DECLARE vWarehouseInventoryFk INT;
DECLARE vSupplierInventoryFk INT;
2024-07-04 07:17:22 +00:00
SELECT IF(vDated, uc.mockUtcTime, c.inventoried) INTO vDateInventory
FROM config c
JOIN util.config uc;
SELECT COALESCE(vDated, vDateInventory) - INTERVAL MAX(life) DAY
INTO vLifeScope
FROM itemType;
SELECT warehouseOutFk, supplierFk
INTO vWarehouseInventoryFk, vSupplierInventoryFk
FROM inventoryConfig;
IF NOT vWarehouseInventoryFk OR NOT vSupplierInventoryFk THEN
CALL util.throw('Config variables are not set');
END IF;
2024-07-04 07:17:22 +00:00
-- Calcula el ultimo dia de vida para cada producto
CREATE OR REPLACE TEMPORARY TABLE tItemRange
(PRIMARY KEY (itemFk))
ENGINE = MEMORY
SELECT i.id itemFk,
util.dayEnd(c.maxLanded + INTERVAL it.life DAY) ended,
it.life
FROM item i
LEFT JOIN (
SELECT b.itemFk, MAX(t.landed) maxLanded
FROM buy b
JOIN entry e ON b.entryFk = e.id
JOIN travel t ON t.id = e.travelFk
JOIN warehouse w ON w.id = t.warehouseInFk
JOIN item i ON i.id = b.itemFk
JOIN itemType it ON it.id = i.typeFk
WHERE t.landed BETWEEN vLifeScope AND COALESCE(vDated, vDateInventory)
AND t.warehouseInFk = vWarehouseFk
AND t.warehouseOutFk <> vWarehouseInventoryFk
AND it.life
AND NOT e.isExcludedFromAvailable
GROUP BY b.itemFk
) c ON i.id = c.itemFk
JOIN itemType it ON it.id = i.typeFk
HAVING ended >= COALESCE(vDated, vDateInventory) OR life IS NULL;
CREATE OR REPLACE TEMPORARY TABLE tItemDiary
ENGINE = MEMORY
2024-05-10 11:05:44 +00:00
WITH entriesIn AS (
SELECT 'entry' `type`,
tr.landed shipped,
b.quantity `in`,
NULL `out`,
st.alertLevel ,
st.name stateName,
s.name `name`,
e.invoiceNumber reference,
e.id origin,
s.id clientFk,
IF(st.`code` = 'DELIVERED', TRUE, FALSE) isPicked,
FALSE isTicket,
b.id lineFk,
NULL `order`,
NULL clientType,
NULL claimFk,
vSupplierInventoryFk inventorySupplierFk
2024-05-10 11:05:44 +00:00
FROM vn.buy b
JOIN vn.entry e ON e.id = b.entryFk
JOIN vn.travel tr ON tr.id = e.travelFk
JOIN vn.supplier s ON s.id = e.supplierFk
JOIN vn.state st ON st.`code` = IF(tr.landed < util.VN_CURDATE()
OR (util.VN_CURDATE() AND tr.isReceived),
2024-05-10 11:05:44 +00:00
'DELIVERED',
'FREE')
WHERE tr.landed >= vDateInventory
2024-07-04 07:17:22 +00:00
AND tr.warehouseInFk = vWarehouseFk
AND (s.id <> vSupplierInventoryFk OR vDated IS NULL)
AND b.itemFk = vItemFk
AND NOT e.isExcludedFromAvailable
AND NOT e.isRaid
2024-07-04 07:17:22 +00:00
),
entriesOut AS (
SELECT 'entry',
tr.shipped,
2024-05-10 11:05:44 +00:00
NULL,
b.quantity,
st.alertLevel,
st.name stateName,
2024-07-04 07:17:22 +00:00
s.name,
2024-05-10 11:05:44 +00:00
e.invoiceNumber,
e.id entryFk,
s.id supplierFk,
IF(st.`code` = 'DELIVERED' , TRUE, FALSE),
FALSE isTicket,
b.id,
NULL `order`,
NULL clientType,
NULL claimFk,
vSupplierInventoryFk
2024-05-10 11:05:44 +00:00
FROM vn.buy b
JOIN vn.entry e ON e.id = b.entryFk
JOIN vn.travel tr ON tr.id = e.travelFk
JOIN vn.warehouse w ON w.id = tr.warehouseOutFk
JOIN vn.supplier s ON s.id = e.supplierFk
JOIN vn.state st ON st.`code` = IF(tr.shipped < util.VN_CURDATE()
OR (tr.shipped = util.VN_CURDATE() AND tr.isReceived),
'DELIVERED',
'FREE')
2024-05-10 11:05:44 +00:00
JOIN vn.entryConfig ec
WHERE tr.shipped >= vDateInventory
AND vWarehouseFk = tr.warehouseOutFk
2024-07-04 07:17:22 +00:00
AND (s.id <> vSupplierInventoryFk OR vDated IS NULL)
2024-05-10 11:05:44 +00:00
AND b.itemFk = vItemFk
AND NOT e.isExcludedFromAvailable
AND NOT w.isFeedStock
AND NOT e.isRaid
2024-07-04 07:17:22 +00:00
),
sales AS (
WITH itemSales AS (
SELECT DATE(t.shipped) shipped,
s.quantity,
st2.alertLevel,
st2.name,
t.nickname,
t.refFk,
t.id ticketFk,
t.clientFk,
s.id saleFk,
st.`order`,
c.typeFk,
cb.claimFk
FROM vn.sale s
JOIN vn.ticket t ON t.id = s.ticketFk
LEFT JOIN vn.ticketState ts ON ts.ticketFk = t.id
LEFT JOIN vn.state st ON st.`code` = ts.`code`
JOIN vn.client c ON c.id = t.clientFk
JOIN vn.state st2 ON st2.`code` = IF(t.shipped < util.VN_CURDATE(),
'DELIVERED',
IF (t.shipped > util.dayEnd(util.VN_CURDATE()),
'FREE',
IFNULL(ts.code, 'FREE')))
LEFT JOIN vn.claimBeginning cb ON s.id = cb.saleFk
WHERE t.shipped >= vDateInventory
AND s.itemFk = vItemFk
AND vWarehouseFk = t.warehouseFk
)
SELECT 'ticket',
s.shipped,
NULL `in`,
s.quantity,
s.alertLevel,
s.name,
s.nickname,
s.refFk,
s.ticketFk,
s.clientFk,
2024-07-04 07:17:22 +00:00
IF(stk.saleFk, TRUE, FALSE),
TRUE,
s.saleFk,
s.`order`,
s.typeFk,
s.claimFk,
NULL
2024-07-04 07:17:22 +00:00
FROM itemSales s
2024-05-10 11:05:44 +00:00
LEFT JOIN vn.state stPrep ON stPrep.`code` = 'PREPARED'
LEFT JOIN vn.saleTracking stk ON stk.saleFk = s.saleFk
AND stk.stateFk = stPrep.id
GROUP BY s.saleFk
2024-07-04 07:17:22 +00:00
),
orders AS (
SELECT 'order' `type`,
r.shipment,
2024-07-04 07:17:22 +00:00
NULL 'in',
r.amount,
NULL alertLevel,
NULL stateName,
c.name,
2024-07-04 07:17:22 +00:00
NULL invoiceNumber,
o.id origin,
c.id,
2024-07-04 07:17:22 +00:00
FALSE,
FALSE isTicket,
NULL buyFk,
NULL 'order',
c.typeFk,
NULL claimFk,
NULL
2024-07-04 07:17:22 +00:00
FROM hedera.orderRow r
JOIN hedera.`order` o ON o.id = r.orderFk
JOIN tItemRange ir ON ir.itemFk = r.itemFk
JOIN vn.client c ON c.id = o.customer_id
WHERE r.shipment >= vDateInventory
AND (ir.ended IS NULL OR r.shipment <= ir.ended)
AND r.warehouseFk = vWarehouseFk
AND NOT o.confirmed
AND r.itemFk = vItemFk
)
SELECT * FROM entriesIn
2024-05-10 11:05:44 +00:00
UNION ALL
SELECT * FROM entriesOut
UNION ALL
2024-07-04 07:17:22 +00:00
SELECT * FROM sales
UNION ALL
SELECT * FROM orders
2024-05-10 11:05:44 +00:00
ORDER BY shipped,
(inventorySupplierFk = clientFk) DESC,
alertLevel DESC,
isTicket,
`order` DESC,
isPicked DESC,
`in` DESC,
`out` DESC;
2024-07-04 07:17:22 +00:00
IF vDated IS NULL THEN
SET @a := 0;
SET @currentLineFk := 0;
SET @shipped := '';
SELECT t.type,
DATE(@shipped:= t.shipped) shipped,
2024-04-08 09:35:10 +00:00
t.alertLevel,
t.stateName,
t.origin,
t.reference,
t.clientFk,
t.name,
t.`in` invalue,
t.`out`,
@a := @a + IFNULL(t.`in`, 0) - IFNULL(t.`out`, 0) balance,
@currentLineFk := IF (@shipped < util.VN_CURDATE()
2024-04-08 11:28:38 +00:00
OR (@shipped = util.VN_CURDATE() AND (t.isPicked OR a.`code` >= 'ON_PREPARATION')),
2024-04-08 09:35:10 +00:00
t.lineFk,
@currentLineFk) lastPreparedLineFk,
2024-04-08 09:35:10 +00:00
t.isTicket,
t.lineFk,
t.isPicked,
t.clientType,
t.claimFk,
t.`order`
2024-04-08 09:35:10 +00:00
FROM tItemDiary t
LEFT JOIN alertLevel a ON a.id = t.alertLevel;
ELSE
2024-05-03 05:51:04 +00:00
SELECT IFNULL(SUM(IFNULL(`in`, 0)) - SUM(IFNULL(`out`, 0)), 0) INTO @a
FROM tItemDiary
2024-07-04 07:17:22 +00:00
WHERE shipped < vDated;
SELECT NULL `type`,
vDated shipped,
NULL alertLevel,
NULL stateName,
NULL origin,
NULL reference,
NULL clientFk,
'Inventario calculado',
@a invalue,
NULL `out`,
@a balance,
NULL lastPreparedLineFk,
NULL isTicket,
NULL lineFk,
NULL isPicked,
NULL clientType,
NULL claimFk,
NULL `order`
UNION ALL
SELECT `type`,
shipped,
alertlevel,
stateName,
origin,
reference,
clientFk,
name, `in`,
`out`,
@a := @a + IFNULL(`in`, 0) - IFNULL(`out`, 0),
0,
isTicket,
lineFk,
isPicked,
clientType,
2024-04-08 08:06:33 +00:00
claimFk,
`order`
FROM tItemDiary
2024-07-04 07:17:22 +00:00
WHERE shipped >= vDated;
END IF;
2024-07-04 07:17:22 +00:00
DROP TEMPORARY TABLE
tItemDiary,
tItemRange;
END$$
DELIMITER ;