priceDifference decimal round
gitea/salix/test This commit looks good Details

This commit is contained in:
Joan Sanchez 2019-09-04 13:55:21 +02:00
parent 711b94c835
commit ea531a23f2
1 changed files with 23 additions and 19 deletions

View File

@ -53,7 +53,7 @@ module.exports = Self => {
Self.priceDifference = async(ctx, id, landed, addressId, agencyModeId, zoneId, warehouseId) => { Self.priceDifference = async(ctx, id, landed, addressId, agencyModeId, zoneId, warehouseId) => {
const models = Self.app.models; const models = Self.app.models;
const isEditable = await models.Ticket.isEditable(ctx, id); const isEditable = await Self.isEditable(ctx, id);
const userId = ctx.req.accessToken.userId; const userId = ctx.req.accessToken.userId;
if (!isEditable) if (!isEditable)
@ -67,19 +67,18 @@ module.exports = Self => {
throw new UserError(`You don't have privileges to change the zone`); throw new UserError(`You don't have privileges to change the zone`);
} }
let salesObj = {}; let salesObj = {
salesObj.items = await models.Sale.find({ items: await models.Sale.find({
where: { where: {
ticketFk: id ticketFk: id
}, },
order: 'concept ASC', order: 'concept ASC',
include: [{ include: 'item'
relation: 'item' }),
}] totalUnitPrice: 0.00,
}); totalNewPrice: 0.00,
salesObj.totalUnitPrice = 0.00; totalDifference: 0.00,
salesObj.totalNewPrice = 0.00; };
salesObj.totalDifference = 0.00;
const query = `CALL vn.ticket_priceDifference(?, ?, ?, ?, ?)`; const query = `CALL vn.ticket_priceDifference(?, ?, ?, ?, ?)`;
const args = [id, landed, addressId, zoneId, warehouseId]; const args = [id, landed, addressId, zoneId, warehouseId];
@ -91,20 +90,25 @@ module.exports = Self => {
map.set(difComponent.saleFk, difComponent); map.set(difComponent.saleFk, difComponent);
}); });
function round(value) {
return Math.round(value * 100) / 100;
}
salesObj.items.forEach(sale => { salesObj.items.forEach(sale => {
const difComponent = map.get(sale.id); const difComponent = map.get(sale.id);
if (difComponent) { if (difComponent) {
sale.component = difComponent; sale.component = difComponent;
salesObj.totalNewPrice += sale.component.newPrice; salesObj.totalDifference += difComponent.difference;
salesObj.totalDifference += sale.component.difference; salesObj.totalDifference = round(salesObj.totalDifference);
salesObj.totalUnitPrice = Math.round(salesObj.totalUnitPrice * 100) / 100;
salesObj.totalNewPrice = Math.round(salesObj.totalNewPrice * 100) / 100; salesObj.totalNewPrice += difComponent.newPrice;
salesObj.totalNewPrice = round(salesObj.totalNewPrice);
} }
salesObj.totalUnitPrice += sale.price; salesObj.totalUnitPrice += sale.price;
salesObj.totalDifference = Math.round(salesObj.totalDifference * 100) / 100; salesObj.totalUnitPrice = round(salesObj.totalUnitPrice);
}); });
return salesObj; return salesObj;