salix/db/routines/vn2008/procedures/agencia_volume.sql

45 lines
1.4 KiB
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn2008`.`agencia_volume`()
BEGIN
DECLARE vStarted DATETIME DEFAULT TIMESTAMP(util.VN_CURDATE());
DECLARE vEnded DATETIME DEFAULT TIMESTAMP(util.VN_CURDATE(), '23:59:59');
DROP TEMPORARY TABLE IF EXISTS tmp.ticket_PackagingEstimated;
CREATE TEMPORARY TABLE tmp.ticket_PackagingEstimated
(
ticketFk INT PRIMARY KEY
,boxes INT DEFAULT 0
);
INSERT INTO tmp.ticket_PackagingEstimated(ticketFk, boxes)
SELECT sv.ticketFk, CEIL(1000 * sum(sv.volume) / vc.standardFlowerBox)
FROM vn.ticket t
JOIN vn.saleVolume sv ON sv.ticketFk = t.id
JOIN vn.volumeConfig vc
WHERE t.shipped BETWEEN vStarted AND vEnded
AND IFNULL(t.packages,0) = 0
GROUP BY t.id;
SELECT * FROM
(
SELECT ag.id agency_id,
CONCAT(RPAD(c.country, 16,' _') ,' ',ag.name) Agencia,
count(*) expediciones,
sum(t.packages) Bultos,
sum(tpe.boxes) Faltan
FROM vn.ticket t
JOIN vn.warehouse w ON w.id = t.warehouseFk
JOIN vn.country c ON w.countryFk = c.id
JOIN vn.address a ON a.id = t.addressFk
JOIN vn.agencyMode am ON am.id = t.agencyModeFk
JOIN vn.agency ag ON ag.id = am.agencyFk
JOIN tmp.ticket_PackagingEstimated tpe ON tpe.ticketFk = t.id
WHERE t.shipped BETWEEN vStarted AND vEnded
AND ag.isOwn = FALSE
GROUP BY ag.id
) sub
ORDER BY Agencia;
DROP TEMPORARY TABLE tmp.ticket_PackagingEstimated;
END$$
DELIMITER ;