-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSinglePrice.php
70 lines (62 loc) · 1.67 KB
/
SinglePrice.php
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
<?php
/**
* PHP Billing Library
*
* @link https://github.com/hiqdev/php-billing
* @package php-billing
* @license BSD-3-Clause
* @copyright Copyright (c) 2017-2020, HiQDev (http://hiqdev.com/)
*/
namespace hiqdev\php\billing\price;
use hiqdev\php\billing\plan\PlanInterface;
use hiqdev\php\billing\target\TargetInterface;
use hiqdev\php\billing\type\TypeInterface;
use hiqdev\php\units\Quantity;
use hiqdev\php\units\QuantityInterface;
use Money\Money;
/**
* Single Price.
*
* - no charge for quantity less then prepaid
* - same price for any quantity above prepaid
*
* @see PriceInterface
*
* @author Andrii Vasyliev <[email protected]>
*/
class SinglePrice extends AbstractPrice implements PriceWithQuantityInterface, PriceWithMoneyInterface
{
use HasMoney;
use HasQuantity;
public function __construct(
$id,
TypeInterface $type,
TargetInterface $target,
PlanInterface $plan = null,
QuantityInterface $prepaid,
Money $price
) {
parent::__construct($id, $type, $target, $plan);
$this->prepaid = $prepaid;
$this->price = $price;
}
/**
* {@inheritdoc}
*/
public function calculateUsage(QuantityInterface $quantity): ?QuantityInterface
{
$usage = $quantity->convert($this->prepaid->getUnit())->subtract($this->prepaid);
if ($usage->isPositive()) {
return $usage;
}
return Quantity::create($this->prepaid->getUnit()->getName(), 0);
}
/**
* {@inheritdoc}
* Same price for any usage.
*/
public function calculatePrice(QuantityInterface $quantity): ?Money
{
return $this->price;
}
}