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

36 lines
1.1 KiB
MySQL
Raw Normal View History

2024-06-20 11:45:38 +00:00
WITH tickets AS(
SELECT id, addressFk, packages, refFk
FROM vn.ticket
WHERE refFk = ?
2024-06-20 11:45:38 +00:00
), volume AS(
SELECT SUM(volume) volume, MAX(weight)weight
2024-06-20 11:45:38 +00:00
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
), totalPackages AS(
SELECT SUM(packages)packages
FROM tickets s
)
SELECT tp.packages,
io.incotermsFk,
2024-06-20 11:45:38 +00:00
ic.name incotermsName,
v.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 vn.invoiceOut io
JOIN vn.incoterms ic ON ic.code = io.incotermsFk
LEFT JOIN vn.customsAgent ca ON ca.id = io.customsAgentFk
2024-06-20 11:45:38 +00:00
JOIN volume v
JOIN intrastat i
JOIN totalPackages tp
WHERE `ref` = (SELECT DISTINCT refFk FROM tickets)