salix/modules/invoiceOut/back/models/invoice-out.js

92 lines
3.5 KiB
JavaScript
Raw Normal View History

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-14 07:36:19 +00:00
const UserError = require('vn-loopback/util/user-error');
2024-05-21 13:11:32 +00:00
const isProduction = require('vn-loopback/server/boot/isProduction');
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);
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);
require('../methods/invoiceOut/delete')(Self);
require('../methods/invoiceOut/book')(Self);
2021-03-11 07:58:34 +00:00
require('../methods/invoiceOut/createPdf')(Self);
require('../methods/invoiceOut/createManualInvoice')(Self);
require('../methods/invoiceOut/clientsToInvoice')(Self);
2022-10-20 05:23:57 +00:00
require('../methods/invoiceOut/invoiceClient')(Self);
require('../methods/invoiceOut/makePdfList')(Self);
require('../methods/invoiceOut/makePdfAndNotify')(Self);
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);
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'];
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
}, options);
2023-06-22 05:49:35 +00:00
2024-05-21 13:11:32 +00:00
if (isProduction()) {
2023-06-22 05:49:35 +00:00
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
});
}
};
2024-03-14 07:36:19 +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
};