diff --git a/modules/entry/back/methods/entry/importBuys.js b/modules/entry/back/methods/entry/importBuys.js index 38eb2c68dc..e3ff2aaf48 100644 --- a/modules/entry/back/methods/entry/importBuys.js +++ b/modules/entry/back/methods/entry/importBuys.js @@ -88,13 +88,11 @@ module.exports = Self => { throw new Error('The item is required'); const lastItemBuy = lastItemBuys.get(buy.itemFk); - let lastBuy; - if (lastItemBuy) - lastBuy = await models.Buy.findById(lastItemBuy.buyFk, null, myOptions); + if (!lastItemBuy) continue; + + const lastBuy = await models.Buy.findById(lastItemBuy.buyFk, null, myOptions); const stickers = 1; - const groupingMode = lastBuy && lastBuy.groupingMode ? lastBuy.groupingMode : 1; - const weight = lastBuy && lastBuy.weight ? lastBuy.weight : 1; const quantity = (stickers * buy.packing); buys.push({ entryFk: entry.id, @@ -105,8 +103,8 @@ module.exports = Self => { grouping: buy.grouping, buyingValue: buy.buyingValue, packageFk: buy.packageFk, - groupingMode: groupingMode, - weight: weight + groupingMode: lastBuy.groupingMode, + weight: lastBuy.weight }); await models.ItemMatchProperties.upsert({ @@ -117,6 +115,8 @@ module.exports = Self => { }, myOptions); } + if (buys.length) return; + const createdBuys = await models.Buy.create(buys, myOptions); const buyIds = createdBuys.map(buy => buy.id); diff --git a/modules/entry/back/methods/entry/lastItemBuys.js b/modules/entry/back/methods/entry/lastItemBuys.js new file mode 100644 index 0000000000..b9216bb949 --- /dev/null +++ b/modules/entry/back/methods/entry/lastItemBuys.js @@ -0,0 +1,91 @@ +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; +const mergeFilters = require('vn-loopback/util/filter').mergeFilters; + +module.exports = Self => { + Self.remoteMethod('lastItemBuys', { + description: 'Returns a list of buys', + accepts: [ + { + arg: 'id', + type: 'number', + required: true, + description: 'origin itemId', + http: {source: 'path'} + }, + { + arg: 'filter', + type: 'object', + description: `Filter defining where, order, offset, and limit - must be a JSON-encoded string` + } + ], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/:id/lastItemBuys`, + verb: 'GET' + } + }); + + Self.lastItemBuys = async(id, filter, options) => { + const conn = Self.dataSource.connector; + const models = Self.app.models; + const where = {isActive: true}; + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + filter = mergeFilters(filter, {where}); + + const entry = await models.Entry.findById(id, { + include: [{ + relation: 'travel', + scope: { + fields: ['warehouseInFk', 'landed'], + } + }] + }); + + const travel = entry.travel(); + + const stmts = []; + let stmt; + + stmt = new ParameterizedSQL(`CALL buyUltimate(?, ?)`, [ + travel.warehouseInFk, + travel.landed + ]); + stmts.push(stmt); + + stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.item'); + stmt = new ParameterizedSQL( + `CREATE TEMPORARY TABLE tmp.item + (PRIMARY KEY (id)) + ENGINE = MEMORY + SELECT + i.*, + p.name AS producerName, + nk.name AS inkName + FROM item i + JOIN producer p ON p.id = i.producerFk + JOIN ink nk ON nk.id = i.inkFk + JOIN tmp.buyUltimate bu ON i.id = bu.itemFk + AND bu.warehouseFk = ? + `, [travel.warehouseInFk]); + stmts.push(stmt); + + stmt = new ParameterizedSQL('SELECT * FROM tmp.item'); + stmt.merge(conn.makeSuffix(filter)); + + const itemsIndex = stmts.push(stmt) - 1; + + stmts.push('DROP TEMPORARY TABLE tmp.item'); + + const sql = ParameterizedSQL.join(stmts, ';'); + const result = await conn.executeStmt(sql, myOptions); + + return result[itemsIndex]; + }; +}; diff --git a/modules/entry/back/methods/entry/specs/importBuys.spec.js b/modules/entry/back/methods/entry/specs/importBuys.spec.js index f6da44f2aa..4d0585b3e0 100644 --- a/modules/entry/back/methods/entry/specs/importBuys.spec.js +++ b/modules/entry/back/methods/entry/specs/importBuys.spec.js @@ -3,9 +3,7 @@ const LoopBackContext = require('loopback-context'); describe('entry import()', () => { const buyerId = 35; - const companyId = 442; - const travelId = 1; - const supplierId = 1; + const entryId = 3; const activeCtx = { accessToken: {userId: buyerId}, }; @@ -51,20 +49,12 @@ describe('entry import()', () => { const tx = await models.Entry.beginTransaction({}); try { const options = {transaction: tx}; - const newEntry = await models.Entry.create({ - dated: new Date(), - supplierFk: supplierId, - travelFk: travelId, - companyFk: companyId, - observation: 'The entry', - ref: 'Entry ref' - }, options); - await models.Entry.importBuys(ctx, newEntry.id, options); + await models.Entry.importBuys(ctx, entryId, options); - const updatedEntry = await models.Entry.findById(newEntry.id, null, options); + const updatedEntry = await models.Entry.findById(entryId, null, options); const entryBuys = await models.Buy.find({ - where: {entryFk: newEntry.id} + where: {entryFk: entryId} }, options); expect(updatedEntry.observation).toEqual(expectedObservation); diff --git a/modules/entry/back/models/entry.js b/modules/entry/back/models/entry.js index 88611963f8..573e5b1cb7 100644 --- a/modules/entry/back/models/entry.js +++ b/modules/entry/back/models/entry.js @@ -5,4 +5,5 @@ module.exports = Self => { require('../methods/entry/addBuy')(Self); require('../methods/entry/importBuys')(Self); require('../methods/entry/importBuysPreview')(Self); + require('../methods/entry/lastItemBuys')(Self); }; diff --git a/modules/entry/front/buy/import/index.html b/modules/entry/front/buy/import/index.html index de61259859..ccb1a5dacd 100644 --- a/modules/entry/front/buy/import/index.html +++ b/modules/entry/front/buy/import/index.html @@ -59,7 +59,7 @@ + ng-model="$ctrl.itemFilterParams.size" + ng-keydown="$ctrl.onKeyPress($event)"> @@ -135,6 +138,7 @@ @@ -142,6 +146,7 @@ @@ -155,7 +160,7 @@ @@ -186,8 +191,8 @@ {{::item.name}} {{::item.size}} - {{::item.producer.name}} - {{::item.ink.name}} + {{::item.producerName}} + {{::item.inkName}} diff --git a/modules/entry/front/buy/import/index.js b/modules/entry/front/buy/import/index.js index a884762409..2249702c54 100644 --- a/modules/entry/front/buy/import/index.js +++ b/modules/entry/front/buy/import/index.js @@ -128,7 +128,7 @@ class Controller extends Section { switch (key) { case 'name': - where[key] = {like: `%${value}%`}; + where['i.name'] = {like: `%${value}%`}; break; case 'producerFk': case 'typeFk': @@ -141,6 +141,11 @@ class Controller extends Section { filter.where = where; this.$.itemsModel.applyFilter(filter); } + + onKeyPress($event) { + if ($event.key === 'Enter') + this.filter(); + } } Controller.$inject = ['$element', '$scope']; diff --git a/modules/order/front/locale/es.yml b/modules/order/front/locale/es.yml index 62f7116599..37481b1a49 100644 --- a/modules/order/front/locale/es.yml +++ b/modules/order/front/locale/es.yml @@ -27,4 +27,5 @@ Confirm: Confirmar Real hour: Hora real T. Hour: Hora T. Theoretical hour: Hora TeĆ³rica -Go to the order: Ir al pedido \ No newline at end of file +Go to the order: Ir al pedido +Basket: Cesta \ No newline at end of file diff --git a/modules/order/front/summary/index.html b/modules/order/front/summary/index.html index 67bc26fce3..b4562c3176 100644 --- a/modules/order/front/summary/index.html +++ b/modules/order/front/summary/index.html @@ -7,7 +7,7 @@ - Ticket #{{$ctrl.summary.id}} - {{$ctrl.summary.client.name}} + Basket #{{$ctrl.summary.id}} - {{$ctrl.summary.client.name}} ({{$ctrl.summary.client.salesPersonFk}})