2024-02-07 15:52:43 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2024-01-15 14:00:36 +00:00
|
|
|
|
|
|
|
module.exports = Self => {
|
2024-02-16 13:15:13 +00:00
|
|
|
Self.remoteMethod('createShipment', {
|
2024-02-19 11:54:24 +00:00
|
|
|
description: 'Create an expedition and return a base64Binary label from de MRW WebService',
|
2024-01-15 14:00:36 +00:00
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'expeditionFk',
|
|
|
|
type: 'number',
|
|
|
|
required: true
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: ['object'],
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
2024-01-18 06:36:21 +00:00
|
|
|
path: `/createShipment`,
|
2024-01-15 14:00:36 +00:00
|
|
|
verb: 'POST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-06-10 10:24:23 +00:00
|
|
|
Self.createShipment = async expeditionFk => {
|
2024-06-14 15:40:08 +00:00
|
|
|
const models = Self.app.models;
|
2024-06-10 10:24:23 +00:00
|
|
|
const mrw = await Self.getConfig();
|
2024-07-10 08:41:52 +00:00
|
|
|
const clientType = await models.MrwConfig.getClientType(expeditionFk);
|
2024-02-16 07:28:40 +00:00
|
|
|
|
2024-06-10 10:24:23 +00:00
|
|
|
const today = Date.vnNew();
|
|
|
|
const [hours, minutes] = mrw?.expeditionDeadLine ? mrw.expeditionDeadLine.split(':').map(Number) : [0, 0];
|
2024-02-16 13:15:13 +00:00
|
|
|
|
2024-06-10 10:24:23 +00:00
|
|
|
const deadLine = Date.vnNew();
|
|
|
|
deadLine.setHours(hours, minutes, 0);
|
2024-01-15 14:00:36 +00:00
|
|
|
|
2024-06-10 10:24:23 +00:00
|
|
|
if (today > deadLine && (!mrw.notified || mrw.notified.setHours(0, 0, 0, 0) !== today.setHours(0, 0, 0, 0))) {
|
2024-06-14 15:40:08 +00:00
|
|
|
await models.NotificationQueue.create({notificationFk: 'mrw-deadline'});
|
2024-06-10 10:24:23 +00:00
|
|
|
await mrw.updateAttributes({notified: Date.vnNow()});
|
|
|
|
}
|
2024-02-07 15:52:43 +00:00
|
|
|
|
2024-02-16 13:15:13 +00:00
|
|
|
const query =
|
2024-05-20 10:14:59 +00:00
|
|
|
`SELECT
|
|
|
|
CASE co.code
|
2024-02-16 13:15:13 +00:00
|
|
|
WHEN 'ES' THEN a.postalCode
|
2024-05-31 09:51:32 +00:00
|
|
|
WHEN 'PT' THEN LEFT(a.postalCode, mc.portugalPostCodeTrim)
|
2024-02-16 13:15:13 +00:00
|
|
|
WHEN 'AD' THEN REPLACE(a.postalCode, 'AD', '00')
|
|
|
|
END postalCode,
|
|
|
|
a.city,
|
|
|
|
a.street,
|
|
|
|
co.code countryCode,
|
|
|
|
c.fi,
|
|
|
|
c.name clientName,
|
|
|
|
c.phone,
|
|
|
|
DATE_FORMAT(t.shipped, '%d/%m/%Y') created,
|
|
|
|
t.shipped,
|
2024-05-31 09:51:32 +00:00
|
|
|
CONCAT( e.ticketFk, LPAD(e.counter, mc.counterWidth, '0')) reference,
|
2024-07-08 08:50:15 +00:00
|
|
|
LPAD(IF(mw.serviceType IS NULL, ms.serviceType, mw.serviceType), mc.serviceTypeWidth, '0') serviceType,
|
2024-05-31 09:51:32 +00:00
|
|
|
IF(mw.weekdays, 'S', 'N') weekDays,
|
2024-07-10 08:41:52 +00:00
|
|
|
oa.description deliveryObservation
|
2024-02-16 13:15:13 +00:00
|
|
|
FROM expedition e
|
|
|
|
JOIN ticket t ON e.ticketFk = t.id
|
|
|
|
JOIN agencyMode am ON am.id = t.agencyModeFk
|
|
|
|
JOIN mrwService ms ON ms.agencyModeCodeFk = am.code
|
2024-06-10 10:24:23 +00:00
|
|
|
LEFT JOIN mrwServiceWeekday mw ON mw.agencyModeCodeFk = am.code
|
|
|
|
AND mw.weekDays & (1 << WEEKDAY(t.landed))
|
2024-02-16 13:15:13 +00:00
|
|
|
JOIN client c ON t.clientFk = c.id
|
|
|
|
JOIN address a ON t.addressFk = a.id
|
2024-05-31 09:51:32 +00:00
|
|
|
LEFT JOIN addressObservation oa ON oa.addressFk = a.id
|
|
|
|
LEFT JOIN observationType ot ON ot.id = oa.observationTypeFk
|
|
|
|
AND ot.code = 'delivery'
|
2024-02-16 13:15:13 +00:00
|
|
|
JOIN province p ON a.provinceFk = p.id
|
|
|
|
JOIN country co ON co.id = p.countryFk
|
2024-05-31 09:51:32 +00:00
|
|
|
JOIN mrwConfig mc
|
2024-02-16 13:15:13 +00:00
|
|
|
WHERE e.id = ?
|
|
|
|
LIMIT 1`;
|
|
|
|
|
2024-06-10 10:24:23 +00:00
|
|
|
const [expeditionData] = await Self.rawSql(query, [expeditionFk]);
|
2024-02-16 13:15:13 +00:00
|
|
|
|
2024-06-10 10:24:23 +00:00
|
|
|
if (expeditionData?.shipped.setHours(0, 0, 0, 0) < today.setHours(0, 0, 0, 0))
|
2024-02-16 13:15:13 +00:00
|
|
|
throw new UserError(`This ticket has a shipped date earlier than today`);
|
2024-02-16 07:28:40 +00:00
|
|
|
|
2024-06-10 10:24:23 +00:00
|
|
|
const shipmentResponse = await Self.sendXmlDoc(
|
|
|
|
__dirname + `/createShipment.ejs`,
|
2024-07-10 08:41:52 +00:00
|
|
|
{mrw, expeditionData, clientType},
|
2024-06-10 10:24:23 +00:00
|
|
|
'application/soap+xml'
|
|
|
|
);
|
|
|
|
const shipmentId = Self.getTextByTag(shipmentResponse, 'NumeroEnvio');
|
2024-02-16 07:28:40 +00:00
|
|
|
|
2024-06-14 15:40:08 +00:00
|
|
|
if (!shipmentId) throw new UserError(Self.getTextByTag(shipmentResponse, 'Mensaje'));
|
2024-02-16 07:28:40 +00:00
|
|
|
|
2024-07-10 08:41:52 +00:00
|
|
|
const file = await models.MrwConfig.getLabel(shipmentId, clientType);
|
2024-05-20 11:58:32 +00:00
|
|
|
|
|
|
|
return {shipmentId, file};
|
2024-02-16 07:28:40 +00:00
|
|
|
};
|
2024-01-15 14:00:36 +00:00
|
|
|
};
|