Added invoice schedule method
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2022-04-28 14:59:29 +02:00
parent c923526342
commit fea7df269c
7 changed files with 122 additions and 50 deletions

View File

@ -2,7 +2,7 @@ create table `vn`.`invoiceOut_queue`
(
invoiceFk int(10) unsigned not null,
dated datetime default now() null,
printed tinyint(1) default 0 null,
`status` VARCHAR(50) default '' null,
constraint invoiceOut_queue_pk
primary key (invoiceFk),
constraint invoiceOut_queue_invoiceOut_id_fk

View File

@ -1,5 +1,3 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('globalInvoicing', {
description: 'Make a global invoice',

View File

@ -1,45 +0,0 @@
const db = require('vn-print/core/database');
const Email = require('vn-print/core/email');
module.exports = async function(request, response, next) {
try {
const reqArgs = request.body;
if (!reqArgs.clientIds)
throw new Error('The argument clientIds is required');
if (!reqArgs.from)
throw new Error('The argument from is required');
if (!reqArgs.to)
throw new Error('The argument to is required');
response.status(200).json({
message: 'Success'
});
const invoices = await db.rawSql(`
SELECT
* FROM invoiceOut_queue
WHERE status = 'PENDING'`);
// const clientData = new Map();
// for (const client of clients)
// clientData.set(client.id, client);
// for (const clientId of reqArgs.clientIds) {
// const client = clientData.get(clientId);
// if (client) {
// const args = Object.assign({
// recipientId: clientId,
// recipient: client.email,
// replyTo: client.salesPersonEmail
// }, response.locals);
// const email = new Email('campaign-metrics', args);
// await email.send();
// }
// }
} catch (error) {
next(error);
}
};

View File

@ -16,7 +16,7 @@ module.exports = [
cb: require('./closure')
},
{
url: '/api/notify',
cb: require('./notify')
url: '/api/schedule',
cb: require('./schedule')
}
];

View File

@ -2,5 +2,6 @@ const express = require('express');
const router = new express.Router();
router.post('/consumption', require('./consumption'));
router.post('/invoice', require('./invoice'));
module.exports = router;

View File

@ -0,0 +1,118 @@
const db = require('vn-print/core/database');
const Email = require('vn-print/core/email');
const Report = require('vn-print/core/report');
const storage = require('vn-print/core/storage');
module.exports = async function(request, response, next) {
try {
const reqArgs = request.body;
response.status(200).json({
message: 'Success'
});
const invoices = await db.rawSql(`
SELECT
io.id,
io.clientFk,
io.issued,
io.ref,
c.email recipient,
c.salesPersonFk,
c.isToBeMailed,
c.hasToInvoice,
co.hasDailyInvoice,
eu.email salesPersonEmail
FROM invoiceOut_queue ioq
JOIN invoiceOut io ON io.id = ioq.invoiceFk
JOIN client c ON c.id = io.clientFk
JOIN province p ON p.id = c.provinceFk
JOIN country co ON co.id = p.countryFk
LEFT JOIN account.emailUser eu ON eu.userFk = c.salesPersonFk
WHERE status = ''`);
for (const invoiceOut of invoices) {
try {
const args = Object.assign({
invoiceId: invoiceOut.id,
recipientId: invoiceOut.clientFk,
recipient: invoiceOut.recipient,
replyTo: invoiceOut.salesPersonEmail
}, reqArgs);
const invoiceReport = new Report('invoice', args);
const stream = await invoiceReport.toPdfStream();
const issued = invoiceOut.issued;
const year = issued.getFullYear().toString();
const month = (issued.getMonth() + 1).toString();
const day = issued.getDate().toString();
const fileName = `${year}${invoiceOut.ref}.pdf`;
// Store invoice
storage.write(stream, {
type: 'invoice',
path: `${year}/${month}/${day}`,
fileName: fileName
});
await db.rawSql('UPDATE invoiceOut SET hasPdf = true WHERE id = ?', [invoiceOut.id]);
const isToBeMailed = invoiceOut.recipient && invoiceOut.salesPersonFk && invoiceOut.isToBeMailed;
if (isToBeMailed) {
const mailOptions = {
overrideAttachments: true,
attachments: []
};
const invoiceAttachment = {
filename: fileName,
content: stream
};
if (invoiceOut.serial == 'E' && invoiceOut.companyCode == 'VNL') {
const exportation = new Report('exportation', args);
const stream = await exportation.toPdfStream();
const fileName = `CITES-${invoiceOut.ref}.pdf`;
mailOptions.attachments.push({
filename: fileName,
content: stream
});
}
mailOptions.attachments.push(invoiceAttachment);
const email = new Email('invoice', args);
await email.send(mailOptions);
}
} catch (error) {
console.log(error);
}
}
// const clientData = new Map();
// for (const client of clients)
// clientData.set(client.id, client);
// for (const clientId of reqArgs.clientIds) {
// const client = clientData.get(clientId);
// if (client) {
// const args = Object.assign({
// recipientId: clientId,
// recipient: client.email,
// replyTo: client.salesPersonEmail
// }, response.locals);
// const email = new Email('campaign-metrics', args);
// await email.send();
// }
// }
} catch (error) {
next(error);
}
};