2023-01-09 14:11:53 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2023-01-10 14:03:23 +00:00
|
|
|
const axios = require('axios');
|
2023-01-09 14:11:53 +00:00
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('upload', {
|
|
|
|
description: 'Upload an docuware PDF',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'id',
|
|
|
|
type: 'number',
|
|
|
|
description: 'The ticket id',
|
|
|
|
http: {source: 'path'}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'fileCabinet',
|
|
|
|
type: 'string',
|
|
|
|
description: 'The file cabinet'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'dialog',
|
|
|
|
type: 'string',
|
|
|
|
description: 'The dialog'
|
|
|
|
}
|
|
|
|
],
|
2023-01-10 14:03:23 +00:00
|
|
|
returns: [],
|
2023-01-09 14:11:53 +00:00
|
|
|
http: {
|
|
|
|
path: `/:id/upload`,
|
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-01-11 14:19:09 +00:00
|
|
|
Self.upload = async function(ctx, id, fileCabinet) {
|
2023-01-09 14:11:53 +00:00
|
|
|
const models = Self.app.models;
|
2023-01-11 14:19:09 +00:00
|
|
|
const action = 'store';
|
2023-01-09 14:11:53 +00:00
|
|
|
|
2023-01-11 14:19:09 +00:00
|
|
|
const options = await Self.getOptions();
|
|
|
|
const fileCabinetId = await Self.getFileCabinet(fileCabinet);
|
|
|
|
const dialogId = await Self.getDialog(fileCabinet, action, fileCabinetId);
|
2023-01-09 14:11:53 +00:00
|
|
|
|
2023-01-11 14:19:09 +00:00
|
|
|
// get delivery note
|
2023-01-10 14:03:23 +00:00
|
|
|
const deliveryNote = await models.Ticket.deliveryNotePdf(ctx, {
|
|
|
|
id,
|
|
|
|
type: 'deliveryNote'
|
|
|
|
});
|
2023-01-09 14:11:53 +00:00
|
|
|
|
2023-01-10 14:03:23 +00:00
|
|
|
// get ticket data
|
|
|
|
const ticket = await models.Ticket.findById(id, {
|
|
|
|
include: [{
|
|
|
|
relation: 'client',
|
|
|
|
scope: {
|
|
|
|
fields: ['id', 'socialName', 'fi']
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
const [taxes] = await models.Ticket.rawSql('CALL vn.ticketGetTaxAdd(?)', [id]);
|
2023-01-09 14:11:53 +00:00
|
|
|
|
2023-01-10 14:03:23 +00:00
|
|
|
// upload file
|
|
|
|
const templateJson = {
|
|
|
|
'Fields': [
|
|
|
|
{
|
|
|
|
'FieldName': 'N__ALBAR_N',
|
|
|
|
'ReadOnly': false,
|
|
|
|
'SystemField': false,
|
|
|
|
'ItemElementName': 'string',
|
|
|
|
'PointAndShootInfo': {
|
|
|
|
'Box': [
|
|
|
|
{
|
|
|
|
'Left': 2531,
|
|
|
|
'Top': 3645,
|
|
|
|
'Width': 257,
|
|
|
|
'Height': 230,
|
|
|
|
'PageNumber': 0
|
|
|
|
}
|
|
|
|
],
|
|
|
|
'PageNumber': 0
|
2023-01-09 14:11:53 +00:00
|
|
|
},
|
2023-01-10 14:03:23 +00:00
|
|
|
'IsAutoNumber': false,
|
|
|
|
'IsNull': false,
|
|
|
|
'Item': id,
|
|
|
|
'FieldValue': id
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'CIF_PROVEEDOR',
|
|
|
|
'ReadOnly': false,
|
|
|
|
'SystemField': false,
|
|
|
|
'ItemElementName': 'string',
|
|
|
|
'PointAndShootInfo': {
|
|
|
|
'Box': [
|
|
|
|
{
|
|
|
|
'Left': 6176,
|
|
|
|
'Top': 4624,
|
|
|
|
'Width': 839,
|
|
|
|
'Height': 168,
|
|
|
|
'PageNumber': 0
|
|
|
|
}
|
|
|
|
],
|
|
|
|
'PageNumber': 0
|
2023-01-09 14:11:53 +00:00
|
|
|
},
|
2023-01-10 14:03:23 +00:00
|
|
|
'IsAutoNumber': false,
|
|
|
|
'IsNull': false,
|
|
|
|
'Item': ticket.client().fi,
|
|
|
|
'FieldValue': ticket.client().fi
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'CODIGO_PROVEEDOR',
|
|
|
|
'ReadOnly': false,
|
|
|
|
'SystemField': false,
|
|
|
|
'ItemElementName': 'string',
|
|
|
|
'PointAndShootInfo': {
|
|
|
|
'Box': [
|
|
|
|
{
|
|
|
|
'Left': 2531,
|
|
|
|
'Top': 3240,
|
|
|
|
'Width': 514,
|
|
|
|
'Height': 230,
|
|
|
|
'PageNumber': 0
|
|
|
|
}
|
|
|
|
],
|
|
|
|
'PageNumber': 0
|
2023-01-09 14:11:53 +00:00
|
|
|
},
|
2023-01-10 14:03:23 +00:00
|
|
|
'IsAutoNumber': false,
|
|
|
|
'IsNull': false,
|
|
|
|
'Item': ticket.client().id,
|
|
|
|
'FieldValue': ticket.client().id
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'NOMBRE_PROVEEDOR',
|
|
|
|
'ReadOnly': false,
|
|
|
|
'SystemField': false,
|
|
|
|
'ItemElementName': 'string',
|
|
|
|
'PointAndShootInfo': {
|
|
|
|
'Box': [
|
|
|
|
{
|
|
|
|
'Left': 6175,
|
|
|
|
'Top': 4264,
|
|
|
|
'Width': 858,
|
|
|
|
'Height': 168,
|
|
|
|
'PageNumber': 0
|
|
|
|
}
|
|
|
|
],
|
|
|
|
'PageNumber': 0
|
2023-01-09 14:11:53 +00:00
|
|
|
},
|
2023-01-10 14:03:23 +00:00
|
|
|
'IsAutoNumber': false,
|
|
|
|
'IsNull': false,
|
|
|
|
'Item': ticket.client().socialName,
|
|
|
|
'FieldValue': ticket.client().socialName
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'FECHA_FACTURA',
|
|
|
|
'ReadOnly': false,
|
|
|
|
'SystemField': false,
|
|
|
|
'ItemElementName': 'date',
|
|
|
|
'PointAndShootInfo': {
|
|
|
|
'Box': [
|
|
|
|
{
|
|
|
|
'Left': 2531,
|
|
|
|
'Top': 4050,
|
|
|
|
'Width': 1181,
|
|
|
|
'Height': 230,
|
|
|
|
'PageNumber': 0
|
|
|
|
}
|
|
|
|
],
|
|
|
|
'PageNumber': 0
|
2023-01-09 14:11:53 +00:00
|
|
|
},
|
2023-01-10 14:03:23 +00:00
|
|
|
'IsAutoNumber': false,
|
|
|
|
'IsNull': false,
|
|
|
|
'Item': ticket.shipped,
|
|
|
|
'FieldValue': ticket.shipped
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'TIPO_IVA__1_',
|
|
|
|
'ReadOnly': false,
|
|
|
|
'SystemField': false,
|
|
|
|
'ItemElementName': 'Decimal',
|
|
|
|
'PointAndShootInfo': {
|
|
|
|
'Box': [
|
|
|
|
{
|
|
|
|
'Left': 9537,
|
|
|
|
'Top': 10057,
|
|
|
|
'Width': 615,
|
|
|
|
'Height': 168,
|
|
|
|
'PageNumber': 0
|
|
|
|
}
|
|
|
|
],
|
|
|
|
'PageNumber': 0
|
2023-01-09 14:11:53 +00:00
|
|
|
},
|
2023-01-10 14:03:23 +00:00
|
|
|
'IsAutoNumber': false,
|
|
|
|
'IsNull': false,
|
|
|
|
'Item': taxes[0].rate,
|
|
|
|
'FieldValue': taxes[0].rate
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'BASE_IMPONIBLE__1_',
|
|
|
|
'ReadOnly': false,
|
|
|
|
'SystemField': false,
|
|
|
|
'ItemElementName': 'Decimal',
|
|
|
|
'PointAndShootInfo': {
|
|
|
|
'Box': [
|
|
|
|
{
|
|
|
|
'Left': 8907,
|
|
|
|
'Top': 10567,
|
|
|
|
'Width': 419,
|
|
|
|
'Height': 168,
|
|
|
|
'PageNumber': 0
|
|
|
|
}
|
|
|
|
],
|
|
|
|
'PageNumber': 0
|
2023-01-09 14:11:53 +00:00
|
|
|
},
|
2023-01-10 14:03:23 +00:00
|
|
|
'IsAutoNumber': false,
|
|
|
|
'IsNull': false,
|
|
|
|
'Item': ticket.totalWithoutVat,
|
|
|
|
'FieldValue': ticket.totalWithoutVat
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'IMPORTE_IVA__1_',
|
|
|
|
'ReadOnly': false,
|
|
|
|
'SystemField': false,
|
|
|
|
'ItemElementName': 'Decimal',
|
|
|
|
'PointAndShootInfo': {
|
|
|
|
'Box': [
|
|
|
|
{
|
|
|
|
'Left': 10423,
|
|
|
|
'Top': 10057,
|
|
|
|
'Width': 419,
|
|
|
|
'Height': 168,
|
|
|
|
'PageNumber': 0
|
|
|
|
}
|
|
|
|
],
|
|
|
|
'PageNumber': 0
|
2023-01-09 14:11:53 +00:00
|
|
|
},
|
2023-01-10 14:03:23 +00:00
|
|
|
'IsAutoNumber': false,
|
|
|
|
'IsNull': false,
|
|
|
|
'Item': taxes[0].tax,
|
|
|
|
'FieldValue': taxes[0].tax
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'TIPO_IVA__2_',
|
|
|
|
'ReadOnly': false,
|
|
|
|
'SystemField': false,
|
|
|
|
'ItemElementName': 'Decimal',
|
|
|
|
'PointAndShootInfo': null,
|
|
|
|
'IsAutoNumber': false,
|
|
|
|
'IsNull': true,
|
|
|
|
'Item': null,
|
|
|
|
'FieldValue': null
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'BASE_IMPONIBLE__2_',
|
|
|
|
'ReadOnly': false,
|
|
|
|
'SystemField': false,
|
|
|
|
'ItemElementName': 'Decimal',
|
|
|
|
'PointAndShootInfo': null,
|
|
|
|
'IsAutoNumber': false,
|
|
|
|
'IsNull': true,
|
|
|
|
'Item': null,
|
|
|
|
'FieldValue': null
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'IMPORTE_IVA__2_',
|
|
|
|
'ReadOnly': false,
|
|
|
|
'SystemField': false,
|
|
|
|
'ItemElementName': 'Decimal',
|
|
|
|
'PointAndShootInfo': null,
|
|
|
|
'IsAutoNumber': false,
|
|
|
|
'IsNull': true,
|
|
|
|
'Item': null,
|
|
|
|
'FieldValue': null
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'TIPO_IVA__3_',
|
|
|
|
'ReadOnly': false,
|
|
|
|
'SystemField': false,
|
|
|
|
'ItemElementName': 'Decimal',
|
|
|
|
'PointAndShootInfo': null,
|
|
|
|
'IsAutoNumber': false,
|
|
|
|
'IsNull': true,
|
|
|
|
'Item': null,
|
|
|
|
'FieldValue': null
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'BASE_IMPONIBLE__3_',
|
|
|
|
'ReadOnly': false,
|
|
|
|
'SystemField': false,
|
|
|
|
'ItemElementName': 'Decimal',
|
|
|
|
'PointAndShootInfo': null,
|
|
|
|
'IsAutoNumber': false,
|
|
|
|
'IsNull': true,
|
|
|
|
'Item': null,
|
|
|
|
'FieldValue': null
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'IMPORTE_IVA__3_',
|
|
|
|
'ReadOnly': false,
|
|
|
|
'SystemField': false,
|
|
|
|
'ItemElementName': 'Decimal',
|
|
|
|
'PointAndShootInfo': null,
|
|
|
|
'IsAutoNumber': false,
|
|
|
|
'IsNull': true,
|
|
|
|
'Item': null,
|
|
|
|
'FieldValue': null
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'IRPF',
|
|
|
|
'ReadOnly': false,
|
|
|
|
'SystemField': false,
|
|
|
|
'ItemElementName': 'Decimal',
|
|
|
|
'PointAndShootInfo': null,
|
|
|
|
'IsAutoNumber': false,
|
|
|
|
'IsNull': true,
|
|
|
|
'Item': null,
|
|
|
|
'FieldValue': null
|
2023-01-09 14:11:53 +00:00
|
|
|
},
|
2023-01-10 14:03:23 +00:00
|
|
|
{
|
|
|
|
'FieldName': 'TOTAL_FACTURA',
|
|
|
|
'ReadOnly': false,
|
|
|
|
'SystemField': false,
|
|
|
|
'ItemElementName': 'Decimal',
|
|
|
|
'PointAndShootInfo': {
|
|
|
|
'Box': [
|
|
|
|
{
|
|
|
|
'Left': 10423,
|
|
|
|
'Top': 10958,
|
|
|
|
'Width': 419,
|
|
|
|
'Height': 168,
|
|
|
|
'PageNumber': 0
|
|
|
|
}
|
|
|
|
],
|
|
|
|
'PageNumber': 0
|
2023-01-09 14:11:53 +00:00
|
|
|
},
|
2023-01-10 14:03:23 +00:00
|
|
|
'IsAutoNumber': false,
|
|
|
|
'IsNull': false,
|
|
|
|
'Item': ticket.totalWithVat,
|
|
|
|
'FieldValue': ticket.totalWithVat
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'ESTADO',
|
|
|
|
'ReadOnly': false,
|
|
|
|
'SystemField': false,
|
|
|
|
'ItemElementName': 'string',
|
|
|
|
'PointAndShootInfo': null,
|
|
|
|
'IsAutoNumber': false,
|
|
|
|
'IsNull': false,
|
|
|
|
'Item': 'Pendiente procesar',
|
|
|
|
'FieldValue': 'Pendiente procesar'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'URL',
|
|
|
|
'ReadOnly': false,
|
|
|
|
'SystemField': false,
|
|
|
|
'ItemElementName': 'string',
|
|
|
|
'PointAndShootInfo': null,
|
|
|
|
'IsAutoNumber': false,
|
|
|
|
'IsNull': true,
|
|
|
|
'Item': null,
|
|
|
|
'FieldValue': ''
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'FIRMA_',
|
|
|
|
'ReadOnly': false,
|
|
|
|
'SystemField': false,
|
|
|
|
'ItemElementName': 'string',
|
|
|
|
'PointAndShootInfo': null,
|
|
|
|
'IsAutoNumber': false,
|
|
|
|
'IsNull': false,
|
|
|
|
'Item': 'Si',
|
|
|
|
'FieldValue': 'Si'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'FILTRO_TABLET',
|
|
|
|
'ReadOnly': false,
|
|
|
|
'SystemField': false,
|
|
|
|
'ItemElementName': 'string',
|
|
|
|
'PointAndShootInfo': null,
|
|
|
|
'IsAutoNumber': false,
|
|
|
|
'IsNull': false,
|
|
|
|
'Item': 'Tablet1',
|
|
|
|
'FieldValue': 'Tablet1'
|
2023-01-09 14:11:53 +00:00
|
|
|
}
|
2023-01-10 14:03:23 +00:00
|
|
|
]
|
|
|
|
};
|
2023-01-09 14:11:53 +00:00
|
|
|
|
2023-01-12 14:16:38 +00:00
|
|
|
// if (process.env.NODE_ENV != 'production')
|
|
|
|
// throw new UserError('Action not allowed on the test environment');
|
|
|
|
const docuwareFile = await models.Docuware.checkFile(ctx, id, fileCabinet);
|
|
|
|
|
|
|
|
console.log(docuwareFile, id, fileCabinet);
|
|
|
|
// replace
|
|
|
|
if (docuwareFile) {
|
|
|
|
console.log(docuwareFile);
|
|
|
|
const uri = `${options.url}/FileCabinets/${fileCabinetId}/Sections?DocId=${docuwareFile.id}`;
|
|
|
|
console.log(uri);
|
|
|
|
return await axios.post(uri, deliveryNote[0], {headers: {
|
|
|
|
'Content-Type': 'application/pdf',
|
|
|
|
'Content-Disposition': 'file; filename="10.pdf"',
|
|
|
|
'X-File-ModifiedDate': '2020-08-26T00:00:00.000Z'
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let uploadUri = `${options.url}/FileCabinets/${fileCabinetId}/Documents?StoreDialogId=${dialogId}`;
|
2023-01-09 14:11:53 +00:00
|
|
|
|
2023-01-10 14:03:23 +00:00
|
|
|
const FormData = require('form-data');
|
|
|
|
const data = new FormData();
|
|
|
|
|
|
|
|
data.append('document', JSON.stringify(templateJson), 'schema.json');
|
|
|
|
data.append('file[]', deliveryNote[0], 'file.pdf');
|
|
|
|
const uploadOptions = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'multipart/form-data',
|
|
|
|
'X-File-ModifiedDate': new Date(),
|
2023-01-11 14:19:09 +00:00
|
|
|
'Cookie': options.headers.headers.Cookie,
|
2023-01-10 14:03:23 +00:00
|
|
|
...data.getHeaders()
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return await axios.post(uploadUri, data, uploadOptions)
|
|
|
|
.catch(() => {
|
|
|
|
throw new UserError('Failed to upload file');
|
|
|
|
});
|
2023-01-09 14:11:53 +00:00
|
|
|
};
|
|
|
|
};
|