1453 ticket UpdateDiscount refactor
gitea/salix/dev This commit looks good Details

This commit is contained in:
Bernat 2019-05-29 11:11:54 +02:00
parent 12e0268870
commit 84a4b049f0
4 changed files with 40 additions and 27 deletions

View File

@ -1,15 +1,19 @@
let UserError = require('vn-loopback/util/user-error'); let UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('updateDiscount', { Self.remoteMethod('updateDiscount', {
description: 'Changes the discount of a sale', description: 'Changes the discount of a sale',
accessType: 'WRITE', accessType: 'WRITE',
accepts: [{ accepts: [{
arg: 'params', arg: 'ticketFk',
type: 'object', type: 'number',
required: true, required: true,
description: 'sale ID, newDiscount, price', description: 'ticket id',
http: {source: 'body'} }, {
arg: 'sales',
type: ['object'],
required: true,
description: 'sale ID, newDiscount, price, ticket',
}], }],
returns: { returns: {
type: 'string', type: 'string',
@ -21,12 +25,16 @@ module.exports = Self => {
} }
}); });
Self.updateDiscount = async(ctx, params) => { Self.updateDiscount = async(ticketFk, sales) => {
if (isNaN(params.editLines[0].discount)) const validDiscounts = sales.every(sale => {
return !isNaN(sale.discount);
});
if (!validDiscounts)
throw new UserError(`The value should be a number`); throw new UserError(`The value should be a number`);
let model = Self.app.models; let model = Self.app.models;
let ticket = await model.Ticket.findById(params.editLines[0].ticketFk, { let ticket = await model.Ticket.findById(ticketFk, {
include: { include: {
relation: 'client', relation: 'client',
scope: { scope: {
@ -38,25 +46,28 @@ module.exports = Self => {
if (ticket.refFk) if (ticket.refFk)
throw new UserError(`The sales of this ticket can't be modified`); 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 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) for (let i = 0; i < sales.length; i++) {
componentToUse = 37; let currentLine = await model.Sale.findOne({where: {id: sales[i].id}, fields: 'price'});
else let value = ((-currentLine.price * sales[i].discount) / 100);
componentToUse = 34;
for (let i = 0; i < params.editLines.length; i++) { await model.SaleComponent.upsert({saleFk: sales[i].id, value: value, componentFk: componentId});
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.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) { if (usesMana) {
query = ` query = `call vn.manaSpellersRequery(?)`;
call vn.manaSpellersRequery(?)`;
await Self.rawSql(query, [ticket.client().salesPersonFk]); await Self.rawSql(query, [ticket.client().salesPersonFk]);
} }
}; };

View File

@ -2,16 +2,15 @@
<h5>MANÁ: {{$ctrl.mana | currency: 'EUR':0}}</h5> <h5>MANÁ: {{$ctrl.mana | currency: 'EUR':0}}</h5>
</vn-horizontal> </vn-horizontal>
<div pad-medium> <div pad-medium>
<vn-textfield <vn-input-number
vn-focus vn-focus
label="Discount" label="Discount"
model="$ctrl.newDiscount" model="$ctrl.newDiscount"
type="text"
on-change="$ctrl.updateDiscount()"> on-change="$ctrl.updateDiscount()">
<t-right-icons> <t-right-icons>
<span class="filter">%</span> <span class="filter">%</span>
</t-right-icons> </t-right-icons>
</vn-textfield> </vn-input-number>
<div class="simulator"> <div class="simulator">
<p class="simulatorTitle" translate>New price</p> <p class="simulatorTitle" translate>New price</p>
<p>{{($ctrl.edit[0].quantity * $ctrl.edit[0].price) <p>{{($ctrl.edit[0].quantity * $ctrl.edit[0].price)

View File

@ -38,12 +38,15 @@ class Controller {
let modified = false; let modified = false;
for (let i = 0; i < this.edit.length; i++) { for (let i = 0; i < this.edit.length; i++) {
if (this.newDiscount != this.edit[0].discount || this.bulk || !this.newDiscount) { 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; modified = true;
} }
} }
if (modified) { 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.vnApp.showSuccess(this.$translate.instant('Data saved!'));
this.clear(); this.clear();
modified = false; modified = false;
@ -53,7 +56,7 @@ class Controller {
this.onHide(); this.onHide();
} else } 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() { clear() {

View File

@ -69,7 +69,7 @@ describe('Ticket', () => {
spyOn(controller.vnApp, 'showError'); spyOn(controller.vnApp, 'showError');
controller.updateDiscount(); controller.updateDiscount();
expect(controller.vnApp.showError).toHaveBeenCalledWith('There is no changes to save'); expect(controller.vnApp.showError).toHaveBeenCalledWith('There are no changes to save');
}); });
}); });