2024-10-01 09:30:34 +00:00
|
|
|
DELIMITER $$
|
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`stockBought_calculate`(
|
|
|
|
vDated DATE
|
|
|
|
)
|
|
|
|
proc: BEGIN
|
|
|
|
/**
|
|
|
|
* Calculate the stock of the auction warehouse from the inventory date to vDated
|
|
|
|
* without taking into account the outputs of the same day vDated
|
|
|
|
*
|
2024-11-12 06:23:46 +00:00
|
|
|
* @param vDated Date to calculate the stock
|
2024-10-01 09:30:34 +00:00
|
|
|
*/
|
|
|
|
IF vDated < util.VN_CURDATE() THEN
|
|
|
|
LEAVE proc;
|
|
|
|
END IF;
|
|
|
|
|
2024-11-08 12:44:20 +00:00
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tCurrentData
|
2024-10-01 09:30:34 +00:00
|
|
|
SELECT workerFk, reserve
|
|
|
|
FROM stockBought
|
|
|
|
WHERE dated = vDated
|
|
|
|
AND reserve;
|
|
|
|
|
|
|
|
DELETE FROM stockBought WHERE dated = vDated;
|
|
|
|
|
2024-11-08 12:44:20 +00:00
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tStockSold
|
|
|
|
SELECT it.workerFk,
|
|
|
|
SUM(ic.cm3 * ito.quantity / vc.palletM3 / 1000000) sold
|
|
|
|
FROM itemTicketOut ito
|
|
|
|
JOIN item i ON i.id = ito.itemFk
|
|
|
|
JOIN itemType it ON it.id = i.typeFk
|
|
|
|
JOIN ticket t ON t.id = ito.ticketFk
|
|
|
|
JOIN warehouse wh ON wh.id = t.warehouseFk
|
|
|
|
JOIN itemCost ic ON ic.itemFk = ito.itemFk
|
|
|
|
AND ic.warehouseFk = t.warehouseFk
|
|
|
|
JOIN volumeConfig vc
|
|
|
|
WHERE ito.shipped BETWEEN vDated AND util.dayEnd(vDated)
|
|
|
|
AND wh.code = 'VNH'
|
|
|
|
GROUP BY it.workerFk;
|
|
|
|
|
2024-10-01 09:30:34 +00:00
|
|
|
CALL item_calculateStock(vDated);
|
|
|
|
|
2024-11-08 12:44:20 +00:00
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tStockBought
|
2024-10-01 09:30:34 +00:00
|
|
|
SELECT it.workerFk,
|
2024-11-08 12:44:20 +00:00
|
|
|
SUM((ti.quantity / b.packing) *
|
|
|
|
buy_getVolume(b.id)
|
|
|
|
) / vc.palletM3 / 1000000 bought
|
2024-10-01 09:30:34 +00:00
|
|
|
FROM itemType it
|
|
|
|
JOIN item i ON i.typeFk = it.id
|
|
|
|
LEFT JOIN tmp.item ti ON ti.itemFk = i.id
|
|
|
|
JOIN itemCategory ic ON ic.id = it.categoryFk
|
|
|
|
JOIN warehouse wh ON wh.code = 'VNH'
|
|
|
|
JOIN tmp.buyUltimate bu ON bu.itemFk = i.id
|
|
|
|
AND bu.warehouseFk = wh.id
|
|
|
|
JOIN buy b ON b.id = bu.buyFk
|
|
|
|
JOIN volumeConfig vc
|
|
|
|
WHERE ic.display
|
|
|
|
GROUP BY it.workerFk
|
|
|
|
HAVING bought;
|
|
|
|
|
2024-11-08 12:44:20 +00:00
|
|
|
|
|
|
|
INSERT INTO stockBought(workerFk, bought, dated)
|
|
|
|
SELECT tb.workerFk,
|
2024-11-13 08:10:18 +00:00
|
|
|
ROUND(GREATEST(tb.bought - IFNULL(ts.sold, 0), 0), 2),
|
2024-11-08 12:44:20 +00:00
|
|
|
vDated
|
|
|
|
FROM tStockBought tb
|
|
|
|
LEFT JOIN tStockSold ts ON ts.workerFk = tb.workerFk;
|
|
|
|
|
2024-10-01 09:30:34 +00:00
|
|
|
UPDATE stockBought s
|
2024-11-08 12:44:20 +00:00
|
|
|
JOIN tCurrentData ts ON ts.workerFk = s.workerFk
|
2024-10-01 09:30:34 +00:00
|
|
|
SET s.reserve = ts.reserve
|
|
|
|
WHERE s.dated = vDated;
|
|
|
|
|
|
|
|
INSERT INTO stockBought (workerFk, reserve, dated)
|
|
|
|
SELECT ts.workerFk, ts.reserve, vDated
|
2024-11-08 12:44:20 +00:00
|
|
|
FROM tCurrentData ts
|
2024-10-01 09:30:34 +00:00
|
|
|
WHERE ts.workerFk NOT IN (
|
|
|
|
SELECT workerFk
|
|
|
|
FROM stockBought
|
|
|
|
WHERE dated = vDated
|
|
|
|
);
|
|
|
|
|
2024-11-08 12:44:20 +00:00
|
|
|
UPDATE stockBought s
|
|
|
|
JOIN tStockSold ts ON ts.workerFk = s.workerFk
|
2024-11-19 05:21:00 +00:00
|
|
|
SET s.bought = IF(s.bought < ABS(ts.sold), 0, ROUND(s.bought - ABS(ts.sold), 1))
|
2024-11-08 12:44:20 +00:00
|
|
|
WHERE s.dated = vDated;
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE tCurrentData, tmp.item, tmp.buyUltimate, tStockSold;
|
2024-10-01 09:30:34 +00:00
|
|
|
END$$
|
|
|
|
DELIMITER ;
|