salix/db/routines/vn/procedures/sale_getBoxPickingList.sql

74 lines
2.2 KiB
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`sale_getBoxPickingList`(vSectorFk INT, vDated DATE)
BEGIN
/**
* Returns a suitable boxPicking sales list
*
* @param vSectorFk Identifier for vn.sector table
* @param vDated Date for vn.tickets.shipping
*
* @return tmp.sale
*/
DECLARE vWarehouseFk INT;
SELECT warehouseFk INTO vWarehouseFk
FROM sector
WHERE id = vSectorFk;
CALL productionControl(vWarehouseFk, 0);
CREATE OR REPLACE TEMPORARY TABLE tmp.sale
(saleFk INT PRIMARY KEY)
SELECT
s.ticketFk,
s.id saleFk,
s.itemFk,
s.concept,
s.quantity,
MAKETIME(pb.HH,pb.mm,0) etd,
pb.routeFk,
FLOOR(s.quantity / IF(i.isBoxPickingMode, ish.packing, i.packingOut)) stickers,
IF(i.isBoxPickingMode, ish.packing, i.packingOut) packing,
b.packagingFk
FROM sale s
JOIN item i ON i.id = s.itemFk
JOIN itemShelving ish ON ish.itemFk = s.itemFk
LEFT JOIN ( SELECT iss.itemShelvingFk,
s.itemFk,
SUM(iss.quantity) reserve
FROM itemShelvingSale iss
JOIN sale s ON s.id = iss.saleFk
WHERE iss.isPicked = FALSE
AND iss.created >= vDated
GROUP BY iss.itemShelvingFk, s.itemFk) tISS
ON tISS.itemFk = ish.itemFk AND tISS.itemShelvingFk = ish.id
JOIN shelving sh ON sh.code = ish.shelvingFk
JOIN parking p ON p.id = sh.parkingFk
JOIN tmp.productionBuffer pb ON pb.ticketFk = s.ticketFk
JOIN agencyMode am ON am.id = pb.agencyModeFk
LEFT JOIN routesMonitor rm ON rm.routeFk = pb.routeFk
LEFT JOIN saleGroupDetail sgd ON sgd.saleFk = s.id
LEFT JOIN ticketState ts ON ts.ticketFk = s.ticketFk
LEFT JOIN cache.last_buy lb ON lb.item_id = i.id AND lb.warehouse_id = vWarehouseFk
LEFT JOIN buy b ON b.id = lb.buy_id
WHERE IF(i.isBoxPickingMode, ish.packing, i.packingOut)
<= LEAST(s.quantity, ish.visible - IFNULL(tISS.reserve,0))
AND NOT pb.problem
AND sgd.saleFk IS NULL
AND p.sectorFk = vSectorFk
AND ts.isPreviousPreparable
AND ((rm.bufferFk AND rm.isPickingAllowed)
OR am.code = 'REC_ALG')
AND pb.shipped = vDated
GROUP BY s.id
ORDER BY etd;
SELECT *
FROM tmp.sale
WHERE stickers;
DROP TEMPORARY TABLE tmp.productionBuffer;
DROP TEMPORARY TABLE tmp.sale;
END$$
DELIMITER ;