Merge pull request 'feat: refs #6769 itemDiary' (!2683) from 6769-itemDiary into dev
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
Reviewed-on: #2683 Reviewed-by: Javi Gallego <jgallego@verdnatura.es>
This commit is contained in:
commit
f8f6950de1
|
@ -2,94 +2,84 @@ DELIMITER $$
|
|||
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`item_getBalance`(
|
||||
vItemFk INT,
|
||||
vWarehouseFk INT,
|
||||
vDate DATETIME
|
||||
vDated DATETIME
|
||||
)
|
||||
BEGIN
|
||||
/**
|
||||
* @vItemFk item a buscar
|
||||
* @vWarehouseFk almacen donde buscar
|
||||
* @vDate 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 vDate.
|
||||
* Calcula el balance de un artículo.
|
||||
*
|
||||
* @vItemFk Id de artículo
|
||||
* @vWarehouseFk Id de almacén
|
||||
* @vDated Fecha a calcular, si es NULL muestra el histórico desde el inventario
|
||||
*/
|
||||
DECLARE vDateInventory DATETIME;
|
||||
DECLARE vDateInventory DATETIME
|
||||
DEFAULT (
|
||||
SELECT IF(vDated, uc.mockUtcTime, c.inventoried)
|
||||
FROM config c
|
||||
JOIN util.config uc
|
||||
);
|
||||
DECLARE vSupplierInventoryFk INT
|
||||
DEFAULT (SELECT supplierFk FROM inventoryConfig);
|
||||
|
||||
IF vDate IS NULL THEN
|
||||
SELECT inventoried INTO vDateInventory
|
||||
FROM config;
|
||||
ELSE
|
||||
SELECT mockUtcTime INTO vDateInventory
|
||||
FROM util.config;
|
||||
IF NOT vSupplierInventoryFk OR NOT vDateInventory THEN
|
||||
CALL util.throw('Config variables are not set');
|
||||
END IF;
|
||||
|
||||
CREATE OR REPLACE TEMPORARY TABLE tItemDiary(
|
||||
shipped DATE,
|
||||
`in` INT(11),
|
||||
`out` INT(11),
|
||||
alertLevel INT(11),
|
||||
stateName VARCHAR(20),
|
||||
`name` VARCHAR(50),
|
||||
reference VARCHAR(50),
|
||||
origin INT(11),
|
||||
clientFk INT(11),
|
||||
isPicked INT(11),
|
||||
isTicket TINYINT(1),
|
||||
lineFk INT(11),
|
||||
`order` TINYINT(3) UNSIGNED,
|
||||
clientType VARCHAR(20),
|
||||
claimFk INT(10) UNSIGNED,
|
||||
inventorySupplierFk INT(10)
|
||||
);
|
||||
|
||||
INSERT INTO tItemDiary
|
||||
CREATE OR REPLACE TEMPORARY TABLE tItemDiary
|
||||
ENGINE = MEMORY
|
||||
WITH entriesIn AS (
|
||||
SELECT tr.landed shipped,
|
||||
SELECT 'entry' originType,
|
||||
e.id originId,
|
||||
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,
|
||||
'supplier' entityType,
|
||||
s.id entityId,
|
||||
s.name entityName,
|
||||
IF(st.`code` = 'DELIVERED', TRUE, FALSE) isPicked,
|
||||
FALSE isTicket,
|
||||
b.id lineFk,
|
||||
NULL `order`,
|
||||
NULL clientType,
|
||||
NULL claimFk,
|
||||
ec.inventorySupplierFk
|
||||
vSupplierInventoryFk inventorySupplierFk
|
||||
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()
|
||||
JOIN vn.state st ON st.`code` = IF(tr.landed < util.VN_CURDATE()
|
||||
OR (util.VN_CURDATE() AND tr.isReceived),
|
||||
'DELIVERED',
|
||||
'FREE')
|
||||
JOIN vn.entryConfig ec
|
||||
WHERE tr.landed >= vDateInventory
|
||||
AND vWarehouseFk = tr.warehouseInFk
|
||||
AND (s.id <> ec.inventorySupplierFk OR vDate IS NULL)
|
||||
AND tr.warehouseInFk = vWarehouseFk
|
||||
AND (s.id <> vSupplierInventoryFk OR vDated IS NULL)
|
||||
AND b.itemFk = vItemFk
|
||||
AND NOT e.isExcludedFromAvailable
|
||||
AND NOT e.isRaid
|
||||
), entriesOut AS (
|
||||
SELECT tr.shipped,
|
||||
),
|
||||
entriesOut AS (
|
||||
SELECT 'entry',
|
||||
e.id originType,
|
||||
tr.shipped,
|
||||
NULL,
|
||||
b.quantity,
|
||||
st.alertLevel,
|
||||
st.name stateName,
|
||||
s.name ,
|
||||
e.invoiceNumber,
|
||||
e.id entryFk,
|
||||
s.id supplierFk,
|
||||
'supplier' entityType,
|
||||
s.id entityId,
|
||||
s.name,
|
||||
IF(st.`code` = 'DELIVERED' , TRUE, FALSE),
|
||||
FALSE isTicket,
|
||||
b.id,
|
||||
NULL `order`,
|
||||
NULL clientType,
|
||||
NULL claimFk,
|
||||
ec.inventorySupplierFk
|
||||
vSupplierInventoryFk
|
||||
FROM vn.buy b
|
||||
JOIN vn.entry e ON e.id = b.entryFk
|
||||
JOIN vn.travel tr ON tr.id = e.travelFk
|
||||
|
@ -99,23 +89,25 @@ BEGIN
|
|||
OR (tr.shipped = util.VN_CURDATE() AND tr.isReceived),
|
||||
'DELIVERED',
|
||||
'FREE')
|
||||
JOIN vn.entryConfig ec
|
||||
WHERE tr.shipped >= vDateInventory
|
||||
AND vWarehouseFk = tr.warehouseOutFk
|
||||
AND (s.id <> ec.inventorySupplierFk OR vDate IS NULL)
|
||||
AND tr.warehouseOutFk = vWarehouseFk
|
||||
AND (s.id <> vSupplierInventoryFk OR vDated IS NULL)
|
||||
AND b.itemFk = vItemFk
|
||||
AND NOT e.isExcludedFromAvailable
|
||||
AND NOT w.isFeedStock
|
||||
AND NOT e.isRaid
|
||||
), sales AS (
|
||||
),
|
||||
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,
|
||||
'client' entityType,
|
||||
t.clientFk entityId,
|
||||
t.nickname,
|
||||
s.id saleFk,
|
||||
st.`order`,
|
||||
c.typeFk,
|
||||
|
@ -123,62 +115,82 @@ BEGIN
|
|||
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`
|
||||
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
|
||||
IFNULL(st.code, 'FREE')))
|
||||
LEFT JOIN vn.claimBeginning cb ON cb.saleFk = s.id
|
||||
WHERE t.shipped >= vDateInventory
|
||||
AND s.itemFk = vItemFk
|
||||
AND vWarehouseFk = t.warehouseFk
|
||||
),sale AS (
|
||||
SELECT s.shipped,
|
||||
AND t.warehouseFk = vWarehouseFk
|
||||
)
|
||||
SELECT 'ticket',
|
||||
s.ticketFk,
|
||||
s.shipped,
|
||||
NULL `in`,
|
||||
s.quantity,
|
||||
s.alertLevel,
|
||||
s.name,
|
||||
s.nickname,
|
||||
s.refFk,
|
||||
s.ticketFk,
|
||||
s.clientFk,
|
||||
IF(stk.saleFk, TRUE, NULL),
|
||||
s.entityType,
|
||||
s.entityId,
|
||||
s.nickname,
|
||||
IF(stk.saleFk, TRUE, FALSE),
|
||||
TRUE,
|
||||
s.saleFk,
|
||||
s.`order`,
|
||||
s.typeFk,
|
||||
s.claimFk,
|
||||
NULL
|
||||
FROM sales s
|
||||
FROM itemSales s
|
||||
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
|
||||
) SELECT shipped,
|
||||
`in`,
|
||||
`out`,
|
||||
alertLevel,
|
||||
stateName,
|
||||
`name`,
|
||||
reference,
|
||||
origin,
|
||||
clientFk,
|
||||
isPicked,
|
||||
isTicket,
|
||||
lineFk,
|
||||
`order`,
|
||||
clientType,
|
||||
claimFk,
|
||||
inventorySupplierFk
|
||||
FROM entriesIn
|
||||
),
|
||||
orders AS (
|
||||
SELECT 'order' originType,
|
||||
o.id originId,
|
||||
r.shipment,
|
||||
NULL 'in',
|
||||
r.amount,
|
||||
NULL alertLevel,
|
||||
NULL stateName,
|
||||
NULL invoiceNumber,
|
||||
'client' entityType,
|
||||
c.id,
|
||||
c.name,
|
||||
FALSE,
|
||||
FALSE isTicket,
|
||||
NULL buyFk,
|
||||
NULL 'order',
|
||||
c.typeFk,
|
||||
NULL claimFk,
|
||||
NULL
|
||||
FROM hedera.orderRow r
|
||||
JOIN hedera.`order` o ON o.id = r.orderFk
|
||||
JOIN vn.client c ON c.id = o.customer_id
|
||||
WHERE r.shipment >= vDateInventory
|
||||
AND r.warehouseFk = vWarehouseFk
|
||||
AND r.created >= (
|
||||
SELECT SUBTIME(util.VN_NOW(), reserveTime)
|
||||
FROM hedera.orderConfig
|
||||
)
|
||||
AND NOT o.confirmed
|
||||
AND r.itemFk = vItemFk
|
||||
)
|
||||
SELECT * FROM entriesIn
|
||||
UNION ALL
|
||||
SELECT * FROM entriesOut
|
||||
UNION ALL
|
||||
SELECT * FROM sale
|
||||
SELECT * FROM sales
|
||||
UNION ALL
|
||||
SELECT * FROM orders
|
||||
ORDER BY shipped,
|
||||
(inventorySupplierFk = clientFk) DESC,
|
||||
(inventorySupplierFk = entityId) DESC,
|
||||
alertLevel DESC,
|
||||
isTicket,
|
||||
`order` DESC,
|
||||
|
@ -186,19 +198,20 @@ BEGIN
|
|||
`in` DESC,
|
||||
`out` DESC;
|
||||
|
||||
IF vDate IS NULL THEN
|
||||
|
||||
IF vDated IS NULL THEN
|
||||
SET @a := 0;
|
||||
SET @currentLineFk := 0;
|
||||
SET @shipped := '';
|
||||
|
||||
SELECT DATE(@shipped:= t.shipped) shipped,
|
||||
SELECT t.originType,
|
||||
t.originId,
|
||||
DATE(@shipped:= t.shipped) shipped,
|
||||
t.alertLevel,
|
||||
t.stateName,
|
||||
t.origin,
|
||||
t.reference,
|
||||
t.clientFk,
|
||||
t.name,
|
||||
t.entityType,
|
||||
t.entityId,
|
||||
t.entityName,
|
||||
t.`in` invalue,
|
||||
t.`out`,
|
||||
@a := @a + IFNULL(t.`in`, 0) - IFNULL(t.`out`, 0) balance,
|
||||
|
@ -214,37 +227,41 @@ BEGIN
|
|||
t.`order`
|
||||
FROM tItemDiary t
|
||||
LEFT JOIN alertLevel a ON a.id = t.alertLevel;
|
||||
|
||||
ELSE
|
||||
SELECT IFNULL(SUM(IFNULL(`in`, 0)) - SUM(IFNULL(`out`, 0)), 0) INTO @a
|
||||
FROM tItemDiary
|
||||
WHERE shipped < vDate;
|
||||
WHERE shipped < vDated;
|
||||
|
||||
SELECT vDate shipped,
|
||||
0 alertLevel,
|
||||
0 stateName,
|
||||
0 origin,
|
||||
'' reference,
|
||||
0 clientFk,
|
||||
SELECT NULL originType,
|
||||
NULL originId,
|
||||
vDated shipped,
|
||||
NULL alertLevel,
|
||||
NULL stateName,
|
||||
NULL reference,
|
||||
NULL entityType,
|
||||
NULL entityId,
|
||||
'Inventario calculado',
|
||||
@a invalue,
|
||||
NULL `out`,
|
||||
@a balance,
|
||||
0 lastPreparedLineFk,
|
||||
0 isTicket,
|
||||
0 lineFk,
|
||||
0 isPicked,
|
||||
0 clientType,
|
||||
0 claimFk,
|
||||
NULL lastPreparedLineFk,
|
||||
NULL isTicket,
|
||||
NULL lineFk,
|
||||
NULL isPicked,
|
||||
NULL clientType,
|
||||
NULL claimFk,
|
||||
NULL `order`
|
||||
UNION ALL
|
||||
SELECT shipped,
|
||||
SELECT originType,
|
||||
originId,
|
||||
shipped,
|
||||
alertlevel,
|
||||
stateName,
|
||||
origin,
|
||||
reference,
|
||||
clientFk,
|
||||
name, `in`,
|
||||
entityType,
|
||||
entityId,
|
||||
entityName,
|
||||
`in`,
|
||||
`out`,
|
||||
@a := @a + IFNULL(`in`, 0) - IFNULL(`out`, 0),
|
||||
0,
|
||||
|
@ -255,7 +272,7 @@ BEGIN
|
|||
claimFk,
|
||||
`order`
|
||||
FROM tItemDiary
|
||||
WHERE shipped >= vDate;
|
||||
WHERE shipped >= vDated;
|
||||
END IF;
|
||||
|
||||
DROP TEMPORARY TABLE tItemDiary;
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
CREATE INDEX travel_landed_IDX USING BTREE ON vn.travel (landed DESC,warehouseInFk,warehouseOutFk);
|
|
@ -61,7 +61,7 @@ describe('item getBalance()', () => {
|
|||
const secondItemBalance = await models.Item.getBalance(ctx, secondFilter, options);
|
||||
|
||||
expect(firstItemBalance[9].claimFk).toEqual(null);
|
||||
expect(secondItemBalance[7].claimFk).toEqual(2);
|
||||
expect(secondItemBalance[7].claimFk).toEqual(1);
|
||||
|
||||
await tx.rollback();
|
||||
} catch (e) {
|
||||
|
|
Loading…
Reference in New Issue