90 lines
2.4 KiB
MySQL
90 lines
2.4 KiB
MySQL
|
DELIMITER $$
|
||
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_doCmr`(vSelf INT)
|
||
|
BEGIN
|
||
|
/**
|
||
|
* Crea u actualiza la información del CMR asociado con
|
||
|
* un ticket específico en caso de que sea necesario.
|
||
|
*
|
||
|
* @param vSelf El id del ticket
|
||
|
*/
|
||
|
DECLARE vCmrFk, vPreviousCmrFk, vCurrentCmrFk INT;
|
||
|
SELECT cmrFk INTO vCmrFk
|
||
|
FROM ticket
|
||
|
WHERE id = vSelf;
|
||
|
|
||
|
CREATE OR REPLACE TEMPORARY TABLE tTicket
|
||
|
SELECT wo.firstName,
|
||
|
v.numberPlate,
|
||
|
com.id companyFk,
|
||
|
a.id addressFk,
|
||
|
c2.defaultAddressFk,
|
||
|
su.id supplierFk,
|
||
|
t.landed
|
||
|
FROM ticket t
|
||
|
JOIN ticketState ts ON ts.ticketFk = t.id
|
||
|
JOIN `state` s ON s.id = ts.stateFk
|
||
|
JOIN alertLevel al ON al.id = s.alertLevel
|
||
|
JOIN client c ON c.id = t.clientFk
|
||
|
JOIN `address` a ON a.id = t.addressFk
|
||
|
JOIN province p ON p.id = a.provinceFk
|
||
|
JOIN country co ON co.id = p.countryFk
|
||
|
JOIN agencyMode am ON am.id = t.agencyModeFk
|
||
|
JOIN deliveryMethod dm ON dm.id = am.deliveryMethodFk
|
||
|
JOIN warehouse w ON w.id = t.warehouseFk
|
||
|
JOIN company com ON com.id = t.companyFk
|
||
|
JOIN client c2 ON c2.id = com.clientFk
|
||
|
JOIN supplierAccount sa ON sa.id = com.supplierAccountFk
|
||
|
JOIN supplier su ON su.id = sa.supplierFk
|
||
|
LEFT JOIN route r ON r.id = t.routeFk
|
||
|
LEFT JOIN worker wo ON wo.id = r.workerFk
|
||
|
LEFT JOIN vehicle v ON v.id = r.vehicleFk
|
||
|
WHERE t.shipped BETWEEN util.yesterday() AND util.dayEnd(util.VN_CURDATE())
|
||
|
AND al.code IN ('PACKED', 'DELIVERED')
|
||
|
AND co.code <> 'ES'
|
||
|
AND am.name <> 'ABONO'
|
||
|
AND w.code = 'ALG'
|
||
|
AND dm.code = 'DELIVERY'
|
||
|
AND t.id = vSelf
|
||
|
GROUP BY t.id;
|
||
|
|
||
|
START TRANSACTION;
|
||
|
|
||
|
IF vCmrFk THEN
|
||
|
UPDATE cmr c
|
||
|
JOIN tTicket t
|
||
|
SET c.senderInstruccions = t.firstName,
|
||
|
c.truckPlate = t.numberPlate,
|
||
|
c.companyFk = t.companyFk,
|
||
|
c.addressToFk = t.addressFk,
|
||
|
c.addressFromFk = t.defaultAddressFk,
|
||
|
c.supplierFk = t.supplierFk,
|
||
|
c.ead = t.landed
|
||
|
WHERE id = vCmrFk;
|
||
|
ELSE
|
||
|
SELECT MAX(id) INTO vPreviousCmrFk FROM cmr;
|
||
|
|
||
|
INSERT INTO cmr (
|
||
|
senderInstruccions,
|
||
|
truckPlate,
|
||
|
companyFk,
|
||
|
addressToFk,
|
||
|
addressFromFk,
|
||
|
supplierFk,
|
||
|
ead
|
||
|
)
|
||
|
SELECT * FROM tTicket;
|
||
|
|
||
|
SELECT MAX(id) INTO vCurrentCmrFk FROM cmr;
|
||
|
|
||
|
IF vPreviousCmrFk <> vCurrentCmrFk THEN
|
||
|
UPDATE ticket
|
||
|
SET cmrFk = vCurrentCmrFk
|
||
|
WHERE id = vSelf;
|
||
|
END IF;
|
||
|
END IF;
|
||
|
|
||
|
COMMIT;
|
||
|
DROP TEMPORARY TABLE tTicket;
|
||
|
END$$
|
||
|
DELIMITER ;
|