refs #4734 feat: xml en otro archivo
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Vicent Llopis 2023-06-19 14:27:24 +02:00
parent 303be74382
commit cfe340eeff
5 changed files with 285 additions and 163 deletions

View File

@ -1,4 +1,5 @@
const axios = require('axios');
const fs = require('fs');
module.exports = Self => {
Self.remoteMethod('internationalExpedition', {
@ -19,173 +20,78 @@ module.exports = Self => {
}
});
Self.internationalExpedition = async(expeditionFk, options) => {
Self.internationalExpedition = async expeditionFk => {
const models = Self.app.models;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
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);
const viaexpressConfig = await models.ViaexpressConfig.findOne({
fields: ['url', 'clientViaexpress', 'userViaexpress', 'passwordViaexpress', 'defaultWeight', 'deliveryType']
}, myOptions);
fields: ['url']
});
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}`;
const renderedXml = await models.ViaexpressConfig.renderer(expeditionFk);
// const renderedXml = `<?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>10</Peso>
// <Bultos>1</Bultos>
// <Reembolso>0</Reembolso>
// <Fecha>2023-06-12</Fecha>
// <ConRetorno>0</ConRetorno>
// <Tipo>E</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>VERDNATURA LEVANTE SL</RazonSocial>
// <Domicilio>FENOLLARS, 2</Domicilio>
// <Cpostal>46680</Cpostal>
// <Poblacion>Algemesi</Poblacion>
// <Provincia>Valencia</Provincia>
// <Contacto></Contacto>
// <Telefono>963677177</Telefono>
// <Email>pako.natek@gmail.com</Email>
// </ObjetoRemitente>
// <ObjetoDestinatario>
// <RazonSocial>ROSAMARY FLORISTERIA</RazonSocial>
// <Domicilio>C SANTA ROSA,25</Domicilio>
// <Cpostal>03802</Cpostal>
// <Poblacion>ALCOY</Poblacion>
// <Municipio>ALCOY</Municipio>
// <Provincia>Alicante</Provincia>
// <Contacto></Contacto>
// <Telefono>653967489</Telefono>
// <Email>correo@floristeriarosamary.com</Email>
// <Pais>ES</Pais>
// </ObjetoDestinatario>
// <ObjetoLogin>
// <IdCliente>16092</IdCliente>
// <Usuario>B97367486</Usuario>
// <Password>VERDNA22</Password>
// </ObjetoLogin>
// </ObjetoEnvio>
// </PutExpedicionInternacional>
// </soap12:Body>
// </soap12:Envelope>`;
const response = await axios.post(`${viaexpressConfig.url}ServicioVxClientes.asmx`, renderedXml, {
headers: {
'Content-Type': 'application/soap+xml; charset=utf-8'
}
});
console.log(response);
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);
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>${viaexpressConfig.defaultWeight}</Peso>
<Bultos>1</Bultos>
<Reembolso>0</Reembolso>
<Fecha>${date}</Fecha>
<ConRetorno>0</ConRetorno>
<Tipo>${viaexpressConfig.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>${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>
<Contacto></Contacto>
<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>
</ObjetoRemitente>
<ObjetoDestinatario>
<RazonSocial>${expedition.ticket().address().nickname}</RazonSocial>
<Domicilio>${expedition.ticket().address().street}</Domicilio>
<Cpostal>${expedition.ticket().address().postalCode}</Cpostal>
<Poblacion>${expedition.ticket().address().city}</Poblacion>
<Municipio></Municipio>
<Provincia>${expedition.ticket().address().province().name}</Provincia>
<Contacto></Contacto>
<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>
</ObjetoDestinatario>
<ObjetoLogin>
<IdCliente>${viaexpressConfig.clientViaexpress}</IdCliente>
<Usuario>${viaexpressConfig.userViaexpress}</Usuario>
<Password>${viaexpressConfig.passwordViaexpress}</Password>
</ObjetoLogin>
</ObjetoEnvio>
</PutExpedicionInternacional>
</soap12:Body>
</soap12:Envelope>`;
try {
const response = await axios.post(`${viaexpressConfig.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) {
if (error?.response?.data) throw Error(error.response.data);
throw Error(error);
}
return referenciaVx;
};
};

View File

@ -0,0 +1,162 @@
const fs = require('fs');
const handlebars = require('handlebars');
module.exports = Self => {
Self.remoteMethod('renderer', {
description: 'Returns the lastest campaigns',
accessType: 'READ',
accepts: [{
arg: 'expeditionFk',
type: 'number',
required: true
}],
returns: {
type: ['object'],
root: true
},
http: {
path: `/renderer`,
verb: 'GET'
}
});
Self.renderer = async expeditionFk => {
const models = Self.app.models;
const viaexpressConfig = await models.ViaexpressConfig.findOne({
fields: ['clientViaexpress', 'userViaexpress', 'passwordViaexpress', 'defaultWeight', 'deliveryType']
});
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']
}
}
}
}
}
}
}
}
]
}
}
]
});
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}`;
const data = {
defaultWeight: viaexpressConfig.defaultWeight,
date: date, // const date = new Date().toISOString();
deliveryType: viaexpressConfig.deliveryType,
senderName: expedition.ticket().company().client().socialName,
senderStreet: expedition.ticket().company().client().defaultAddress().street,
senderPostalCode: expedition.ticket().company().client().defaultAddress().postalCode,
senderCity: expedition.ticket().company().client().defaultAddress().city,
senderProvince: expedition.ticket().company().client().defaultAddress().province().name,
senderPhone: expedition.ticket().company().client().defaultAddress().mobile
|| expedition.ticket().company().client().defaultAddress().phone
|| expedition.ticket().company().client().mobile
|| expedition.ticket().company().client().phone,
senderEmail: expedition.ticket().company().client().email,
receiverName: expedition.ticket().address().nickname,
receiverStreet: expedition.ticket().address().street,
receiverPostalCode: expedition.ticket().address().postalCode,
receiverCity: expedition.ticket().address().city,
receiverProvince: expedition.ticket().address().province().name,
receiverPhone: expedition.ticket().address().mobile
|| expedition.ticket().address().phone
|| expedition.ticket().client().mobile
|| expedition.ticket().client().phone,
receiverEmail: expedition.ticket().client().email,
receiverCountry: expedition.ticket().address().province().country().code,
clientViaexpress: viaexpressConfig.clientViaexpress,
userViaexpress: viaexpressConfig.userViaexpress,
passwordViaexpress: viaexpressConfig.passwordViaexpress
};
const templateXml = fs.readFileSync(__dirname + '/template.xml', 'utf-8');
// // Crea una instancia de Vue con los datos
// const vueInstance = new Vue({
// data: data,
// template: templateXml,
// });
// // Renderiza la plantilla con los datos utilizando vue-server-renderer
// const renderer = createRenderer();
// return renderer.renderToString(vueInstance);
const compiledTemplate = handlebars.compile(templateXml);
const renderedHtml = compiledTemplate(data);
return renderedHtml;
};
};

View File

@ -0,0 +1,52 @@
<?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>{{ defaultWeight }}</Peso>
<Bultos>1</Bultos>
<Reembolso>0</Reembolso>
<Fecha>{{ date }}</Fecha>
<ConRetorno>0</ConRetorno>
<Tipo>{{ 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>{{ senderName }}</RazonSocial>
<Domicilio>{{ senderStreet }}</Domicilio>
<Cpostal>{{ senderPostalCode }}</Cpostal>
<Poblacion>{{ senderCity }}</Poblacion>
<Provincia>{{ senderProvince }}</Provincia>
<Contacto></Contacto>
<Telefono>{{ senderPhone }}</Telefono>
<Email>{{ senderEmail }}</Email>
</ObjetoRemitente>
<ObjetoDestinatario>
<RazonSocial>{{ receiverName }}</RazonSocial>
<Domicilio>{{ receiverStreet }}</Domicilio>
<Cpostal>{{ receiverPostalCode }}</Cpostal>
<Poblacion>{{ receiverCity }}</Poblacion>
<Municipio></Municipio>
<Provincia>{{ receiverProvince }}</Provincia>
<Contacto></Contacto>
<Telefono>{{ receiverPhone }}</Telefono>
<Email>{{ receiverEmail }}</Email>
<Pais>{{ receiverCountry }}</Pais>
</ObjetoDestinatario>
<ObjetoLogin>
<IdCliente>{{ clientViaexpress }}</IdCliente>
<Usuario>{{ userViaexpress }}</Usuario>
<Password>{{ passwordViaexpress }}</Password>
</ObjetoLogin>
</ObjetoEnvio>
</PutExpedicionInternacional>
</soap12:Body>
</soap12:Envelope>

View File

@ -1,3 +1,4 @@
module.exports = Self => {
require('../methods/viaexpress-config/internationalExpedition')(Self);
require('../methods/viaexpress-config/renderer')(Self);
};

View File

@ -22,6 +22,7 @@
"ftps": "^1.2.0",
"gm": "^1.25.0",
"got": "^10.7.0",
"handlebars": "^4.7.7",
"helmet": "^3.21.2",
"i18n": "^0.8.4",
"image-type": "^4.1.0",