2734 - Recreate invoice
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
This commit is contained in:
parent
1e36b460a1
commit
1719f53111
|
@ -68,5 +68,16 @@
|
||||||
"image/jpeg",
|
"image/jpeg",
|
||||||
"image/jpg"
|
"image/jpg"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
"invoiceStorage": {
|
||||||
|
"name": "invoiceStorage",
|
||||||
|
"connector": "loopback-component-storage",
|
||||||
|
"provider": "filesystem",
|
||||||
|
"root": "./storage/pdfs/invoice",
|
||||||
|
"maxFileSize": "52428800",
|
||||||
|
"allowedContentTypes": [
|
||||||
|
"application/octet-stream",
|
||||||
|
"application/pdf"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
};
|
|
@ -20,10 +20,7 @@ module.exports = Self => {
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.regenerate = async(ctx, id) => {
|
Self.regenerate = async(ctx, id) => {
|
||||||
const userId = ctx.req.accessToken.userId;
|
|
||||||
const models = Self.app.models;
|
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({});
|
const tx = await Self.beginTransaction({});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -35,10 +32,8 @@ module.exports = Self => {
|
||||||
hasPdf: false
|
hasPdf: false
|
||||||
});
|
});
|
||||||
|
|
||||||
// Send to print queue
|
// Create invoice PDF
|
||||||
await Self.rawSql(`
|
await models.InvoiceOut.createPdf(ctx, id);
|
||||||
INSERT INTO vn.printServerQueue (reportFk, param1, workerFk)
|
|
||||||
VALUES (?, ?, ?)`, [invoiceReportFk, id, worker.id], options);
|
|
||||||
|
|
||||||
await tx.commit();
|
await tx.commit();
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
{
|
{
|
||||||
"InvoiceOut": {
|
"InvoiceOut": {
|
||||||
"dataSource": "vn"
|
"dataSource": "vn"
|
||||||
|
},
|
||||||
|
"InvoiceContainer": {
|
||||||
|
"dataSource": "invoiceStorage"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
{
|
||||||
|
"name": "InvoiceContainer",
|
||||||
|
"base": "Container",
|
||||||
|
"acls": [{
|
||||||
|
"accessType": "READ",
|
||||||
|
"principalType": "ROLE",
|
||||||
|
"principalId": "$everyone",
|
||||||
|
"permission": "ALLOW"
|
||||||
|
}]
|
||||||
|
}
|
|
@ -5,4 +5,5 @@ module.exports = Self => {
|
||||||
require('../methods/invoiceOut/regenerate')(Self);
|
require('../methods/invoiceOut/regenerate')(Self);
|
||||||
require('../methods/invoiceOut/delete')(Self);
|
require('../methods/invoiceOut/delete')(Self);
|
||||||
require('../methods/invoiceOut/book')(Self);
|
require('../methods/invoiceOut/book')(Self);
|
||||||
|
require('../methods/invoiceOut/createPdf')(Self);
|
||||||
};
|
};
|
||||||
|
|
|
@ -67,11 +67,7 @@ module.exports = function(Self) {
|
||||||
|
|
||||||
if (serial != 'R' && invoiceId) {
|
if (serial != 'R' && invoiceId) {
|
||||||
await Self.rawSql('CALL invoiceOutBooking(?)', [invoiceId], options);
|
await Self.rawSql('CALL invoiceOutBooking(?)', [invoiceId], options);
|
||||||
await models.PrintServerQueue.create({
|
await models.InvoiceOut.createPdf(ctx, invoiceId);
|
||||||
reportFk: 3, // Tarea #2734 (Nueva): crear informe facturas
|
|
||||||
param1: invoiceId,
|
|
||||||
workerFk: userId
|
|
||||||
}, options);
|
|
||||||
}
|
}
|
||||||
await tx.commit();
|
await tx.commit();
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
"loopback": "^3.26.0",
|
"loopback": "^3.26.0",
|
||||||
"loopback-boot": "^2.27.1",
|
"loopback-boot": "^2.27.1",
|
||||||
"loopback-component-explorer": "^6.5.0",
|
"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-mysql": "^5.4.3",
|
||||||
"loopback-connector-remote": "^3.4.1",
|
"loopback-connector-remote": "^3.4.1",
|
||||||
"loopback-context": "^3.4.0",
|
"loopback-context": "^3.4.0",
|
||||||
|
|
|
@ -67,7 +67,7 @@ const dbHelper = {
|
||||||
*/
|
*/
|
||||||
findValueFromDef(queryName, params) {
|
findValueFromDef(queryName, params) {
|
||||||
return this.findOneFromDef(queryName, params).then(row => {
|
return this.findOneFromDef(queryName, params).then(row => {
|
||||||
return Object.values(row)[0];
|
if (row) return Object.values(row)[0];
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue