diff --git a/modules/invoiceOut/back/methods/invoiceOut/transferInvoiceOut.js b/modules/invoiceOut/back/methods/invoiceOut/transferInvoiceOut.js index 86de59e9d..e059aa431 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/transferInvoiceOut.js +++ b/modules/invoiceOut/back/methods/invoiceOut/transferInvoiceOut.js @@ -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; }; }; diff --git a/modules/invoiceOut/back/models/invoiceCorrection.json b/modules/invoiceOut/back/models/invoice-correction.json similarity index 100% rename from modules/invoiceOut/back/models/invoiceCorrection.json rename to modules/invoiceOut/back/models/invoice-correction.json diff --git a/modules/ticket/back/methods/sale/clone.js b/modules/ticket/back/methods/sale/clone.js index e1793d088..dae15d7df 100644 --- a/modules/ticket/back/methods/sale/clone.js +++ b/modules/ticket/back/methods/sale/clone.js @@ -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); diff --git a/modules/ticket/back/methods/sale/refund.js b/modules/ticket/back/methods/sale/refund.js index c85bcd834..a69c05f36 100644 --- a/modules/ticket/back/methods/sale/refund.js +++ b/modules/ticket/back/methods/sale/refund.js @@ -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;