feat: refs #7264 Refactor proc itemShelvingRadar and added columns
gitea/salix/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Guillermo Bonet 2024-05-15 13:54:22 +02:00
parent 23eea27268
commit 53fa78894c
1 changed files with 132 additions and 119 deletions

View File

@ -2,7 +2,7 @@ DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`itemShelvingRadar`( CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`itemShelvingRadar`(
vSectorFk INT vSectorFk INT
) )
proc:BEGIN BEGIN
/** /**
* Calcula la información detallada respecto un sector. * Calcula la información detallada respecto un sector.
* *
@ -15,37 +15,24 @@ proc:BEGIN
DECLARE vWarehouseFk INT DEFAULT 0; DECLARE vWarehouseFk INT DEFAULT 0;
DECLARE vSonSectorFk INT; DECLARE vSonSectorFk INT;
DECLARE vWorkerFk INT; DECLARE vWorkerFk INT;
SELECT s.workerFk SELECT s.workerFk INTO vWorkerFk
INTO vWorkerFk FROM sector s
FROM vn.sector s
WHERE s.id = vSectorFk; WHERE s.id = vSectorFk;
SELECT w.id, s.warehouseFk INTO vBuyerFk, vWarehouseFk
FROM vn.worker w
JOIN vn.sector s ON s.code = w.code
WHERE s.id = vSectorFk;
SELECT s.id INTO vSectorFk
FROM vn.sector s
WHERE s.warehouseFk = vWarehouseFk
AND s.isMain;
SELECT COUNT(*) INTO hasFatherSector SELECT COUNT(*) INTO hasFatherSector
FROM vn.sector FROM sector
WHERE sonFk = vSectorFk; WHERE sonFk = vSectorFk;
SELECT warehouseFk, sonFk INTO vWarehouseFk, vSonSectorFk SELECT warehouseFk, sonFk INTO vWarehouseFk, vSonSectorFk
FROM vn.sector FROM sector
WHERE id = vSectorFk; WHERE id = vSectorFk;
CALL cache.visible_refresh(vCalcVisibleFk, TRUE, vWarehouseFk); CALL cache.visible_refresh(vCalcVisibleFk, TRUE, vWarehouseFk);
CALL cache.available_refresh(vCalcAvailableFk, FALSE, vWarehouseFk, util.VN_CURDATE()); CALL cache.available_refresh(vCalcAvailableFk, FALSE, vWarehouseFk, util.VN_CURDATE());
DROP TEMPORARY TABLE IF EXISTS tmp.itemShelvingRadar;
IF hasFatherSector THEN IF hasFatherSector THEN
CREATE TEMPORARY TABLE tmp.itemShelvingRadar CREATE OR REPLACE TEMPORARY TABLE tmp.itemShelvingRadar
(PRIMARY KEY (itemFk)) (PRIMARY KEY (itemFk))
ENGINE = MEMORY ENGINE = MEMORY
SELECT * SELECT *
@ -54,57 +41,77 @@ proc:BEGIN
i.longName, i.longName,
i.size, i.size,
i.subName producer, i.subName producer,
IFNULL(a.available,0) available, IFNULL(a.available, 0) available,
SUM(IF(s.sonFk = vSectorFk, IFNULL(iss.visible,0), 0)) upstairs, SUM(IF(s.sonFk = vSectorFk, IFNULL(iss.visible, 0), 0)) upstairs,
SUM(IF(iss.sectorFk = vSectorFk, IFNULL(iss.visible,0), 0)) downstairs, SUM(IF(iss.sectorFk = vSectorFk, IFNULL(iss.visible, 0), 0)) downstairs,
IF(it.isPackaging, NULL, IFNULL(v.visible,0)) as visible, IF(it.isPackaging, NULL, IFNULL(v.visible, 0)) visible,
vSectorFk sectorFk vSectorFk sectorFk,
FROM vn.itemShelvingStock iss ish.isChecked,
JOIN vn.sector s ON s.id = iss.sectorFk sub.isAllChecked
JOIN vn.item i on i.id = iss.itemFk FROM itemShelvingStock iss
JOIN vn.itemType it ON it.id = i.typeFk AND vBuyerFk IN (0,it.workerFk) JOIN itemShelving ish ON ish.shelvingFk = iss.shelvingFk
LEFT JOIN cache.available a ON a.item_id = iss.itemFk AND a.calc_id = vCalcAvailableFk LEFT JOIN (
LEFT JOIN cache.visible v ON v.item_id = iss.itemFk AND v.calc_id = vCalcVisibleFk SELECT itemFk,
IF(
COUNT(*) = SUM(IF(isChecked >= 0, 1, 0)),
TRUE,
FALSE
) isAllChecked
FROM itemShelving is2
GROUP BY itemFk
) sub ON sub.itemFk = ish.itemFk
JOIN sector s ON s.id = iss.sectorFk
JOIN item i ON i.id = iss.itemFk
JOIN itemType it ON it.id = i.typeFk
LEFT JOIN cache.available a ON a.item_id = iss.itemFk
AND a.calc_id = vCalcAvailableFk
LEFT JOIN cache.visible v ON v.item_id = iss.itemFk
AND v.calc_id = vCalcVisibleFk
WHERE vSectorFk IN (iss.sectorFk, s.sonFk) WHERE vSectorFk IN (iss.sectorFk, s.sonFk)
GROUP BY iss.itemFk GROUP BY iss.itemFk
UNION ALL UNION ALL
SELECT v.item_id,
SELECT v.item_id,
i.longName, i.longName,
i.size, i.size,
i.subName producer, i.subName,
IFNULL(a.available,0) as available, IFNULL(a.available, 0),
0 upstairs, 0,
0 downstairs, 0,
IF(it.isPackaging, NULL, v.visible) visible, IF(it.isPackaging, NULL, v.visible),
vSectorFk as sectorFk vSectorFk,
NULL,
NULL
FROM cache.visible v FROM cache.visible v
JOIN vn.item i on i.id = v.item_id JOIN item i ON i.id = v.item_id
JOIN vn.itemType it ON it.id = i.typeFk AND vBuyerFk IN (0,it.workerFk) JOIN itemType it ON it.id = i.typeFk
LEFT JOIN vn.itemShelvingStock iss ON iss.itemFk = v.item_id AND iss.warehouseFk = vWarehouseFk LEFT JOIN itemShelvingStock iss ON iss.itemFk = v.item_id
LEFT JOIN cache.available a ON a.item_id = v.item_id AND a.calc_id = vCalcAvailableFk AND iss.warehouseFk = vWarehouseFk
LEFT JOIN cache.available a ON a.item_id = v.item_id
AND a.calc_id = vCalcAvailableFk
WHERE v.calc_id = vCalcVisibleFk WHERE v.calc_id = vCalcVisibleFk
AND iss.itemFk IS NULL AND iss.itemFk IS NULL
AND it.isInventory AND it.isInventory
) sub GROUP BY itemFk; ) sub
GROUP BY itemFk;
SELECT ishr.*, SELECT ishr.*,
CAST(visible - upstairs - downstairs AS DECIMAL(10,0)) AS nicho, CAST(visible - upstairs - downstairs AS DECIMAL(10, 0)) nicho,
CAST(downstairs - IFNULL(notPickedYed,0) AS DECIMAL(10,0)) as pendiente CAST(downstairs - IFNULL(notPickedYed, 0) AS DECIMAL(10, 0)) pendiente
FROM tmp.itemShelvingRadar ishr FROM tmp.itemShelvingRadar ishr
JOIN vn.item i ON i.id = ishr.itemFk JOIN item i ON i.id = ishr.itemFk
LEFT JOIN (SELECT s.itemFk, sum(s.quantity) as notPickedYed LEFT JOIN (
FROM vn.ticket t SELECT s.itemFk, SUM(s.quantity) notPickedYed
JOIN vn.ticketStateToday tst ON tst.ticketFk = t.id FROM ticket t
JOIN vn.sale s ON s.ticketFk = t.id JOIN ticketStateToday tst ON tst.ticketFk = t.id
WHERE t.warehouseFk = vWarehouseFk JOIN alertLevel al ON al.id = tst.alertLevel
AND tst.alertLevel = 0 JOIN sale s ON s.ticketFk = t.id
GROUP BY s.itemFk WHERE t.warehouseFk = vWarehouseFk
) sub ON sub.itemFk = ishr.itemFk AND al.code = 'FREE'
ORDER BY i.typeFk, i.longName; GROUP BY s.itemFk
) sub ON sub.itemFk = ishr.itemFk
ORDER BY i.typeFk, i.longName;
ELSE ELSE
CREATE TEMPORARY TABLE tmp.itemShelvingRadar CREATE OR REPLACE TEMPORARY TABLE tmp.itemShelvingRadar
(PRIMARY KEY (itemFk)) (PRIMARY KEY (itemFk))
ENGINE = MEMORY ENGINE = MEMORY
SELECT iss.itemFk, SELECT iss.itemFk,
@ -115,73 +122,79 @@ proc:BEGIN
i.size, i.size,
i.subName producer, i.subName producer,
i.upToDown, i.upToDown,
IFNULL(a.available,0) available, IFNULL(a.available, 0) available,
IFNULL(v.visible - iss.visible,0) dayEndVisible, IFNULL(v.visible - iss.visible, 0) dayEndVisible,
IFNULL(v.visible - iss.visible,0) firstNegative, IFNULL(v.visible - iss.visible, 0) firstNegative,
IFNULL(v.visible - iss.visible,0) itemPlacementVisible, IFNULL(v.visible - iss.visible, 0) itemPlacementVisible,
IFNULL(i.minimum * b.packing,0) itemPlacementSize, IFNULL(i.minimum * b.packing, 0) itemPlacementSize,
ips.onTheWay, ips.onTheWay,
iss.visible itemShelvingStock, iss.visible itemShelvingStock,
IFNULL(v.visible,0) visible, IFNULL(v.visible, 0) visible,
b.isPickedOff, b.isPickedOff,
iss.sectorFk iss.sectorFk
FROM vn.itemShelvingStock iss FROM itemShelvingStock iss
JOIN vn.item i on i.id = iss.itemFk JOIN item i ON i.id = iss.itemFk
LEFT JOIN cache.last_buy lb ON lb.item_id = iss.itemFk AND lb.warehouse_id = vWarehouseFk LEFT JOIN cache.last_buy lb ON lb.item_id = iss.itemFk
LEFT JOIN vn.buy b ON b.id = lb.buy_id AND lb.warehouse_id = vWarehouseFk
LEFT JOIN cache.available a ON a.item_id = iss.itemFk AND a.calc_id = vCalcAvailableFk LEFT JOIN buy b ON b.id = lb.buy_id
LEFT JOIN cache.visible v ON v.item_id = iss.itemFk AND v.calc_id = vCalcVisibleFk LEFT JOIN cache.available a ON a.item_id = iss.itemFk
LEFT JOIN (SELECT itemFk, sum(saldo) as onTheWay AND a.calc_id = vCalcAvailableFk
FROM vn.itemPlacementSupplyList LEFT JOIN cache.visible v ON v.item_id = iss.itemFk
WHERE saldo > 0 AND v.calc_id = vCalcVisibleFk
GROUP BY itemFk LEFT JOIN (
) ips ON ips.itemFk = i.id SELECT itemFk, SUM(saldo) onTheWay
WHERE IFNULL(iss.sectorFk,0) IN (0, vSectorFk) FROM itemPlacementSupplyList
OR iss.sectorFk = vSectorFk; WHERE saldo > 0
GROUP BY itemFk
) ips ON ips.itemFk = i.id
WHERE iss.sectorFk = vSectorFk
OR iss.sectorFk IS NULL;
DROP TEMPORARY TABLE IF EXISTS tmp.itemOutTime; CREATE OR REPLACE TEMPORARY TABLE tmp.itemOutTime
CREATE TEMPORARY TABLE tmp.itemOutTime SELECT *, SUM(amount) quantity
SELECT *,SUM(amount) quantity FROM (
FROM SELECT io.itemFk,
(SELECT io.itemFk, io.quantity amount,
io.quantity amount, IF(HOUR(t.shipped), HOUR(t.shipped), HOUR(z.`hour`)) `hours`,
IF(HOUR(t.shipped), HOUR(t.shipped), HOUR(z.`hour`)) as hours, IF(MINUTE(t.shipped), MINUTE(t.shipped), MINUTE(z.`hour`)) `minutes`
IF(MINUTE(t.shipped), MINUTE(t.shipped), MINUTE(z.`hour`)) as minutes FROM itemTicketOut `io`
FROM itemTicketOut io JOIN tmp.itemShelvingRadar isr ON isr.itemFk = io.itemFk
JOIN tmp.itemShelvingRadar isr ON isr.itemFk = io.itemFk JOIN ticket t ON t.id= io.ticketFk
JOIN vn.ticket t on t.id= io.ticketFk JOIN ticketState ts ON ts.ticketFk = io.ticketFk
JOIN vn.ticketState ts on ts.ticketFk = io.ticketFk JOIN `state` s ON s.id = ts.stateFk
JOIN vn.state s ON s.id = ts.stateFk LEFT JOIN `zone` z ON z.id = t.zoneFk
LEFT JOIN vn.zone z ON z.id = t.zoneFk LEFT JOIN (
LEFT JOIN (SELECT DISTINCT saleFk SELECT DISTINCT saleFk
FROM vn.saleTracking st FROM saleTracking st
WHERE st.created > util.VN_CURDATE() WHERE st.created > util.VN_CURDATE()
AND st.isChecked AND st.isChecked
) stPrevious ON `stPrevious`.`saleFk` = io.saleFk ) stPrevious ON stPrevious.saleFk = io.saleFk
WHERE t.warehouseFk = vWarehouseFk WHERE t.warehouseFk = vWarehouseFk
AND s.isPicked = 0 AND NOT s.isPicked
AND NOT io.reserved AND NOT io.reserved
AND stPrevious.saleFk IS NULL AND stPrevious.saleFk IS NULL
AND io.shipped >= util.VN_CURDATE() AND io.shipped >= util.VN_CURDATE()
AND io.shipped < util.VN_CURDATE() + INTERVAL 1 DAY AND io.shipped < util.VN_CURDATE() + INTERVAL 1 DAY
) sub ) sub
GROUP BY itemFk, hours, minutes; GROUP BY itemFk, `hours`, `minutes`;
INSERT INTO tmp.itemShelvingRadar (itemFk) INSERT INTO tmp.itemShelvingRadar (itemFk)
SELECT itemFk FROM tmp.itemOutTime SELECT itemFk FROM tmp.itemOutTime
ON DUPLICATE KEY UPDATE dayEndVisible = dayEndVisible + quantity, ON DUPLICATE KEY UPDATE dayEndVisible = dayEndVisible + quantity,
firstNegative = if (firstNegative < 0, firstNegative, firstNegative + quantity), firstNegative = IF(firstNegative < 0, firstNegative, firstNegative + quantity),
`hour` = ifnull(if (firstNegative > 0 , `hour`, hours),0), `hour` = IFNULL(IF(firstNegative > 0 , `hour`, `hours`), 0),
`minute` = ifnull(if (firstNegative > 0, `minute`, minutes),0); `minute` = IFNULL(IF(firstNegative > 0, `minute`, `minutes`), 0);
UPDATE tmp.itemShelvingRadar isr UPDATE tmp.itemShelvingRadar isr
JOIN (SELECT s.itemFk, sum(s.quantity) amount JOIN (
FROM sale s SELECT s.itemFk, SUM(s.quantity) amount
JOIN ticket t ON t.id = s.ticketFk FROM sale s
JOIN ticketLastState tls ON tls.ticketFk = t.id JOIN ticket t ON t.id = s.ticketFk
WHERE t.shipped BETWEEN util.VN_CURDATE() AND util.dayend(util.VN_CURDATE()) JOIN ticketState ts ON ts.ticketFk = t.id
AND tls.name = 'Prep Camara' WHERE t.shipped BETWEEN util.VN_CURDATE() AND util.dayend(util.VN_CURDATE())
GROUP BY s.itemFk) sub ON sub.itemFk = isr.itemFk AND ts.code = 'COOLER_PREPARATION'
GROUP BY s.itemFk
) sub ON sub.itemFk = isr.itemFk
SET isr.dayEndVisible = dayEndVisible + sub.amount, SET isr.dayEndVisible = dayEndVisible + sub.amount,
firstNegative = firstNegative + sub.amount; firstNegative = firstNegative + sub.amount;