salix/print/templates/reports/invoice/sql/intrastat.sql

37 lines
1.5 KiB
SQL

SELECT *
FROM (
SELECT i.intrastatFk code,
it.description,
CAST(SUM(ROUND((s.quantity * s.price * (100 - s.discount) / 100 ) , 2))AS DECIMAL(10, 2)) subtotal,
SUM(IFNULL(i.stems, 1) * s.quantity) stems,
CAST(SUM(IFNULL(i.stems, 1)
* s.quantity
* IF(ic.grams, ic.grams, IFNULL(i.weightByPiece, 0)) / 1000)
* IF(sub.totalWeight, sub.totalWeight / vn.invoiceOut_getWeight(?), 1)
AS DECIMAL(10,2)) netKg
FROM sale s
JOIN ticket t ON s.ticketFk = t.id
JOIN supplier su ON su.id = t.companyFk
JOIN item i ON i.id = s.itemFk
JOIN intrastat it ON it.id = i.intrastatFk
LEFT JOIN itemCost ic ON ic.itemFk = i.id AND ic.warehouseFk = t.warehouseFk
LEFT JOIN (
SELECT SUM(weight)totalWeight
FROM vn.ticket
WHERE refFk = ?
AND weight
) sub ON TRUE
WHERE t.refFk =?
GROUP BY i.intrastatFk
UNION ALL
SELECT NULL ,
IF((SUM((ts.quantity * ts.price))), 'Servicios', NULL),
IFNULL(CAST(SUM((ts.quantity * ts.price)) AS DECIMAL(10,2)), 0),
0 ,
0
FROM vn.ticketService ts
JOIN vn.ticket t ON ts.ticketFk = t.id
WHERE t.refFk = ?
) sub2
WHERE `description` IS NOT NULL;