7525-devToTest #2542

Merged
alexm merged 231 commits from 7525-devToTest into test 2024-06-04 07:59:34 +00:00
7 changed files with 82 additions and 49 deletions
Showing only changes of commit 5c2e8c135b - Show all commits

View File

@ -18,7 +18,8 @@ BEGIN
WHERE (al.code = 'PACKED' OR (am.code = 'refund' AND al.code != 'delivered'))
AND t.id = vTicketFk
AND t.refFk IS NULL
GROUP BY t.id);
GROUP BY t.id
);
CALL ticket_close();

View File

@ -324,7 +324,6 @@
"The response is not a PDF": "La respuesta no es un PDF",
"Booking completed": "Reserva completada",
"The ticket is in preparation": "El ticket [{{ticketId}}]({{{ticketUrl}}}) del comercial {{salesPersonId}} está en preparación",
"Incoterms data for consignee is missing": "Faltan los datos de los Incoterms para el consignatario",
"The notification subscription of this worker cant be modified": "La subscripción a la notificación de este trabajador no puede ser modificada",
"User disabled": "Usuario desactivado",
"The amount cannot be less than the minimum": "La cantidad no puede ser menor que la cantidad mínima",

View File

@ -1,5 +1,6 @@
const print = require('vn-print');
const path = require('path');
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
require('../methods/invoiceOut/filter')(Self);
@ -66,4 +67,24 @@ module.exports = Self => {
});
}
};
Self.getSerial = async function(clientId, companyId, addressId, type, myOptions) {
const [{serial}] = await Self.rawSql(
`SELECT vn.invoiceSerial(?, ?, ?) AS serial`,
[
clientId,
companyId,
type
],
myOptions);
const invoiceOutSerial = await Self.app.models.InvoiceOutSerial.findById(serial);
if (invoiceOutSerial?.taxAreaFk == 'WORLD') {
const address = await Self.app.models.Address.findById(addressId);
if (!address || !address.customsAgentFk || !address.incotermsFk)
throw new UserError('The address of the customer must have information about Incoterms and Customs Agent');
}
return serial;
};
};

View File

@ -35,6 +35,7 @@ module.exports = Self => {
SELECT t.id,
t.clientFk,
t.companyFk,
c.id clientFk,
c.name clientName,
c.email recipient,
c.salesPersonFk,

View File

@ -1,3 +1,5 @@
/* eslint max-len: ["error", { "code": 150 }]*/
const Report = require('vn-print/core/report');
const Email = require('vn-print/core/email');
const smtp = require('vn-print/core/smtp');
@ -11,19 +13,27 @@ module.exports = async function(ctx, Self, tickets, reqArgs = {}) {
const failedtickets = [];
for (const ticket of tickets) {
try {
await Self.rawSql(`CALL vn.ticket_closeByTicket(?)`, [ticket.id], {userId});
await Self.app.models.InvoiceOut.getSerial(ticket.clientFk, ticket.companyFk, ticket.addressFk, 'M');
await Self.rawSql(
`CALL vn.ticket_closeByTicket(?)`,
[ticket.id],
{userId}
);
const [invoiceOut] = await Self.rawSql(`
const [invoiceOut] = await Self.rawSql(
`
SELECT io.id, io.ref, io.serial, cny.code companyCode, io.issued
FROM ticket t
JOIN invoiceOut io ON io.ref = t.refFk
JOIN company cny ON cny.id = io.companyFk
WHERE t.id = ?
`, [ticket.id]);
`,
[ticket.id],
);
const mailOptions = {
overrideAttachments: true,
attachments: []
attachments: [],
};
const isToBeMailed = ticket.recipient && ticket.salesPersonFk && ticket.isToBeMailed;
@ -33,7 +43,7 @@ module.exports = async function(ctx, Self, tickets, reqArgs = {}) {
reference: invoiceOut.ref,
recipientId: ticket.clientFk,
recipient: ticket.recipient,
replyTo: ticket.salesPersonEmail
replyTo: ticket.salesPersonEmail,
};
const invoiceReport = new Report('invoice', args);
@ -50,15 +60,19 @@ module.exports = async function(ctx, Self, tickets, reqArgs = {}) {
await storage.write(stream, {
type: 'invoice',
path: `${year}/${month}/${day}`,
fileName: fileName
fileName: fileName,
});
await Self.rawSql('UPDATE invoiceOut SET hasPdf = true WHERE id = ?', [invoiceOut.id], {userId});
await Self.rawSql(
'UPDATE invoiceOut SET hasPdf = true WHERE id = ?',
[invoiceOut.id],
{userId},
);
if (isToBeMailed) {
const invoiceAttachment = {
filename: fileName,
content: stream
content: stream,
};
if (invoiceOut.serial == 'E' && invoiceOut.companyCode == 'VNL') {
@ -68,7 +82,7 @@ module.exports = async function(ctx, Self, tickets, reqArgs = {}) {
mailOptions.attachments.push({
filename: fileName,
content: stream
content: stream,
});
}
@ -82,7 +96,7 @@ module.exports = async function(ctx, Self, tickets, reqArgs = {}) {
id: ticket.id,
recipientId: ticket.clientFk,
recipient: ticket.recipient,
replyTo: ticket.salesPersonEmail
replyTo: ticket.salesPersonEmail,
};
const email = new Email('delivery-note-link', args);
@ -90,14 +104,17 @@ module.exports = async function(ctx, Self, tickets, reqArgs = {}) {
}
// Incoterms authorization
const [{firstOrder}] = await Self.rawSql(`
const [{firstOrder}] = await Self.rawSql(
`
SELECT COUNT(*) as firstOrder
FROM ticket t
JOIN client c ON c.id = t.clientFk
WHERE t.clientFk = ?
AND NOT t.isDeleted
AND c.isVies
`, [ticket.clientFk]);
`,
[ticket.clientFk],
);
if (firstOrder == 1) {
const args = {
@ -106,7 +123,7 @@ module.exports = async function(ctx, Self, tickets, reqArgs = {}) {
recipientId: ticket.clientFk,
recipient: ticket.recipient,
replyTo: ticket.salesPersonEmail,
addressId: ticket.addressFk
addressId: ticket.addressFk,
};
const email = new Email('incoterms-authorization', args);
@ -116,21 +133,25 @@ module.exports = async function(ctx, Self, tickets, reqArgs = {}) {
`SELECT id
FROM sample
WHERE code = 'incoterms-authorization'
`);
`,
);
await Self.rawSql(`
await Self.rawSql(
`
INSERT INTO clientSample (clientFk, typeFk, companyFk) VALUES(?, ?, ?)
`, [ticket.clientFk, sample.id, ticket.companyFk], {userId});
`,
[ticket.clientFk, sample.id, ticket.companyFk],
{userId},
);
}
} catch (error) {
// Domain not found
if (error.responseCode == 450)
return invalidEmail(ticket);
if (error.responseCode == 450) return invalidEmail(ticket);
// Save tickets on a list of failed ids
failedtickets.push({
id: ticket.id,
stacktrace: error
stacktrace: error,
});
}
}
@ -147,24 +168,26 @@ module.exports = async function(ctx, Self, tickets, reqArgs = {}) {
smtp.send({
to: config.app.reportEmail,
subject: '[API] Nightly ticket closure report',
html: body
html: body,
});
}
async function invalidEmail(ticket) {
await Self.rawSql(`UPDATE client SET email = NULL WHERE id = ?`, [
ticket.clientFk
], {userId});
await Self.rawSql(
`UPDATE client SET email = NULL WHERE id = ?`,
[ticket.clientFk],
{userId},
);
const oldInstance = `{"email": "${ticket.recipient}"}`;
const newInstance = `{"email": ""}`;
await Self.rawSql(`
await Self.rawSql(
`
INSERT INTO clientLog (originFk, userFk, action, changedModel, oldInstance, newInstance)
VALUES (?, NULL, 'UPDATE', 'Client', ?, ?)`, [
ticket.clientFk,
oldInstance,
newInstance
], {userId});
VALUES (?, NULL, 'UPDATE', 'Client', ?, ?)`,
[ticket.clientFk, oldInstance, newInstance],
{userId},
);
const body = `No se ha podido enviar el albarán <strong>${ticket.id}</strong>
al cliente <strong>${ticket.clientFk} - ${ticket.clientName}</strong>
@ -176,7 +199,7 @@ module.exports = async function(ctx, Self, tickets, reqArgs = {}) {
smtp.send({
to: ticket.salesPersonEmail,
subject: 'No se ha podido enviar el albarán',
html: body
html: body,
});
}
};

View File

@ -77,22 +77,10 @@ module.exports = function(Self) {
if (!clientCanBeInvoiced)
throw new UserError(`This client can't be invoiced`);
const [{serial}] = invoiceCorrection ? [{serial: 'R'}] : await Self.rawSql(
`SELECT vn.invoiceSerial(?, ?, ?) AS serial`,
[
clientId,
companyFk,
invoiceType
],
myOptions);
const serial = !invoiceCorrection
? await models.InvoiceOut.getSerial(clientId, companyFk, firstTicket.addressFk, invoiceType, myOptions)
: 'R';
const invoiceOutSerial = await models.InvoiceOutSerial.findById(serial);
if (invoiceOutSerial?.taxAreaFk == 'WORLD') {
const address = await models.Address.findById(firstTicket.addressFk);
if (!address || !address.customsAgentFk || !address.incotermsFk)
throw new UserError('Incoterms data for consignee is missing');
}
await Self.rawSql('CALL invoiceOut_new(?, ?, null, @invoiceId)', [serial, invoiceDate], myOptions);
const [resultInvoice] = await Self.rawSql('SELECT @invoiceId id', [], myOptions);

View File

@ -77,6 +77,6 @@ describe('ticket makeInvoice()', () => {
await tx.rollback();
}
expect(error.message).toEqual(`Incoterms data for consignee is missing`);
expect(error.message).toEqual(`The address of the customer must have information about Incoterms and Customs Agent`);
});
});