From e7c62d2e71b14197765c990bbe983d63abfc9cef Mon Sep 17 00:00:00 2001 From: joan Date: Wed, 16 Jun 2021 13:04:54 +0200 Subject: [PATCH] 2956 - Fix for extra community report --- print/core/database.js | 2 +- .../extra-community/extra-community.js | 28 +++--------- .../reports/extra-community/sql/entries.sql | 30 +++++++------ .../reports/extra-community/sql/travels.sql | 45 +++++++++---------- 4 files changed, 45 insertions(+), 60 deletions(-) diff --git a/print/core/database.js b/print/core/database.js index 4592294be..6c690afc6 100644 --- a/print/core/database.js +++ b/print/core/database.js @@ -24,7 +24,7 @@ module.exports = { * * @return {Object} - Pool connection */ - getConnection(name) { + getConnection(name = 'default') { let pool = this.pool; return new Promise((resolve, reject) => { pool.getConnection(name, function(error, connection) { diff --git a/print/templates/reports/extra-community/extra-community.js b/print/templates/reports/extra-community/extra-community.js index ddeabb49b..f2e5f9ca6 100755 --- a/print/templates/reports/extra-community/extra-community.js +++ b/print/templates/reports/extra-community/extra-community.js @@ -15,22 +15,11 @@ module.exports = { continent: this.continent }; - const connection = await db.pool.getConnection(); - - await this.fetchTravels(args, connection); - - const travels = await this.rawSql(` - SELECT * FROM tmp.travel`, connection); - const entries = await this.fetchEntries(connection); - - await this.rawSql(` - DROP TEMPORARY TABLE - tmp.travel`, connection); - - await connection.release(); + const travels = await this.fetchTravels(args); + const travelsId = travels.map(travel => travel.id); + const entries = await this.fetchEntries(travelsId); const map = new Map(); - for (let travel of travels) map.set(travel.id, travel); @@ -71,7 +60,7 @@ module.exports = { } }, methods: { - fetchTravels(args, connection) { + fetchTravels(args) { const where = db.buildWhere(args, (key, value) => { switch (key) { case 'shippedFrom': @@ -91,14 +80,11 @@ module.exports = { query = db.merge(query, where); query = db.merge(query, 'GROUP BY t.id'); - return this.rawSql(query, connection); + return this.rawSql(query); }, - fetchEntries(connection) { - let query = this.getSqlFromDef('entries'); - query = db.merge(query, 'GROUP BY e.id'); - - return this.rawSql(query, connection); + fetchEntries(travelsId) { + return this.rawSqlFromDef('entries', [travelsId]); } }, components: { diff --git a/print/templates/reports/extra-community/sql/entries.sql b/print/templates/reports/extra-community/sql/entries.sql index 5cf22fe44..ad2529a1c 100644 --- a/print/templates/reports/extra-community/sql/entries.sql +++ b/print/templates/reports/extra-community/sql/entries.sql @@ -1,15 +1,17 @@ SELECT - e.id, - e.travelFk, - e.ref, - s.name AS supplierName, - SUM(b.stickers) AS stickers, - CAST(SUM(i.density * b.stickers * IF(pkg.volume, pkg.volume, pkg.width * pkg.depth * pkg.height) / 1000000 ) as DECIMAL(10,0)) as loadedKg, - CAST(SUM(167.5 * b.stickers * IF(pkg.volume, pkg.volume, pkg.width * pkg.depth * pkg.height) / 1000000 ) as DECIMAL(10,0)) as volumeKg - FROM tmp.travel t - JOIN entry e ON e.travelFk = t.id - JOIN buy b ON b.entryFk = e.id - JOIN packaging pkg ON pkg.id = b.packageFk - JOIN item i ON i.id = b.itemFk - JOIN itemType it ON it.id = i.typeFk - JOIN supplier s ON s.id = e.supplierFk \ No newline at end of file + e.id, + e.travelFk, + e.ref, + s.name AS supplierName, + SUM(b.stickers) AS stickers, + CAST(SUM(i.density * b.stickers * IF(pkg.volume, pkg.volume, pkg.width * pkg.depth * pkg.height) / 1000000 ) as DECIMAL(10,0)) as loadedKg, + CAST(SUM(167.5 * b.stickers * IF(pkg.volume, pkg.volume, pkg.width * pkg.depth * pkg.height) / 1000000 ) as DECIMAL(10,0)) as volumeKg +FROM travel t + JOIN entry e ON e.travelFk = t.id + JOIN buy b ON b.entryFk = e.id + JOIN packaging pkg ON pkg.id = b.packageFk + JOIN item i ON i.id = b.itemFk + JOIN itemType it ON it.id = i.typeFk + JOIN supplier s ON s.id = e.supplierFk + WHERE t.id IN(?) +GROUP BY e.id \ No newline at end of file diff --git a/print/templates/reports/extra-community/sql/travels.sql b/print/templates/reports/extra-community/sql/travels.sql index f9b421df7..022767608 100644 --- a/print/templates/reports/extra-community/sql/travels.sql +++ b/print/templates/reports/extra-community/sql/travels.sql @@ -1,24 +1,21 @@ -CREATE TEMPORARY TABLE tmp.travel - (INDEX (id)) - ENGINE = MEMORY - SELECT - t.id, - t.ref, - t.shipped, - t.landed, - t.kg, - SUM(b.stickers) AS stickers, - CAST(SUM(i.density * b.stickers * IF(pkg.volume, pkg.volume, pkg.width * pkg.depth * pkg.height) / 1000000 ) as DECIMAL(10,0)) as loadedKg, - CAST(SUM(167.5 * b.stickers * IF(pkg.volume, pkg.volume, pkg.width * pkg.depth * pkg.height) / 1000000 ) as DECIMAL(10,0)) as volumeKg - FROM travel t - LEFT JOIN supplier s ON s.id = t.cargoSupplierFk - LEFT JOIN entry e ON e.travelFk = t.id - LEFT JOIN buy b ON b.entryFk = e.id - LEFT JOIN packaging pkg ON pkg.id = b.packageFk - LEFT JOIN item i ON i.id = b.itemFk - LEFT JOIN itemType it ON it.id = i.typeFk - JOIN warehouse w ON w.id = t.warehouseInFk - JOIN warehouse wo ON wo.id = t.warehouseOutFk - JOIN country c ON c.id = wo.countryFk - LEFT JOIN continent cnt ON cnt.id = c.continentFk - JOIN agencyMode am ON am.id = t.agencyFk \ No newline at end of file +SELECT + t.id, + t.ref, + t.shipped, + t.landed, + t.kg, + SUM(b.stickers) AS stickers, + CAST(SUM(i.density * b.stickers * IF(pkg.volume, pkg.volume, pkg.width * pkg.depth * pkg.height) / 1000000 ) as DECIMAL(10,0)) as loadedKg, + CAST(SUM(167.5 * b.stickers * IF(pkg.volume, pkg.volume, pkg.width * pkg.depth * pkg.height) / 1000000 ) as DECIMAL(10,0)) as volumeKg +FROM travel t + LEFT JOIN supplier s ON s.id = t.cargoSupplierFk + LEFT JOIN entry e ON e.travelFk = t.id + LEFT JOIN buy b ON b.entryFk = e.id + LEFT JOIN packaging pkg ON pkg.id = b.packageFk + LEFT JOIN item i ON i.id = b.itemFk + LEFT JOIN itemType it ON it.id = i.typeFk + JOIN warehouse w ON w.id = t.warehouseInFk + JOIN warehouse wo ON wo.id = t.warehouseOutFk + JOIN country c ON c.id = wo.countryFk + LEFT JOIN continent cnt ON cnt.id = c.continentFk + JOIN agencyMode am ON am.id = t.agencyFk \ No newline at end of file