2023-06-22 05:49:35 +00:00
|
|
|
const print = require('vn-print');
|
2023-06-23 20:52:03 +00:00
|
|
|
const path = require('path');
|
2024-03-08 06:48:55 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2023-06-22 05:49:35 +00:00
|
|
|
|
2019-03-28 10:55:23 +00:00
|
|
|
module.exports = Self => {
|
2019-04-25 06:28:08 +00:00
|
|
|
require('../methods/invoiceOut/filter')(Self);
|
2019-03-28 10:55:23 +00:00
|
|
|
require('../methods/invoiceOut/summary')(Self);
|
2021-06-14 09:38:57 +00:00
|
|
|
require('../methods/invoiceOut/getTickets')(Self);
|
2019-03-28 10:55:23 +00:00
|
|
|
require('../methods/invoiceOut/download')(Self);
|
2022-10-24 12:20:09 +00:00
|
|
|
require('../methods/invoiceOut/downloadZip')(Self);
|
2019-04-26 10:05:49 +00:00
|
|
|
require('../methods/invoiceOut/delete')(Self);
|
2019-04-30 09:03:48 +00:00
|
|
|
require('../methods/invoiceOut/book')(Self);
|
2021-03-11 07:58:34 +00:00
|
|
|
require('../methods/invoiceOut/createPdf')(Self);
|
2021-07-27 07:07:04 +00:00
|
|
|
require('../methods/invoiceOut/createManualInvoice')(Self);
|
2022-10-26 08:36:41 +00:00
|
|
|
require('../methods/invoiceOut/clientsToInvoice')(Self);
|
2022-10-20 05:23:57 +00:00
|
|
|
require('../methods/invoiceOut/invoiceClient')(Self);
|
2024-01-17 09:31:07 +00:00
|
|
|
require('../methods/invoiceOut/makePdfList')(Self);
|
2023-06-23 09:42:30 +00:00
|
|
|
require('../methods/invoiceOut/makePdfAndNotify')(Self);
|
2022-07-14 11:40:50 +00:00
|
|
|
require('../methods/invoiceOut/refund')(Self);
|
2022-09-26 11:33:27 +00:00
|
|
|
require('../methods/invoiceOut/invoiceEmail')(Self);
|
|
|
|
require('../methods/invoiceOut/exportationPdf')(Self);
|
2022-10-03 06:28:47 +00:00
|
|
|
require('../methods/invoiceOut/invoiceCsv')(Self);
|
|
|
|
require('../methods/invoiceOut/invoiceCsvEmail')(Self);
|
2023-01-26 12:49:09 +00:00
|
|
|
require('../methods/invoiceOut/invoiceOutPdf')(Self);
|
2023-02-23 08:25:23 +00:00
|
|
|
require('../methods/invoiceOut/getInvoiceDate')(Self);
|
2023-04-21 06:20:38 +00:00
|
|
|
require('../methods/invoiceOut/negativeBases')(Self);
|
|
|
|
require('../methods/invoiceOut/negativeBasesCsv')(Self);
|
2023-09-20 09:06:30 +00:00
|
|
|
require('../methods/invoiceOut/transferInvoice')(Self);
|
2023-06-22 05:49:35 +00:00
|
|
|
|
2023-06-23 20:52:03 +00:00
|
|
|
Self.filePath = async function(id, options) {
|
|
|
|
const fields = ['ref', 'issued'];
|
2023-06-22 10:48:50 +00:00
|
|
|
const invoiceOut = await Self.findById(id, {fields}, options);
|
2023-06-22 05:49:35 +00:00
|
|
|
|
|
|
|
const issued = invoiceOut.issued;
|
|
|
|
const year = issued.getFullYear().toString();
|
|
|
|
const month = (issued.getMonth() + 1).toString();
|
|
|
|
const day = issued.getDate().toString();
|
|
|
|
|
2023-06-23 20:52:03 +00:00
|
|
|
return {
|
|
|
|
path: path.join(year, month, day),
|
|
|
|
name: `${year}${invoiceOut.ref}.pdf`,
|
|
|
|
year
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
Self.makePdf = async function(id, options) {
|
|
|
|
const fields = ['id', 'hasPdf', 'ref'];
|
|
|
|
const invoiceOut = await Self.findById(id, {fields}, options);
|
|
|
|
const invoiceReport = new print.Report('invoice', {
|
|
|
|
reference: invoiceOut.ref
|
|
|
|
});
|
|
|
|
const buffer = await invoiceReport.toPdfStream();
|
|
|
|
|
|
|
|
const pdfFile = await Self.filePath(id, options);
|
2023-06-22 05:49:35 +00:00
|
|
|
|
|
|
|
// Store invoice
|
|
|
|
|
|
|
|
await invoiceOut.updateAttributes({
|
|
|
|
hasPdf: true
|
2023-06-22 10:48:50 +00:00
|
|
|
}, options);
|
2023-06-22 05:49:35 +00:00
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'test') {
|
|
|
|
await print.storage.write(buffer, {
|
|
|
|
type: 'invoice',
|
2023-06-23 20:52:03 +00:00
|
|
|
path: pdfFile.path,
|
|
|
|
fileName: pdfFile.name
|
2023-06-22 05:49:35 +00:00
|
|
|
});
|
|
|
|
}
|
2023-06-22 10:48:50 +00:00
|
|
|
};
|
2024-03-08 06:48:55 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
2019-03-28 10:55:23 +00:00
|
|
|
};
|