diff --git a/modules/invoiceOut/back/methods/invoiceOut/transferInvoiceOut.js b/modules/invoiceOut/back/methods/invoiceOut/transferInvoiceOut.js index d53581ac6..86de59e9d 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/transferInvoiceOut.js +++ b/modules/invoiceOut/back/methods/invoiceOut/transferInvoiceOut.js @@ -6,10 +6,35 @@ module.exports = Self => { accessType: 'WRITE', accepts: [ { - arg: 'data', - type: 'Object', + arg: 'id', + type: 'number', required: true - } + }, + { + arg: 'ref', + type: 'string', + required: true + }, + { + arg: 'newClientFk', + type: 'number', + required: true + }, + { + arg: 'cplusRectificationId', + type: 'number', + required: true + }, + { + arg: 'cplusInvoiceType477Id', + type: 'number', + required: true + }, + { + arg: 'invoiceCorrectionTypeId', + type: 'number', + required: true + }, ], returns: { type: 'boolean', @@ -21,7 +46,7 @@ module.exports = Self => { } }); - Self.transferInvoiceOut = async(ctx, params, options) => { + Self.transferInvoiceOut = async(ctx, id, ref, newClientFk, cplusRectificationId, cplusInvoiceType477Id, invoiceCorrectionTypeId, options) => { const models = Self.app.models; const myOptions = {}; let tx; @@ -34,16 +59,12 @@ module.exports = Self => { myOptions.transaction = tx; } try { - const {id, ref, newClientFk, cplusRectificationId, cplusInvoiceType477FkId, invoiceCorrectionTypeId} = params; - if (!id || !ref || !newClientFk || !cplusRectificationId || !cplusInvoiceType477FkId || !invoiceCorrectionTypeId) - throw new UserError(`There are missing fields.`); - // Refund tickets and group const filter = {where: {refFk: ref}}; 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: { @@ -58,25 +79,20 @@ module.exports = Self => { const refoundZoneId = refundAgencyMode.zones()[0].id; const services = await models.TicketService.find(filter, myOptions); const servicesIds = services.map(service => service.id); - const salesFilter = { - where: {id: {inq: salesIds}}, - include: { - relation: 'components', - scope: { - fields: ['saleFk', 'componentFk', 'value'] - } - } - }; + const salesFilter = {where: {ticketFk: {inq: ticketsIds}}}; const sales = await models.Sale.find(salesFilter, myOptions); - const isRefund = false; - const clonedTicketIds = await models.Sale.clone(sales, refundAgencyMode, refoundZoneId, servicesIds, null, isRefund, myOptions); - + const clonedTickets = await models.Sale.clone(sales, refundAgencyMode, refoundZoneId, servicesIds, null, false, false, myOptions); + console.log('cloned tickets'); // Update client - for (const clonedTicketId of clonedTicketIds) { - const ticket = await models.Ticket.findById(clonedTicketId, myOptions); - await ticket.updateAttributes({clientFk: newClientFk}); + for (const clonedTicket of clonedTickets) { + // const ticket = await models.Ticket.findById(clonedTicketId, myOptions); + console.log(clonedTicket); + 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 @@ -85,7 +101,7 @@ module.exports = Self => { correctingFk: invoiceId, correctedFk: id, cplusRectificationTypeFk: cplusRectificationId, - cplusInvoiceType477Fk: cplusInvoiceType477FkId, + cplusInvoiceType477Fk: cplusInvoiceType477Id, invoiceCorrectionType: invoiceCorrectionTypeId }); } diff --git a/modules/invoiceOut/front/descriptor-menu/index.html b/modules/invoiceOut/front/descriptor-menu/index.html index 1655290f4..dc5c2a8e9 100644 --- a/modules/invoiceOut/front/descriptor-menu/index.html +++ b/modules/invoiceOut/front/descriptor-menu/index.html @@ -222,12 +222,13 @@ console.log(res.data)); } diff --git a/modules/ticket/back/methods/sale/clone.js b/modules/ticket/back/methods/sale/clone.js index 96e66de2c..e1793d088 100644 --- a/modules/ticket/back/methods/sale/clone.js +++ b/modules/ticket/back/methods/sale/clone.js @@ -1,67 +1,104 @@ module.exports = Self => { - Self.clone = async(sales, refundAgencyMode, refoundZoneId, servicesIds, withWarehouse, isRefund, myOptions) => { + Self.clone = async(sales, refundAgencyMode, refoundZoneId, servicesIds, withWarehouse, group, negative, myOptions) => { const models = Self.app.models; - const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))]; + let tx; + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))]; + const [firstTicketId] = ticketsIds; const now = Date.vnNew(); let updatedTickets = []; + let newTicket; + const filter = { + include: [ + {relation: 'address'}, + { + relation: 'sale', + inq: sales, + }, + ] + }; + try { + for (const [index, ticketId] of ticketsIds.entries()) { + const ticket = await models.Ticket.findById(ticketId, filter, myOptions); + if (!group || !index) { + newTicket = await models.Ticket.create({ + clientFk: ticket.clientFk, + shipped: now, + addressFk: ticket.address().id, + agencyModeFk: refundAgencyMode.id, + nickname: ticket.address().nickname, + warehouseFk: withWarehouse ? ticket.warehouseFk : null, + companyFk: ticket.companyFk, + landed: now, + zoneFk: refoundZoneId + }, myOptions); + updatedTickets.push(newTicket); + } + const sales = ticket.sale(); + const saleIds = sales.map(sale => sale.id); + const saleComponentsFilter = { + where: {saleFk: {inq: saleIds}}, + scope: { + fields: ['saleFk', 'componentFk', 'value'] + } + }; + for (const sale of sales) { + const createdSale = await models.Sale.create({ + ticketFk: newTicket.id, + itemFk: sale.itemFk, + quantity: (negative) ? - sale.quantity : sale.quantity, + concept: sale.concept, + price: sale.price, + discount: sale.discount, + }, myOptions); + const components = await models.SaleComponent.find(saleComponentsFilter, myOptions); + // const components = sale.components(); + for (const component of components) + component.saleFk = createdSale.id; - for (const ticketId of ticketsIds) { - const filter = {include: {relation: 'address'}}; - const ticket = await models.Ticket.findById(ticketId, filter, myOptions); - - const ticketUpdated = await models.Ticket.create({ - clientFk: ticket.clientFk, - shipped: now, - addressFk: ticket.address().id, - agencyModeFk: refundAgencyMode.id, - nickname: ticket.address().nickname, - warehouseFk: withWarehouse ? ticket.warehouseFk : null, - companyFk: ticket.companyFk, - landed: now, - zoneFk: refoundZoneId - }, myOptions); - updatedTickets.push(ticketUpdated); - } - - for (const sale of sales) { - const createdSale = await models.Sale.create({ - ticketFk: sale.ticketFk, - itemFk: sale.itemFk, - quantity: (isRefund) ? - sale.quantity : sale.quantity, - concept: sale.concept, - price: sale.price, - discount: sale.discount, - }, myOptions); - - const components = sale.components(); - for (const component of components) - component.saleFk = createdSale.id; - - await models.SaleComponent.create(components, myOptions); - } - - if (servicesIds && servicesIds.length > 0) { - const servicesFilter = { - where: {id: {inq: servicesIds}} - }; - const services = await models.TicketService.find(servicesFilter, myOptions); - for (const service of services) { - await models.TicketService.create({ - description: service.description, - quantity: (isRefund) ? - service.quantity : service.quantity, - price: service.price, - taxClassFk: service.taxClassFk, - ticketFk: service.ticketFk, - ticketServiceTypeFk: service.ticketServiceTypeFk, - }, myOptions); + await models.SaleComponent.create(components, myOptions); + } } + if (servicesIds && servicesIds.length > 0) { + const servicesFilter = { + where: {id: {inq: servicesIds}} + }; + const services = await models.TicketService.find(servicesFilter, myOptions); + for (const service of services) { + await models.TicketService.create({ + description: service.description, + quantity: (negative) ? - service.quantity : service.quantity, + price: service.price, + taxClassFk: service.taxClassFk, + ticketFk: service.ticketFk, + ticketServiceTypeFk: service.ticketServiceTypeFk, + }, myOptions); + } + } + + const query = `CALL vn.ticket_recalc(?, NULL)`; + + if (group) { + await Self.rawSql(query, [newTicket.id], myOptions); + if (tx) await tx.commit(); + return { + refundTicket: newTicket, + originalTicketFk: firstTicketId + }; + } else { + for (const updatedTicket of updatedTickets) + await Self.rawSql(query, [updatedTicket.id], myOptions); + if (tx) await tx.commit(); + return updatedTickets/* updatedTickets.map(updatedTicket => updatedTicket.id) */; + } + } catch (e) { + if (tx) await tx.rollback(); + throw e; } - - const query = `CALL vn.ticket_recalc(?, NULL)`; - - for (const updatedTicket of updatedTickets) - await Self.rawSql(query, [updatedTicket.id], myOptions); - return updatedTickets.map(updatedTicket => updatedTicket.id); }; }; diff --git a/modules/ticket/back/methods/sale/refund.js b/modules/ticket/back/methods/sale/refund.js index 6bb131e4d..c85bcd834 100644 --- a/modules/ticket/back/methods/sale/refund.js +++ b/modules/ticket/back/methods/sale/refund.js @@ -155,19 +155,23 @@ module.exports = Self => { } }; const sales = await models.Sale.find(salesFilter, myOptions); - const group = true; - const isRefund = true; - - const refundTicket = await cloneAndGroup( + const clonedTicket = await models.Sale.clone( sales, refundAgencyMode, refoundZoneId, servicesIds, withWarehouse, - group, - isRefund, + true, + true, myOptions ); + + const refundTicket = clonedTicket.refundTicket; + + await models.TicketRefund.create({ + refundTicketFk: refundTicket.id, + originalTicketFk: clonedTicket.originalTicketFk, + }, myOptions); if (tx) await tx.commit(); return refundTicket; @@ -177,74 +181,7 @@ module.exports = Self => { } }; - async function cloneAndGroup(sales, refundAgencyMode, refoundZoneId, servicesIds, withWarehouse, group, isRefund, myOptions) { - if (!group) { - const tickets = await models.Sale.clone(sales, - refundAgencyMode, - refoundZoneId, servicesIds, - withWarehouse, - isRefund, - myOptions); - return tickets; - } - - const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))]; - const [firstTicketId] = ticketsIds; - const filter = {include: {relation: 'address'}}; - const ticket = await models.Ticket.findById(firstTicketId, filter, myOptions); - - const ticketUpdated = await models.Ticket.create({ - clientFk: ticket.clientFk, - shipped: now, - addressFk: ticket.address().id, - agencyModeFk: refundAgencyMode.id, - nickname: ticket.address().nickname, - warehouseFk: withWarehouse ? ticket.warehouseFk : null, - companyFk: ticket.companyFk, - landed: now, - zoneFk: refoundZoneId - }, myOptions); - - for (const sale of sales) { - const createdSale = await models.Sale.create({ - ticketFk: ticketUpdated.id, - itemFk: sale.itemFk, - quantity: (isRefund) ? - sale.quantity : sale.quantity, - concept: sale.concept, - price: sale.price, - discount: sale.discount, - }, myOptions); - - const components = sale.components(); - for (const component of components) - component.saleFk = createdSale.id; - - await models.SaleComponent.create(components, myOptions); - } - - if (servicesIds && servicesIds.length > 0) { - const servicesFilter = { - where: {id: {inq: servicesIds}} - }; - const services = await models.TicketService.find(servicesFilter, myOptions); - for (const service of services) { - await models.TicketService.create({ - description: service.description, - quantity: (isRefund) ? - service.quantity : service.quantity, - price: service.price, - taxClassFk: service.taxClassFk, - ticketFk: ticketUpdated.id, - ticketServiceTypeFk: service.ticketServiceTypeFk, - }, myOptions); - } - } - - const query = `CALL vn.ticket_recalc(?, NULL)`; - await Self.rawSql(query, [refundTicket.id], myOptions); - return ticketUpdated; - } - - async function createTicketRefund(ticketId, now, refundAgencyMode, refoundZoneId, withWarehouse, myOptions) { + /* async function createTicketRefund(ticketId, now, refundAgencyMode, refoundZoneId, withWarehouse, myOptions) { const models = Self.app.models; const filter = {include: {relation: 'address'}}; @@ -268,5 +205,5 @@ module.exports = Self => { }, myOptions); return refundTicket; - } + } */ }; diff --git a/modules/ticket/back/methods/sale/specs/refund.spec.js b/modules/ticket/back/methods/sale/specs/refund.spec.js index b81f7f84d..727ce2fac 100644 --- a/modules/ticket/back/methods/sale/specs/refund.spec.js +++ b/modules/ticket/back/methods/sale/specs/refund.spec.js @@ -1,7 +1,7 @@ const models = require('vn-loopback/server/server').models; const LoopBackContext = require('loopback-context'); -describe('Sale refund()', () => { +fdescribe('Sale refund()', () => { const userId = 5; const ctx = {req: {accessToken: userId}}; const activeCtx = { diff --git a/modules/ticket/back/methods/ticket/refund.js b/modules/ticket/back/methods/ticket/refund.js index c99b6aa83..758384ae2 100644 --- a/modules/ticket/back/methods/ticket/refund.js +++ b/modules/ticket/back/methods/ticket/refund.js @@ -39,7 +39,6 @@ module.exports = Self => { try { const filter = {where: {ticketFk: {inq: ticketsIds}}}; - const sales = await models.Sale.find(filter, myOptions); const salesIds = sales.map(sale => sale.id); diff --git a/modules/ticket/back/models/ticket.json b/modules/ticket/back/models/ticket.json index ec4193bed..1c5610a2a 100644 --- a/modules/ticket/back/models/ticket.json +++ b/modules/ticket/back/models/ticket.json @@ -136,6 +136,11 @@ "type": "belongsTo", "model": "Zone", "foreignKey": "zoneFk" + }, + "sale": { + "type": "hasMany", + "model": "Sale", + "foreignKey": "ticketFk" } } }