From c092bf58b497821f75129e680ededaaeac1dbff5 Mon Sep 17 00:00:00 2001 From: joan Date: Tue, 9 Feb 2021 07:59:55 +0100 Subject: [PATCH] Import buys --- db/dump/fixtures.sql | 11 +++++- modules/entry/back/methods/entry/import.js | 35 ++++++++++++++++++- .../entry/back/methods/entry/importPreview.js | 19 ++++++++-- modules/entry/back/models/entry.js | 1 + modules/entry/back/models/entry.json | 2 +- modules/entry/front/buy/import/index.html | 17 ++++++--- modules/entry/front/buy/import/index.js | 10 ++++-- 7 files changed, 84 insertions(+), 11 deletions(-) diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 8742c161f..1a35cafa3 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2174,4 +2174,13 @@ INSERT INTO `hedera`.`image`(`collectionFk`, `name`) INSERT INTO `hedera`.`imageCollectionSize`(`id`, `collectionFk`,`width`, `height`) VALUES - (1, 4, 160, 160); \ No newline at end of file + (1, 4, 160, 160); + +INSERT INTO `vn`.`rateConfig`(`rate0`, `rate1`, `rate2`, `rate3`) + VALUES + (36, 31, 25, 21); + +INSERT INTO `vn`.`rate`(`dated`, `warehouseFk`, `rate0`, `rate1`, `rate2`, `rate3`) + VALUES + (DATE_ADD(CURDATE(), INTERVAL -1 YEAR), 1, 10, 15, 20, 25), + (CURDATE(), 1, 12, 17, 22, 27); diff --git a/modules/entry/back/methods/entry/import.js b/modules/entry/back/methods/entry/import.js index 4b33fe964..137d67441 100644 --- a/modules/entry/back/methods/entry/import.js +++ b/modules/entry/back/methods/entry/import.js @@ -1,3 +1,5 @@ + +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; module.exports = Self => { Self.remoteMethodCtx('import', { description: 'Imports the buys from a JSON file', @@ -9,6 +11,16 @@ module.exports = Self => { description: 'The entry id', http: {source: 'path'} }, + { + arg: 'ref', + type: 'string', + description: 'The buyed boxes ids', + }, + { + arg: 'observation', + type: 'string', + description: 'The observation', + }, { arg: 'buys', type: ['Object'], @@ -25,10 +37,14 @@ module.exports = Self => { }); Self.import = async(ctx, id) => { + const conn = Self.dataSource.connector; const args = ctx.args; const models = Self.app.models; const entry = await models.Entry.findById(id); + await entry.updateAttributes({ + observation: args.notes + }); const buys = []; for (let buy of args.buys) { @@ -44,6 +60,23 @@ module.exports = Self => { }); } - await models.Buy.create(buys); + const createdBuys = await models.Buy.create(buys); + const buyIds = createdBuys.map(buy => buy.id); + + /* let stmts = []; + let stmt; + + stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.buyRecalc'); + stmt = new ParameterizedSQL( + `CREATE TEMPORARY TABLE tmp.buyRecalc + (INDEX (id)) + ENGINE = MEMORY + SELECT ? AS id`, [buyIds]); + + stmts.push(stmt); + stmts.push('CALL buy_recalcPrices()'); + + const sql = ParameterizedSQL.join(stmts, ';'); + await conn.executeStmt(sql); */ }; }; diff --git a/modules/entry/back/methods/entry/importPreview.js b/modules/entry/back/methods/entry/importPreview.js index 074f27ca7..38fbcd3c7 100644 --- a/modules/entry/back/methods/entry/importPreview.js +++ b/modules/entry/back/methods/entry/importPreview.js @@ -2,13 +2,18 @@ module.exports = Self => { Self.remoteMethod('importPreview', { description: '', accessType: 'READ', - accepts: { + accepts: [{ arg: 'id', type: 'number', required: true, description: 'The entry id', http: {source: 'path'} }, + { + arg: 'buys', + type: ['Object'], + description: 'The buys', + }], returns: { type: ['Object'], root: true @@ -19,7 +24,17 @@ module.exports = Self => { } }); - Self.importPreview = async id => { + Self.importPreview = async(id, buys) => { + const models = Self.app.models; + for (let buy of buys) { + const packaging = await models.Packaging.findOne({ + fields: ['id'], + where: {volume: {gte: buy.volume}}, + order: 'volume ASC' + }); + buy.packageFk = packaging.id; + } + return buys; }; }; diff --git a/modules/entry/back/models/entry.js b/modules/entry/back/models/entry.js index b5fafaaea..55e226a4f 100644 --- a/modules/entry/back/models/entry.js +++ b/modules/entry/back/models/entry.js @@ -3,4 +3,5 @@ module.exports = Self => { require('../methods/entry/getEntry')(Self); require('../methods/entry/getBuys')(Self); require('../methods/entry/import')(Self); + require('../methods/entry/importPreview')(Self); }; diff --git a/modules/entry/back/models/entry.json b/modules/entry/back/models/entry.json index 40d6d29dd..78d3c5e4f 100644 --- a/modules/entry/back/models/entry.json +++ b/modules/entry/back/models/entry.json @@ -28,7 +28,7 @@ "type": "boolean" }, "notes": { - "type": "String" + "type": "string" }, "isConfirmed": { "type": "boolean" diff --git a/modules/entry/front/buy/import/index.html b/modules/entry/front/buy/import/index.html index 231b086fd..903c61edc 100644 --- a/modules/entry/front/buy/import/index.html +++ b/modules/entry/front/buy/import/index.html @@ -59,8 +59,8 @@ + label="Observation" + ng-model="$ctrl.import.observation"> @@ -90,7 +90,7 @@ Packing Grouping Buying value - Package + Box Volume @@ -125,7 +125,16 @@ {{::buy.buyingValue | currency: 'EUR':2}} - {{::buy.packageFk | dashIfEmpty}} + + + + {{::buy.volume | number}} diff --git a/modules/entry/front/buy/import/index.js b/modules/entry/front/buy/import/index.js index 08e515380..15cdba704 100644 --- a/modules/entry/front/buy/import/index.js +++ b/modules/entry/front/buy/import/index.js @@ -30,7 +30,7 @@ class Controller extends Section { this.$.$applyAsync(() => { this.import.invoice = invoice.id_invoice; - this.import.description = invoice.tx_awb; + this.import.observation = invoice.tx_awb; const boxes = invoice.boxes; const buys = []; @@ -48,7 +48,13 @@ class Controller extends Section { }); } } - this.import.buys = buys; + + const params = {buys}; + const query = `Entries/${this.entry.id}/importPreview`; + this.$http.get(query, {params}).then(res => { + this.import.buys = res.data; + }); + // this.import.buys = buys; }); }