2023-06-13 13:05:37 +00:00
|
|
|
const axios = require('axios');
|
|
|
|
|
|
|
|
module.exports = Self => {
|
2023-06-16 11:49:21 +00:00
|
|
|
Self.remoteMethod('internationalExpedition', {
|
2023-06-13 13:05:37 +00:00
|
|
|
description: 'Returns the lastest campaigns',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'expeditionFk',
|
|
|
|
type: 'number',
|
|
|
|
required: true
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: ['object'],
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
2023-06-16 11:49:21 +00:00
|
|
|
path: `/internationalExpedition`,
|
2023-06-13 13:05:37 +00:00
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-06-16 11:49:21 +00:00
|
|
|
Self.internationalExpedition = async(expeditionFk, options) => {
|
2023-06-16 12:24:47 +00:00
|
|
|
const models = Self.app.models;
|
2023-06-13 13:05:37 +00:00
|
|
|
const myOptions = {};
|
|
|
|
|
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
|
|
|
|
2023-06-16 12:24:47 +00:00
|
|
|
const expedition = await models.Expedition.findOne({
|
|
|
|
fields: ['id', 'ticketFk'],
|
|
|
|
where: {id: expeditionFk},
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
relation: 'ticket',
|
|
|
|
scope: {
|
|
|
|
fields: ['shipped', 'addressFk', 'clientFk', 'companyFk'],
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
relation: 'client',
|
|
|
|
scope: {
|
|
|
|
fields: ['mobile', 'phone', 'email']
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
relation: 'address',
|
|
|
|
scope: {
|
|
|
|
fields: [
|
|
|
|
'nickname',
|
|
|
|
'street',
|
|
|
|
'postalCode',
|
|
|
|
'city',
|
|
|
|
'mobile',
|
|
|
|
'phone',
|
|
|
|
'provinceFk'
|
|
|
|
],
|
|
|
|
include: {
|
|
|
|
relation: 'province',
|
|
|
|
scope: {
|
|
|
|
fields: ['name', 'countryFk'],
|
|
|
|
include: {
|
|
|
|
relation: 'country',
|
|
|
|
scope: {
|
|
|
|
fields: ['code'],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
relation: 'company',
|
|
|
|
scope: {
|
|
|
|
fields: ['clientFk'],
|
|
|
|
include: {
|
|
|
|
relation: 'client',
|
|
|
|
scope: {
|
|
|
|
fields: ['socialName', 'mobile', 'phone', 'email', 'defaultAddressFk'],
|
|
|
|
include: {
|
|
|
|
relation: 'defaultAddress',
|
|
|
|
scope: {
|
|
|
|
fields: [
|
|
|
|
'street',
|
|
|
|
'postalCode',
|
|
|
|
'city',
|
|
|
|
'mobile',
|
|
|
|
'phone',
|
|
|
|
'provinceFk'
|
|
|
|
],
|
|
|
|
include: {
|
|
|
|
relation: 'province',
|
|
|
|
scope: {
|
|
|
|
fields: ['name']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}, myOptions);
|
2023-06-19 09:55:07 +00:00
|
|
|
|
|
|
|
const viaexpressConfig = await models.ViaexpressConfig.findOne({
|
|
|
|
fields: ['url', 'clientViaexpress', 'userViaexpress', 'passwordViaexpress', 'defaultWeight', 'deliveryType']
|
|
|
|
}, myOptions);
|
|
|
|
|
|
|
|
const shipped = expedition.ticket().shipped;
|
|
|
|
const year = shipped.getFullYear();
|
|
|
|
const month = shipped.getMonth() + 1;
|
|
|
|
const day = shipped.getDate();
|
|
|
|
const date = `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`;
|
2023-06-16 12:24:47 +00:00
|
|
|
|
2023-06-13 13:05:37 +00:00
|
|
|
const xmlData = `<?xml version="1.0" encoding="utf-8"?>
|
|
|
|
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
|
|
|
|
<soap12:Body>
|
|
|
|
<PutExpedicionInternacional xmlns="http://82.223.6.71:82">
|
|
|
|
<ObjetoEnvio>
|
2023-06-19 09:55:07 +00:00
|
|
|
<Peso>${viaexpressConfig.defaultWeight}</Peso>
|
2023-06-13 13:05:37 +00:00
|
|
|
<Bultos>1</Bultos>
|
|
|
|
<Reembolso>0</Reembolso>
|
2023-06-19 09:55:07 +00:00
|
|
|
<Fecha>${date}</Fecha>
|
2023-06-13 13:05:37 +00:00
|
|
|
<ConRetorno>0</ConRetorno>
|
2023-06-19 09:55:07 +00:00
|
|
|
<Tipo>${viaexpressConfig.deliveryType}</Tipo>
|
2023-06-13 13:05:37 +00:00
|
|
|
<Debidos>0</Debidos>
|
|
|
|
<Asegurado>0</Asegurado>
|
|
|
|
<Imprimir>0</Imprimir>
|
|
|
|
<ConDevolucionAlbaran>0</ConDevolucionAlbaran>
|
|
|
|
<Intradia>0</Intradia>
|
|
|
|
<Observaciones></Observaciones>
|
|
|
|
<AlbaranRemitente></AlbaranRemitente>
|
|
|
|
<Modo>0</Modo>
|
|
|
|
<TextoAgencia></TextoAgencia>
|
|
|
|
<Terminal></Terminal>
|
|
|
|
<ObjetoRemitente>
|
2023-06-19 09:55:07 +00:00
|
|
|
<RazonSocial>${expedition.ticket().company().client().socialName}</RazonSocial>
|
|
|
|
<Domicilio>${expedition.ticket().company().client().defaultAddress().street}</Domicilio>
|
|
|
|
<Cpostal>${expedition.ticket().company().client().defaultAddress().postalCode}</Cpostal>
|
|
|
|
<Poblacion>${expedition.ticket().company().client().defaultAddress().city}</Poblacion>
|
|
|
|
<Provincia>${expedition.ticket().company().client().defaultAddress().province().name}</Provincia>
|
2023-06-13 13:05:37 +00:00
|
|
|
<Contacto></Contacto>
|
2023-06-19 09:55:07 +00:00
|
|
|
<Telefono>${expedition.ticket().company().client().defaultAddress().mobile || expedition.ticket().company().client().defaultAddress().phone || expedition.ticket().company().client().mobile || expedition.ticket().company().client().phone}</Telefono>
|
|
|
|
<Email>${expedition.ticket().company().client().email}</Email>
|
2023-06-13 13:05:37 +00:00
|
|
|
</ObjetoRemitente>
|
|
|
|
<ObjetoDestinatario>
|
2023-06-19 09:55:07 +00:00
|
|
|
<RazonSocial>${expedition.ticket().address().nickname}</RazonSocial>
|
|
|
|
<Domicilio>${expedition.ticket().address().street}</Domicilio>
|
|
|
|
<Cpostal>${expedition.ticket().address().postalCode}</Cpostal>
|
|
|
|
<Poblacion>${expedition.ticket().address().city}</Poblacion>
|
2023-06-13 13:05:37 +00:00
|
|
|
<Municipio></Municipio>
|
2023-06-19 09:55:07 +00:00
|
|
|
<Provincia>${expedition.ticket().address().province().name}</Provincia>
|
2023-06-13 13:05:37 +00:00
|
|
|
<Contacto></Contacto>
|
2023-06-19 09:55:07 +00:00
|
|
|
<Telefono>${expedition.ticket().address().mobile || expedition.ticket().address().phone || expedition.ticket().client().mobile || expedition.ticket().client().phone}</Telefono>
|
|
|
|
<Email>${expedition.ticket().client().email}</Email>
|
|
|
|
<Pais>${expedition.ticket().address().province().country().code}</Pais>
|
2023-06-13 13:05:37 +00:00
|
|
|
</ObjetoDestinatario>
|
|
|
|
<ObjetoLogin>
|
2023-06-19 09:55:07 +00:00
|
|
|
<IdCliente>${viaexpressConfig.clientViaexpress}</IdCliente>
|
|
|
|
<Usuario>${viaexpressConfig.userViaexpress}</Usuario>
|
|
|
|
<Password>${viaexpressConfig.passwordViaexpress}</Password>
|
2023-06-13 13:05:37 +00:00
|
|
|
</ObjetoLogin>
|
|
|
|
</ObjetoEnvio>
|
|
|
|
</PutExpedicionInternacional>
|
|
|
|
</soap12:Body>
|
|
|
|
</soap12:Envelope>`;
|
|
|
|
|
|
|
|
try {
|
2023-06-19 09:55:07 +00:00
|
|
|
const response = await axios.post(`${viaexpressConfig.url}ServicioVxClientes.asmx`, xmlData, {
|
2023-06-13 13:05:37 +00:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/soap+xml; charset=utf-8'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const startTag = '<ReferenciaVx>';
|
|
|
|
const endTag = '</ReferenciaVx>';
|
|
|
|
const startIndex = response.data.indexOf(startTag) + startTag.length;
|
|
|
|
const endIndex = response.data.indexOf(endTag);
|
|
|
|
const referenciaVx = response.data.substring(startIndex, endIndex);
|
|
|
|
|
|
|
|
return referenciaVx;
|
|
|
|
} catch (error) {
|
2023-06-16 12:27:29 +00:00
|
|
|
if (error?.response?.data) throw Error(error.response.data);
|
|
|
|
throw Error(error);
|
2023-06-13 13:05:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|