diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index a35c90528..b1cc35f63 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -654,7 +654,7 @@ INSERT INTO `vn`.`sale`(`id`, `itemFk`, `ticketFk`, `concept`, `quantity`, `pric (11, 1, 16, 'Ranged weapon longbow 2m', 2, 9.10, 0, 0, 0, CURDATE()), (12, 4, 16, 'Melee weapon heavy shield 1x0.5m', 20, 3.06, 0, 0, 0, CURDATE()), (13, 2, 8, 'Melee weapon combat fist 15cm', 15, 1.30, 0, 0, 0, DATE_ADD(CURDATE(), INTERVAL +1 MONTH)), - (14, 1, 8, 'Ranged weapon longbow 2m', 10, 2.30, 0, 0, 0, DATE_ADD(CURDATE(), INTERVAL +1 MONTH)), + (14, 1, 8, 'Ranged weapon longbow 2m', 10, 9.28, 0, 0, 0, DATE_ADD(CURDATE(), INTERVAL +1 MONTH)), (15, 1, 19, 'Ranged weapon longbow 2m', 10, 1.50, 0, 0, 0, DATE_ADD(CURDATE(), INTERVAL +1 DAY)), (16, 2, 20, 'Melee weapon combat fist 15cm', 15, 1.30, 0, 0, 0, DATE_ADD(CURDATE(), INTERVAL +1 DAY)), (17, 2, 22, 'Melee weapon combat fist 15cm', 30, 2.30, 0, 0, 0, DATE_ADD(CURDATE(), INTERVAL +1 DAY)), diff --git a/e2e/paths/06-claim-module/03_detail.spec.js b/e2e/paths/06-claim-module/03_detail.spec.js index 9eb3fee1c..551701c78 100644 --- a/e2e/paths/06-claim-module/03_detail.spec.js +++ b/e2e/paths/06-claim-module/03_detail.spec.js @@ -45,7 +45,7 @@ describe('Claim detail', () => { .waitToGetProperty(selectors.claimDetail.totalClaimed, 'innerText'); expect(claimedQuantity).toEqual('10'); - expect(totalClaimed).toContain('29.50'); + expect(totalClaimed).toContain('99.30'); }); it('should delete the second item from the claim', async() => { diff --git a/modules/ticket/back/methods/sale/updatePrice.js b/modules/ticket/back/methods/sale/updatePrice.js index aa4df9d28..246a9a4e5 100644 --- a/modules/ticket/back/methods/sale/updatePrice.js +++ b/modules/ticket/back/methods/sale/updatePrice.js @@ -1,14 +1,14 @@ let UserError = require('vn-loopback/util/user-error'); module.exports = Self => { - Self.remoteMethodCtx('updatePrice', { + Self.remoteMethod('updatePrice', { description: 'Changes the discount of a sale', accessType: 'WRITE', accepts: [{ arg: 'params', type: 'object', required: true, - description: 'sale ID, newPrice', + description: 'sale ID, newPrice, ticketFk', http: {source: 'body'} }], returns: { @@ -21,10 +21,51 @@ module.exports = Self => { } }); - Self.getTicket = async params => { + Self.updatePrice = async params => { + if (!params.price) params.price = 0; + + let model = Self.app.models; + let manaDiscount; + let buyerDiscount = await model.ComponentRate.findOne({where: {code: 'buyerDiscount'}}); + let ticket = await getTicket(params); + let usesMana = await model.WorkerMana.findOne({where: {workerFk: ticket[0].client().salesPersonFk}, fields: 'amount'}); + let currentLine = await Self.app.models.Sale.findOne({where: {id: params.id}}); + let componentId = buyerDiscount.id; + + if (usesMana) { + manaDiscount = await model.ComponentRate.findOne({where: {code: 'mana'}}); + componentId = manaDiscount.id; + } + let value = (params.price - currentLine.price); + + let saleComponent = await Self.app.models.SaleComponent.findOne({ + where: { + componentFk: componentId, + saleFk: params.id + } + }); + + if (saleComponent) { + saleComponent.updateAttributes({value: saleComponent.value + value}).catch(() => { + throw new UserError(`Enter a valid number`); + }); + } else { + await Self.app.models.SaleComponent.create({ + saleFk: params.id, + componentFk: componentId, + value: value + }); + } + + await currentLine.updateAttributes({price: params.price}); + + query = `call vn.manaSpellersRequery(?)`; + await Self.rawSql(query, [ticket[0].client().salesPersonFk]); + }; + + async function getTicket(params) { let model = Self.app.models; let thisTicketIsEditable = await model.Ticket.isEditable(params.ticketFk); - if (!thisTicketIsEditable) throw new UserError(`The sales of this ticket can't be modified`); @@ -40,48 +81,5 @@ module.exports = Self => { }], fields: ['id', 'clientFk'] }); - }; - - Self.updatePrice = async params => { - if (isNaN(params.price)) - throw new UserError(`The value should be a number`); - - if (!params.price) params.price = 0; - - let model = Self.app.models; - - let ticket = await Self.getTicket(params); - let usesMana = await model.WorkerMana.findOne({where: {workerFk: ticket[0].client().salesPersonFk}, fields: 'amount'}); - let currentLine = await Self.app.models.Sale.findOne({where: {id: params.id}}); - - let componentToUse; - if (usesMana) - componentToUse = 37; - else - componentToUse = 34; - - let value = (params.price - currentLine.price); - - let saleComponent = await Self.app.models.SaleComponent.findOne({ - where: { - componentFk: componentToUse, - saleFk: params.id - } - }); - - if (saleComponent) - saleComponent.updateAttributes({value: saleComponent.value + value}); - else { - await Self.app.models.SaleComponent.create({ - saleFk: params.id, - componentFk: componentToUse, - value: value - }); - } - - await currentLine.updateAttributes({price: params.price}); - - query = `call vn.manaSpellersRequery(?)`; - await Self.rawSql(query, [ticket[0].client().salesPersonFk]); - }; + } }; diff --git a/modules/ticket/front/sale/index.html b/modules/ticket/front/sale/index.html index 19b5414f8..b15d04090 100644 --- a/modules/ticket/front/sale/index.html +++ b/modules/ticket/front/sale/index.html @@ -173,16 +173,17 @@
MANÁ: {{$ctrl.mana | currency: 'EUR':0}}
- - +

New price

{{($ctrl.sale.quantity * $ctrl.editedPrice)