salix/print/templates/reports/invoice-incoterms/sql/incoterms.sql

32 lines
954 B
MySQL
Raw Normal View History

2024-06-20 11:45:38 +00:00
WITH tickets AS(
SELECT id, packages, addressFk, weight
FROM ticket
2024-06-20 12:03:31 +00:00
WHERE refFk= ?
2024-06-20 11:45:38 +00:00
), volume AS(
SELECT SUM(volume) volume
FROM tickets t
JOIN vn.saleVolume sv ON sv.ticketFk = t.id
), intrastat AS(
SELECT GROUP_CONCAT(DISTINCT ir.description ORDER BY ir.description SEPARATOR '. ' ) intrastat
FROM tickets t
JOIN vn.sale s ON t.id = s.ticketFk
JOIN vn.item i ON i.id = s.itemFk
JOIN vn.intrastat ir ON ir.id = i.intrastatFk
2024-06-26 15:21:22 +00:00
)SELECT SUM(t.packages) packages,
2024-06-20 11:45:38 +00:00
a.incotermsFk,
ic.name incotermsName,
2024-06-21 09:47:34 +00:00
MAX(t.weight) weight,
2024-06-20 11:45:38 +00:00
ca.fiscalName customsAgentName,
ca.street customsAgentStreet,
ca.nif customsAgentNif,
ca.phone customsAgentPhone,
ca.email customsAgentEmail,
CAST(v.volume AS DECIMAL (10,2)) volume,
i.intrastat
FROM tickets t
JOIN vn.address a ON a.id = t.addressFk
JOIN vn.incoterms ic ON ic.code = a.incotermsFk
LEFT JOIN vn.customsAgent ca ON ca.id = a.customsAgentFk
JOIN volume v
JOIN intrastat i