salix/back/methods/viaexpress-config/internationalExpedition.js

136 lines
6.1 KiB
JavaScript
Raw Normal View History

const axios = require('axios');
module.exports = Self => {
Self.remoteMethod('internationalExpedition', {
description: 'Returns the lastest campaigns',
accessType: 'WRITE',
accepts: [{
arg: 'expeditionFk',
type: 'number',
required: true
}],
returns: {
type: ['object'],
root: true
},
http: {
path: `/internationalExpedition`,
verb: 'POST'
}
});
Self.internationalExpedition = async(expeditionFk, options) => {
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const [data] = await Self.rawSql(`
SELECT
url,
clientViaexpress,
userViaexpress,
passwordViaexpress,
defaultWeight,
DATE_FORMAT(t.shipped, '%Y-%m-%d') shipped,
deliveryType,
cv.socialName senderName,
av.street senderStreet,
av.postalCode senderPostalCode,
av.city senderCity,
pv.name senderProvince,
IFNULL(av.mobile, IFNULL(av.phone, IFNULL(cv.mobile, cv.phone))) senderPhone,
SUBSTRING_INDEX(cv.email, ',', 1) senderEmail,
a.nickname receiverName,
a.street receiverStreet,
a.postalCode receiverPostalCode,
a.city receiverCity,
p.name receiverProvince,
IFNULL(a.mobile, IFNULL(a.phone, IFNULL(c.mobile, c.phone))) receiverPhone,
SUBSTRING_INDEX(c.email, ',', 1) receiverEmail,
c2.code receiverCountry
FROM vn.viaexpressConfig
JOIN vn.expedition e ON e.id = ?
JOIN vn.ticket t ON t.id = e.ticketFk
JOIN vn.address a ON a.id = t.addressFk
JOIN vn.province p ON p.id = a.provinceFk
JOIN vn.client c ON c.id = t.clientFk
JOIN vn.company co ON co.id = t.companyFk
JOIN vn.client cv ON cv.id = co.clientFk
JOIN vn.address av ON av.id = cv.defaultAddressFk
JOIN vn.province pv ON pv.id = av.provinceFk
JOIN vn.country c2 ON c2.id = p.countryFk`, [expeditionFk], myOptions);
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>
<Peso>${data.defaultWeight}</Peso>
<Bultos>1</Bultos>
<Reembolso>0</Reembolso>
<Fecha>${data.shipped}</Fecha>
<ConRetorno>0</ConRetorno>
<Tipo>${data.deliveryType}</Tipo>
<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>
<RazonSocial>${data.senderName}</RazonSocial>
<Domicilio>${data.senderStreet}</Domicilio>
<Cpostal>${data.senderPostalCode}</Cpostal>
<Poblacion>${data.senderCity}</Poblacion>
<Provincia>${data.senderProvince}</Provincia>
<Contacto></Contacto>
<Telefono>${data.senderPhone}</Telefono>
<Email>${data.senderEmail}</Email>
</ObjetoRemitente>
<ObjetoDestinatario>
<RazonSocial>${data.receiverName}</RazonSocial>
<Domicilio>${data.receiverStreet}</Domicilio>
<Cpostal>${data.receiverPostalCode}</Cpostal>
<Poblacion>${data.receiverCity}</Poblacion>
<Municipio></Municipio>
<Provincia>${data.receiverProvince}</Provincia>
<Contacto></Contacto>
<Telefono>${data.receiverPhone}</Telefono>
<Email>${data.receiverEmail}</Email>
<Pais>${data.receiverCountry}</Pais>
</ObjetoDestinatario>
<ObjetoLogin>
<IdCliente>${data.clientViaexpress}</IdCliente>
<Usuario>${data.userViaexpress}</Usuario>
<Password>${data.passwordViaexpress}</Password>
</ObjetoLogin>
</ObjetoEnvio>
</PutExpedicionInternacional>
</soap12:Body>
</soap12:Envelope>`;
try {
const response = await axios.post(`${data.url}ServicioVxClientes.asmx`, xmlData, {
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) {
throw Error(error.response.data);
}
};
};