fix(transferInvoice): separate invoice functionality from generating PDF and adapt tests #2528

Merged
alexm merged 4 commits from 6147-transferInvoice_split into master 2024-06-07 08:17:24 +00:00
8 changed files with 81 additions and 60 deletions
Showing only changes of commit bd82bb011a - Show all commits

View File

@ -227,5 +227,6 @@
"They're not your subordinate": "They're not your subordinate", "They're not your subordinate": "They're not your subordinate",
"InvoiceIn is already booked": "InvoiceIn is already booked", "InvoiceIn is already booked": "InvoiceIn is already booked",
"This workCenter is already assigned to this agency": "This workCenter is already assigned to this agency", "This workCenter is already assigned to this agency": "This workCenter is already assigned to this agency",
"You can only have one PDA": "You can only have one PDA" "You can only have one PDA": "You can only have one PDA",
} "It has been invoiced but the PDF could not be generated": "It has been invoiced but the PDF could not be generated"
}

View File

@ -359,5 +359,6 @@
"It was not able to create the invoice": "No se pudo crear la factura", "It was not able to create the invoice": "No se pudo crear la factura",
"ticketCommercial": "El ticket {{ ticket }} para el vendedor {{ salesMan }} está en preparación. (mensaje generado automáticamente)", "ticketCommercial": "El ticket {{ ticket }} para el vendedor {{ salesMan }} está en preparación. (mensaje generado automáticamente)",
"This PDA is already assigned to another user": "Este PDA ya está asignado a otro usuario", "This PDA is already assigned to another user": "Este PDA ya está asignado a otro usuario",
"You can only have one PDA": "Solo puedes tener un PDA" "You can only have one PDA": "Solo puedes tener un PDA",
} "It has been invoiced but the PDF could not be generated": "Se ha facturado pero no se ha podido generar el PDF"
}

View File

@ -356,5 +356,6 @@
"InvoiceIn is already booked": "La facture reçue est déjà comptabilisée", "InvoiceIn is already booked": "La facture reçue est déjà comptabilisée",
"This workCenter is already assigned to this agency": "Ce centre de travail est déjà assigné à cette agence", "This workCenter is already assigned to this agency": "Ce centre de travail est déjà assigné à cette agence",
"Select ticket or client": "Choisissez un ticket ou un client", "Select ticket or client": "Choisissez un ticket ou un client",
"It was not able to create the invoice": "Il n'a pas été possible de créer la facture" "It was not able to create the invoice": "Il n'a pas été possible de créer la facture",
} "It has been invoiced but the PDF could not be generated": "La facture a été émise mais le PDF n'a pas pu être généré"
}

View File

@ -356,5 +356,6 @@
"InvoiceIn is already booked": "InvoiceIn já está reservado", "InvoiceIn is already booked": "InvoiceIn já está reservado",
"This workCenter is already assigned to this agency": "Este centro de trabalho já está atribuído a esta agência", "This workCenter is already assigned to this agency": "Este centro de trabalho já está atribuído a esta agência",
"Select ticket or client": "Selecione um ticket ou cliente", "Select ticket or client": "Selecione um ticket ou cliente",
"It was not able to create the invoice": "Não foi possível criar a fatura" "It was not able to create the invoice": "Não foi possível criar a fatura",
} "It has been invoiced but the PDF could not be generated": "Foi faturado, mas o PDF não pôde ser gerado"
}

View File

@ -1,4 +1,4 @@
/* eslint max-len: ["error", { "code": 150 }]*/
const models = require('vn-loopback/server/server').models; const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context'); const LoopBackContext = require('loopback-context');
@ -17,27 +17,19 @@ describe('InvoiceOut transferInvoice()', () => {
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx active: activeCtx
}); });
spyOn(models.InvoiceOut, 'makePdfAndNotify');
}); });
it('should return the id of the created issued invoice', async() => { it('should return the id of the created issued invoice', async() => {
const tx = await models.InvoiceOut.beginTransaction({}); const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx}; const options = {transaction: tx};
const args = { const id = 4;
id: '4', const newClient = 1;
refFk: 'T4444444', spyOn(models.InvoiceOut, 'makePdfList');
newClientFk: 1,
cplusRectificationTypeFk: 1,
siiTypeInvoiceOutFk: 1,
invoiceCorrectionTypeFk: 1
};
ctx.args = args;
try { try {
const {clientFk: oldClient} = await models.InvoiceOut.findById(args.id, {fields: ['clientFk']}); const {clientFk: oldClient} = await models.InvoiceOut.findById(id, {fields: ['clientFk']});
const invoicesBefore = await models.InvoiceOut.find({}, options); const invoicesBefore = await models.InvoiceOut.find({}, options);
const result = await models.InvoiceOut.transferInvoice( const result = await models.InvoiceOut.transferInvoice(ctx, id, 'T4444444', newClient, 1, 1, 1, true, options);
ctx,
options);
const invoicesAfter = await models.InvoiceOut.find({}, options); const invoicesAfter = await models.InvoiceOut.find({}, options);
const rectificativeInvoice = invoicesAfter[invoicesAfter.length - 2]; const rectificativeInvoice = invoicesAfter[invoicesAfter.length - 2];
const newInvoice = invoicesAfter[invoicesAfter.length - 1]; const newInvoice = invoicesAfter[invoicesAfter.length - 1];
@ -45,7 +37,7 @@ describe('InvoiceOut transferInvoice()', () => {
expect(result).toBeDefined(); expect(result).toBeDefined();
expect(invoicesAfter.length - invoicesBefore.length).toEqual(2); expect(invoicesAfter.length - invoicesBefore.length).toEqual(2);
expect(rectificativeInvoice.clientFk).toEqual(oldClient); expect(rectificativeInvoice.clientFk).toEqual(oldClient);
expect(newInvoice.clientFk).toEqual(args.newClientFk); expect(newInvoice.clientFk).toEqual(newClient);
await tx.rollback(); await tx.rollback();
} catch (e) { } catch (e) {
@ -54,22 +46,13 @@ describe('InvoiceOut transferInvoice()', () => {
} }
}); });
it('should throw an UserError when it is the same client', async() => { it('should throw an error when it is the same client', async() => {
const tx = await models.InvoiceOut.beginTransaction({}); const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx}; const options = {transaction: tx};
const args = { spyOn(models.InvoiceOut, 'makePdfList');
id: '1',
refFk: 'T1111111',
newClientFk: 1101,
cplusRectificationTypeFk: 1,
siiTypeInvoiceOutFk: 1,
invoiceCorrectionTypeFk: 1
};
ctx.args = args;
try { try {
await models.InvoiceOut.transferInvoice( await models.InvoiceOut.transferInvoice(ctx, '1', 'T1111111', 1101, 1, 1, 1, true, options);
ctx,
options);
await tx.rollback(); await tx.rollback();
} catch (e) { } catch (e) {
expect(e.message).toBe(`Select a different client`); expect(e.message).toBe(`Select a different client`);
@ -77,26 +60,49 @@ describe('InvoiceOut transferInvoice()', () => {
} }
}); });
it('should throw an UserError when it is refund', async() => { it('should throw an error when it is refund', async() => {
const tx = await models.InvoiceOut.beginTransaction({}); const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx}; const options = {transaction: tx};
const args = { spyOn(models.InvoiceOut, 'makePdfList');
id: '1',
refFk: 'T1111111',
newClientFk: 1102,
cplusRectificationTypeFk: 1,
siiTypeInvoiceOutFk: 1,
invoiceCorrectionTypeFk: 1
};
ctx.args = args;
try { try {
await models.InvoiceOut.transferInvoice( await models.InvoiceOut.transferInvoice(ctx, '1', 'T1111111', 1102, 1, 1, 1, true, options);
ctx,
options);
await tx.rollback(); await tx.rollback();
} catch (e) { } catch (e) {
expect(e.message).toContain(`This ticket is already a refund`); expect(e.message).toContain(`This ticket is already a refund`);
await tx.rollback(); await tx.rollback();
} }
}); });
it('should throw an error when pdf failed', async() => {
const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx};
spyOn(models.InvoiceOut, 'makePdfList').and.returnValue(() => {
throw new Error('test');
});
try {
await models.InvoiceOut.transferInvoice(ctx, '1', 'T1111111', 1102, 1, 1, 1, true, options);
await tx.rollback();
} catch (e) {
expect(e.message).toContain(`It has been invoiced but the PDF could not be generated`);
await tx.rollback();
}
});
it('should not generate an invoice', async() => {
const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx};
spyOn(models.InvoiceOut, 'makePdfList');
let response;
try {
response = await models.InvoiceOut.transferInvoice(ctx, '1', 'T1111111', 1102, 1, 1, 1, false, options);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
expect(response).not.toBeDefined();
});
}); });

View File

@ -1,3 +1,4 @@
/* eslint max-len: ["error", { "code": 160 }]*/
const UserError = require('vn-loopback/util/user-error'); const UserError = require('vn-loopback/util/user-error');
module.exports = Self => { module.exports = Self => {
@ -37,13 +38,13 @@ module.exports = Self => {
required: true required: true
}, },
{ {
arg: 'checked', arg: 'makeInvoice',
type: 'boolean', type: 'boolean',
required: true required: true
}, },
], ],
returns: { returns: {
type: 'boolean', type: 'object',
root: true root: true
}, },
http: { http: {
@ -52,11 +53,11 @@ module.exports = Self => {
} }
}); });
Self.transferInvoice = async(ctx, options) => { Self.transferInvoice = async(ctx, id, refFk, newClientFk, cplusRectificationTypeFk, siiTypeInvoiceOutFk, invoiceCorrectionTypeFk, makeInvoice, 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};
const {id, refFk, newClientFk, cplusRectificationTypeFk, siiTypeInvoiceOutFk, invoiceCorrectionTypeFk} = ctx.args; let invoiceId;
const checked = ctx.args.checked;
let tx; let tx;
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
@ -102,13 +103,22 @@ module.exports = Self => {
await models.Ticket.invoiceTickets(ctx, refundTicketIds, invoiceCorrection, myOptions); await models.Ticket.invoiceTickets(ctx, refundTicketIds, invoiceCorrection, myOptions);
if (!checked) { if (makeInvoice)
const [invoiceId] = await models.Ticket.invoiceTicketsAndPdf(ctx, clonedTicketIds, null, myOptions); invoiceId = await models.Ticket.invoiceTickets(ctx, clonedTicketIds, null, myOptions);
return invoiceId;
} tx && await tx.commit();
} catch (e) { } catch (e) {
if (tx) await tx.rollback(); if (tx) await tx.rollback();
throw e; throw e;
} }
if (tx && makeInvoice) {
try {
await models.InvoiceOut.makePdfList(ctx, invoiceId, null);
} catch (e) {
throw new UserError('It has been invoiced but the PDF could not be generated');
}
}
return invoiceId;
}; };
}; };

View File

@ -158,7 +158,7 @@ class Controller extends Section {
cplusRectificationTypeFk: this.cplusRectificationType, cplusRectificationTypeFk: this.cplusRectificationType,
siiTypeInvoiceOutFk: this.siiTypeInvoiceOut, siiTypeInvoiceOutFk: this.siiTypeInvoiceOut,
invoiceCorrectionTypeFk: this.invoiceCorrectionType, invoiceCorrectionTypeFk: this.invoiceCorrectionType,
checked: this.checked makeInvoice: this.checked
}; };
this.$http.get(`Clients/${this.clientId}`).then(response => { this.$http.get(`Clients/${this.clientId}`).then(response => {

View File

@ -135,6 +135,7 @@ module.exports = Self => {
const now = Date.vnNew(); const now = Date.vnNew();
const ticket = await models.Ticket.findById(ticketId, null, myOptions); const ticket = await models.Ticket.findById(ticketId, null, myOptions);
if (!ctx.args) ctx.args = {};
ctx.args.clientId = ticket.clientFk; ctx.args.clientId = ticket.clientFk;
ctx.args.shipped = now; ctx.args.shipped = now;
ctx.args.landed = now; ctx.args.landed = now;