diff --git a/db/routines/vn/procedures/ticket_doCmr.sql b/db/routines/vn/procedures/ticket_doCmr.sql index 2da8464b4..e9cd1d659 100644 --- a/db/routines/vn/procedures/ticket_doCmr.sql +++ b/db/routines/vn/procedures/ticket_doCmr.sql @@ -18,7 +18,7 @@ BEGIN com.id companyFk, a.id addressFk, c2.defaultAddressFk, - su.id supplierFk, + IFNULL(sat.supplierFk, su.id) supplierFk, t.landed FROM ticket t JOIN ticketState ts ON ts.ticketFk = t.id @@ -28,15 +28,18 @@ BEGIN 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 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 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 + 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 WHERE al.code IN ('PACKED', 'DELIVERED') AND co.code <> 'ES' AND am.name <> 'ABONO'