1485 refactor updatePrice
gitea/salix/dev This commit has test failures Details

This commit is contained in:
Bernat 2019-05-29 12:41:26 +02:00
parent 85761043cd
commit 7a7ea03bff
4 changed files with 51 additions and 52 deletions

View File

@ -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)),

View File

@ -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() => {

View File

@ -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]);
};
}
};

View File

@ -173,16 +173,17 @@
<h5>MANÁ: {{$ctrl.mana | currency: 'EUR':0}}</h5>
</vn-horizontal>
<div pad-medium>
<vn-textfield
<vn-input-number
vn-focus
label="Price"
model="$ctrl.editedPrice"
type="text"
step="0.01"
on-change="$ctrl.updatePrice()">
<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.sale.quantity * $ctrl.editedPrice)