refs #5914 WIP fixing transacticions
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Jorge Penadés 2023-08-03 14:01:50 +02:00
parent bd122e6327
commit 725fe674c0
4 changed files with 27 additions and 125 deletions

View File

@ -1,5 +1,3 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
Self.remoteMethodCtx('transferInvoiceOut', { Self.remoteMethodCtx('transferInvoiceOut', {
description: 'Transfer an invoice out to another client', description: 'Transfer an invoice out to another client',
@ -48,7 +46,7 @@ module.exports = Self => {
Self.transferInvoiceOut = async(ctx, id, ref, newClientFk, cplusRectificationId, cplusInvoiceType477Id, invoiceCorrectionTypeId, options) => { Self.transferInvoiceOut = async(ctx, id, ref, newClientFk, cplusRectificationId, cplusInvoiceType477Id, invoiceCorrectionTypeId, options) => {
const models = Self.app.models; const models = Self.app.models;
const myOptions = {userId: ctx.req.accessToken.userId}; const myOptions = {};
let tx; let tx;
if (typeof options == 'object') if (typeof options == 'object')
@ -65,42 +63,35 @@ module.exports = Self => {
const ticketsIds = tickets.map(ticket => ticket.id); const ticketsIds = tickets.map(ticket => ticket.id);
await models.Ticket.refund(ctx, ticketsIds, null, myOptions); await models.Ticket.refund(ctx, ticketsIds, null, myOptions);
// Clone tickets // Clone tickets
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 services = await models.TicketService.find(filter, myOptions); const services = await models.TicketService.find(filter, myOptions);
const servicesIds = services.map(service => service.id); const servicesIds = services.map(service => service.id);
const salesFilter = {where: {ticketFk: {inq: ticketsIds}}}; const salesFilter = {where: {ticketFk: {inq: ticketsIds}}};
const sales = await models.Sale.find(salesFilter, myOptions); const sales = await models.Sale.find(salesFilter, myOptions);
const clonedTickets = await models.Sale.clone(sales, refundAgencyMode, refoundZoneId, servicesIds, null, false, false, myOptions); const clonedTickets = await models.Sale.clone(sales, servicesIds, null, false, false, myOptions);
console.log('cloned tickets', clonedTickets);
// Update client // Update client
for (const clonedTicket of clonedTickets) for (const clonedTicket of clonedTickets) {
await clonedTicket.updateAttributes({clientFk: newClientFk}, myOptions); const ticket = await models.Ticket.findById(clonedTicket);
console.log(ticket);
await ticket.updateAttributes({clientFk: newClientFk}, myOptions);
// await clonedTicket.updateAttributes({clientFk: newClientFk}, myOptions);
}
// Quick invoice // Quick invoice
const clonedTicketIds = clonedTickets.map(clonedTicket => clonedTicket.id); const clonedTicketIds = clonedTickets.map(clonedTicket => clonedTicket.id);
const invoiceIds = await models.Ticket.invoiceTickets(ctx, clonedTicketIds, myOptions); const invoiceId = await models.Ticket.invoiceTickets(ctx, clonedTicketIds, myOptions);
// Insert InvoiceCorrection // Insert InvoiceCorrection
for (const invoiceId of invoiceIds) { await models.InvoiceCorrection.create({
await models.InvoiceCorrection.create({ correctingFk: invoiceId,
correctingFk: invoiceId, correctedFk: id,
correctedFk: id, cplusRectificationTypeFk: cplusRectificationId,
cplusRectificationTypeFk: cplusRectificationId, cplusInvoiceType477Fk: cplusInvoiceType477Id,
cplusInvoiceType477Fk: cplusInvoiceType477Id, invoiceCorrectionType: invoiceCorrectionTypeId
invoiceCorrectionType: invoiceCorrectionTypeId });
});
}
if (tx) await tx.commit(); if (tx) await tx.commit();
return true; return invoiceId;
} catch (e) { } catch (e) {
if (tx) await tx.rollback(); if (tx) await tx.rollback();
throw e; throw e;

View File

@ -135,7 +135,11 @@ class Controller extends Section {
cplusInvoiceType477Id: this.cplusInvoiceType477, cplusInvoiceType477Id: this.cplusInvoiceType477,
invoiceCorrectionTypeId: this.invoiceCorrectionType invoiceCorrectionTypeId: this.invoiceCorrectionType
}; };
this.$http.post(`InvoiceOuts/transferInvoice`, params).then(res => console.log(res.data)); this.$http.post(`InvoiceOuts/transferInvoice`, params).then(res => {
newInvoice = res.data;// id de la nueva factura
this.vnApp.showSucces(this.$t('Invoice trasfered!'));
this.$state.go('invoiceOut.index', {id: newInvoice});
});
} }
} }

View File

@ -1,5 +1,5 @@
module.exports = Self => { module.exports = Self => {
Self.clone = async(sales, refundAgencyMode, refoundZoneId, servicesIds, withWarehouse, group, negative, options) => { Self.clone = async(sales, servicesIds, withWarehouse, group, negative, options) => {
const models = Self.app.models; const models = Self.app.models;
const myOptions = {}; const myOptions = {};
let tx; let tx;
@ -13,7 +13,6 @@ module.exports = Self => {
} }
const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))]; const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))];
const [firstTicketId] = ticketsIds;
const now = Date.vnNew(); const now = Date.vnNew();
let updatedTickets = []; let updatedTickets = [];
let newTicket; let newTicket;
@ -22,7 +21,7 @@ module.exports = Self => {
{relation: 'address'}, {relation: 'address'},
{ {
relation: 'sale', relation: 'sale',
inq: sales, where: {saleFk: {inq: sales}},
}, },
] ]
}; };
@ -34,12 +33,10 @@ module.exports = Self => {
clientFk: ticket.clientFk, clientFk: ticket.clientFk,
shipped: now, shipped: now,
addressFk: ticket.address().id, addressFk: ticket.address().id,
agencyModeFk: refundAgencyMode.id,
nickname: ticket.address().nickname, nickname: ticket.address().nickname,
warehouseFk: withWarehouse ? ticket.warehouseFk : null, warehouseFk: withWarehouse ? ticket.warehouseFk : null,
companyFk: ticket.companyFk, companyFk: ticket.companyFk,
landed: now, landed: now,
zoneFk: refoundZoneId
}, myOptions); }, myOptions);
updatedTickets.push(newTicket); updatedTickets.push(newTicket);
} }
@ -95,7 +92,7 @@ module.exports = Self => {
for (const updatedTicket of updatedTickets) for (const updatedTicket of updatedTickets)
await Self.rawSql(query, [updatedTicket.id], myOptions); await Self.rawSql(query, [updatedTicket.id], myOptions);
if (tx) await tx.commit(); if (tx) await tx.commit();
return updatedTickets/* updatedTickets.map(updatedTicket => updatedTicket.id) */; return /* updatedTickets */updatedTickets.map(updatedTicket => updatedTicket.id);
} }
} catch (e) { } catch (e) {
if (tx) await tx.rollback(); if (tx) await tx.rollback();

View File

@ -28,96 +28,6 @@ module.exports = Self => {
} }
}); });
/* Self.refund = async(ctx, salesIds, servicesIds, withWarehouse, options) => {
const models = Self.app.models;
const myOptions = {userId: ctx.req.accessToken.userId};
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, withWarehouse, 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);
}
}
const query = `CALL vn.ticket_recalc(?, NULL)`;
await Self.rawSql(query, [refundTicket.id], myOptions);
if (tx) await tx.commit();
return refundTicket;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
}; */
Self.refund = async(ctx, salesIds, servicesIds, withWarehouse, options) => { Self.refund = async(ctx, salesIds, servicesIds, withWarehouse, options) => {
const models = Self.app.models; const models = Self.app.models;
const myOptions = {userId: ctx.req.accessToken.userId}; const myOptions = {userId: ctx.req.accessToken.userId};