diff --git a/db/changes/232201/00-buyConfig_travelConfig.sql b/db/changes/232401/00-buyConfig_travelConfig.sql similarity index 96% rename from db/changes/232201/00-buyConfig_travelConfig.sql rename to db/changes/232401/00-buyConfig_travelConfig.sql index 40fc057de..0f73ddc8c 100644 --- a/db/changes/232201/00-buyConfig_travelConfig.sql +++ b/db/changes/232401/00-buyConfig_travelConfig.sql @@ -24,4 +24,5 @@ CREATE TABLE `vn`.`travelConfig` ( INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) VALUES ('Entry', 'addFromPackaging', 'WRITE', 'ALLOW', 'ROLE', 'production'), + ('Entry', 'addFromBuy', 'WRITE', 'ALLOW', 'ROLE', 'production'), ('Supplier', 'getItemsPackaging', 'READ', 'ALLOW', 'ROLE', 'production'); diff --git a/modules/entry/back/methods/entry/addFromBuy.js b/modules/entry/back/methods/entry/addFromBuy.js new file mode 100644 index 000000000..a40dd3412 --- /dev/null +++ b/modules/entry/back/methods/entry/addFromBuy.js @@ -0,0 +1,107 @@ + +module.exports = Self => { + Self.remoteMethodCtx('addFromBuy', { + description: 'Modify a field of a buy or creates a new one with default values', + accessType: 'WRITE', + accepts: [{ + arg: 'id', + type: 'number', + required: true, + description: 'The entry id', + http: {source: 'path'} + }, { + arg: 'item', + type: 'number', + required: true, + description: 'The item id', + }, { + arg: 'printedStickers', + type: 'number', + required: true, + description: 'The field to modify', + }], + returns: { + type: 'object', + root: true + }, + http: { + path: `/:id/addFromBuy`, + verb: 'POST' + } + }); + + Self.addFromBuy = async(ctx, options) => { + const args = ctx.args; + const models = Self.app.models; + const userId = ctx.req.accessToken.userId; + const myOptions = {userId}; + let tx; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + let buy = await models.Buy.findOne({where: {entryFk: args.id}}, myOptions); + if (buy) + await buy.updateAttribute('printedStickers', args.printedStickers); + else { + const userConfig = await models.UserConfig.findById(userId, {fields: ['warehouseFk']}, myOptions); + await Self.rawSql( + 'CALL vn.buyUltimate(?,?)', + [userConfig.warehouseFk, null], + myOptions + ); + let buyUltimate = await Self.rawSql( + `SELECT buyFk + FROM tmp.buyUltimate + WHERE itemFk = ?`, + [args.item], + myOptions + ); + buyUltimate = await models.Buy.findById(buyUltimate[0].buyFk, null, myOptions); + buy = await models.Buy.create({ + entryFk: args.id, + itemFk: args.item, + quantity: 0, + dispatched: buyUltimate.dispatched, + buyingValue: buyUltimate.buyingValue, + freightValue: buyUltimate.freightValue, + isIgnored: buyUltimate.isIgnored, + stickers: buyUltimate.stickers, + packing: buyUltimate.packing, + grouping: buyUltimate.grouping, + groupingMode: buyUltimate.groupingMode, + containerFk: buyUltimate.containerFk, + comissionValue: buyUltimate.comissionValue, + packageValue: buyUltimate.packageValue, + location: buyUltimate.location, + packageFk: buyUltimate.packageFk, + price1: buyUltimate.price1, + price2: buyUltimate.price2, + price3: buyUltimate.price3, + minPrice: buyUltimate.minPrice, + printedStickers: args.printedStickers, + workerFk: buyUltimate.workerFk, + isChecked: buyUltimate.isChecked, + isPickedOff: buyUltimate.isPickedOff, + created: buyUltimate.created, + ektFk: buyUltimate.ektFk, + weight: buyUltimate.weight, + deliveryFk: buyUltimate.deliveryFk, + itemOriginalFk: buyUltimate.itemOriginalFk + }, myOptions); + } + + if (tx) await tx.commit(); + return buy; + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } + }; +}; diff --git a/modules/entry/back/methods/entry/specs/addFromBuy.spec.js b/modules/entry/back/methods/entry/specs/addFromBuy.spec.js new file mode 100644 index 000000000..b6ed475b3 --- /dev/null +++ b/modules/entry/back/methods/entry/specs/addFromBuy.spec.js @@ -0,0 +1,51 @@ +const models = require('vn-loopback/server/server').models; + +describe('entry addFromBuy()', () => { + const ctx = {req: {accessToken: {userId: 18}}}; + + it('should change the printedStickers of an existent buy', async() => { + const id = 1; + const item = 1; + const buy = 1; + + const tx = await models.Entry.beginTransaction({}); + const options = {transaction: tx}; + try { + const currentBuy = await models.Buy.findById(buy, {fields: ['printedStickers']}, options); + const printedStickers = currentBuy.printedStickers + 10; + ctx.args = {id, item, printedStickers}; + const newBuy = await models.Entry.addFromBuy(ctx, options); + + expect(newBuy.printedStickers).toEqual(printedStickers); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); + + it('should create for an entry without a concrete item a new buy', async() => { + const id = 8; + const item = 1; + const printedStickers = 10; + + const tx = await models.Entry.beginTransaction({}); + const options = {transaction: tx}; + try { + const emptyBuy = await models.Buy.findOne({where: {entryFk: id}}, options); + ctx.args = {id, item, printedStickers}; + const newBuy = await models.Entry.addFromBuy(ctx, options); + + expect(emptyBuy).toEqual(null); + expect(newBuy.entryFk).toEqual(id); + expect(newBuy.printedStickers).toEqual(printedStickers); + expect(newBuy.itemFk).toEqual(item); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); +}); diff --git a/modules/entry/back/models/buy.json b/modules/entry/back/models/buy.json index 379e55427..af205533f 100644 --- a/modules/entry/back/models/buy.json +++ b/modules/entry/back/models/buy.json @@ -39,6 +39,9 @@ "packageValue": { "type": "number" }, + "price1": { + "type": "number" + }, "price2": { "type": "number" }, @@ -47,7 +50,44 @@ }, "weight": { "type": "number" + }, + "printedStickers": { + "type": "number" + }, + "dispatched": { + "type": "number" + }, + "isIgnored": { + "type": "boolean" + }, + "containerFk": { + "type": "number" + }, + "location": { + "type": "number" + }, + "minPrice": { + "type": "number" + }, + "isChecked": { + "type": "boolean" + }, + "isPickedOff": { + "type": "boolean" + }, + "created": { + "type": "date" + }, + "ektFk": { + "type": "number" + }, + "itemOriginalFk": { + "type": "number" + }, + "editorFk": { + "type": "number" } + }, "relations": { "entry": { @@ -64,6 +104,16 @@ "type": "belongsTo", "model": "Packaging", "foreignKey": "packageFk" + }, + "worker": { + "type": "belongsTo", + "model": "Worker", + "foreignKey": "workerFk" + }, + "delivery": { + "type": "belongsTo", + "model": "Delivery", + "foreignKey": "deliveryFk" } } } diff --git a/modules/entry/back/models/entry.js b/modules/entry/back/models/entry.js index 36944471f..0eabd70ee 100644 --- a/modules/entry/back/models/entry.js +++ b/modules/entry/back/models/entry.js @@ -9,6 +9,7 @@ module.exports = Self => { require('../methods/entry/lastItemBuys')(Self); require('../methods/entry/entryOrderPdf')(Self); require('../methods/entry/addFromPackaging')(Self); + require('../methods/entry/addFromBuy')(Self); Self.observe('before save', async function(ctx, options) { if (ctx.isNewInstance) return;