diff --git a/print/templates/reports/invoice-incoterms/invoice-incoterms.js b/print/templates/reports/invoice-incoterms/invoice-incoterms.js index 2c183b6cc..9cc2600af 100755 --- a/print/templates/reports/invoice-incoterms/invoice-incoterms.js +++ b/print/templates/reports/invoice-incoterms/invoice-incoterms.js @@ -7,7 +7,7 @@ module.exports = { this.invoice = await this.findOneFromDef('invoice', [this.reference]); this.checkMainEntity(this.invoice); this.client = await this.findOneFromDef('client', [this.reference]); - this.incoterms = await this.findOneFromDef('incoterms', [this.reference, this.reference, this.reference]); + this.incoterms = await this.findOneFromDef('incoterms', [this.reference, this.reference, this.reference, this.reference]); }, props: { reference: { diff --git a/print/templates/reports/invoice-incoterms/sql/client.sql b/print/templates/reports/invoice-incoterms/sql/client.sql index 3e66c15c9..f3734bcd9 100644 --- a/print/templates/reports/invoice-incoterms/sql/client.sql +++ b/print/templates/reports/invoice-incoterms/sql/client.sql @@ -1,13 +1,12 @@ -SELECT - c.id, - c.socialName, - c.street AS postalAddress, - IF (ios.taxAreaFk IS NOT NULL, CONCAT(cty.code, c.fi), c.fi) fi, - CONCAT(c.postcode, ' - ', c.city) postcodeCity -FROM vn.invoiceOut io - JOIN vn.client c ON c.id = io.clientFk - JOIN vn.country cty ON cty.id = c.countryFk - LEFT JOIN vn.invoiceOutSerial ios ON ios.code = io.serial - AND ios.taxAreaFk = 'CEE' - LEFT JOIN ticket t ON t.refFk = io.ref -WHERE t.refFk = ? \ No newline at end of file +SELECT c.id, + c.socialName, + c.street postalAddress, + IF (ios.taxAreaFk IS NOT NULL, CONCAT(cty.code, c.fi), c.fi) fi, + CONCAT(c.postcode, ' - ', c.city) postcodeCity + FROM vn.invoiceOut io + JOIN vn.client c ON c.id = io.clientFk + JOIN vn.country cty ON cty.id = c.countryFk + LEFT JOIN vn.invoiceOutSerial ios ON ios.code = io.serial + AND ios.taxAreaFk = 'CEE' + LEFT JOIN ticket t ON t.refFk = io.ref + WHERE t.refFk = ? \ No newline at end of file diff --git a/print/templates/reports/invoice-incoterms/sql/incoterms.sql b/print/templates/reports/invoice-incoterms/sql/incoterms.sql index 0c4af803d..b36f50084 100644 --- a/print/templates/reports/invoice-incoterms/sql/incoterms.sql +++ b/print/templates/reports/invoice-incoterms/sql/incoterms.sql @@ -1,38 +1,14 @@ -SELECT io.issued, - c.socialName, - c.street postalAddress, - c.fi, - io.clientFk, - c.postcode, - c.city, - io.companyFk, - io.ref, - tc.code, - s.concept, - s.quantity, - s.price, - s.discount, - s.ticketFk, - t.shipped, - t.refFk, - a.nickname, - s.itemFk, - s.id saleFk, - pm.name AS pmname, - sa.iban, - c.phone, - MAX(t.packages) packages, +SELECT pack.packages, a.incotermsFk, - ic.name incotermsName , + ic.name incotermsName, t.weight, - t.observations, ca.fiscalName customsAgentName, ca.street customsAgentStreet, ca.nif customsAgentNif, ca.phone customsAgentPhone, ca.email customsAgentEmail, - CAST(sub2.volume AS DECIMAL (10,2)) volume, - sub3.intrastat + CAST(vol.volume AS DECIMAL (10,2)) volume, + intr.intrastat FROM vn.invoiceOut io JOIN vn.supplier su ON su.id = io.companyFk JOIN vn.client c ON c.id = io.clientFk @@ -42,26 +18,35 @@ SELECT io.issued, LEFT JOIN vn.incoterms ic ON ic.code = a.incotermsFk LEFT JOIN vn.customsAgent ca ON ca.id = a.customsAgentFk JOIN vn.sale s ON s.ticketFk = t.id - JOIN (SELECT SUM(volume) volume - FROM vn.invoiceOut io - JOIN vn.ticket t ON t.refFk = io.ref - JOIN vn.saleVolume sv ON sv.ticketFk = t.id - WHERE t.refFk = ? - ) sub2 ON TRUE - JOIN vn.itemTaxCountry itc ON itc.countryFk = su.countryFk AND itc.itemFk = s.itemFk + JOIN ( + SELECT SUM(volume) volume, COUNT(*) packages + FROM vn.invoiceOut io + JOIN vn.ticket t ON t.refFk = io.ref + JOIN vn.saleVolume sv ON sv.ticketFk = t.id + WHERE t.refFk = ? + ) vol + JOIN vn.itemTaxCountry itc ON itc.countryFk = su.countryFk + AND itc.itemFk = s.itemFk JOIN vn.taxClass tc ON tc.id = itc.taxClassFk - JOIN vn.invoiceOutSerial ios ON ios.code = io.serial AND ios.taxAreaFk = 'WORLD' + JOIN vn.invoiceOutSerial ios ON ios.code = io.serial + AND ios.taxAreaFk = 'WORLD' JOIN vn.country cty ON cty.id = c.countryFk JOIN vn.payMethod pm ON pm.id = c .payMethodFk JOIN vn.company co ON co.id=io.companyFk JOIN vn.supplierAccount sa ON sa.id=co.supplierAccountFk - LEFT JOIN (SELECT GROUP_CONCAT(DISTINCT ir.description ORDER BY ir.description SEPARATOR '. ' ) as intrastat - FROM vn.ticket t - JOIN vn.invoiceOut io ON io.ref = t.refFk - 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 - WHERE t.refFk = ? - )sub3 ON TRUE - WHERE t.refFk = ? - + JOIN ( + SELECT GROUP_CONCAT(DISTINCT ir.description ORDER BY ir.description SEPARATOR '. ' ) intrastat + FROM vn.ticket t + JOIN vn.invoiceOut io ON io.ref = t.refFk + 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 + WHERE t.refFk = ? + ) intr + JOIN ( + SELECT SUM(packages) packages + FROM ticket + WHERE refFk = ? + ) pack + WHERE t.refFk = ? + LIMIT 1 diff --git a/print/templates/reports/invoice-incoterms/sql/invoice.sql b/print/templates/reports/invoice-incoterms/sql/invoice.sql index 571ea2af3..02074f5c4 100644 --- a/print/templates/reports/invoice-incoterms/sql/invoice.sql +++ b/print/templates/reports/invoice-incoterms/sql/invoice.sql @@ -1,18 +1,17 @@ -SELECT - io.id, - io.issued, - io.clientFk, - io.companyFk, - io.ref, - pm.code AS payMethodCode, - cny.code companyCode, - sa.iban, - ios.footNotes -FROM invoiceOut io - JOIN client c ON c.id = io.clientFk - JOIN payMethod pm ON pm.id = c.payMethodFk - JOIN company cny ON cny.id = io.companyFk - JOIN supplierAccount sa ON sa.id = cny.supplierAccountFk - LEFT JOIN invoiceOutSerial ios ON ios.code = io.serial - LEFT JOIN ticket t ON t.refFk = io.ref -WHERE t.refFk = ? \ No newline at end of file +SELECT io.id, + io.issued, + io.clientFk, + io.companyFk, + io.ref, + pm.code payMethodCode, + cny.code companyCode, + sa.iban, + ios.footNotes + FROM invoiceOut io + JOIN client c ON c.id = io.clientFk + JOIN payMethod pm ON pm.id = c.payMethodFk + JOIN company cny ON cny.id = io.companyFk + JOIN supplierAccount sa ON sa.id = cny.supplierAccountFk + LEFT JOIN invoiceOutSerial ios ON ios.code = io.serial + LEFT JOIN ticket t ON t.refFk = io.ref + WHERE t.refFk = ? \ No newline at end of file