diff --git a/db/changes/225201/00-invoiceOut_new.sql b/db/changes/225201/00-invoiceOut_new.sql index ada4e73f6..10a42d40d 100644 --- a/db/changes/225201/00-invoiceOut_new.sql +++ b/db/changes/225201/00-invoiceOut_new.sql @@ -209,20 +209,7 @@ BEGIN JOIN item i ON i.id = s.itemFk JOIN vn.itemCost ic ON ic.itemFk = i.id AND ic.warehouseFk = t.warehouseFk JOIN intrastat ir ON ir.id = i.intrastatFk - WHERE t.refFk = vNewRef - GROUP BY i.intrastatFk - UNION ALL - SELECT - vNewInvoiceInId invoiceInFk, - NULL intrastatFk, - IF(CAST(SUM((ts.quantity * ts.price)) AS DECIMAL(10,2)), CAST(SUM((ts.quantity * ts.price)) AS DECIMAL(10,2)), 0) subtotal, - 0 AS stems, - NULL countryFk, - 0 AS netKg - FROM ticketService ts - JOIN ticket t ON ts.ticketFk = t.id - WHERE t.refFk = vNewRef - ORDER BY intrastatFk; + WHERE t.refFk = vNewRef; DROP TEMPORARY TABLE tmp.ticket; DROP TEMPORARY TABLE tmp.ticketAmount; diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index c4ce78658..f5a33bdc7 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -560,7 +560,9 @@ INSERT INTO `vn`.`invoiceOut`(`id`, `serial`, `amount`, `issued`,`clientFk`, `cr (2, 'T', 121.36, util.VN_CURDATE(), 1102, util.VN_CURDATE(), 442, util.VN_CURDATE(), util.VN_CURDATE(), 1, 0), (3, 'T', 8.88, util.VN_CURDATE(), 1103, util.VN_CURDATE(), 442, util.VN_CURDATE(), util.VN_CURDATE(), 1, 0), (4, 'T', 8.88, util.VN_CURDATE(), 1103, util.VN_CURDATE(), 442, util.VN_CURDATE(), util.VN_CURDATE(), 1, 0), - (5, 'A', 8.88, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 1103, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 442, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 1, 0); + (5, 'A', 8.88, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 1103, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 442, DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), DATE_ADD(util.VN_CURDATE(), INTERVAL -1 MONTH), 1, 0), + (6, 'V', 135, util.VN_CURDATE(), 1102, util.VN_CURDATE(), 442, util.VN_CURDATE(), util.VN_CURDATE(), 1, 0); + UPDATE `vn`.`invoiceOut` SET ref = 'T1111111' WHERE id = 1; UPDATE `vn`.`invoiceOut` SET ref = 'T2222222' WHERE id = 2; @@ -597,7 +599,11 @@ INSERT INTO `vn`.`invoiceOutExpence`(`id`, `invoiceOutFk`, `amount`, `expenceFk` (4, 2, 110.33, 2000000000, util.VN_CURDATE()), (5, 3, 8.07, 2000000000, util.VN_CURDATE()), (6, 4, 8.07, 2000000000, util.VN_CURDATE()), - (7, 5, 8.07, 2000000000, util.VN_CURDATE()); + (7, 5, 8.07, 2000000000, util.VN_CURDATE()), + (8, 6, 111.60, 2000000000, util.VN_CURDATE()), + (9, 6, 17.90, 4751000000, util.VN_CURDATE()), + (10,6, 5.50, 7001000000, util.VN_CURDATE()); + INSERT INTO `vn`.`zone` (`id`, `name`, `hour`, `agencyModeFk`, `travelingDays`, `price`, `bonus`, `itemMaxSize`) VALUES diff --git a/print/templates/reports/invoice/invoice.js b/print/templates/reports/invoice/invoice.js index b2d2c8037..f7011ad81 100755 --- a/print/templates/reports/invoice/invoice.js +++ b/print/templates/reports/invoice/invoice.js @@ -82,7 +82,7 @@ module.exports = { return this.rawSqlFromDef(`taxes`, [reference]); }, fetchIntrastat(reference) { - return this.rawSqlFromDef(`intrastat`, [reference, reference]); + return this.rawSqlFromDef(`intrastat`, [reference, reference, reference]); }, fetchRectified(reference) { return this.rawSqlFromDef(`rectified`, [reference]); diff --git a/print/templates/reports/invoice/sql/intrastat.sql b/print/templates/reports/invoice/sql/intrastat.sql index b277bf0a2..7d5cebd47 100644 --- a/print/templates/reports/invoice/sql/intrastat.sql +++ b/print/templates/reports/invoice/sql/intrastat.sql @@ -1,20 +1,27 @@ -SELECT ir.id code, - ir.description, - iii.stems, - iii.net netKg, - iii.amount subtotal - FROM vn.invoiceInIntrastat iii - LEFT JOIN vn.invoiceIn ii ON ii.id = iii.invoiceInFk - LEFT JOIN vn.invoiceOut io ON io.ref = ii.supplierRef - LEFT JOIN vn.intrastat ir ON ir.id = iii.intrastatFk - WHERE io.`ref` = ? -UNION ALL -SELECT NULL code, - 'Servicios' description, - 0 stems, - 0 netKg, - IF(CAST(SUM((ts.quantity * ts.price)) AS DECIMAL(10,2)), CAST(SUM((ts.quantity * ts.price)) AS DECIMAL(10,2)), 0) subtotal - FROM vn.ticketService ts - JOIN vn.ticket t ON ts.ticketFk = t.id - WHERE t.refFk = ? - ORDER BY code; +SELECT * + FROM invoiceOut io + JOIN invoiceOutSerial ios ON io.serial = ios.code + JOIN( + SELECT ir.id code, + ir.description, + iii.stems, + iii.net netKg, + iii.amount subtotal + FROM vn.invoiceInIntrastat iii + LEFT JOIN vn.invoiceIn ii ON ii.id = iii.invoiceInFk + LEFT JOIN vn.invoiceOut io ON io.ref = ii.supplierRef + LEFT JOIN vn.intrastat ir ON ir.id = iii.intrastatFk + WHERE io.`ref` = ? + UNION ALL + SELECT NULL code, + 'Servicios' description, + 0 stems, + 0 netKg, + IF(CAST(SUM((ts.quantity * ts.price)) AS DECIMAL(10,2)), CAST(SUM((ts.quantity * ts.price)) AS DECIMAL(10,2)), 0) subtotal + FROM vn.ticketService ts + JOIN vn.ticket t ON ts.ticketFk = t.id + WHERE t.refFk = ? + ) sub + WHERE io.ref = ? AND ios.isCEE + ORDER BY sub.code; +