Completed
Push — master ( e83455...d2af9f )
by Andrii
06:07 queued 04:16
created

src/order/Calculator.php (1 issue)

Labels
Severity
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\action\ActionInterface;
14
use hiqdev\php\billing\charge\Charge;
15
use hiqdev\php\billing\charge\ChargeInterface;
16
use hiqdev\php\billing\charge\ChargeModifier;
17
use hiqdev\php\billing\charge\GeneralizerInterface;
18
use hiqdev\php\billing\plan\Plan;
19
use hiqdev\php\billing\plan\PlanInterface;
20
use hiqdev\php\billing\plan\PlanRepositoryInterface;
21
use hiqdev\php\billing\price\PriceInterface;
22
use hiqdev\php\billing\sale\Sale;
23
use hiqdev\php\billing\sale\SaleInterface;
24
use hiqdev\php\billing\sale\SaleRepositoryInterface;
25
26
/**
27
 * Calculator calculates charges for given order or action.
28
 *
29
 * @author Andrii Vasyliev <[email protected]>
30
 */
31
class Calculator implements CalculatorInterface
32
{
33
    /**
34
     * @var GeneralizerInterface
35
     */
36
    protected $generalizer;
37
    /**
38
     * @var SaleRepositoryInterface
39
     */
40
    private $saleRepository;
41
    /**
42
     * @var PlanRepositoryInterface
43
     */
44
    private $planRepository;
45
46
    /**
47
     * @param GeneralizerInterface $generalizer
48
     * @param SaleRepositoryInterface|null $saleRepository
49
     * @param PlanRepositoryInterface $planRepository
50
     */
51 33
    public function __construct(
52
        GeneralizerInterface $generalizer,
53
        ?SaleRepositoryInterface $saleRepository,
54
        ?PlanRepositoryInterface $planRepository
55
    ) {
56 33
        $this->generalizer    = $generalizer;
57 33
        $this->saleRepository = $saleRepository;
58 33
        $this->planRepository = $planRepository;
59 33
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 2
    public function calculateOrder(OrderInterface $order): array
65
    {
66 2
        $plans = $this->findPlans($order);
67 2
        $charges = [];
68 2
        foreach ($order->getActions() as $actionKey => $action) {
69 2
            if (empty($plans[$actionKey])) {
70
                /* XXX not sure... think more
71
                throw new FailedFindPlan();
72
                 */
73
                continue;
74
            }
75
76 2
            $charges[$actionKey] = $this->calculatePlan($plans[$actionKey], $action);
77
        }
78
79 2
        return $charges;
80
    }
81
82 4
    public function calculatePlan(PlanInterface $plan, ActionInterface $action): array
83
    {
84 4
        $result = [];
85 4
        foreach ($plan->getPrices() as $price) {
0 ignored issues
show
The method getPrices() does not exist on hiqdev\php\billing\plan\PlanInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to hiqdev\php\billing\plan\PlanInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
        foreach ($plan->/** @scrutinizer ignore-call */ getPrices() as $price) {
Loading history...
86 4
            $charges = $this->calculatePrice($price, $action);
87 4
            if (!empty($charges)) {
88 4
                $result = array_merge($result, $charges);
89
            }
90
        }
91
92 4
        return $result;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 4
    public function calculatePrice(PriceInterface $price, ActionInterface $action): array
99
    {
100 4
        $charge = $this->calculateCharge($price, $action);
101 4
        if ($charge === null) {
102 4
            return [];
103
        }
104
105 4
        if ($price instanceof ChargeModifier) {
106 4
            $charges = $price->modifyCharge($charge, $action);
107
        } else {
108
            $charges = [$charge];
109
        }
110
111 4
        if ($action->isFinished()) {
112
            foreach ($charges as $charge) {
113
                $charge->setFinished();
114
            }
115
        }
116
117 4
        return $charges;
118
    }
119
120
    /**
121
     * Calculates charge for given action and price.
122
     * Returns `null`, if $price is not applicable to $action.
123
     *
124
     * @param PriceInterface $price
125
     * @param ActionInterface $action
126
     * @return ChargeInterface|Charge|null
127
     */
128 25
    public function calculateCharge(PriceInterface $price, ActionInterface $action): ?ChargeInterface
129
    {
130 25
        if (!$action->isApplicable($price)) {
131 8
            return null;
132
        }
133
134 21
        $usage = $price->calculateUsage($action->getQuantity());
135 21
        if ($usage === null) {
136 4
            return null;
137
        }
138
139 17
        $sum = $price->calculateSum($action->getQuantity());
140 17
        if ($sum === null) {
141
            return null;
142
        }
143
144 17
        $type = $this->generalizer->specializeType($price->getType(), $action->getType());
145 17
        $target = $this->generalizer->specializeTarget($price->getTarget(), $action->getTarget());
146
147
        /* sorry, debugging facility
148
         * var_dump([
149
            'unit'      => $usage->getUnit()->getName(),
150
            'quantity'  => $usage->getQuantity(),
151
            'price'     => $price->calculatePrice($usage)->getAmount(),
152
            'sum'       => $sum->getAmount(),
153
        ]);*/
154
155 17
        return new Charge(null, $type, $target, $action, $price, $usage, $sum);
156
    }
157
158
    /**
159
     * @param OrderInterface $order
160
     * @return PlanInterface[]|Plan
161
     * @throws \Exception
162
     */
163 2
    public function findPlans(OrderInterface $order)
164
    {
165 2
        $sales = $this->findSales($order);
166 2
        $plans = [];
167 2
        $lookPlanIds = [];
168 2
        foreach ($order->getActions() as $actionKey => $action) {
169 2
            if (empty($sales[$actionKey])) {
170
                /// it is ok when no sale found for upper resellers
171
                /// throw new \Exception('not found sale');
172
                $plans[$actionKey] = null;
173
            } else {
174 2
                $sale = $sales[$actionKey];
175
                /** @var Plan|PlanInterface[] $plan */
176 2
                $plan = $sale->getPlan();
177 2
                if ($plan->hasPrices()) {
178 2
                    $plans[$actionKey] = $plan;
179
                } else {
180 2
                    $lookPlanIds[$actionKey] = $plan->getId();
181
                }
182
            }
183
        }
184
185 2
        if ($lookPlanIds) {
186
            $foundPlans = $this->planRepository->findByIds($lookPlanIds);
187
            foreach ($foundPlans as $actionKey => $plan) {
188
                $foundPlans[$plan->getId()] = $plan;
189
            }
190
            foreach ($lookPlanIds as $actionKey => $planId) {
191
                if (empty($foundPlans[$planId])) {
192
                    throw new \Exception('not found plan');
193
                }
194
                $plans[$actionKey] = $foundPlans[$planId];
195
            }
196
        }
197
198 2
        return $plans;
199
    }
200
201
    /**
202
     * @param OrderInterface $order
203
     * @return SaleInterface[]|Sale
204
     */
205 2
    public function findSales(OrderInterface $order)
206
    {
207 2
        $sales = [];
208 2
        $lookActions = [];
209 2
        foreach ($order->getActions() as $actionKey => $action) {
210 2
            $sale = $action->getSale();
211 2
            if ($sale) {
212
                $sales[$actionKey] = $sale;
213
            } else {
214 2
                $lookActions[$actionKey] = $action;
215
            }
216
        }
217
218 2
        if ($lookActions) {
219 2
            $lookOrder = new Order(null, $order->getCustomer(), $lookActions);
220 2
            $foundSales = $this->saleRepository->findByOrder($lookOrder);
221 2
            foreach ($foundSales as $actionKey => $plan) {
222 2
                $sales[$actionKey] = $plan;
223
            }
224
        }
225
226 2
        return $sales;
227
    }
228
}
229