diff --git a/modules/ticket/back/methods/sale/updateDiscount.js b/modules/ticket/back/methods/sale/updateDiscount.js index 641182076..1539a4283 100644 --- a/modules/ticket/back/methods/sale/updateDiscount.js +++ b/modules/ticket/back/methods/sale/updateDiscount.js @@ -1,15 +1,19 @@ let UserError = require('vn-loopback/util/user-error'); module.exports = Self => { - Self.remoteMethodCtx('updateDiscount', { + Self.remoteMethod('updateDiscount', { description: 'Changes the discount of a sale', accessType: 'WRITE', accepts: [{ - arg: 'params', - type: 'object', + arg: 'ticketFk', + type: 'number', required: true, - description: 'sale ID, newDiscount, price', - http: {source: 'body'} + description: 'ticket id', + }, { + arg: 'sales', + type: ['object'], + required: true, + description: 'sale ID, newDiscount, price, ticket', }], returns: { type: 'string', @@ -21,12 +25,16 @@ module.exports = Self => { } }); - Self.updateDiscount = async(ctx, params) => { - if (isNaN(params.editLines[0].discount)) + Self.updateDiscount = async(ticketFk, sales) => { + const validDiscounts = sales.every(sale => { + return !isNaN(sale.discount); + }); + + if (!validDiscounts) throw new UserError(`The value should be a number`); let model = Self.app.models; - let ticket = await model.Ticket.findById(params.editLines[0].ticketFk, { + let ticket = await model.Ticket.findById(ticketFk, { include: { relation: 'client', scope: { @@ -38,25 +46,28 @@ module.exports = Self => { if (ticket.refFk) throw new UserError(`The sales of this ticket can't be modified`); - let componentToUse; + let manaDiscount; + let buyerDiscount = await model.ComponentRate.findOne({where: {code: 'buyerDiscount'}}); let usesMana = await model.WorkerMana.findOne({where: {workerFk: ticket.client().salesPersonFk}, fields: 'amount'}); + let componentId = buyerDiscount.id; + console.log(usesMana); + if (usesMana) { + manaDiscount = await model.ComponentRate.findOne({where: {code: 'mana'}}); + componentId = manaDiscount.id; + } - if (usesMana) - componentToUse = 37; - else - componentToUse = 34; + for (let i = 0; i < sales.length; i++) { + let currentLine = await model.Sale.findOne({where: {id: sales[i].id}, fields: 'price'}); + let value = ((-currentLine.price * sales[i].discount) / 100); - for (let i = 0; i < params.editLines.length; i++) { - let currentLine = await model.Sale.findOne({where: {id: params.editLines[i].id}, fields: 'price'}); - let value = ((-currentLine.price * params.editLines[i].discount) / 100); - await model.SaleComponent.upsert({saleFk: params.editLines[i].id, value: value, componentFk: componentToUse}); + await model.SaleComponent.upsert({saleFk: sales[i].id, value: value, componentFk: componentId}); - await model.Sale.update({id: params.editLines[i].id}, {discount: params.editLines[i].discount}); + await model.Sale.update({id: sales[i].id}, {discount: sales[i].discount}); } if (usesMana) { - query = ` - call vn.manaSpellersRequery(?)`; + query = `call vn.manaSpellersRequery(?)`; + await Self.rawSql(query, [ticket.client().salesPersonFk]); } }; diff --git a/modules/ticket/front/sale/editDiscount.html b/modules/ticket/front/sale/editDiscount.html index cdc5f0839..7648d2ddc 100644 --- a/modules/ticket/front/sale/editDiscount.html +++ b/modules/ticket/front/sale/editDiscount.html @@ -2,16 +2,15 @@
MANÁ: {{$ctrl.mana | currency: 'EUR':0}}
- % - +

New price

{{($ctrl.edit[0].quantity * $ctrl.edit[0].price) diff --git a/modules/ticket/front/sale/editDiscount.js b/modules/ticket/front/sale/editDiscount.js index 3df684ff3..0694c67d5 100644 --- a/modules/ticket/front/sale/editDiscount.js +++ b/modules/ticket/front/sale/editDiscount.js @@ -38,12 +38,15 @@ class Controller { let modified = false; for (let i = 0; i < this.edit.length; i++) { if (this.newDiscount != this.edit[0].discount || this.bulk || !this.newDiscount) { - editLines.push({id: this.edit[i].id, discount: this.newDiscount, ticketFk: this.$state.params.id}); + editLines.push({id: this.edit[i].id, discount: this.newDiscount}); modified = true; } } + if (modified) { - this.$http.post(`/ticket/api/Sales/updateDiscount`, {editLines}).then(() => { + const ticketId = parseInt(this.$state.params.id); + const params = {ticketFk: ticketId, sales: editLines}; + this.$http.post(`/ticket/api/Sales/updateDiscount`, params).then(() => { this.vnApp.showSuccess(this.$translate.instant('Data saved!')); this.clear(); modified = false; @@ -53,7 +56,7 @@ class Controller { this.onHide(); } else - this.vnApp.showError(this.$translate.instant('There is no changes to save')); + this.vnApp.showError(this.$translate.instant('There are no changes to save')); } clear() { diff --git a/modules/ticket/front/sale/editDiscount.spec.js b/modules/ticket/front/sale/editDiscount.spec.js index 2fef13106..e48fdbb63 100644 --- a/modules/ticket/front/sale/editDiscount.spec.js +++ b/modules/ticket/front/sale/editDiscount.spec.js @@ -69,7 +69,7 @@ describe('Ticket', () => { spyOn(controller.vnApp, 'showError'); controller.updateDiscount(); - expect(controller.vnApp.showError).toHaveBeenCalledWith('There is no changes to save'); + expect(controller.vnApp.showError).toHaveBeenCalledWith('There are no changes to save'); }); });