diff --git a/print/templates/reports/invoice/invoice.js b/print/templates/reports/invoice/invoice.js index 3406710ba..a6e37d591 100755 --- a/print/templates/reports/invoice/invoice.js +++ b/print/templates/reports/invoice/invoice.js @@ -3,7 +3,6 @@ const Report = require(`${appPath}/core/report`); const reportHeader = new Component('report-header'); const reportFooter = new Component('report-footer'); const invoiceIncoterms = new Report('invoice-incoterms'); -const db = require(`${appPath}/core/database`); module.exports = { name: 'invoice', @@ -72,17 +71,8 @@ module.exports = { fetchTickets(invoiceId) { return this.rawSqlFromDef('tickets', [invoiceId]); }, - async fetchSales(invoiceId) { - const connection = await db.getConnection('default'); - await this.rawSql(`DROP TEMPORARY TABLE IF EXISTS tmp.invoiceTickets`, connection); - await this.rawSqlFromDef('invoiceTickets', [invoiceId], connection); - - const sales = this.rawSqlFromDef('sales', connection); - - await this.rawSql(`DROP TEMPORARY TABLE tmp.invoiceTickets`, connection); - await connection.release(); - - return sales; + fetchSales(invoiceId) { + return this.rawSqlFromDef('sales', [invoiceId, invoiceId]); }, fetchTaxes(invoiceId) { return this.rawSqlFromDef(`taxes`, [invoiceId]); diff --git a/print/templates/reports/invoice/sql/invoiceTickets.sql b/print/templates/reports/invoice/sql/invoiceTickets.sql deleted file mode 100644 index 089911a63..000000000 --- a/print/templates/reports/invoice/sql/invoiceTickets.sql +++ /dev/null @@ -1,20 +0,0 @@ -CREATE TEMPORARY TABLE tmp.invoiceTickets - ENGINE = MEMORY - SELECT - t.id AS ticketFk, - t.clientFk, - t.shipped, - t.nickname, - io.ref, - c.socialName, - sa.iban, - pm.name AS payMethod, - su.countryFk AS supplierCountryFk - FROM vn.invoiceOut io - JOIN vn.supplier su ON su.id = io.companyFk - JOIN vn.ticket t ON t.refFk = io.ref - JOIN vn.client c ON c.id = t.clientFk - 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 - WHERE io.id = ? \ No newline at end of file diff --git a/print/templates/reports/invoice/sql/sales.sql b/print/templates/reports/invoice/sql/sales.sql index 0665fc0ff..cff8794db 100644 --- a/print/templates/reports/invoice/sql/sales.sql +++ b/print/templates/reports/invoice/sql/sales.sql @@ -1,11 +1,11 @@ SELECT - it.ref, - it.socialName, - it.iban, - it.payMethod, - it.clientFk, - it.shipped, - it.nickname, + io.ref, + c.socialName, + sa.iban, + pm.name AS payMethod, + t.clientFk, + t.shipped, + t.nickname, s.ticketFk, s.itemFk, s.concept, @@ -20,27 +20,34 @@ SELECT i.value7, tc.code AS vatType, ib.ediBotanic botanical - FROM tmp.invoiceTickets it - JOIN vn.sale s ON s.ticketFk = it.ticketFk - JOIN item i ON i.id = s.itemFk - LEFT JOIN itemType it ON it.id = i.typeFk - LEFT JOIN itemCategory ic ON ic.id = it.categoryFk - LEFT JOIN itemBotanicalWithGenus ib ON ib.itemFk = i.id - AND ic.code = 'plant' - AND ib.ediBotanic IS NOT NULL - JOIN vn.itemTaxCountry itc ON itc.countryFk = it.supplierCountryFk - AND itc.itemFk = s.itemFk - JOIN vn.taxClass tc ON tc.id = itc.taxClassFk -UNION ALL + FROM vn.invoiceOut io + JOIN vn.ticket t ON t.refFk = io.ref + JOIN vn.supplier su ON su.id = io.companyFk + JOIN vn.client c ON c.id = t.clientFk + 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 + JOIN vn.sale s ON s.ticketFk = t.id + JOIN item i ON i.id = s.itemFk + LEFT JOIN itemType it ON it.id = i.typeFk + LEFT JOIN itemCategory ic ON ic.id = it.categoryFk + LEFT JOIN itemBotanicalWithGenus ib ON ib.itemFk = i.id + AND ic.code = 'plant' + AND ib.ediBotanic IS NOT NULL + JOIN vn.itemTaxCountry itc ON itc.countryFk = su.countryFk + AND itc.itemFk = s.itemFk + JOIN vn.taxClass tc ON tc.id = itc.taxClassFk + WHERE io.id = ? + UNION ALL SELECT - it.ref, - it.socialName, - it.iban, - it.payMethod, - it.clientFk, - it.shipped, - it.nickname, - it.ticketFk, + io.ref, + c.socialName, + sa.iban, + pm.name AS payMethod, + t.clientFk, + t.shipped, + t.nickname, + t.id AS ticketFk, '', ts.description concept, ts.quantity, @@ -54,6 +61,12 @@ SELECT NULL AS value7, tc.code AS vatType, NULL AS botanical - FROM tmp.invoiceTickets it - JOIN vn.ticketService ts ON ts.ticketFk = it.ticketFk - JOIN vn.taxClass tc ON tc.id = ts.taxClassFk \ No newline at end of file + FROM vn.invoiceOut io + JOIN vn.ticket t ON t.refFk = io.ref + JOIN vn.ticketService ts ON ts.ticketFk = t.id + JOIN vn.client c ON c.id = t.clientFk + 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 + JOIN vn.taxClass tc ON tc.id = ts.taxClassFk + WHERE io.id = ? \ No newline at end of file