KItinerary

priceutil.cpp
1/*
2 SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "priceutil.h"
7
8#include <KItinerary/JsonLdDocument>
9#include <KItinerary/Reservation>
10#include <KItinerary/Ticket>
11
12#include <cmath>
13
14using namespace KItinerary;
15
17{
18 if (!PriceUtil::canHavePrice(item)) {
19 return false;
20 }
21
22 if (!std::isnan(JsonLdDocument::readProperty(item, "totalPrice").toDouble()) && !JsonLdDocument::readProperty(item, "priceCurrency").isNull()) {
23 return true;
24 }
25
27 return PriceUtil::hasPrice(JsonLd::convert<Reservation>(item).reservedTicket());
28 }
29
30 return false;
31}
32
37
38double PriceUtil::price(const QVariant &item)
39{
41 const auto res = JsonLd::convert<Reservation>(item);
42 return !std::isnan(res.totalPrice()) ? res.totalPrice() : PriceUtil::price(res.reservedTicket());
43 }
44
45 if (JsonLd::isA<Ticket>(item)) {
46 return item.value<Ticket>().totalPrice();
47 }
48
49 return NAN;
50}
51
53{
55 const auto res = JsonLd::convert<Reservation>(item);
56 return !res.priceCurrency().isEmpty() ? res.priceCurrency() : PriceUtil::currency(res.reservedTicket());
57 }
58
59 if (JsonLd::isA<Ticket>(item)) {
60 return item.value<Ticket>().priceCurrency();
61 }
62
63 return {};
64}
65
66void PriceUtil::setPrice(QVariant &item, double price, const QString &currency)
67{
68 // TODO check if there is a price on a nested ticket already, and set/replace that
69 JsonLdDocument::writeProperty(item, "totalPrice", price);
70 JsonLdDocument::writeProperty(item, "priceCurrency", currency);
71}
72
73// ### keep sorted by ISO code, only needs to contain those != 2
74// @see https://en.wikipedia.org/wiki/List_of_circulating_currencies
75struct {
76 const char isoCode[4];
77 const uint8_t decimals;
78} static constexpr const currency_decimals_map[] = {
79 { "BHD", 3 },
80 { "CNY", 1 },
81 { "IQD", 3 },
82 { "IRR", 0 },
83 { "KWD", 3 },
84 { "LYD", 3 },
85 { "MGA", 1 },
86 { "MRU", 1 },
87 { "OMR", 3 },
88 { "TND", 3 },
89 { "VND", 1 },
90};
91
93{
94 const auto it = std::lower_bound(
95 std::begin(currency_decimals_map), std::end(currency_decimals_map),
96 currency, [](const auto &m, QStringView isoCode) {
97 return QLatin1StringView(m.isoCode, 3) < isoCode;
98 });
99 if (it != std::end(currency_decimals_map) &&
100 QLatin1StringView((*it).isoCode, 3) == currency) {
101 return (*it).decimals;
102 }
103 return 2;
104}
105
106int PriceUtil::decimalCount(const QString &currency)
107{
109}
110
111#include "moc_priceutil.cpp"
static void writeProperty(QVariant &obj, const char *name, const QVariant &value)
Set property name on object obj to value value.
static QVariant readProperty(const QVariant &obj, const char *name)
Read property name on object obj.
static Q_INVOKABLE bool canHavePrice(const QVariant &item)
Returns true if item can have price/currency information.
Definition priceutil.cpp:33
static Q_INVOKABLE void setPrice(QVariant &item, double price, const QString &currency)
Sets price and currency on item.
Definition priceutil.cpp:66
static Q_INVOKABLE QString currency(const QVariant &item)
Returns the currency value from item.
Definition priceutil.cpp:52
static int decimalCount(QStringView currency)
Returns the number of decimals to represent the sub-unit of currency.
Definition priceutil.cpp:92
static Q_INVOKABLE double price(const QVariant &item)
Returns the price value from item.
Definition priceutil.cpp:38
static Q_INVOKABLE bool hasPrice(const QVariant &item)
Returns true if item has valid price and currency information.
Definition priceutil.cpp:16
A booked ticket.
Definition ticket.h:41
bool isA(const QVariant &value)
Returns true if value is of type T.
Definition datatypes.h:24
bool canConvert(const QVariant &value)
Checks if the given value can be up-cast to T.
Definition datatypes.h:31
T convert(const QVariant &value)
Up-cast value to T.
Definition datatypes.h:47
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.