fix(transaction): priceDifference now commits its created transaction
This commit is contained in:
parent
270b2802c8
commit
de7af0fee2
|
@ -66,70 +66,77 @@ module.exports = Self => {
|
|||
myOptions.transaction = tx;
|
||||
}
|
||||
|
||||
const isEditable = await Self.isEditable(ctx, args.id, myOptions);
|
||||
try {
|
||||
const isEditable = await Self.isEditable(ctx, args.id, myOptions);
|
||||
|
||||
if (!isEditable)
|
||||
throw new UserError(`The sales of this ticket can't be modified`);
|
||||
if (!isEditable)
|
||||
throw new UserError(`The sales of this ticket can't be modified`);
|
||||
|
||||
const isProductionBoss = await models.Account.hasRole(userId, 'productionBoss', myOptions);
|
||||
if (!isProductionBoss) {
|
||||
const zoneShipped = await models.Agency.getShipped(
|
||||
args.landed,
|
||||
args.addressId,
|
||||
args.agencyModeId,
|
||||
args.warehouseId,
|
||||
myOptions);
|
||||
const isProductionBoss = await models.Account.hasRole(userId, 'productionBoss', myOptions);
|
||||
if (!isProductionBoss) {
|
||||
const zoneShipped = await models.Agency.getShipped(
|
||||
args.landed,
|
||||
args.addressId,
|
||||
args.agencyModeId,
|
||||
args.warehouseId,
|
||||
myOptions);
|
||||
|
||||
if (!zoneShipped || zoneShipped.zoneFk != args.zoneId)
|
||||
throw new UserError(`You don't have privileges to change the zone`);
|
||||
}
|
||||
|
||||
const items = await models.Sale.find({
|
||||
where: {
|
||||
ticketFk: args.id
|
||||
},
|
||||
order: 'concept ASC',
|
||||
include: 'item'
|
||||
}, myOptions);
|
||||
|
||||
const salesObj = {
|
||||
items: items,
|
||||
totalUnitPrice: 0.00,
|
||||
totalNewPrice: 0.00,
|
||||
totalDifference: 0.00,
|
||||
};
|
||||
|
||||
const query = `CALL vn.ticket_priceDifference(?, ?, ?, ?, ?)`;
|
||||
const params = [args.id, args.landed, args.addressId, args.zoneId, args.warehouseId];
|
||||
const [difComponents] = await Self.rawSql(query, params, myOptions);
|
||||
|
||||
const map = new Map();
|
||||
|
||||
// Sale price component, one per sale
|
||||
for (difComponent of difComponents)
|
||||
map.set(difComponent.saleFk, difComponent);
|
||||
|
||||
function round(value) {
|
||||
return Math.round(value * 100) / 100;
|
||||
}
|
||||
|
||||
for (sale of salesObj.items) {
|
||||
const difComponent = map.get(sale.id);
|
||||
|
||||
if (difComponent) {
|
||||
sale.component = difComponent;
|
||||
|
||||
salesObj.totalDifference += difComponent.difference;
|
||||
salesObj.totalDifference = round(salesObj.totalDifference);
|
||||
|
||||
salesObj.totalNewPrice += difComponent.newPrice;
|
||||
salesObj.totalNewPrice = round(salesObj.totalNewPrice);
|
||||
if (!zoneShipped || zoneShipped.zoneFk != args.zoneId)
|
||||
throw new UserError(`You don't have privileges to change the zone`);
|
||||
}
|
||||
|
||||
salesObj.totalUnitPrice += sale.price;
|
||||
salesObj.totalUnitPrice = round(salesObj.totalUnitPrice);
|
||||
}
|
||||
const items = await models.Sale.find({
|
||||
where: {
|
||||
ticketFk: args.id
|
||||
},
|
||||
order: 'concept ASC',
|
||||
include: 'item'
|
||||
}, myOptions);
|
||||
|
||||
return salesObj;
|
||||
const salesObj = {
|
||||
items: items,
|
||||
totalUnitPrice: 0.00,
|
||||
totalNewPrice: 0.00,
|
||||
totalDifference: 0.00,
|
||||
};
|
||||
|
||||
const query = `CALL vn.ticket_priceDifference(?, ?, ?, ?, ?)`;
|
||||
const params = [args.id, args.landed, args.addressId, args.zoneId, args.warehouseId];
|
||||
const [difComponents] = await Self.rawSql(query, params, myOptions);
|
||||
|
||||
const map = new Map();
|
||||
|
||||
// Sale price component, one per sale
|
||||
for (difComponent of difComponents)
|
||||
map.set(difComponent.saleFk, difComponent);
|
||||
|
||||
for (sale of salesObj.items) {
|
||||
const difComponent = map.get(sale.id);
|
||||
|
||||
if (difComponent) {
|
||||
sale.component = difComponent;
|
||||
|
||||
salesObj.totalDifference += difComponent.difference;
|
||||
salesObj.totalDifference = round(salesObj.totalDifference);
|
||||
|
||||
salesObj.totalNewPrice += difComponent.newPrice;
|
||||
salesObj.totalNewPrice = round(salesObj.totalNewPrice);
|
||||
}
|
||||
|
||||
salesObj.totalUnitPrice += sale.price;
|
||||
salesObj.totalUnitPrice = round(salesObj.totalUnitPrice);
|
||||
}
|
||||
|
||||
if (tx) await tx.commit();
|
||||
|
||||
return salesObj;
|
||||
} catch (e) {
|
||||
if (tx) await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
function round(value) {
|
||||
return Math.round(value * 100) / 100;
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue