refs #5014 WIP trasactions added
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Jorge Penadés 2023-08-01 16:13:24 +02:00
parent fc32f48b64
commit bd122e6327
4 changed files with 23 additions and 27 deletions

View File

@ -48,7 +48,7 @@ module.exports = Self => {
Self.transferInvoiceOut = async(ctx, id, ref, newClientFk, cplusRectificationId, cplusInvoiceType477Id, invoiceCorrectionTypeId, options) => {
const models = Self.app.models;
const myOptions = {};
const myOptions = {userId: ctx.req.accessToken.userId};
let tx;
if (typeof options == 'object')
@ -64,7 +64,6 @@ module.exports = Self => {
const tickets = await models.Ticket.find(filter, myOptions);
const ticketsIds = tickets.map(ticket => ticket.id);
await models.Ticket.refund(ctx, ticketsIds, null, myOptions);
console.log('Ticket refunded');
// Clone tickets
const refundAgencyMode = await models.AgencyMode.findOne({
include: {
@ -82,22 +81,17 @@ module.exports = Self => {
const salesFilter = {where: {ticketFk: {inq: ticketsIds}}};
const sales = await models.Sale.find(salesFilter, myOptions);
const clonedTickets = await models.Sale.clone(sales, refundAgencyMode, refoundZoneId, servicesIds, null, false, false, myOptions);
console.log('cloned tickets');
// Update client
for (const clonedTicket of clonedTickets) {
// const ticket = await models.Ticket.findById(clonedTicketId, myOptions);
console.log(clonedTicket);
for (const clonedTicket of clonedTickets)
await clonedTicket.updateAttributes({clientFk: newClientFk}, myOptions);
console.log(clonedTicket);
}
// Quick invoice
const clonedTicketIds = clonedTickets.map(clonedTicket => clonedTicket.id);
console.log(clonedTicketIds);
const invoiceIds = await models.Ticket.invoiceTickets(ctx, clonedTicketIds, myOptions);
// Insert InvoiceCorrection
for (const invoiceId of invoiceIds) {
await models.invoiceCorrection.create({
await models.InvoiceCorrection.create({
correctingFk: invoiceId,
correctedFk: id,
cplusRectificationTypeFk: cplusRectificationId,
@ -105,12 +99,11 @@ module.exports = Self => {
invoiceCorrectionType: invoiceCorrectionTypeId
});
}
if (tx) await tx.commit();
return true;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
return true;
};
};

View File

@ -1,8 +1,12 @@
module.exports = Self => {
Self.clone = async(sales, refundAgencyMode, refoundZoneId, servicesIds, withWarehouse, group, negative, myOptions) => {
Self.clone = async(sales, refundAgencyMode, refoundZoneId, servicesIds, withWarehouse, group, negative, options) => {
const models = Self.app.models;
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
@ -39,15 +43,15 @@ module.exports = Self => {
}, myOptions);
updatedTickets.push(newTicket);
}
const sales = ticket.sale();
const saleIds = sales.map(sale => sale.id);
const salesByTicket = ticket.sale();
const saleIds = salesByTicket.map(sale => sale.id);
const saleComponentsFilter = {
where: {saleFk: {inq: saleIds}},
scope: {
fields: ['saleFk', 'componentFk', 'value']
}
};
for (const sale of sales) {
for (const sale of salesByTicket) {
const createdSale = await models.Sale.create({
ticketFk: newTicket.id,
itemFk: sale.itemFk,
@ -56,7 +60,7 @@ module.exports = Self => {
price: sale.price,
discount: sale.discount,
}, myOptions);
const components = await models.SaleComponent.find(saleComponentsFilter, myOptions);
const components = await models.SaleComponent.find(saleComponentsFilter, myOptions); // Revisar con Alex
// const components = sale.components();
for (const component of components)
component.saleFk = createdSale.id;
@ -86,10 +90,7 @@ module.exports = Self => {
if (group) {
await Self.rawSql(query, [newTicket.id], myOptions);
if (tx) await tx.commit();
return {
refundTicket: newTicket,
originalTicketFk: firstTicketId
};
return newTicket;
} else {
for (const updatedTicket of updatedTickets)
await Self.rawSql(query, [updatedTicket.id], myOptions);

View File

@ -155,7 +155,7 @@ module.exports = Self => {
}
};
const sales = await models.Sale.find(salesFilter, myOptions);
const clonedTicket = await models.Sale.clone(
const refundTicket = await models.Sale.clone(
sales,
refundAgencyMode,
refoundZoneId,
@ -166,12 +166,14 @@ module.exports = Self => {
myOptions
);
const refundTicket = clonedTicket.refundTicket;
const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))];
for (const ticketId of ticketsIds) {
await models.TicketRefund.create({
refundTicketFk: refundTicket.id,
originalTicketFk: ticketId,
}, myOptions);
}
await models.TicketRefund.create({
refundTicketFk: refundTicket.id,
originalTicketFk: clonedTicket.originalTicketFk,
}, myOptions);
if (tx) await tx.commit();
return refundTicket;