2024-04-10 10:00:52 +00:00
|
|
|
DELIMITER $$
|
2024-07-22 09:52:42 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` PROCEDURE `vn`.`ticket_doCmr`(vSelf INT)
|
2024-01-15 11:31:03 +00:00
|
|
|
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
|
|
|
|
*/
|
2024-05-07 11:03:50 +00:00
|
|
|
DECLARE vCmrFk INT;
|
2024-01-15 11:31:03 +00:00
|
|
|
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,
|
2024-06-11 08:48:24 +00:00
|
|
|
IFNULL(sat.supplierFk, su.id) supplierFk,
|
2024-01-15 11:31:03 +00:00
|
|
|
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 warehouse w ON w.id = t.warehouseFk
|
|
|
|
JOIN company com ON com.id = t.companyFk
|
|
|
|
JOIN client c2 ON c2.id = com.clientFk
|
2024-06-11 08:48:24 +00:00
|
|
|
JOIN supplierAccount sa ON sa.id = com.supplierAccountFk
|
2024-01-15 11:31:03 +00:00
|
|
|
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
|
2024-06-11 08:48:24 +00:00
|
|
|
LEFT JOIN agencyMode am ON am.id = r.agencyModeFk
|
|
|
|
LEFT JOIN agency ag ON ag.id = am.agencyFk
|
|
|
|
LEFT JOIN supplierAgencyTerm sat ON sat.agencyFk = ag.id
|
|
|
|
AND wo.isFreelance
|
2024-04-10 10:00:52 +00:00
|
|
|
WHERE al.code IN ('PACKED', 'DELIVERED')
|
2024-01-15 11:31:03 +00:00
|
|
|
AND co.code <> 'ES'
|
|
|
|
AND am.name <> 'ABONO'
|
|
|
|
AND w.code = 'ALG'
|
|
|
|
AND t.id = vSelf
|
|
|
|
GROUP BY t.id;
|
|
|
|
|
|
|
|
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;
|
2024-05-07 11:03:50 +00:00
|
|
|
ELSE
|
2024-01-15 11:31:03 +00:00
|
|
|
INSERT INTO cmr (
|
|
|
|
senderInstruccions,
|
|
|
|
truckPlate,
|
|
|
|
companyFk,
|
|
|
|
addressToFk,
|
|
|
|
addressFromFk,
|
|
|
|
supplierFk,
|
|
|
|
ead
|
|
|
|
)
|
|
|
|
SELECT * FROM tTicket;
|
|
|
|
|
2024-05-07 11:03:50 +00:00
|
|
|
IF (SELECT EXISTS(SELECT * FROM tTicket)) THEN
|
2024-01-15 11:31:03 +00:00
|
|
|
UPDATE ticket
|
2024-05-08 06:48:08 +00:00
|
|
|
SET cmrFk = LAST_INSERT_ID()
|
|
|
|
WHERE id = vSelf;
|
2024-01-15 11:31:03 +00:00
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE tTicket;
|
2024-04-10 10:00:52 +00:00
|
|
|
END$$
|
|
|
|
DELIMITER ;
|