2017-12-04 07:17:29 +00:00
|
|
|
var express = require('express');
|
|
|
|
var router = new express.Router();
|
|
|
|
var template = require('../template.js');
|
|
|
|
var config = require('../config.js');
|
|
|
|
var pdf = require('html-pdf');
|
|
|
|
var path = require('path');
|
2017-12-26 09:02:47 +00:00
|
|
|
var auth = require('../auth.js');
|
|
|
|
|
|
|
|
// Auth middleware
|
|
|
|
var requestToken = function(request, response, next) {
|
|
|
|
auth.init(request, response, next);
|
|
|
|
};
|
2017-12-04 07:17:29 +00:00
|
|
|
|
|
|
|
// Sepa core
|
2017-12-26 09:02:47 +00:00
|
|
|
router.get('/sepa-core/:companyId/:clientId', requestToken, function(request, response, next) {
|
|
|
|
let params = {
|
2018-10-01 08:58:44 +00:00
|
|
|
clientId: request.params.clientId,
|
2017-12-26 09:02:47 +00:00
|
|
|
companyId: request.params.companyId
|
|
|
|
};
|
|
|
|
|
|
|
|
template.get('sepa-core', params, (error, result) => {
|
2018-10-01 08:58:44 +00:00
|
|
|
if (error)
|
|
|
|
return response.status(400).json({message: error.message});
|
2017-12-04 07:17:29 +00:00
|
|
|
|
2017-12-26 09:02:47 +00:00
|
|
|
pdf.create(result.body, config.pdf).toStream(function(error, stream) {
|
2017-12-04 07:17:29 +00:00
|
|
|
if (error)
|
|
|
|
throw Error(error);
|
|
|
|
|
2017-12-26 09:02:47 +00:00
|
|
|
response.setHeader('Content-Disposition', 'attachment; filename="sepa-core.pdf"');
|
|
|
|
response.setHeader('Content-type', 'application/pdf');
|
2017-12-04 07:17:29 +00:00
|
|
|
stream.pipe(response);
|
|
|
|
});
|
2018-10-01 08:58:44 +00:00
|
|
|
});
|
|
|
|
});
|
2017-12-04 07:17:29 +00:00
|
|
|
|
2017-12-26 09:02:47 +00:00
|
|
|
// Sepa core html preview
|
|
|
|
router.get('/sepa-core/:companyId/:clientId/preview', requestToken, function(request, response, next) {
|
|
|
|
let params = {
|
2018-10-01 08:58:44 +00:00
|
|
|
clientId: request.params.clientId,
|
2017-12-26 09:02:47 +00:00
|
|
|
companyId: request.params.companyId,
|
|
|
|
isPreview: true
|
|
|
|
};
|
|
|
|
|
|
|
|
template.get('sepa-core', params, (error, result) => {
|
2018-10-01 08:58:44 +00:00
|
|
|
if (error)
|
|
|
|
return response.status(400).json({message: error.message});
|
|
|
|
|
2017-12-26 09:02:47 +00:00
|
|
|
response.send(result.body);
|
2018-10-01 08:58:44 +00:00
|
|
|
});
|
|
|
|
});
|
2017-12-26 09:02:47 +00:00
|
|
|
|
|
|
|
// Debtor letter
|
|
|
|
router.get('/letter-debtor/:companyId/:clientId', requestToken, function(request, response, next) {
|
|
|
|
let params = {
|
2018-10-01 08:58:44 +00:00
|
|
|
clientId: request.params.clientId,
|
2017-12-26 09:02:47 +00:00
|
|
|
companyId: request.params.companyId
|
|
|
|
};
|
|
|
|
|
|
|
|
template.get('letter-debtor', params, (error, result) => {
|
2018-10-01 08:58:44 +00:00
|
|
|
if (error)
|
|
|
|
return response.status(400).json({message: error.message});
|
|
|
|
|
|
|
|
pdf.create(result.body, config.pdf).toStream(function(error, stream) {
|
|
|
|
if (error)
|
|
|
|
throw Error(error);
|
2017-12-26 09:02:47 +00:00
|
|
|
|
|
|
|
response.setHeader('Content-Disposition', 'attachment; filename="extracto.pdf"');
|
2017-12-04 07:17:29 +00:00
|
|
|
response.setHeader('Content-type', 'application/pdf');
|
|
|
|
stream.pipe(response);
|
2018-10-01 08:58:44 +00:00
|
|
|
});
|
2017-12-04 07:17:29 +00:00
|
|
|
});
|
2018-10-01 08:58:44 +00:00
|
|
|
});
|
2017-12-26 09:02:47 +00:00
|
|
|
|
|
|
|
// Debtor letter html preview
|
|
|
|
router.get('/letter-debtor/:companyId/:clientId/preview', requestToken, function(request, response, next) {
|
|
|
|
let params = {
|
2018-10-01 08:58:44 +00:00
|
|
|
clientId: request.params.clientId,
|
2017-12-26 09:02:47 +00:00
|
|
|
companyId: request.params.companyId,
|
|
|
|
isPreview: true
|
|
|
|
};
|
2017-12-04 07:17:29 +00:00
|
|
|
|
2017-12-26 09:02:47 +00:00
|
|
|
template.get('letter-debtor', params, (error, result) => {
|
2018-10-01 08:58:44 +00:00
|
|
|
if (error)
|
|
|
|
return response.status(400).json({message: error.message});
|
|
|
|
|
2017-12-04 07:17:29 +00:00
|
|
|
response.send(result.body);
|
2018-10-01 08:58:44 +00:00
|
|
|
});
|
|
|
|
});
|
2017-12-04 07:17:29 +00:00
|
|
|
|
2018-05-28 10:22:35 +00:00
|
|
|
// Delivery note
|
|
|
|
router.get('/delivery-note/:ticketId/preview', requestToken, function(request, response) {
|
|
|
|
let params = {
|
|
|
|
ticketId: request.params.ticketId,
|
|
|
|
isPreview: true
|
|
|
|
};
|
2018-10-01 08:58:44 +00:00
|
|
|
|
2018-05-28 10:22:35 +00:00
|
|
|
template.get('delivery-note', params, (error, result) => {
|
2018-10-01 08:58:44 +00:00
|
|
|
if (error)
|
|
|
|
return response.status(400).json(error);
|
2018-05-28 10:22:35 +00:00
|
|
|
|
|
|
|
response.send(result.body);
|
2018-10-01 08:58:44 +00:00
|
|
|
});
|
|
|
|
});
|
2018-05-28 10:22:35 +00:00
|
|
|
|
2018-10-01 08:58:44 +00:00
|
|
|
router.get('/delivery-note/:ticketId', requestToken, function(request, response, next) {
|
2018-05-28 10:22:35 +00:00
|
|
|
let params = {
|
|
|
|
ticketId: request.params.ticketId
|
|
|
|
};
|
|
|
|
|
|
|
|
template.get('delivery-note', params, (error, result) => {
|
2018-10-01 08:58:44 +00:00
|
|
|
if (error)
|
|
|
|
return response.status(400).json({message: error.message});
|
|
|
|
|
|
|
|
pdf.create(result.body, config.pdf).toStream(function(error, stream) {
|
|
|
|
if (error)
|
|
|
|
throw Error(error);
|
2018-05-28 10:22:35 +00:00
|
|
|
|
|
|
|
stream.pipe(response);
|
2018-10-01 08:58:44 +00:00
|
|
|
});
|
2018-05-28 10:22:35 +00:00
|
|
|
});
|
2018-10-01 08:58:44 +00:00
|
|
|
});
|
2017-12-26 09:02:47 +00:00
|
|
|
|
2018-10-01 08:58:44 +00:00
|
|
|
router.get('/claim-pickup/:claimId/preview', requestToken, function(request, response) {
|
|
|
|
let params = {
|
|
|
|
claimId: request.params.claimId,
|
|
|
|
isPreview: true
|
|
|
|
};
|
|
|
|
|
|
|
|
template.get('claim-pickup', params, (error, result) => {
|
|
|
|
if (error)
|
|
|
|
return response.status(400).json(error);
|
|
|
|
|
|
|
|
response.send(result.body);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
router.get('/claim-pickup/:claimId', requestToken, function(request, response) {
|
|
|
|
let params = {
|
|
|
|
claimId: request.params.claimId
|
|
|
|
};
|
|
|
|
|
|
|
|
template.get('claim-pickup', params, (error, result) => {
|
|
|
|
if (error)
|
|
|
|
return response.status(400).json({message: error.message});
|
|
|
|
|
|
|
|
pdf.create(result.body, config.pdf).toStream(function(error, stream) {
|
|
|
|
if (error)
|
|
|
|
throw Error(error);
|
|
|
|
stream.pipe(response);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|
2017-12-26 09:02:47 +00:00
|
|
|
|
|
|
|
// store pdf
|
|
|
|
/* router.post('/sepa-core/:clientId', function(request, response, next) {
|
|
|
|
template.get('sepa-core', {recipient: request.params.clientId}, (error, result) => {
|
|
|
|
if (error)
|
|
|
|
return response.status(400).json({message: error.message});
|
|
|
|
|
|
|
|
pdf.create(result.body).toFile('./tmp/test.pdf', function(error, result) {
|
|
|
|
if (error)
|
|
|
|
return response.status(400).json({message: error});
|
|
|
|
|
|
|
|
console.log(result);
|
|
|
|
return response.json();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-10-01 08:58:44 +00:00
|
|
|
*/
|