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: [
|
|
|
|
{
|
2023-06-12 13:20:46 +00:00
|
|
|
arg: 'ticketIds',
|
|
|
|
type: ['number'],
|
|
|
|
description: 'The ticket ids',
|
|
|
|
required: true
|
2023-01-09 14:11:53 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'fileCabinet',
|
|
|
|
type: 'string',
|
2023-06-12 13:20:46 +00:00
|
|
|
description: 'The file cabinet',
|
|
|
|
required: true
|
2023-01-09 14:11:53 +00:00
|
|
|
}
|
|
|
|
],
|
2023-06-13 09:37:35 +00:00
|
|
|
returns: {
|
|
|
|
type: 'object',
|
|
|
|
root: true
|
|
|
|
},
|
2023-01-09 14:11:53 +00:00
|
|
|
http: {
|
2023-06-12 13:20:46 +00:00
|
|
|
path: `/upload`,
|
2023-01-09 14:11:53 +00:00
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-06-12 13:20:46 +00:00
|
|
|
Self.upload = async function(ctx, ticketIds, fileCabinet) {
|
|
|
|
delete ctx.args.ticketIds;
|
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-06-12 13:20:46 +00:00
|
|
|
const uploaded = [];
|
2023-01-09 14:11:53 +00:00
|
|
|
|
2023-06-12 13:20:46 +00:00
|
|
|
// get delivery note
|
|
|
|
for (id of ticketIds) {
|
|
|
|
// get delivery note
|
|
|
|
ctx.args.id = id;
|
|
|
|
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', 'name', 'fi']
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
});
|
2023-01-09 14:11:53 +00:00
|
|
|
|
2023-06-12 13:20:46 +00:00
|
|
|
// upload file
|
|
|
|
const templateJson = {
|
|
|
|
'Fields': [
|
|
|
|
{
|
|
|
|
'FieldName': 'N__ALBAR_N',
|
|
|
|
'ItemElementName': 'string',
|
|
|
|
'Item': id,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'CIF_PROVEEDOR',
|
|
|
|
'ItemElementName': 'string',
|
|
|
|
'Item': ticket.client().fi,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'CODIGO_PROVEEDOR',
|
|
|
|
'ItemElementName': 'string',
|
|
|
|
'Item': ticket.client().id,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'NOMBRE_PROVEEDOR',
|
|
|
|
'ItemElementName': 'string',
|
|
|
|
'Item': ticket.client().name + ' - ' + id,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'FECHA_FACTURA',
|
|
|
|
'ItemElementName': 'date',
|
|
|
|
'Item': ticket.shipped,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'TOTAL_FACTURA',
|
|
|
|
'ItemElementName': 'Decimal',
|
|
|
|
'Item': ticket.totalWithVat,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'ESTADO',
|
|
|
|
'ItemElementName': 'string',
|
|
|
|
'Item': 'Pendiente procesar',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'FIRMA_',
|
|
|
|
'ItemElementName': 'string',
|
|
|
|
'Item': 'Si',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'FieldName': 'FILTRO_TABLET',
|
|
|
|
'ItemElementName': 'string',
|
|
|
|
'Item': 'Tablet1',
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
2023-01-09 14:11:53 +00:00
|
|
|
|
2023-06-13 09:37:35 +00:00
|
|
|
if (process.env.NODE_ENV != 'production')
|
|
|
|
throw new UserError('Action not allowed on the test environment');
|
2023-01-12 14:16:38 +00:00
|
|
|
|
2023-06-12 13:20:46 +00:00
|
|
|
// delete old
|
|
|
|
const docuwareFile = await models.Docuware.checkFile(ctx, id, fileCabinet, false);
|
|
|
|
if (docuwareFile) {
|
|
|
|
const deleteJson = {
|
|
|
|
'Field': [{'FieldName': 'ESTADO', 'Item': 'Pendiente eliminar', 'ItemElementName': 'String'}]
|
|
|
|
};
|
|
|
|
const deleteUri = `${options.url}/FileCabinets/${fileCabinetId}/Documents/${docuwareFile.id}/Fields`;
|
|
|
|
await axios.put(deleteUri, deleteJson, options.headers);
|
|
|
|
}
|
2023-01-12 14:16:38 +00:00
|
|
|
|
2023-06-12 13:20:46 +00:00
|
|
|
const uploadUri = `${options.url}/FileCabinets/${fileCabinetId}/Documents?StoreDialogId=${dialogId}`;
|
|
|
|
const FormData = require('form-data');
|
|
|
|
const data = new FormData();
|
2023-01-10 14:03:23 +00:00
|
|
|
|
2023-06-12 13:20:46 +00:00
|
|
|
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': Date.vnNew(),
|
|
|
|
'Cookie': options.headers.headers.Cookie,
|
|
|
|
...data.getHeaders()
|
|
|
|
},
|
|
|
|
};
|
2023-01-10 14:03:23 +00:00
|
|
|
|
2023-06-12 13:20:46 +00:00
|
|
|
try {
|
|
|
|
await axios.post(uploadUri, data, uploadOptions);
|
|
|
|
} catch (err) {
|
|
|
|
const $t = ctx.req.__;
|
|
|
|
const message = $t('Failed to upload ticket', {id});
|
|
|
|
if (uploaded.length)
|
|
|
|
await models.TicketTracking.setDelivered(ctx, uploaded);
|
|
|
|
throw new UserError(message);
|
|
|
|
}
|
|
|
|
uploaded.push(id);
|
|
|
|
}
|
|
|
|
return models.TicketTracking.setDelivered(ctx, ticketIds);
|
2023-01-09 14:11:53 +00:00
|
|
|
};
|
|
|
|
};
|