diff --git a/back/methods/mrw-config/renderer.js b/back/methods/mrw-config/renderer.js new file mode 100644 index 000000000..33ac5cdd0 --- /dev/null +++ b/back/methods/mrw-config/renderer.js @@ -0,0 +1,59 @@ +const fs = require('fs'); +const ejs = require('ejs'); + +module.exports = Self => { + Self.remoteMethod('renderer', { + description: 'Renders the data from an XML', + accessType: 'READ', + accepts: [{ + arg: 'expeditionFk', + type: 'number', + required: true + }], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/renderer`, + verb: 'GET' + } + }); + + Self.renderer = async expeditionFk => { + const mrw = await Self.app.models.MrwConfig.findOne(); + + const [expedition] = await Self.rawSql( + `SELECT CASE co.code + WHEN 'ES' THEN a.postalCode + WHEN 'PT' THEN LEFT(a.postalCode, 4) + WHEN 'AD' THEN REPLACE(a.postalCode, 'AD', '00') + END postalCode, + a.city, + a.street, + co.code countryCode, + c.fi, + c.name clientName, + c.phone, + e.created, + e.id expeditionId, + LPAD(ms.serviceType, 4 ,'0') serviceType, + pa.height, + pa.depth, + pa.width + FROM expedition e + JOIN packaging pa ON pa.id = e.packagingFk + JOIN ticket t ON e.ticketFk = t.id + JOIN agencyMode am ON am.id = t.agencyModeFk + JOIN mrwService ms ON ms.agencyModeCodeFk = am.code + JOIN client c ON t.clientFk = c.id + JOIN address a ON t.addressFk = a.id + JOIN province p ON a.provinceFk = p.id + JOIN country co ON co.id = p.countryFk + WHERE e.id = ?`, [expeditionFk] + ); + + const template = fs.readFileSync(__dirname + '/template.ejs', 'utf-8'); + return ejs.render(template, {mrw, expedition}); + }; +}; diff --git a/back/methods/mrw-config/shipmentCreation.js b/back/methods/mrw-config/shipmentCreation.js new file mode 100644 index 000000000..c75fb793d --- /dev/null +++ b/back/methods/mrw-config/shipmentCreation.js @@ -0,0 +1,46 @@ +const axios = require('axios'); +const {DOMParser} = require('xmldom'); + +module.exports = Self => { + Self.remoteMethod('shipmentCreation', { + description: 'Create an expedition and return a label', + accessType: 'WRITE', + accepts: [{ + arg: 'expeditionFk', + type: 'number', + required: true + }], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/shipmentCreation`, + verb: 'POST' + } + }); + + Self.shipmentCreation = async expeditionFk => { + const models = Self.app.models; + + const MrwConfig = await models.MrwConfig.findOne({ + fields: ['url'] + }); + + const renderedXml = await models.MrwConfig.renderer(expeditionFk); + const response = await axios.post(MrwConfig.url, renderedXml, { + headers: { + 'Content-Type': 'application/soap+xml; charset=utf-8' + } + }); + + const xmlString = response.data; + const parser = new DOMParser(); + const xmlDoc = parser.parseFromString(xmlString, 'text/xml'); + const transmEnvioResponse = xmlDoc.getElementsByTagName('TransmEnvioResponse')[0]; + console.log(xmlDoc.getElementsByTagName('TransmEnvioResponse')); + console.log('transmEnvioResponse: ', transmEnvioResponse); + + return transmEnvioResponse.textContent; + }; +}; diff --git a/back/methods/mrw-config/template.ejs b/back/methods/mrw-config/template.ejs new file mode 100644 index 000000000..1641c9ec2 --- /dev/null +++ b/back/methods/mrw-config/template.ejs @@ -0,0 +1,40 @@ + + + + <%= mrw.franchiseCode %> + <%= mrw.subscriberCode %> + <%= mrw.user %> + <%= mrw.password %> + + + + + + + + <%= expedition.street %> + <%= expedition.postalCode %> + <%= expedition.city %> + <%= expedition.countryCode %> + + <%= expedition.fi %> + <%= expedition.clientName %> + <%= expedition.phone %> + + + <%= expedition.created %> + <%= expedition.expeditionId %> + <%= expedition.serviceType %> + + + <%= expedition.height %> + <%= expedition.depth %> + <%= expedition.width %> + + + <%= expedition.kg %> + + + + + \ No newline at end of file diff --git a/back/model-config.json b/back/model-config.json index ebc0e321b..b501680d0 100644 --- a/back/model-config.json +++ b/back/model-config.json @@ -156,6 +156,9 @@ }, "ViaexpressConfig": { "dataSource": "vn" + }, + "MrwConfig": { + "dataSource": "vn" } } diff --git a/back/models/mrw-config.js b/back/models/mrw-config.js new file mode 100644 index 000000000..ccc0ca3ac --- /dev/null +++ b/back/models/mrw-config.js @@ -0,0 +1,4 @@ +module.exports = Self => { + require('../methods/mrw-config/shipmentCreation')(Self); + require('../methods/mrw-config/renderer')(Self); +}; diff --git a/back/models/mrw-config.json b/back/models/mrw-config.json new file mode 100644 index 000000000..39e5f4318 --- /dev/null +++ b/back/models/mrw-config.json @@ -0,0 +1,28 @@ +{ + "name": "MrwConfig", + "base": "VnModel", + "options": { + "mysql": { + "table": "mrwConfig" + } + }, + "properties": { + "id": { + "type": "number", + "required": true + }, + "url": { + "type": "string", + "required": true + }, + "user": { + "type": "string" + }, + "franchiseCode": { + "type": "string" + }, + "subscriberCode": { + "type": "string" + } + } +} diff --git a/db/changes/240401/00-mrwWebService.sql b/db/changes/240401/00-mrwWebService.sql new file mode 100644 index 000000000..9a802104f --- /dev/null +++ b/db/changes/240401/00-mrwWebService.sql @@ -0,0 +1,35 @@ +CREATE TABLE IF NOT EXISTS vn.mrwConfig ( + id INT auto_increment NULL, + url varchar(100) NULL, + `user` varchar(100) NULL, + password varchar(100) NULL, + franchiseCode varchar(100) NULL, + subscriberCode varchar(100) NULL, + CONSTRAINT mrwConfig_pk PRIMARY KEY (id) +) +ENGINE=InnoDB +DEFAULT CHARSET=utf8mb3 +COLLATE=utf8mb3_unicode_ci; + + +INSERT INTO vn.mrwConfig (url, `user`,password,franchiseCode,subscriberCode) + VALUES ('https://sagec-test.mrw.es/MRWEnvio.asmx', '04301SGVERDNATURA', 'Verdnatura@4301V', '009731', '04301'); + +INSERT INTO salix.ACL (model,property,accessType,permission,principalType,principalId) + VALUES ('MrwConfig','renderer','READ','ALLOW','ROLE','employee'); + +INSERT INTO salix.ACL (model,property,accessType,permission,principalType,principalId) + VALUES ('MrwConfig','shipmentCreation','WRITE','ALLOW','ROLE','employee'); + + +INSERT INTO vn.agency (name,warehouseFk,warehouseAliasFk,isOwn,isAnyVolumeAllowed) + VALUES ('MRW',1,1,0,0); + +INSERT INTO vn.agencyMode (id, name, description, deliveryMethodFk, m3, web, agencyFk, inflation, isVolumetric, reportMail, showAgencyName, isActive, isExternalAgency, flag, code, isRiskFree, hasWeightVolumetric) + VALUES(25, 'MRW', NULL, NULL, 0.0, 0, 11, 0.00, 0, NULL, 1, 1, 0, NULL, 'MRW', 0, 0); + +INSERT INTO vn.ticket (id, clientFk, warehouseFk, shipped, nickname, refFk, addressFk, workerFk, observations, isSigned, isLabeled, isPrinted, packages, location, `hour`, created, isBlocked, solution, routeFk, priority, hasPriority, companyFk, agencyModeFk, landed, isBoxed, isDeleted, zoneFk, zonePrice, zoneBonus, totalWithVat, totalWithoutVat, weight, clonedFrom, cmrFk, editorFk) + VALUES(33, 1101, 1, '2001-01-01 00:00:00.000', 'MRW', NULL, 1, NULL, NULL, 0, 0, 0, 1, NULL, 0, '2001-01-01 00:00:00.000', 1, NULL, 6, NULL, 1, 442, 25, '2001-01-02', 0, 0, 3, 5.00, 1.00, 8.88, 8.07, NULL, NULL, NULL, 100); + +INSERT INTO vn.expedition (agencyModeFk,ticketFk,freightItemFk,created,counter,workerFk,packagingFk,hostFk,stateTypeFk,hasNewRoute,isBox,editorFk) + VALUES (25,33,71,'2001-01-01 01:00:00.000',1,18,'94','',3,0,71,100);