File indexing completed on 2024-11-24 04:45:10

0001 /*
0002     SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "kitinerary_export.h"
0010 #include "ssbticketbase.h"
0011 
0012 #include <QDateTime>
0013 #include <QMetaType>
0014 
0015 namespace KItinerary {
0016 
0017 /** ERA SSB ticket barcode (version 3).
0018  *  @see ERA TAP TSI TD B.12 Digital Security Elements For Rail Passenger Ticketing - ยง7 SSB - Small Structured Barcode
0019  */
0020 class KITINERARY_EXPORT SSBv3Ticket : protected SSBTicketBase {
0021     Q_GADGET
0022     // low-level raw value access
0023     // header
0024     SSB_NUM_PROPERTY(version, 0, 4)
0025     SSB_NUM_PROPERTY(issuerCode, 4, 14)
0026     SSB_NUM_PROPERTY(id, 18, 4)
0027     SSB_NUM_PROPERTY(ticketTypeCode, 22, 5)
0028 
0029     // open data common part for type 1-4
0030     SSB_NUM_PROPERTY(numberOfAdultPassengers, 27, 7)
0031     SSB_NUM_PROPERTY(numberOfChildPassengers, 34, 7)
0032     SSB_NUM_PROPERTY(specimen, 41, 1)
0033     SSB_NUM_PROPERTY(classOfTravel, 42, 6)
0034     SSB_STR_PROPERTY(tcn, 48, 14)
0035     SSB_NUM_PROPERTY(yearOfIssue, 132, 4) // last digit only
0036     SSB_NUM_PROPERTY(issuingDay, 136, 9)
0037 
0038     // open data type 1 (IRT, RES, BOA) variant
0039     SSB_NUM_PROPERTY(type1SubTicketType, 145, 2)
0040     SSB_NUM_PROPERTY(type1StationCodeNumericOrAlpha, 147, 1)
0041     SSB_NUM_PROPERTY(type1StationCodeListType, 148, 4)
0042     SSB_NUM_PROPERTY(type1DepartureStationNum, 152, 28)
0043     SSB_STR_PROPERTY(type1DepartureStationAlpha, 148, 5)
0044     SSB_NUM_PROPERTY(type1ArrivalStationNum, 180, 28)
0045     SSB_STR_PROPERTY(type1ArrivalStationAlpha, 178, 5)
0046     SSB_NUM_PROPERTY(type1DepartureDate, 208, 9)
0047     SSB_NUM_PROPERTY(type1DepartureTime, 217, 11)
0048     SSB_STR_PROPERTY(type1TrainNumber, 228, 5)
0049     SSB_NUM_PROPERTY(type1CoachNumber, 258, 10)
0050     SSB_STR_PROPERTY(type1SeatNumber, 268, 3)
0051     SSB_NUM_PROPERTY(type1OverbookingIndicator, 286, 1)
0052     SSB_NUM_PROPERTY(type1InformationMessages, 287, 14)
0053     SSB_STR_PROPERTY(type1OpenText, 301, 27)
0054 
0055     // open data type 2 (NRT) variant
0056     SSB_NUM_PROPERTY(type2ReturnJourneyFlag, 145, 1)
0057     SSB_NUM_PROPERTY(type2FirstDayOfValidity, 146, 9)
0058     SSB_NUM_PROPERTY(type2LastDayOfValidity, 155, 9)
0059     SSB_NUM_PROPERTY(type2StationCodeNumericOrAlpha, 164, 1)
0060     SSB_NUM_PROPERTY(type2StationCodeListType, 165, 4)
0061     SSB_NUM_PROPERTY(type2DepartureStationNum, 169, 28)
0062     SSB_STR_PROPERTY(type2DepartureStationAlpha, 165, 5)
0063     SSB_NUM_PROPERTY(type2ArrivalStationNum, 197, 28)
0064     SSB_STR_PROPERTY(type2ArrivalStationAlpha, 195, 5)
0065     SSB_NUM_PROPERTY(type2InformationMessages, 225, 14)
0066     SSB_STR_PROPERTY(type2OpenText, 239, 37)
0067 
0068     // open data type 3 (GRT) variant
0069     SSB_NUM_PROPERTY(type3ReturnJourneyFlag, 145, 1)
0070     SSB_NUM_PROPERTY(type3FirstDayOfValidity, 146, 9)
0071     SSB_NUM_PROPERTY(type3LastDayOfValidity, 155, 9)
0072     SSB_NUM_PROPERTY(type3StationCodeNumericOrAlpha, 164, 1)
0073     SSB_NUM_PROPERTY(type3StationCodeListType, 165, 4)
0074     SSB_NUM_PROPERTY(type3DepartureStationNum, 169, 28)
0075     SSB_STR_PROPERTY(type3DepartureStationAlpha, 165, 5)
0076     SSB_NUM_PROPERTY(type3ArrivalStationNum, 197, 28)
0077     SSB_STR_PROPERTY(type3ArrivalStationAlpha, 195, 5)
0078     SSB_STR_PROPERTY(type3NameOfGroupLeader, 225, 12)
0079     SSB_NUM_PROPERTY(type3CountermarkNumber, 297, 8)
0080     SSB_NUM_PROPERTY(type3InformationMessages, 305, 14)
0081     SSB_STR_PROPERTY(type3OpenText, 305, 24)
0082 
0083     // open data type 4 (RPT) variant
0084     SSB_NUM_PROPERTY(type4RPTSubTicketType, 145, 2)
0085     SSB_NUM_PROPERTY(type4FirstDayOfValidity, 147, 9)
0086     SSB_NUM_PROPERTY(type4LastDayOfValidity, 156, 9)
0087     SSB_NUM_PROPERTY(type4NumberOfDaysOfTravelAllowed, 165, 7)
0088     SSB_NUM_PROPERTY(type4CountryCode1, 172, 7)
0089     SSB_NUM_PROPERTY(type4CountryCode2, 179, 7)
0090     SSB_NUM_PROPERTY(type4CountryCode3, 186, 7)
0091     SSB_NUM_PROPERTY(type4CountryCode4, 193, 7)
0092     SSB_NUM_PROPERTY(type4SecondPage, 200, 1)
0093     SSB_NUM_PROPERTY(type4InformationMessages, 201, 14)
0094     SSB_STR_PROPERTY(type4OpenText, 215, 40)
0095 
0096     enum TicketType {
0097         IRT_RES_BOA = 1,
0098         NRT = 2,
0099         GRT = 3,
0100         RPT = 4
0101     };
0102     Q_ENUM(TicketType)
0103 
0104     Q_PROPERTY(QByteArray rawData READ rawData STORED false)
0105 
0106 public:
0107     SSBv3Ticket();
0108     explicit SSBv3Ticket(const QByteArray &data);
0109     ~SSBv3Ticket();
0110 
0111     /** Returns @c true if this is a valid SSB ticket. */
0112     bool isValid() const;
0113 
0114     /** Date of issue. */
0115     Q_INVOKABLE QDate issueDate(const QDateTime &contextDate = QDateTime::currentDateTime()) const;
0116     /** Departure day for type 1 (IRT/RES/BOA) tickets. */
0117     Q_INVOKABLE QDate type1DepartureDay(const QDateTime &contextDate = QDateTime::currentDateTime()) const;
0118     /** First day of validity for type 2 (NRT) tickets. */
0119     Q_INVOKABLE QDate type2ValidFrom(const QDateTime &contextDate = QDateTime::currentDateTime()) const;
0120     /** Last day of validity for type 2 (NRT) tickets. */
0121     Q_INVOKABLE QDate type2ValidUntil(const QDateTime &contextDate = QDateTime::currentDateTime()) const;
0122 
0123     /** Raw barcode data. */
0124     QByteArray rawData() const;
0125 
0126     /** Returns @c true if @p data might be an ERA SSB ticket. */
0127     static bool maybeSSB(const QByteArray &data);
0128 
0129 private:
0130     QString readString(int start, int length) const;
0131 };
0132 
0133 }
0134 
0135 Q_DECLARE_METATYPE(KItinerary::SSBv3Ticket)
0136