2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `stock`.`log_refreshBuy`(
|
2024-04-08 07:11:40 +00:00
|
|
|
`vTableName` VARCHAR(255),
|
|
|
|
`vTableId` INT)
|
2024-01-15 11:31:03 +00:00
|
|
|
BEGIN
|
|
|
|
DROP TEMPORARY TABLE IF EXISTS tValues;
|
|
|
|
CREATE TEMPORARY TABLE tValues
|
|
|
|
ENGINE = MEMORY
|
|
|
|
SELECT
|
|
|
|
b.id buyFk,
|
|
|
|
e.id entryFk,
|
|
|
|
t.id travelFk,
|
|
|
|
b.itemFk,
|
2024-04-08 07:11:40 +00:00
|
|
|
e.isRaid,
|
2024-01-15 11:31:03 +00:00
|
|
|
ADDTIME(t.shipped,
|
|
|
|
IFNULL(t.shipmentHour, '00:00:00')) shipped,
|
|
|
|
t.warehouseOutFk,
|
|
|
|
t.isDelivered,
|
|
|
|
ADDTIME(t.landed,
|
|
|
|
IFNULL(t.landingHour, '00:00:00')) landed,
|
|
|
|
t.warehouseInFk,
|
|
|
|
t.isReceived,
|
|
|
|
tp.life,
|
|
|
|
ABS(b.quantity) quantity,
|
|
|
|
b.created,
|
|
|
|
b.quantity > 0 isIn,
|
2024-04-08 07:11:40 +00:00
|
|
|
t.shipped < vn.getInventoryDate() lessThanInventory
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM vn.buy b
|
|
|
|
JOIN vn.entry e ON e.id = b.entryFk
|
|
|
|
JOIN vn.travel t ON t.id = e.travelFk
|
|
|
|
JOIN vn.item i ON i.id = b.itemFk
|
|
|
|
JOIN vn.itemType tp ON tp.id = i.typeFk
|
|
|
|
WHERE (
|
|
|
|
vTableId IS NULL
|
|
|
|
OR (vTableName = 'travel' AND t.id = vTableId)
|
|
|
|
OR (vTableName = 'entry' AND e.id = vTableId)
|
|
|
|
OR (vTableName = 'buy' AND b.id = vTableId)
|
|
|
|
)
|
|
|
|
AND t.landed >= vn.getInventoryDate()
|
|
|
|
AND b.quantity != 0;
|
|
|
|
|
|
|
|
REPLACE INTO inbound (
|
|
|
|
tableName, tableId, warehouseFk, dated,
|
|
|
|
itemFk, expired, quantity, isPicked
|
|
|
|
)
|
|
|
|
SELECT 'buy',
|
|
|
|
buyFk,
|
|
|
|
IF(isIn, warehouseInFk, warehouseOutFk),
|
|
|
|
@dated := IF(isIn, landed, shipped),
|
|
|
|
itemFk,
|
|
|
|
TIMESTAMPADD(DAY, life, @dated),
|
|
|
|
quantity,
|
|
|
|
IF(isIn, isReceived, isDelivered) AND !isRaid
|
|
|
|
FROM tValues
|
2024-04-08 07:11:40 +00:00
|
|
|
WHERE isIn OR !lessThanInventory;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
REPLACE INTO outbound (
|
|
|
|
tableName, tableId, warehouseFk, dated,
|
|
|
|
itemFk, created, quantity, isPicked
|
|
|
|
)
|
|
|
|
SELECT 'buy',
|
|
|
|
buyFk,
|
|
|
|
IF(isIn, warehouseOutFk, warehouseInFk),
|
|
|
|
IF(isIn, shipped, landed),
|
|
|
|
itemFk,
|
|
|
|
created,
|
|
|
|
quantity,
|
|
|
|
IF(isIn, isDelivered, isReceived) AND !isRaid
|
|
|
|
FROM tValues
|
2024-04-08 07:11:40 +00:00
|
|
|
WHERE !isIn OR !lessThanInventory;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
DROP TEMPORARY TABLE tValues;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|