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\charge; |
12
|
|
|
|
13
|
|
|
use hiqdev\php\billing\bill\Bill; |
14
|
|
|
use hiqdev\php\billing\bill\BillInterface; |
15
|
|
|
use hiqdev\php\units\QuantityInterface; |
16
|
|
|
use Money\Money; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Andrii Vasyliev <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Aggregator implements AggregatorInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var BillInterface[] |
25
|
|
|
*/ |
26
|
|
|
protected $bills = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var GeneralizerInterface |
30
|
|
|
*/ |
31
|
|
|
protected $generalizer; |
32
|
|
|
|
33
|
1 |
|
public function __construct(GeneralizerInterface $generalizer) |
34
|
|
|
{ |
35
|
1 |
|
$this->generalizer = $generalizer; |
36
|
1 |
|
} |
37
|
|
|
|
38
|
1 |
|
public function aggregateCharges(array $charges) |
39
|
|
|
{ |
40
|
1 |
|
$bills = []; |
41
|
1 |
|
foreach ($charges as $charge) { |
42
|
1 |
|
if (is_array($charge)) { |
43
|
1 |
|
$others = $this->aggregateCharges($charge); |
44
|
1 |
|
$bills = $this->aggregateBills($bills, $others); |
45
|
1 |
|
} elseif ($charge instanceof ChargeInterface) { |
46
|
1 |
|
$bill = $this->generalizer->createBill($charge); |
47
|
1 |
|
$bills = $this->aggregateBills($bills, [$bill]); |
48
|
|
|
} else { |
49
|
1 |
|
throw new \Exception('not a Charge given to Aggregator'); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
return $bills; |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Aggregate arrays of bills. |
58
|
|
|
* @param BillInterface[] $bills |
59
|
|
|
* @param BillInterface[] $others |
60
|
|
|
* @return BillInterface[] |
61
|
|
|
*/ |
62
|
1 |
|
public function aggregateBills(array $bills, array $others) |
63
|
|
|
{ |
64
|
1 |
|
foreach ($others as $bill) { |
65
|
1 |
|
$uid = $bill->getUniqueString(); |
|
|
|
|
66
|
1 |
|
if (empty($bills[$uid])) { |
67
|
1 |
|
$bills[$uid] = $bill; |
68
|
|
|
} else { |
69
|
1 |
|
$bills[$uid] = $this->aggregateBill($bills[$uid], $bill); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
return $bills; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
public function aggregateBill(BillInterface $first, BillInterface $other) |
77
|
|
|
{ |
78
|
1 |
|
return new Bill( |
79
|
1 |
|
null, |
80
|
1 |
|
$first->getType(), |
81
|
1 |
|
$first->getTime(), |
|
|
|
|
82
|
1 |
|
$this->aggregateSum($first->getSum(), $other->getSum()), |
83
|
1 |
|
$this->aggregateQuantity($first->getQuantity(), $other->getQuantity()), |
84
|
1 |
|
$first->getCustomer(), |
85
|
1 |
|
$first->getTarget(), |
86
|
1 |
|
$first->getPlan(), |
87
|
1 |
|
array_merge($first->getCharges(), $other->getCharges()) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
public function aggregateSum(Money $first, Money $other) |
92
|
|
|
{ |
93
|
1 |
|
return $first->add($other); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
public function aggregateQuantity(QuantityInterface $first, QuantityInterface $other) |
97
|
|
|
{ |
98
|
1 |
|
return $first->add($other); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|