2022-01-18 12:15:42 +00:00
|
|
|
const path = require('path');
|
|
|
|
const db = require('vn-print/core/database');
|
|
|
|
|
|
|
|
const {toCSV} = require('../csv');
|
|
|
|
const sqlPath = path.join(__dirname, 'sql');
|
|
|
|
|
|
|
|
module.exports = async function(request, response, next) {
|
|
|
|
try {
|
|
|
|
const reqArgs = request.query;
|
2022-07-21 08:05:37 +00:00
|
|
|
if (!reqArgs.refFk)
|
|
|
|
throw new Error('The argument refFk is required');
|
2022-01-18 12:15:42 +00:00
|
|
|
|
2022-07-21 08:05:37 +00:00
|
|
|
const refFk = reqArgs.refFk;
|
|
|
|
const sales = await db.rawSqlFromDef(`${sqlPath}/sales`, [refFk]);
|
2022-01-18 12:15:42 +00:00
|
|
|
const content = toCSV(sales);
|
2022-07-21 08:05:37 +00:00
|
|
|
const fileName = `invoice_${refFk}.csv`;
|
2022-01-18 12:15:42 +00:00
|
|
|
|
|
|
|
response.setHeader('Content-type', 'text/csv');
|
|
|
|
response.setHeader('Content-Disposition', `inline; filename="${fileName}"`);
|
|
|
|
response.end(content);
|
|
|
|
} catch (error) {
|
|
|
|
next(error);
|
|
|
|
}
|
|
|
|
};
|