File indexing completed on 2024-12-29 04:51:05

0001 /*
0002    SPDX-FileCopyrightText: 2019-2022 Volker Krause <vkrause@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 // see https://community.kde.org/KDE_PIM/KItinerary/Renfe_Barcodes#Renfe_Barcode
0008 function parseBarcode(barcode)
0009 {
0010     if (barcode.trim().length > 56)
0011         return null;
0012 
0013     // trim not set combined commuter section, so merging with a document that has that set works properly
0014     if (barcode.substr(49, 7) == '..00000')
0015         barcode = barcode.substr(0, 49);
0016 
0017     var res = JsonLd.newTrainReservation();
0018     res.reservedTicket.ticketToken = "qrCode:" + barcode.trim();
0019     res.reservedTicket.ticketNumber = barcode.substr(0, 13);
0020     res.reservationFor.trainNumber = barcode.substr(29, 5);
0021     res.reservedTicket.ticketedSeat.seatSection = barcode.substr(34, 3);
0022     res.reservedTicket.ticketedSeat.seatNumber = barcode.substr(37, 3);
0023     res.reservationNumber = barcode.substr(43, 6);
0024     res.reservationFor.departureStation.identifier = "uic:71" + barcode.substr(13, 5);
0025     res.reservationFor.arrivalStation.identifier = "uic:71" + barcode.substr(18, 5);
0026     res.reservationFor.departureDay = JsonLd.toDateTime(barcode.substr(23, 6), 'ddMMyy', 'es');
0027 
0028     res.reservationFor.departureStation.name = barcode.substr(13, 5);
0029     res.reservationFor.arrivalStation.name = barcode.substr(18, 5);
0030     return res;
0031 }
0032 
0033 function parseLeg(text, baseRes)
0034 {
0035     let res = JsonLd.newTrainReservation();
0036 
0037     const dep = text.match(/(?:Salida|Origen:) +(.*?) {2,}([\d\/]+) +(\d\d[:\.]\d\d)/);
0038     res.reservationFor.departureStation.name = dep[1];
0039     res.reservationFor.departureTime = JsonLd.toDateTime(dep[2] + dep[3], ["dd/MM/yyyyhh:mm", "dd/MM/yyyyhh.mm"], "es");
0040 
0041     const arr = text.match(/(?:Llegada|Destino:)\s+(.*?) {2,}([\d\/]+) +(\d\d[:\.]\d\d)\n(?:.* )?([A-Z]+) +(\d+)/);
0042     res.reservationFor.arrivalStation.name = arr[1];
0043     res.reservationFor.arrivalTime = JsonLd.toDateTime(arr[2] + arr[3], ["dd/MM/yyyyhh:mm", "dd/MM/yyyyhh.mm"], "es");
0044     res.reservationFor.trainName = arr[4];
0045 
0046     const train = text.match(/Coche:\s+(\S+)\s+Plaza:\s+(\S+)\s+(\S.*) +(\d{1,5})/);
0047     if (train) {
0048         res.reservedTicket.ticketedSeat.seatSection = train[1];
0049         res.reservedTicket.ticketedSeat.seatNumber = train[2];
0050         res.reservationFor.trainName = train[3];
0051         res.reservationFor.trainNumber = train[4];
0052     }
0053 
0054     res.reservationNumber = baseRes.reservationNumber;
0055     res.reservationFor.provider = baseRes.reservationFor.provider;
0056     res.reservedTicket.ticketToken = baseRes.reservedTicket.ticketToken;
0057     res.priceCurrency = baseRes.priceCurrency;
0058     res.totalPrice = baseRes.totalPrice;
0059     return res;
0060 }
0061 
0062 function parsePdf(pdf, node, triggerNode)
0063 {
0064     const res = triggerNode.result[0];
0065     const page = pdf.pages[triggerNode.location]
0066     const text = page.text;
0067 
0068     const price = text.match(/TOTAL:? *(\d+,\d\d *€)/i);
0069     if (price)
0070         ExtractorEngine.extractPrice(price[1], res);
0071 
0072     if (!text.match(/TRAYECTO/)) {
0073         return JsonLd.apply(res, parseLeg(text, res));
0074     }
0075 
0076     // multi-leg ticket
0077     let res1 = parseLeg(page.textInRect(0.0, 0.0, 0.5, 1.0), res);
0078     let res2 = parseLeg(page.textInRect(0.5, 0.0, 1.0, 1.0), res);
0079     res1.reservationFor.departureStation = JsonLd.apply(res.reservationFor.departureStation, res1.reservationFor.departureStation);
0080     res2.reservationFor.arrivalStation = JsonLd.apply(res.reservationFor.arrivalStation, res2.reservationFor.arrivalStation);
0081     return [res1, res2];
0082 }
0083 
0084 function parsePkPass(content)
0085 {
0086     var res = parseBarcode(content.barcodes[0].message);
0087     res.reservationFor.provider.name = content.organizationName;
0088 
0089     const day = content.field['destinofecha'].value;
0090     res.reservationFor.departureTime = JsonLd.toDateTime(day + ' ' + content.field['boardingTime'].value, 'dd/MM/yyyy hh:mm', 'es');
0091     res.reservationFor.departureStation.name = content.field['boardingTime'].label;
0092     res.reservationFor.arrivalTime = JsonLd.toDateTime(day + ' ' + content.field['destino'].value, 'dd/MM/yyyy hh:mm', 'es');
0093     res.reservationFor.arrivalStation.name = content.field['destino'].label;
0094 
0095     res.underName.name = content.field['nombrepasajero'].value;
0096 
0097     if (content.field['documentos']) {
0098         const program = content.field['documentos'].value.match(/(.*) +: +(.*)/);
0099         if (program) {
0100             res.programMembershipUsed.name = program[1];
0101             res.programMembershipUsed.membershipNumber = program[2];
0102         }
0103     }
0104     ExtractorEngine.extractPrice(content.field['precio'].value, res);
0105     return res;
0106 }
0107 
0108 function parseInternationalPdf(pdf, node, uicNode)
0109 {
0110     const renfeNode = node.findChildNodes({ scope: "Descendants", mimeType: "text/plain", match: "^\\d{25}/\\d{2}/\\d{6}:\\d{16}" }).find(n => n.location === uicNode.location);
0111 
0112     let res = JsonLd.apply(renfeNode.result[0], uicNode.result[0]);
0113     res.reservationFor.departureStation.identifier = undefined;
0114     res.reservationFor.arrivalStation.identifier = undefined;
0115     res.reservationNumber = renfeNode.result[0].reservationNumber;
0116     let renfeRes = JsonLd.clone(res);
0117     renfeRes.reservedTicket.ticketToken = renfeNode.result[0].reservedTicket.ticketToken;
0118     renfeRes.reservedTicket.name = renfeNode.result[0].reservedTicket.name;
0119     return [renfeRes, res];
0120 }