2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2021 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2021-11-25 10:08:01 +00:00
|
|
|
|
|
|
|
#include "subscription.h"
|
|
|
|
#include "user.h"
|
|
|
|
|
|
|
|
Subscription::Subscription(User *user) : m_user(user)
|
|
|
|
{
|
|
|
|
Q_ASSERT(user);
|
|
|
|
}
|
|
|
|
|
2021-11-26 14:49:18 +00:00
|
|
|
//! [calculate-price]
|
|
|
|
|
2021-11-25 10:08:01 +00:00
|
|
|
void Subscription::calculatePrice()
|
|
|
|
{
|
|
|
|
const auto oldPrice = m_price;
|
|
|
|
|
|
|
|
m_price = qRound(calculateDiscount() * m_duration * basePrice());
|
|
|
|
if (m_price != oldPrice)
|
|
|
|
emit priceChanged();
|
|
|
|
}
|
|
|
|
|
2021-11-26 14:49:18 +00:00
|
|
|
//! [calculate-price]
|
|
|
|
|
|
|
|
//! [set-duration]
|
|
|
|
|
2021-11-25 10:08:01 +00:00
|
|
|
void Subscription::setDuration(Duration newDuration)
|
|
|
|
{
|
|
|
|
if (newDuration != m_duration) {
|
|
|
|
m_duration = newDuration;
|
|
|
|
calculatePrice();
|
|
|
|
emit durationChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-26 14:49:18 +00:00
|
|
|
//! [set-duration]
|
|
|
|
|
|
|
|
//! [calculate-discount]
|
|
|
|
|
2021-11-25 10:08:01 +00:00
|
|
|
double Subscription::calculateDiscount() const
|
|
|
|
{
|
|
|
|
switch (m_duration) {
|
|
|
|
case Monthly:
|
|
|
|
return 1;
|
|
|
|
case Quarterly:
|
|
|
|
return 0.9;
|
|
|
|
case Yearly:
|
|
|
|
return 0.6;
|
|
|
|
}
|
|
|
|
Q_ASSERT(false);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-11-26 14:49:18 +00:00
|
|
|
//! [calculate-discount]
|
|
|
|
|
|
|
|
//! [calculate-base-price]
|
|
|
|
|
2021-11-25 10:08:01 +00:00
|
|
|
int Subscription::basePrice() const
|
|
|
|
{
|
2023-02-28 21:31:13 +00:00
|
|
|
if (m_user->country() == User::Country::AnyTerritory)
|
2021-11-25 10:08:01 +00:00
|
|
|
return 0;
|
|
|
|
|
2023-02-28 21:31:13 +00:00
|
|
|
return (m_user->country() == User::Country::Norway) ? 100 : 80;
|
2021-11-25 10:08:01 +00:00
|
|
|
}
|
|
|
|
|
2021-11-26 14:49:18 +00:00
|
|
|
//! [calculate-base-price]
|
|
|
|
|
|
|
|
//! [update-validity]
|
|
|
|
|
2021-11-25 10:08:01 +00:00
|
|
|
void Subscription::updateValidity()
|
|
|
|
{
|
|
|
|
bool isValid = m_isValid;
|
2023-02-28 21:31:13 +00:00
|
|
|
m_isValid = m_user->country() != User::Country::AnyTerritory && m_user->age() > 12;
|
2021-11-25 10:08:01 +00:00
|
|
|
|
|
|
|
if (m_isValid != isValid)
|
|
|
|
emit isValidChanged();
|
|
|
|
}
|
2021-11-26 14:49:18 +00:00
|
|
|
|
|
|
|
//! [update-validity]
|