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 => {
Self.remoteMethodCtx('transferInvoiceOut', {
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) => {
const models = Self.app.models;
const myOptions = {userId: ctx.req.accessToken.userId};
const myOptions = {};
let tx;
if (typeof options == 'object')
@ -65,32 +63,25 @@ module.exports = Self => {
const ticketsIds = tickets.map(ticket => ticket.id);
await models.Ticket.refund(ctx, ticketsIds, null, myOptions);
// 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 servicesIds = services.map(service => service.id);
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);
const clonedTickets = await models.Sale.clone(sales, servicesIds, null, false, false, myOptions);
console.log('cloned tickets', clonedTickets);
// Update client
for (const clonedTicket of clonedTickets)
await clonedTicket.updateAttributes({clientFk: newClientFk}, myOptions);
for (const clonedTicket of clonedTickets) {
const ticket = await models.Ticket.findById(clonedTicket);
console.log(ticket);
await ticket.updateAttributes({clientFk: newClientFk}, myOptions);
// await clonedTicket.updateAttributes({clientFk: newClientFk}, myOptions);
}
// Quick invoice
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
for (const invoiceId of invoiceIds) {
await models.InvoiceCorrection.create({
correctingFk: invoiceId,
correctedFk: id,
@ -98,9 +89,9 @@ module.exports = Self => {
cplusInvoiceType477Fk: cplusInvoiceType477Id,
invoiceCorrectionType: invoiceCorrectionTypeId
});
}
if (tx) await tx.commit();
return true;
return invoiceId;
} catch (e) {
if (tx) await tx.rollback();
throw e;

View File

@ -135,7 +135,11 @@ class Controller extends Section {
cplusInvoiceType477Id: this.cplusInvoiceType477,
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 => {
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 myOptions = {};
let tx;
@ -13,7 +13,6 @@ module.exports = Self => {
}
const ticketsIds = [...new Set(sales.map(sale => sale.ticketFk))];
const [firstTicketId] = ticketsIds;
const now = Date.vnNew();
let updatedTickets = [];
let newTicket;
@ -22,7 +21,7 @@ module.exports = Self => {
{relation: 'address'},
{
relation: 'sale',
inq: sales,
where: {saleFk: {inq: sales}},
},
]
};
@ -34,12 +33,10 @@ module.exports = Self => {
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);
}
@ -95,7 +92,7 @@ module.exports = Self => {
for (const updatedTicket of updatedTickets)
await Self.rawSql(query, [updatedTicket.id], myOptions);
if (tx) await tx.commit();
return updatedTickets/* updatedTickets.map(updatedTicket => updatedTicket.id) */;
return /* updatedTickets */updatedTickets.map(updatedTicket => updatedTicket.id);
}
} catch (e) {
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) => {
const models = Self.app.models;
const myOptions = {userId: ctx.req.accessToken.userId};