75 lines
1.9 KiB
SQL
75 lines
1.9 KiB
SQL
DELIMITER $$
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `stock`.`log_refreshBuy`(
|
|
`vTableName` VARCHAR(255),
|
|
`vTableId` INT)
|
|
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,
|
|
t.daysInForward,
|
|
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,
|
|
t.shipped < vn.getInventoryDate() lessThanInventory
|
|
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 NOT daysInForward
|
|
FROM tValues
|
|
WHERE isIn OR !lessThanInventory;
|
|
|
|
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 NOT daysInForward
|
|
FROM tValues
|
|
WHERE !isIn OR !lessThanInventory;
|
|
|
|
DROP TEMPORARY TABLE tValues;
|
|
END$$
|
|
DELIMITER ;
|