feat(docuware): upload deliveryNote file
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Alex Moreno 2023-01-10 15:03:23 +01:00
parent 382aad2622
commit b8fe919b2a
2 changed files with 393 additions and 443 deletions

View File

@ -1,6 +1,6 @@
const got = require('got');
const UserError = require('vn-loopback/util/user-error');
const request = require('request');
const axios = require('axios');
module.exports = Self => {
Self.remoteMethodCtx('upload', {
@ -24,21 +24,7 @@ module.exports = Self => {
description: 'The dialog'
}
],
returns: [
{
arg: 'body',
type: 'file',
root: true
}, {
arg: 'Content-Type',
type: 'string',
http: {target: 'header'}
}, {
arg: 'Content-Disposition',
type: 'string',
http: {target: 'header'}
}
],
returns: [],
http: {
path: `/:id/upload`,
verb: 'POST'
@ -70,401 +56,376 @@ module.exports = Self => {
}
};
try {
// get fileCabinetId
const fileCabinetResponse = await got.get(`${docuwareUrl}/FileCabinets`, options);
const fileCabinetJson = JSON.parse(fileCabinetResponse.body).FileCabinet;
const fileCabinetId = fileCabinetJson.find(dialogs => dialogs.Name === fileCabinetName).Id;
// get fileCabinetId
const fileCabinetResponse = await got.get(`${docuwareUrl}/FileCabinets`, options);
const fileCabinetJson = JSON.parse(fileCabinetResponse.body).FileCabinet;
const fileCabinetId = fileCabinetJson.find(dialogs => dialogs.Name === fileCabinetName).Id;
// get dialog
const dialogResponse = await got.get(`${docuwareUrl}/FileCabinets/${fileCabinetId}/dialogs`, options);
const dialogJson = JSON.parse(dialogResponse.body).Dialog;
const storeDialogId = dialogJson.find(dialogs => dialogs.DisplayName === 'Archivar').Id;
// get dialog
const dialogResponse = await got.get(`${docuwareUrl}/FileCabinets/${fileCabinetId}/dialogs`, options);
const dialogJson = JSON.parse(dialogResponse.body).Dialog;
const storeDialogId = dialogJson.find(dialogs => dialogs.DisplayName === 'Archivar').Id;
const deliveryNote = await models.Ticket.deliveryNotePdf(ctx, {
id,
type: 'deliveryNote'
});
const deliveryNote = await models.Ticket.deliveryNotePdf(ctx, {
id,
type: 'deliveryNote'
});
// 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]);
// 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
},
'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
},
'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
},
'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
},
'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
},
'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
},
'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
},
'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
},
'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
},
{
'FieldName': 'TOTAL_FACTURA',
'ReadOnly': false,
'SystemField': false,
'ItemElementName': 'Decimal',
'PointAndShootInfo': {
'Box': [
{
'Left': 10423,
'Top': 10958,
'Width': 419,
'Height': 168,
'PageNumber': 0
}
],
'PageNumber': 0
},
'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'
}
]
};
const uploadUri = `${docuwareUrl}/FileCabinets/${fileCabinetId}/Documents?StoreDialogId=${storeDialogId}`;
const optionsUpload = {
'method': 'POST',
'url': uploadUri,
'headers': {
'Content-Type': 'multipart/form-data',
'X-File-ModifiedDate': new Date(),
'Cookie': cookie
},
'formData': {
'document': {
'value': JSON.stringify(templateJson),
'options': {
'filename': 'store.json',
'contentType': null
}
},
'file[]': {
'value': deliveryNote[0],
'options': {
'filename': 'deliveryNote.pdf',
'contentType': deliveryNote[1]
}
}
// 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]);
try {
return new Promise((resolve, reject) => {
request.post(optionsUpload, function(error) {
if (error) return reject(error);
resolve();
});
});
} catch (error) {
console.log(error);
return;
}
} catch (error) {
if (error.code === 'ENOENT')
throw new UserError('The DOCUWARE PDF document does not exists');
// 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
},
'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
},
'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
},
'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
},
'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
},
'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
},
'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
},
'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
},
'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
},
{
'FieldName': 'TOTAL_FACTURA',
'ReadOnly': false,
'SystemField': false,
'ItemElementName': 'Decimal',
'PointAndShootInfo': {
'Box': [
{
'Left': 10423,
'Top': 10958,
'Width': 419,
'Height': 168,
'PageNumber': 0
}
],
'PageNumber': 0
},
'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'
}
]
};
throw error;
}
const uploadUri = `${docuwareUrl}/FileCabinets/${fileCabinetId}/Documents?StoreDialogId=${storeDialogId}`;
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(),
'Cookie': cookie,
...data.getHeaders()
},
};
return await axios.post(uploadUri, data, uploadOptions)
.catch(() => {
throw new UserError('Failed to upload file');
});
};
};

View File

@ -21,11 +21,11 @@
Add turn
</vn-item>
<vn-item class="dropdown"
vn-click-stop="showDeliveryNoteMenu.show($event, 'left')"
vn-click-stop="showDeliveryNoteMenu.show($event, 'left'); $ctrl.hasDocuware()"
translate>
Show Delivery Note...
<vn-menu vn-id="showDeliveryNoteMenu">
<vn-list>
<vn-list ng-if="!$ctrl.hasDocuwareFile">
<vn-item
ng-click="$ctrl.showPdfDeliveryNote('deliveryNote')"
translate>
@ -41,11 +41,29 @@
translate>
as CSV
</vn-item>
<div class="spinner-wrapper" ng-if="$ctrl.isLoadingDocuware">
<vn-spinner enable="$ctrl.isLoadingDocuware"></vn-spinner>
Loading Docuware info
</div>
</vn-list>
<vn-list ng-if="$ctrl.hasDocuwareFile">
<a class="vn-item"
ng-if="$ctrl.hasDocuwareFile"
href='api/Docuwares/{{$ctrl.ticket.id}}/download/deliveryClient/findTicket?access_token={{$ctrl.vnToken.token}}'
target="_blank"
translate>
as PDF
</a>
<vn-item
ng-click="$ctrl.showCsvDeliveryNote()"
translate>
as CSV
</vn-item>
</vn-list>
</vn-menu>
</vn-item>
<vn-item class="dropdown"
vn-click-stop="sendDeliveryNoteMenu.show($event, 'left')"
vn-click-stop="sendDeliveryNoteMenu.show($event, 'left'); $ctrl.hasDocuware()"
translate>
Send Delivery Note...
<vn-menu vn-id="sendDeliveryNoteMenu">
@ -55,6 +73,11 @@
translate>
Send PDF
</vn-item>
<vn-item
ng-click="$ctrl.uploadDocuware()"
translate>
Send PDF to Tablet
</vn-item>
<vn-item
ng-click="sendCsvConfirmation.show({email: $ctrl.ticket.client.email})"
translate>
@ -63,40 +86,6 @@
</vn-list>
</vn-menu>
</vn-item>
<vn-item class="dropdown"
vn-click-stop="showDeliveryNoteMenu.show($event, 'left'); $ctrl.hasDocuware()"
translate>
Docuware options...
<vn-menu vn-id="showDeliveryNoteMenu">
<vn-list ng-if="$ctrl.isLoadingDocuware">
<div class="spinner-wrapper">
<vn-spinner enable="$ctrl.isLoadingDocuware"></vn-spinner>
Loading Docuware info
</div>
</vn-list>
<vn-list ng-if="!$ctrl.isLoadingDocuware">
<a class="vn-item"
ng-if="$ctrl.hasDocuwareFile"
href='api/Docuwares/{{$ctrl.ticket.id}}/download/deliveryClient/findTicket?access_token={{$ctrl.vnToken.token}}'
target="_blank"
translate>
Show PDF
</a>
<vn-item
ng-if="!$ctrl.hasDocuwareFile"
ng-click="$ctrl.uploadDocuware()"
translate>
Upload Salix PDF
</vn-item>
<vn-item
ng-if="$ctrl.hasDocuwareFile"
ng-click="$ctrl.sendDocuwarePdfDeliveryNote({email: $ctrl.ticket.client.email})"
translate>
Send PDF
</vn-item>
</vn-list>
</vn-menu>
</vn-item>
<vn-item
ng-click="$ctrl.showPdfDeliveryNote('proforma')"
translate>