Tarea #1663 ticket-sale al hacer un slit de lineas debe dejar rastro en el log

This commit is contained in:
Bernat 2019-09-10 13:27:41 +02:00
parent 7ecb67052d
commit 8932aa4e5a
2 changed files with 52 additions and 21 deletions

View File

@ -103,5 +103,6 @@
"Weekday cannot be blank": "El día de la semana no puede quedar en blanco",
"You can't delete a confirmed order": "No puedes borrar un pedido confirmado",
"Can't create stowaway for this ticket": "No se puede crear un polizon para este ticket",
"Value has an invalid format": "El valor tiene un formato incorrecto"
"Value has an invalid format": "El valor tiene un formato incorrecto",
"Invalid quantity": "Cantidad invalida"
}

View File

@ -61,33 +61,61 @@ module.exports = Self => {
for (const sale of originalSales)
map.set(sale.id, sale);
const promises = [];
for (const sale of sales) {
const originalSale = map.get(sale.id);
let originalSaleQuantity = originalSale.quantity;
if (sale.quantity == originalSale.quantity) {
const updatedSale = models.Sale.updateAll({
await models.Sale.updateAll({
id: sale.id
}, {ticketFk: ticketId}, options);
promises.push(updatedSale);
} else if (sale.quantity < originalSale.quantity) {
const transferedSale = await transferPartialSale(
await transferPartialSale(
ticketId, originalSale, sale, options);
promises.push(transferedSale);
}
let logTicketOrigin = {
originFk: id,
userFk: 9,
action: 'update',
changedModel: 'Ticket',
changedModelId: ticketId,
oldInstance: {item: sale.itemFk,
quantity: 0,
concept: sale.concept,
ticket: ticketId},
newInstance: {item: sale.itemFk,
quantity: sale.quantity,
concept: sale.concept,
ticket: ticketId}
};
await models.TicketLog.create(logTicketOrigin, options);
let logTicketDestination = {
originFk: ticketId,
userFk: 9,
action: 'update',
changedModel: 'Ticket',
changedModelId: ticketId,
oldInstance: {item: sale.itemFk,
quantity: originalSaleQuantity,
concept: sale.concept,
ticket: id},
newInstance: {item: sale.itemFk,
quantity: originalSaleQuantity - sale.quantity,
concept: sale.concept,
ticket: id}
};
await models.TicketLog.create(logTicketDestination, options);
}
const isTicketEmpty = await models.Ticket.isEmpty(id, options);
if (isTicketEmpty) {
originalTicket.updateAttributes({
isDeleted: true
}, options);
}
if (isTicketEmpty)
await originalTicket.updateAttributes({isDeleted: true}, options);
await Promise.all(promises);
await tx.commit();
if (tx.id) await tx.commit();
return {id: ticketId};
} catch (error) {
@ -127,19 +155,21 @@ module.exports = Self => {
async function transferPartialSale(ticketId, originalSale, sale, options) {
const models = Self.app.models;
if (sale.quantity > originalSale.quantity)
throw new UserError('Invalid quantity');
// Update original sale
const rest = originalSale.quantity - sale.quantity;
const updatedSale = models.Sale.updateAll({
const updatedSale = await models.Sale.updateAll({
id: sale.id
}, {quantity: rest}, options);
// Clone sale with new quantity
const newSale = originalSale;
newSale.id = undefined;
newSale.ticketFk = ticketId;
newSale.quantity = sale.quantity;
const createdSale = await models.Sale.create(newSale, options);
await models.Sale.create(newSale, options);
// Clone sale components
const saleComponents = await models.SaleComponent.find({
@ -150,9 +180,9 @@ module.exports = Self => {
return component;
});
const createdComponents = models.SaleComponent
.create(newComponents, options);
return [updatedSale, createdComponents];
await models.SaleComponent.create(newComponents, options);
return [updatedSale, createdSale];
}
};