1453 ticket UpdateDiscount refactor
gitea/salix/dev This commit looks good
Details
gitea/salix/dev This commit looks good
Details
This commit is contained in:
parent
12e0268870
commit
84a4b049f0
|
@ -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]);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -2,16 +2,15 @@
|
|||
<h5>MANÁ: {{$ctrl.mana | currency: 'EUR':0}}</h5>
|
||||
</vn-horizontal>
|
||||
<div pad-medium>
|
||||
<vn-textfield
|
||||
<vn-input-number
|
||||
vn-focus
|
||||
label="Discount"
|
||||
model="$ctrl.newDiscount"
|
||||
type="text"
|
||||
on-change="$ctrl.updateDiscount()">
|
||||
<t-right-icons>
|
||||
<span class="filter">%</span>
|
||||
</t-right-icons>
|
||||
</vn-textfield>
|
||||
</vn-input-number>
|
||||
<div class="simulator">
|
||||
<p class="simulatorTitle" translate>New price</p>
|
||||
<p>{{($ctrl.edit[0].quantity * $ctrl.edit[0].price)
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue