-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
1415 lines (1302 loc) · 45.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const fs = require('fs');
const readLine = require('readline');
const moment = require('moment');
const {
equipmentStatus,
containerIsoCode,
harmonizedSystemCodes,
codeListDangerousGoods
} = require('./dictionary');
const patchFile = process.argv[2];
let filePath = 'demo_v22.edi';
if (patchFile) {
filePath = patchFile;
}
const lineReader = readLine.createInterface({
// input: fs.createReadStream('DEMO_BAPLIE_22.edi'),
input: fs.createReadStream(filePath)
// input: fs.createReadStream('demo_v15.edi'),
});
const INTERCHANGE_HEADER = 'UNB';
const MESSAGE_HEADER = 'UNH';
const BEGINNING_OF_MESSAGE = 'BGM';
const DATE_TIME_PERIOD = 'DTM';
const DETAILS_OF_TRANSPORT = 'TDT';
const PLACE_LOCATION_IDENTIFICATION = 'LOC';
const GOODS_ITEM_DETAILS = 'GID';
const NATURE_OF_CARGO = 'GDS';
const FREE_TEXT = 'FTX';
const MEASUREMENT = 'MEA';
const DIMENSIONS = 'DIM';
const TEMPERATURE = 'TMP';
const RANGE_DETAILS = 'RNG';
const REFERENCE = 'RFF';
const EQUIPMENT_DETAILS = 'EQD';
const EQUIPMENT_ATTACHED = 'EQA';
const NAME_AND_ADDRESS = 'NAD';
const DANGEROUS_GOODS = 'DGS';
const SMDG_VERSION_15 = 'SMDG15';
const SMDG_VERSION_22 = 'SMDG22';
const containerIsoCodeJson = containerIsoCode;
const equipmentStatusJson = equipmentStatus;
const codeListDangerousGoodsJson = codeListDangerousGoods;
const harmonizedSystemCodesJson = harmonizedSystemCodes;
let data = {};
let parseEdiStatus = {
status: true,
message: ''
};
// Group 1 -> Group 2 (Group 3 -> Group 4)
// Group 1: TDT - LOC - DTM - RFF - FTX
let group1 = false;
// Group 2: LOC - GID - GDS - FTX - MEA - DIM - TMP - RNG - LOC - RFF - grp3 - grp4
let group2 = false;
// Group 3: EQD - EQA - NAD
let group3 = false;
// Group 4:
let group4 = false;
let containerId = 0;
let hasFurtherDetails = false;
let equipmentAttached = [];
let associationAssignedCode = '';
let shouldAddBreakLine = false;
const hrstart = process.hrtime();
lineReader.on('line', line => {
const checkLines = line.split("'");
if (checkLines.length > 2 && checkLines[1].trim()) {
shouldAddBreakLine = true;
}
if (shouldAddBreakLine) {
checkLines.map(newLine => {
processDataLine(newLine);
});
} else {
processDataLine(line);
}
if (!parseEdiStatus.status) {
lineReader.close();
}
});
lineReader.on('close', () => {
if (data.containers) {
data.totalGoodContainers = data.containers.length;
} else {
data.totalGoodContainers = 0;
}
if (data.badContainers) {
data.totalBadContainers = data.badContainers.length;
} else {
data.totalBadContainers = 0;
}
data.totalContainers = data.totalGoodContainers + data.totalBadContainers;
console.log(
'parse completed with total containers: ',
JSON.stringify(data.totalContainers)
);
console.log('parseEdiStatus: ', JSON.stringify(parseEdiStatus));
fs.writeFile('ediParsed.json', JSON.stringify(data), err => {
// In case of a error throw err.
if (err) throw err;
});
const hrend = process.hrtime(hrstart);
console.info('Execution time (hr): %ds %dms', hrend[0], hrend[1] / 1000000);
});
const processDataLine = line => {
processInterchangeHeader(line);
processMessageHeader(line);
processBeginningOfMessage(line);
processDateTimePeriod(line);
processDetailsOfTransport(line);
processContainers(line);
};
const processInterchangeHeader = line => {
if (line.includes(INTERCHANGE_HEADER)) {
const regex = new RegExp(/(UNOA:)(\d)(\W*)(\w*)(\W*)(\w*)(\W*)(\d*):(\d*)/);
// Syntax Version Number
try {
const ediVersion = line.match(regex)[2];
const senderId = line.match(regex)[4];
const recipientId = line.match(regex)[6];
const dateOfPreparation = line.match(regex)[8];
const timeOfPreparation = line.match(regex)[9];
const year = dateOfPreparation.substring(0, 2);
const month = dateOfPreparation.substring(2, 4);
const day = dateOfPreparation.substring(4, 6);
const hour = timeOfPreparation.substring(0, 2);
const minute = timeOfPreparation.substring(2, 4);
const preparationAt = moment(
`20${year}-${month}-${day} ${hour}:${minute}`
).format('YYYYMMDDHHMMSS');
data.interchangeHeader = {
version: ediVersion,
senderId,
recipientId,
preparationAt,
data: line
};
} catch (error) {
parseEdiStatus.status = false;
parseEdiStatus.message =
'Can not convert InterchangeHeader. Line: ' + line;
}
}
};
const processMessageHeader = line => {
if (line.includes(MESSAGE_HEADER)) {
const regex = new RegExp(/SMDG\d+/);
// This will enable the recipient of the message to translate the message
// correctly, even if older versions are still in use.
try {
const result = line.match(regex);
if (result) {
associationAssignedCode = result[0];
} else {
associationAssignedCode = SMDG_VERSION_15;
}
data.messageHeader = {
associationAssignedCode: associationAssignedCode,
data: line
};
} catch (error) {
parseEdiStatus.status = false;
parseEdiStatus.message = 'Can not convert MessageHeader. Line: ' + line;
}
}
};
const processBeginningOfMessage = line => {
if (line.includes(BEGINNING_OF_MESSAGE)) {
data.beginningOfMessage = {
data: line
};
}
};
const processDateTimePeriod = line => {
if (line.includes(DATE_TIME_PERIOD)) {
try {
const regex = new RegExp(/(\d*):(\d*):?(\d*)/);
// "178" = actual date/time of arrival at senders port
// "132" = estimated date or date/time of arrival at the next port of call
// "133" = estimated date or date/time of departure at senders port
// "136" = actual date/time of departure at senders port
// "137" = Document/Message Date/Time
const code = line.match(regex)[1];
const dateTime = line.match(regex)[2];
const dateTimeCode = line.match(regex)[3];
let dateTimeFormat = '';
switch (dateTimeCode) {
case '101':
dateTimeFormat = 'YYMMDD';
break;
case '201':
dateTimeFormat = 'YYMMDDHHMM';
break;
case '301':
// ("ZZZ" = Time zone, e.g. "GMT" or other)
dateTimeFormat = 'YYMMDDHHMMZZZ';
break;
default:
dateTimeFormat = 'YYYYMMDDHHMMSS';
break;
}
switch (code) {
case '178':
data.detailsOfTransport.senderPort.time = {
code,
arrivalTime: dateTime,
dateTimeFormat,
data: line
};
group1 = false;
break;
case '132':
data.detailsOfTransport.locNextPortCall.time = {
code,
estimatedArrivalTime: dateTime,
dateTimeFormat,
data: line
};
group1 = false;
break;
case '133':
data.detailsOfTransport.locDeparture.time = {
code,
estimatedDepartureTime: dateTime,
dateTimeFormat,
data: line
};
group1 = false;
break;
case '136':
data.detailsOfTransport.locDeparture.time = {
code,
departureTime: dateTime,
dateTimeFormat,
data: line
};
break;
case '137':
data.dateTimePeriod = {
code,
dateTime,
dateTimeFormat,
data: line
};
break;
default:
// console.log('processDateTimePeriod Need to consider: ', line);
break;
}
} catch (error) {
parseEdiStatus.status = false;
parseEdiStatus.message = 'Can not convert DateTimePeriod. Line: ' + line;
}
}
};
const processLocation = line => {
try {
if (line.includes(PLACE_LOCATION_IDENTIFICATION)) {
let regex;
let locationQualifier = '';
let locationId = '';
let codeListQualifier = '';
let codeListResponsibleAgency = '';
let placeOfDeparture = '';
let nextPortOfCall = '';
if (associationAssignedCode === SMDG_VERSION_22) {
regex = new RegExp(/(LOC)(\W)(\d*)(\W)(\w*):?(\d*)?:?(\d*)?/);
// "139" = Port.
codeListQualifier = line.match(regex)[6];
// "112" = US, US Census Bureau, Schedule D for U S locations, Schedule K for foreign port locations.
// "6" = UN/ECE - United Nations - Economic Commission for Europe. (UN-Locodes).
codeListResponsibleAgency = line.match(regex)[7];
}
if (associationAssignedCode === SMDG_VERSION_15) {
regex = new RegExp(/(LOC)(\W)(\d*)(\W)(\w*):?:?(\w*)?/);
codeListResponsibleAgency = line.match(regex)[6];
}
locationQualifier = line.match(regex)[3];
locationId = line.match(regex)[5];
switch (locationQualifier) {
case '5': // "5" = Place of Departure
placeOfDeparture = locationId;
break;
case '61': // "61" = Next port of call
nextPortOfCall = locationId;
break;
case '147': // "147" = Stowage Cell
group2 = true;
let bay = '000';
let row = '00';
let tier = '00';
let deck = '00';
// 1. ISO-format
// 2. Ro/Ro-format
// 3. Other non-ISO-format (to be agreed between partners)
// * ISO-format:
// Bay/Row/Tier (BBBRRTT). If Bay number is less than 3
// characters it must be filled with leading zeroes, e.g. "0340210".
// * Ro/Ro-format:
// Deck/Bay/Row/Tier (DDBBBRRTT).
const containerLocation = locationId;
const container = {};
containerId++;
container.id = containerId;
switch (codeListResponsibleAgency) {
case '5': // (ISO-format)
const bayRowTierRegex = new RegExp(/(\d{3})(\d{2})(\d{2})/);
bay = containerLocation.match(bayRowTierRegex)[1];
row = containerLocation.match(bayRowTierRegex)[2];
tier = containerLocation.match(bayRowTierRegex)[3];
container.bay = bay;
container.row = row;
container.tier = tier;
if (parseInt(tier, 10) % 2 === 0) {
data.containers.push(container);
} else {
data.badContainers.push(container);
}
break;
case '87': // (Ro/Ro-format, assigned by the Carrier)
const deckBayRowTierRegex = new RegExp(
/(\d{2})(\d{3})(\d{2})(\d{2})/
);
deck = containerLocation.match(deckBayRowTierRegex)[1];
bay = containerLocation.match(deckBayRowTierRegex)[2];
row = containerLocation.match(deckBayRowTierRegex)[3];
tier = containerLocation.match(deckBayRowTierRegex)[4];
container.deck = deck;
container.bay = bay;
container.row = row;
container.tier = tier;
if (parseInt(tier, 10) % 2 === 0) {
data.containers.push(container);
} else {
data.badContainers.push(container);
}
break;
case 'ZZZ': // (non-ISO-format, mutually defined)
break;
default:
// console.log('processLocation Need to consider: ', line);
break;
}
break;
default:
break;
}
if (group1) {
if (!data.detailsOfTransport.senderPort) {
data.detailsOfTransport.senderPort = {};
}
if (!data.detailsOfTransport.locDeparture) {
data.detailsOfTransport.locDeparture = {};
}
if (!data.detailsOfTransport.locNextPortCall) {
data.detailsOfTransport.locNextPortCall = {};
}
}
if (placeOfDeparture) {
data.detailsOfTransport.locDeparture = {
locationQualifier,
placeOfDeparture,
codeListQualifier,
codeListResponsibleAgency,
data: line
};
}
if (nextPortOfCall) {
data.detailsOfTransport.locNextPortCall = {
locationQualifier,
nextPortOfCall,
codeListQualifier,
codeListResponsibleAgency,
data: line
};
}
}
} catch (error) {
console.log(
'TCL: ConvertEdiToJson -> privateprocessLocation -> error',
error
);
parseEdiStatus.status = false;
parseEdiStatus.message = 'Can not convert Location. Line: ' + line;
}
};
const processDetailsOfTransport = line => {
const lineToMatch = line.split('+')[0];
if (lineToMatch === DETAILS_OF_TRANSPORT) {
try {
group1 = true;
let regex;
let transportStateQualifier = '';
let conveyanceReferenceNumber = '';
let carrierIdentificationCodeOrName = '';
let codeListQualifier1 = '';
let codeListResponsibleAgency1 = '';
let transportId = '';
let codeListQualifier2 = '';
let codeListResponsibleAgency2 = '';
let vesselIdOrName = '';
if (associationAssignedCode === SMDG_VERSION_22) {
regex = new RegExp(
/(TDT)(\W)(\d*)(\W)(\w*)(\W*)([\w\s]*):(\d*):(\d*)(\W*)(\w*):(\d*):(\w*):((\w|\W[^'])*)/
);
// Code "20" (Main Carriage)
transportStateQualifier = line.match(regex)[3];
// Discharge voyage number as assigned by the Operating Carrier or his agent.
// The trade route could be included in this voyage number, if required.
conveyanceReferenceNumber = line.match(regex)[5];
carrierIdentificationCodeOrName = line.match(regex)[7];
// Code List Qualifier: Code "172" (Carrier Code)
codeListQualifier1 = line.match(regex)[8];
// "20" = BIC (Bureau International des Containers)
// "166" = US National Motor Freight Classification Association (SCAC)
// "ZZZ" = Mutually defined.
codeListResponsibleAgency1 = line.match(regex)[9];
// Id of Means of Transport Identification. Is one of following:
// 1. Lloyd’s Code (IMO number)
// 2. Call Sign
// 3. Mutually agreed vessel code
transportId = line.match(regex)[11];
// Code List Qualifier: Allowed qualifiers:
// "103" = Call Sign Directory
// "146" = Means of Transport Identification (Lloyd's Code or IMO number)
// "ZZZ" = Mutually defined or IMO number
codeListQualifier2 = line.match(regex)[12];
// "11" = Lloyd's register of shipping. Only to be used when Lloyd's Code is used for vessel/barge identification
// "ZZZ" = Mutually defined. To be used in all other cases.
codeListResponsibleAgency2 = line.match(regex)[13];
// Id. of means of transport: Vessel name, if required
vesselIdOrName = line.match(regex)[14];
}
if (associationAssignedCode === SMDG_VERSION_15) {
regex = new RegExp(
/(TDT)(\W)(\d*)(\W)(\w*)(\W*)(\w*):(\d*):?:?([\w\s-_]*)?:?(\w*)(\W*)(\w*):(\d*):(\d*)/
);
// Code "20" (Main Carriage)
transportStateQualifier = line.match(regex)[3];
// Discharge voyage number as assigned by the Operating Carrier or his agent.
// The trade route could be included in this voyage number, if required.
conveyanceReferenceNumber = line.match(regex)[5];
carrierIdentificationCodeOrName = line.match(regex)[12];
// Code List Qualifier: Code "172" (Carrier Code)
codeListQualifier1 = line.match(regex)[13];
// "20" = BIC (Bureau International des Containers)
// "166" = US National Motor Freight Classification Association (SCAC)
// "ZZZ" = Mutually defined.
codeListResponsibleAgency1 = line.match(regex)[14];
// Id of Means of Transport Identification. Is one of following:
// 1. Lloyd’s Code (IMO number)
// 2. Call Sign
// 3. Mutually agreed vessel code
transportId = line.match(regex)[7];
// Code List Qualifier: Allowed qualifiers:
// "103" = Call Sign Directory
// "146" = Means of Transport Identification (Lloyd's Code or IMO number)
// "ZZZ" = Mutually defined or IMO number
codeListQualifier2 = line.match(regex)[8];
// Id. of means of transport: Vessel name, if required
vesselIdOrName = line.match(regex)[9];
}
let callSign = '';
let imo = '';
let mutuallyAgreedVesselCode = '';
switch (codeListQualifier2) {
case '103':
callSign = transportId;
break;
case '146':
imo = transportId;
break;
case 'ZZZ':
mutuallyAgreedVesselCode = transportId;
break;
default:
// console.log('processDetailsOfTransport Need to consider: ', line);
break;
}
data.detailsOfTransport = {
transportStateQualifier,
voyageOrLeg: conveyanceReferenceNumber,
carrierIdentificationCodeOrName,
codeListQualifier1,
codeListResponsibleAgency1,
callSign,
imo,
mutuallyAgreedVesselCode,
codeListResponsibleAgency2,
vesselIdOrName,
data: line
};
data.containers = [];
data.badContainers = [];
} catch (error) {
parseEdiStatus.status = false;
console.log('data', data);
parseEdiStatus.message =
'Can not convert DetailsOfTransport. Line: ' + line;
}
}
if (group1) {
processLocation(line);
processDateTimePeriod(line);
}
};
// LOC - GID - GDS - FTX - MEA - DIM - TMP - RNG - LOC - RFF - grp3 - grp4
const processContainers = line => {
// grp2
processLocation(line);
processGoodItemDetail(line);
processNatureOfCargo(line);
if (hasFurtherDetails) {
processFreeText(line);
}
processMeasurement(line);
processDimensions(line);
processTemperature(line);
processRangeDetail(line);
processLocationForContainer(line);
processReference(line);
// grp3
processEquipmentDetails(line);
processEquipmentAttached(line);
processNameAndAddress(line);
// grp4
processDangerousGoods(line);
processFreeTextGroup4(line);
};
const processGoodItemDetail = line => {
if (line.includes(GOODS_ITEM_DETAILS)) {
try {
const regex = new RegExp(/(GID)(\W*)(\d*):(\w*)/);
// Number of packages. The number of packages of non-containerized cargo.
// If the cargo is Ro/Ro then the number "1" is used.
const numberOfPackages = line.match(regex)[3];
// Type of packages identification. Package type for non-containerized cargo.
const typeOfPackagesIdentification = line.match(regex)[4];
const containerInfo = {
goodsItemDetails: {
numberOfPackages,
typeOfPackagesIdentification,
data: line
}
};
setCurrentContainer(containerInfo, data);
} catch (error) {
parseEdiStatus.status = false;
parseEdiStatus.message = 'Can not convert GoodItemDetail. Line: ' + line;
}
}
};
// https://shippingandfreightresource.com/what-is-a-hs-code/
// https://www.foreign-trade.com/reference/hscode.htm
const processNatureOfCargo = line => {
try {
if (line.includes(NATURE_OF_CARGO)) {
const regex = new RegExp(/(GDS)(\W*)(\w*[^'])/);
const natureOfCargoCode = line.match(regex)[3];
let natureOfCargoType = '';
natureOfCargoType = prop(harmonizedSystemCodesJson, natureOfCargoCode);
if (!natureOfCargoType) {
natureOfCargoType = 'Unknown';
}
hasFurtherDetails = true;
const containerInfo = {
natureOfCargo: {
natureOfCargoType,
data: line
}
};
setCurrentContainer(containerInfo, data);
}
} catch (error) {
parseEdiStatus.status = false;
parseEdiStatus.message = 'Can not convert GoodItemDetail. Line: ' + line;
}
};
const processFreeText = line => {
try {
if (line.includes(FREE_TEXT)) {
const regex = new RegExp(/(FTX)(\W*)(\w*)(\W*)(\w*)/);
const textSubjectQualifier = line.match(regex)[3];
let freeText = line.match(regex)[5];
switch (textSubjectQualifier) {
case 'AAA': // Description of Goods
break;
case 'HAN': // Handling Instructions
break;
case 'CLR': // Container Loading Remarks
break;
case 'SIN': // Special instructions
break;
case 'AAI': // General information
break;
case 'AAY': // Certification statements
const anotherRegex = new RegExp(
/(FTX)(\W*)(\w*)(\W*)(\w*)(\W*)(\w*)/
);
freeText = line.match(anotherRegex)[7];
// console.log('Need to consider: ', line);
break;
case 'ZZZ': // Mutually defined use
break;
default:
// console.log('processFreeText Need to consider: ', line);
break;
}
const containerInfo = {
freeText: {
freeText,
data: line
}
};
setCurrentContainer(containerInfo, data);
hasFurtherDetails = false;
}
} catch (error) {
parseEdiStatus.status = false;
parseEdiStatus.message = 'Can not convert FreeText. Line: ' + line;
}
};
const processMeasurement = line => {
try {
if (line.includes(MEASUREMENT)) {
const regex = new RegExp(/(MEA)(\W*)(\w*)(\W*)(\w*):(\d*)/);
// "WT" (gross weight / gross mass) – not confirmed as verified
// “VGM” (verified gross mass) – specified weight is verified
const measurementApplicationQualifier = line.match(regex)[3];
let isGrossWeightOrMassVerified = false;
if (measurementApplicationQualifier === 'WT') {
isGrossWeightOrMassVerified = false;
}
if (measurementApplicationQualifier === 'VGM') {
isGrossWeightOrMassVerified = true;
}
// "KGM" = kilogram = preferred
// "LBR" = pounds
const measureUnitQualifier = line.match(regex)[5];
const measureUnitValue = line.match(regex)[6];
const containerInfo = {
measurement: {
isGrossWeightOrMassVerified,
measureUnitQualifier,
measureUnitValue,
data: line
}
};
setCurrentContainer(containerInfo, data);
}
} catch (error) {
parseEdiStatus.status = false;
parseEdiStatus.message = 'Can not convert Measurement. Line: ' + line;
}
};
const processDimensions = line => {
try {
if (line.includes(DIMENSIONS)) {
const regex = new RegExp(/(DIM)(\W*)(\d*)(\W*)(\w*):(\d*):?(\d*):?(\d*)/);
// * Dimension Qualifier: Allowed qualifiers are:
// Code "1" = Gross dimensions (break-bulk)
// Code "5" = Off-standard dims. (over-length front)
// Code "6" = Off-standard dims. (over-length back)
// Code "7" = Off-standard dims. (over-width right)
// Code "8" = Off-standard dims. (over-width left)
// Code "9" = Off-standard dims. (over-height)
// Code "10" = external equipment dimensions (Non-ISO equipment)
// * Basically allowed qualifier "1" for break-bulk cargo and from "5" to "9" for odd-sized-cargo.
// * However allowed from "5" to "9" for break-bulk cargo as additional information, if required.
const dimensionQualifier = line.match(regex)[3];
const measureUnitQualifier = line.match(regex)[5];
const length = line.match(regex)[6];
const width = line.match(regex)[7];
const height = line.match(regex)[8];
let containerInfo = {};
switch (dimensionQualifier) {
case '1': // Code "1" = Gross dimensions (break-bulk)
containerInfo = {
dimensionBreakBulk: {
dimensionQualifier,
measureUnitQualifier,
length,
width,
height,
data: line
}
};
break;
case '5': // Code "5" = Off-standard dims. (over-length front)
containerInfo = {
dimensionOverLengthFront: {
dimensionQualifier,
measureUnitQualifier,
length,
width,
height,
data: line
}
};
break;
case '6': // Code "6" = Off-standard dims. (over-length back)
containerInfo = {
dimensionOverLengthBack: {
dimensionQualifier,
measureUnitQualifier,
length,
width,
height,
data: line
}
};
break;
case '7': // Code "7" = Off-standard dims. (over-width right)
containerInfo = {
dimensionOverWidthRight: {
dimensionQualifier,
measureUnitQualifier,
length,
width,
height,
data: line
}
};
break;
case '8': // Code "8" = Off-standard dims. (over-width left)
containerInfo = {
dimensionOverWidthLeft: {
dimensionQualifier,
measureUnitQualifier,
length,
width,
height,
data: line
}
};
break;
case '9': // Code "9" = Off-standard dims. (over-height)
containerInfo = {
dimensionOverHeight: {
dimensionQualifier,
measureUnitQualifier,
length,
width,
height,
data: line
}
};
break;
case '10': // Code "10" = external equipment dimensions (Non-ISO equipment)
containerInfo = {
dimensionNonIso: {
dimensionQualifier,
measureUnitQualifier,
length,
width,
height,
data: line
}
};
break;
default:
// console.log('processDimension Need to consider: ', line);
break;
}
setCurrentContainer(containerInfo, data);
}
} catch (error) {
parseEdiStatus.status = false;
parseEdiStatus.message = 'Can not convert Dimensions. Line: ' + line;
}
};
const processTemperature = line => {
try {
if (line.includes(TEMPERATURE)) {
const regex = new RegExp(
/(TMP)(\W*)(\d*)(\W)([-]?[0-9]*[.]?[0-9]*):(\w*)/
);
const temperatureQualifier = line.match(regex)[3];
const temperatureValue = line.match(regex)[5];
// * Measure Unit Qualifier: Allowed qualifiers:
// "CEL" = degrees Celsius = Preferred.
// "FAH" = degrees Fahrenheit
const measureUnitQualifier = line.match(regex)[6];
const containerInfo = {
temperature: {
temperatureQualifier,
temperatureValue,
measureUnitQualifier,
data: line
}
};
setCurrentContainer(containerInfo, data);
}
} catch (error) {
parseEdiStatus.status = false;
parseEdiStatus.message = 'Can not convert Temperature. Line: ' + line;
}
};
const processRangeDetail = line => {
try {
if (line.includes(RANGE_DETAILS)) {
const regex = new RegExp(
/(RNG)(\W*)(\d*)(\W*)(\w*):([-]?[0-9]*[.]?[0-9]*):([-]?[0-9]*[.]?[0-9]*)/
);
const rangeTypeQualifier = line.match(regex)[3];
// * Measure Unit Qualifier: Allowed qualifiers:
// "CEL" = degrees Celsius = Preferred.
// "FAH" = degrees Fahrenheit
const measureUnitQualifier = line.match(regex)[5];
const minRange = line.match(regex)[6];
const maxRange = line.match(regex)[7];
const containerInfo = {
rangeDetail: {
rangeTypeQualifier,
measureUnitQualifier,
minRange,
maxRange,
data: line
}
};
setCurrentContainer(containerInfo, data);
}
} catch (error) {
parseEdiStatus.status = false;
parseEdiStatus.message = 'Can not convert RangeDetail. Line: ' + line;
}
};
const processLocationForContainer = line => {
try {
if (line.includes(PLACE_LOCATION_IDENTIFICATION)) {
const regex = new RegExp(
/(LOC)(\W)(\d*)(\W)(\w*):?(\d*)?:?(\d*)?(\W)?(\w*)?:?(\w*)?:?(\w*)?/
);
const placeLocationQualifier = line.match(regex)[3];
const placeLocationIdentification = line.match(regex)[5];
const codeListQualifier = line.match(regex)[6];
// Code list responsible agency, coded. Allowed codes:
// "112" = US, US Census Bureau, Schedule D for U S locations, Schedule K for foreign port locations.
// "6" = UN/ECE - United Nations - Economic Commission for Europe. (UN-Locodes).
// "ZZZ" = Optional ports.
const codeListResponsibleAgency = line.match(regex)[7];
// Related place/location one identification.
// The name code of the Container Terminal in the port of discharge or the port of loading.
// Terminal codes to be used as defined in SMDG’s Master Terminal Facilities code list.
const relatedPlaceLocationIdentification = line.match(regex)[9];
// "TER" = TERMINALS (leading 3 characters due to limited size).
const codeListQualifier2 = line.match(regex)[10];
// “306” = SMDG (code 306 is defined in D.02B and later)
const codeListResponsibleAgency2 = line.match(regex)[11];
const containerInfo = {};
switch (placeLocationQualifier) {
case '9': // Place/Port of Loading
containerInfo.portOfLoading = {
locationCode: placeLocationIdentification,
codeListQualifier,
codeListResponsibleAgency,
relatedPlaceLocationIdentification,
codeListQualifier2,
codeListResponsibleAgency2,
data: line
};
break;
case '11': // Place/Port of discharge
containerInfo.portOfDischarge = {
locationCode: placeLocationIdentification,
codeListQualifier,
codeListResponsibleAgency,
relatedPlaceLocationIdentification,
codeListQualifier2,
codeListResponsibleAgency2,
data: line
};
break;
case '13': // Transshipment port/Place of transshipment
containerInfo.portOfTransShipment = {
locationCode: placeLocationIdentification,
codeListQualifier,
codeListResponsibleAgency,
relatedPlaceLocationIdentification,
codeListQualifier2,
codeListResponsibleAgency2,
data: line
};
break;
case '64': // 1st optional port of discharge
containerInfo.portOfDischarge1 = {
locationCode: placeLocationIdentification,
codeListQualifier,
codeListResponsibleAgency,
relatedPlaceLocationIdentification,
codeListQualifier2,
codeListResponsibleAgency2,
data: line
};
break;
case '68': // 2nd optional port of discharge
containerInfo.portOfDischarge2 = {
locationCode: placeLocationIdentification,
codeListQualifier,
codeListResponsibleAgency,
relatedPlaceLocationIdentification,
codeListQualifier2,
codeListResponsibleAgency2,
data: line
};
break;
case '70': // 3rd optional port of discharge
containerInfo.portOfDischarge3 = {
locationCode: placeLocationIdentification,
codeListQualifier,
codeListResponsibleAgency,
relatedPlaceLocationIdentification,
codeListQualifier2,
codeListResponsibleAgency2,
data: line