From a34519f6d0d5dbd60e1cd0a41af4a72486eeb5cf Mon Sep 17 00:00:00 2001 From: carlossa Date: Wed, 7 Jun 2023 14:12:04 +0200 Subject: [PATCH] refs #4764 refundTicket --- modules/ticket/back/methods/sale/refund.js | 74 ++++++---- .../ticket/back/methods/services/refund.js | 138 ------------------ modules/ticket/back/models/ticket-methods.js | 1 - modules/ticket/front/services/index.js | 6 +- 4 files changed, 48 insertions(+), 171 deletions(-) delete mode 100644 modules/ticket/back/methods/services/refund.js diff --git a/modules/ticket/back/methods/sale/refund.js b/modules/ticket/back/methods/sale/refund.js index 18ccee976..461bb1f3b 100644 --- a/modules/ticket/back/methods/sale/refund.js +++ b/modules/ticket/back/methods/sale/refund.js @@ -5,8 +5,7 @@ module.exports = Self => { accepts: [ { arg: 'salesIds', - type: ['number'], - required: true + type: ['number'] }, { arg: 'servicesIds', @@ -36,6 +35,7 @@ module.exports = Self => { myOptions.transaction = tx; } + let refundTicket = null; try { const refundAgencyMode = await models.AgencyMode.findOne({ include: { @@ -50,38 +50,54 @@ module.exports = Self => { const refoundZoneId = refundAgencyMode.zones()[0].id; - const salesFilter = { - where: {id: {inq: salesIds}}, - include: { - relation: 'components', - scope: { - fields: ['saleFk', 'componentFk', 'value'] + if (salesIds) { + const salesFilter = { + where: {id: {inq: salesIds}}, + include: { + relation: 'components', + scope: { + fields: ['saleFk', 'componentFk', 'value'] + } } + }; + const sales = await models.Sale.find(salesFilter, myOptions); + const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))]; + + const now = Date.vnNew(); + const [firstTicketId] = ticketsIds; + + refundTicket = await createTicketRefund(firstTicketId, now, refundAgencyMode, refoundZoneId, myOptions); + + for (const sale of sales) { + const createdSale = await models.Sale.create({ + ticketFk: refundTicket.id, + itemFk: sale.itemFk, + 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); } - }; - const sales = await models.Sale.find(salesFilter, myOptions); - const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))]; + } - const now = Date.vnNew(); - const [firstTicketId] = ticketsIds; + if (!refundTicket) { + const servicesFilter = { + where: {id: {inq: servicesIds}} + }; + const services = await models.TicketService.find(servicesFilter, myOptions); + const ticketsIds = [...new Set(services.map(service => service.ticketFk))]; - const refundTicket = await createTicketRefund(firstTicketId, now, refundAgencyMode, refoundZoneId, myOptions); + const now = Date.vnNew(); + const [firstTicketId] = ticketsIds; - for (const sale of sales) { - const createdSale = await models.Sale.create({ - ticketFk: refundTicket.id, - itemFk: sale.itemFk, - 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); + refundTicket = await createTicketRefund(firstTicketId, now, refundAgencyMode, refoundZoneId, myOptions); + return refundTicket; } if (servicesIds && servicesIds.length > 0) { diff --git a/modules/ticket/back/methods/services/refund.js b/modules/ticket/back/methods/services/refund.js deleted file mode 100644 index af58a6286..000000000 --- a/modules/ticket/back/methods/services/refund.js +++ /dev/null @@ -1,138 +0,0 @@ -module.exports = Self => { - Self.remoteMethod('refund', { - description: 'Create refund tickets with sales and services if provided', - accessType: 'WRITE', - accepts: [ - { - arg: 'salesIds', - type: ['number'], - required: true - }, - { - arg: 'servicesIds', - type: ['number'] - } - ], - returns: { - type: ['number'], - root: true - }, - http: { - path: `/refund`, - verb: 'post' - } - }); - - Self.refund = async(salesIds, servicesIds, 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; - } - - try { - const refundAgencyMode = await models.AgencyMode.findOne({ - include: { - relation: 'zones', - scope: { - limit: 1, - field: ['id', 'name'] - } - }, - where: {code: 'refund'} - }, myOptions); - - const refoundZoneId = refundAgencyMode.zones()[0].id; - - const salesFilter = { - where: {id: {inq: salesIds}}, - include: { - relation: 'components', - scope: { - fields: ['saleFk', 'componentFk', 'value'] - } - } - }; - const sales = await models.Sale.find(salesFilter, myOptions); - const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))]; - - const now = Date.vnNew(); - const [firstTicketId] = ticketsIds; - - const refundTicket = await createTicketRefund(firstTicketId, now, refundAgencyMode, refoundZoneId, myOptions); - - for (const sale of sales) { - const createdSale = await models.Sale.create({ - ticketFk: refundTicket.id, - itemFk: sale.itemFk, - 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: - service.quantity, - price: service.price, - taxClassFk: service.taxClassFk, - ticketFk: refundTicket.id, - ticketServiceTypeFk: service.ticketServiceTypeFk, - }, myOptions); - } - } - - if (tx) await tx.commit(); - - return refundTicket; - } catch (e) { - if (tx) await tx.rollback(); - throw e; - } - }; - - async function createTicketRefund(ticketId, now, refundAgencyMode, refoundZoneId, myOptions) { - const models = Self.app.models; - - const filter = {include: {relation: 'address'}}; - const ticket = await models.Ticket.findById(ticketId, filter, myOptions); - - const refundTicket = await models.Ticket.create({ - clientFk: ticket.clientFk, - shipped: now, - addressFk: ticket.address().id, - agencyModeFk: refundAgencyMode.id, - nickname: ticket.address().nickname, - warehouseFk: ticket.warehouseFk, - companyFk: ticket.companyFk, - landed: now, - zoneFk: refoundZoneId - }, myOptions); - - await models.TicketRefund.create({ - refundTicketFk: refundTicket.id, - originalTicketFk: ticket.id, - }, myOptions); - - return refundTicket; - } -}; diff --git a/modules/ticket/back/models/ticket-methods.js b/modules/ticket/back/models/ticket-methods.js index 4acaaea86..e5a8e8d94 100644 --- a/modules/ticket/back/models/ticket-methods.js +++ b/modules/ticket/back/models/ticket-methods.js @@ -39,5 +39,4 @@ module.exports = function(Self) { require('../methods/ticket/collectionLabel')(Self); require('../methods/ticket/expeditionPalletLabel')(Self); require('../methods/ticket/saveSign')(Self); - require('../methods/services/refund')(Self); }; diff --git a/modules/ticket/front/services/index.js b/modules/ticket/front/services/index.js index ae27a19ac..a6f91a110 100644 --- a/modules/ticket/front/services/index.js +++ b/modules/ticket/front/services/index.js @@ -68,19 +68,19 @@ class Controller extends Section { } createRefund() { - const services = this.selectedServices(); + const services = this.selectedValidServices(); if (!services) return; const servicesIds = services.map(service => service.id); const params = {servicesIds: servicesIds}; - const query = 'Tickets/refund'; + const query = 'Sales/refund'; this.$http.post(query, params).then(res => { const refundTicket = res.data; this.vnApp.showSuccess(this.$t('The following refund ticket have been created', { ticketId: refundTicket.id })); - this.$state.go('ticket.card.service', {id: refundTicket.id}); + this.$state.go('ticket.card.sale', {id: refundTicket.id}); this.resetChanges(); });