1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP Billing Library |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/php-billing |
6
|
|
|
* @package php-billing |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\php\billing\order; |
12
|
|
|
|
13
|
|
|
use hiqdev\php\billing\plan\PlanRepositoryInterface; |
14
|
|
|
use hiqdev\php\billing\sale\SaleRepositoryInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Andrii Vasyliev <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class Calculator implements CalculatorInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var SaleRepositoryInterface |
23
|
|
|
*/ |
24
|
|
|
private $saleRepository; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var PlanRepositoryInterface |
28
|
|
|
*/ |
29
|
|
|
private $planRepository; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param PlanRepositoryInterface $planRepository |
33
|
|
|
*/ |
34
|
2 |
|
public function __construct( |
35
|
|
|
?SaleRepositoryInterface $saleRepository, |
36
|
|
|
?PlanRepositoryInterface $planRepository |
37
|
|
|
) { |
38
|
2 |
|
$this->saleRepository = $saleRepository; |
39
|
2 |
|
$this->planRepository = $planRepository; |
40
|
2 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
2 |
|
public function calculateCharges(OrderInterface $order) |
46
|
|
|
{ |
47
|
2 |
|
$plans = $this->findPlans($order); |
48
|
2 |
|
$charges = []; |
49
|
2 |
|
foreach ($order->getActions() as $actionKey => $action) { |
50
|
2 |
|
if (empty($plans[$actionKey])) { |
51
|
|
|
/* XXX not sure... think more |
52
|
|
|
throw new FailedFindPlan(); |
53
|
|
|
*/ |
54
|
|
|
continue; |
55
|
|
|
} |
56
|
2 |
|
$charges[$actionKey] = $plans[$actionKey]->calculateCharges($action); |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
return $charges; |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
public function findPlans(OrderInterface $order) |
63
|
|
|
{ |
64
|
2 |
|
$sales = $this->findSales($order); |
65
|
2 |
|
$plans = []; |
66
|
2 |
|
$lookPlanIds = []; |
67
|
2 |
|
foreach ($order->getActions() as $actionKey => $action) { |
68
|
2 |
|
if (empty($sales[$actionKey])) { |
69
|
|
|
/// it is ok when no sale found for upper resellers |
70
|
|
|
/// throw new \Exception('not found sale'); |
|
|
|
|
71
|
|
|
$plans[$actionKey] = null; |
72
|
|
|
} else { |
73
|
2 |
|
$sale = $sales[$actionKey]; |
74
|
2 |
|
$plan = $sale->getPlan(); |
75
|
2 |
|
if ($plan->hasPrices()) { |
76
|
2 |
|
$plans[$actionKey] = $plan; |
77
|
|
|
} else { |
78
|
2 |
|
$lookPlanIds[$actionKey] = $plan->getId(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
if ($lookPlanIds) { |
84
|
|
|
$foundPlans = $this->planRepository->findByIds($lookPlanIds); |
85
|
|
|
foreach ($foundPlans as $actionKey => $plan) { |
86
|
|
|
$foundPlans[$plan->getId()] = $plan; |
|
|
|
|
87
|
|
|
} |
88
|
|
|
foreach ($lookPlanIds as $actionKey => $planId) { |
89
|
|
|
if (empty($foundPlans[$planId])) { |
90
|
|
|
throw new \Exception('not found plan'); |
91
|
|
|
} |
92
|
|
|
$plans[$actionKey] = $foundPlans[$planId]; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
return $plans; |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
public function findSales(OrderInterface $order) |
100
|
|
|
{ |
101
|
2 |
|
$sales = []; |
102
|
2 |
|
$lookActions = []; |
103
|
2 |
|
foreach ($order->getActions() as $actionKey => $action) { |
104
|
2 |
|
$sale = $action->getSale(); |
105
|
2 |
|
if ($sale) { |
106
|
|
|
$sales[$actionKey] = $sale; |
107
|
|
|
} else { |
108
|
2 |
|
$lookActions[$actionKey] = $action; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
if ($lookActions) { |
113
|
2 |
|
$lookOrder = new Order(null, $order->getCustomer(), $lookActions); |
114
|
2 |
|
$foundSales = $this->saleRepository->findByOrder($lookOrder); |
|
|
|
|
115
|
2 |
|
foreach ($foundSales as $actionKey => $plan) { |
116
|
2 |
|
$sales[$actionKey] = $plan; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
2 |
|
return $sales; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.