2734 - Recreate invoice
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2021-03-11 08:58:34 +01:00
parent 1e36b460a1
commit 1719f53111
9 changed files with 104 additions and 14 deletions

View File

@ -68,5 +68,16 @@
"image/jpeg",
"image/jpg"
]
},
"invoiceStorage": {
"name": "invoiceStorage",
"connector": "loopback-component-storage",
"provider": "filesystem",
"root": "./storage/pdfs/invoice",
"maxFileSize": "52428800",
"allowedContentTypes": [
"application/octet-stream",
"application/pdf"
]
}
}

View File

@ -0,0 +1,74 @@
const fs = require('fs-extra');
const got = require('got');
const path = require('path');
module.exports = Self => {
Self.remoteMethodCtx('createPdf', {
description: 'Creates an invoice PDF',
accessType: 'READ',
accepts: [
{
arg: 'id',
type: 'String',
description: 'The invoice id',
http: {source: 'path'}
}
],
returns: [
{
arg: 'body',
type: 'file',
root: true
}, {
arg: 'Content-Type',
type: 'String',
http: {target: 'header'}
}, {
arg: 'Content-Disposition',
type: 'String',
http: {target: 'header'}
}
],
http: {
path: `/:id/createPdf`,
verb: 'GET'
}
});
Self.createPdf = async function(ctx, id, options = {}) {
const models = Self.app.models;
const headers = ctx.req.headers;
const origin = headers.origin;
const authorization = headers.authorization;
if (process.env.NODE_ENV == 'test')
throw new UserError(`Action not allowed on the test environment`);
const invoiceOut = await Self.findById(id);
await invoiceOut.updateAttributes({
hasPdf: true
});
const response = got.stream(`${origin}/api/report/invoice`, {
query: {
authorization: authorization,
invoiceId: id
}
});
const invoiceYear = invoiceOut.created.getFullYear().toString();
const container = await models.InvoiceContainer.container(invoiceYear);
const rootPath = container.client.root;
const fileName = `${invoiceOut.ref}.pdf`;
const fileSrc = path.join(rootPath, invoiceYear, fileName);
const writeStream = fs.createWriteStream(fileSrc);
writeStream.on('open', () => {
response.pipe(writeStream);
});
writeStream.on('finish', async function() {
writeStream.end();
});
};
};

View File

@ -20,10 +20,7 @@ module.exports = Self => {
});
Self.regenerate = async(ctx, id) => {
const userId = ctx.req.accessToken.userId;
const models = Self.app.models;
const invoiceReportFk = 30; // Should be deprecated
const worker = await models.Worker.findOne({where: {userFk: userId}});
const tx = await Self.beginTransaction({});
try {
@ -35,10 +32,8 @@ module.exports = Self => {
hasPdf: false
});
// Send to print queue
await Self.rawSql(`
INSERT INTO vn.printServerQueue (reportFk, param1, workerFk)
VALUES (?, ?, ?)`, [invoiceReportFk, id, worker.id], options);
// Create invoice PDF
await models.InvoiceOut.createPdf(ctx, id);
await tx.commit();

View File

@ -1,5 +1,8 @@
{
"InvoiceOut": {
"dataSource": "vn"
},
"InvoiceContainer": {
"dataSource": "invoiceStorage"
}
}

View File

@ -0,0 +1,10 @@
{
"name": "InvoiceContainer",
"base": "Container",
"acls": [{
"accessType": "READ",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW"
}]
}

View File

@ -5,4 +5,5 @@ module.exports = Self => {
require('../methods/invoiceOut/regenerate')(Self);
require('../methods/invoiceOut/delete')(Self);
require('../methods/invoiceOut/book')(Self);
require('../methods/invoiceOut/createPdf')(Self);
};

View File

@ -67,11 +67,7 @@ module.exports = function(Self) {
if (serial != 'R' && invoiceId) {
await Self.rawSql('CALL invoiceOutBooking(?)', [invoiceId], options);
await models.PrintServerQueue.create({
reportFk: 3, // Tarea #2734 (Nueva): crear informe facturas
param1: invoiceId,
workerFk: userId
}, options);
await models.InvoiceOut.createPdf(ctx, invoiceId);
}
await tx.commit();

View File

@ -21,7 +21,7 @@
"loopback": "^3.26.0",
"loopback-boot": "^2.27.1",
"loopback-component-explorer": "^6.5.0",
"loopback-component-storage": "^3.6.1",
"loopback-component-storage": "3.6.1",
"loopback-connector-mysql": "^5.4.3",
"loopback-connector-remote": "^3.4.1",
"loopback-context": "^3.4.0",

View File

@ -67,7 +67,7 @@ const dbHelper = {
*/
findValueFromDef(queryName, params) {
return this.findOneFromDef(queryName, params).then(row => {
return Object.values(row)[0];
if (row) return Object.values(row)[0];
});
},