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-2020, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\php\billing\tools; |
12
|
|
|
|
13
|
|
|
use hiqdev\php\billing\bill\BillInterface; |
14
|
|
|
use hiqdev\php\billing\bill\BillRepositoryInterface; |
15
|
|
|
use hiqdev\php\billing\charge\ChargeInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Andrii Vasyliev <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class DbMergingAggregator implements AggregatorInterface, MergerInterface |
21
|
|
|
{ |
22
|
|
|
protected BillRepositoryInterface $billRepository; |
23
|
|
|
protected MergerInterface $merger; |
24
|
|
|
private AggregatorInterface $localAggregator; |
25
|
|
|
|
26
|
|
|
public function __construct( |
27
|
|
|
AggregatorInterface $localAggregator, |
28
|
|
|
BillRepositoryInterface $billRepository, |
29
|
|
|
MergerInterface $merger |
30
|
|
|
) { |
31
|
|
|
$this->billRepository = $billRepository; |
32
|
|
|
$this->merger = $merger; |
33
|
|
|
$this->localAggregator = $localAggregator; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function mergeBills(array $bills): array |
37
|
|
|
{ |
38
|
|
|
$dbBills = $this->billRepository->findByUniqueness($bills); |
39
|
|
|
$filteredLocalBills = $this->excludeLocalOnlyZeroBills($bills, $dbBills); |
40
|
|
|
|
41
|
|
|
return $this->merger->mergeBills(array_merge($filteredLocalBills, $dbBills)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function mergeBill(BillInterface $first, BillInterface $other): BillInterface |
45
|
|
|
{ |
46
|
|
|
return $this->merger->mergeBill($first, $other); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Aggregates given Charges to Bills. |
51
|
|
|
* Then merges them with Bills from DB. |
52
|
|
|
* |
53
|
|
|
* @param ChargeInterface[]|ChargeInterface[][] $charges |
54
|
|
|
* @return BillInterface[] |
55
|
|
|
* @throws \Exception |
56
|
|
|
*/ |
57
|
|
|
public function aggregateCharges(array $charges): array |
58
|
|
|
{ |
59
|
|
|
$localBills = $this->localAggregator->aggregateCharges($charges); |
60
|
|
|
$bills = $this->mergeBills($localBills); |
61
|
|
|
|
62
|
|
|
return $bills; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* When a new Zero bill is being produced, it should not be persisted |
67
|
|
|
* unless there is already a bill of this uniqueness in the DBMS. |
68
|
|
|
* |
69
|
|
|
* @param BillInterface[] $localBills |
70
|
|
|
* @param BillInterface[] $dbBills |
71
|
|
|
* @return BillInterface[] |
72
|
|
|
*/ |
73
|
|
|
private function excludeLocalOnlyZeroBills(array $localBills, array $dbBills): array |
74
|
|
|
{ |
75
|
|
|
foreach ($localBills as $i => $localBill) { |
76
|
|
|
foreach ($localBill->getCharges() as $charge) { |
77
|
|
|
if ($charge->hasEvents()) { |
|
|
|
|
78
|
|
|
continue 2; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
$isZeroSum = $localBill->getSum()->getAmount() === "0"; |
82
|
|
|
if (!$isZeroSum) { |
83
|
|
|
continue; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$localUniqueString = $localBill->getUniqueString(); |
87
|
|
|
foreach ($dbBills as $dbBill) { |
88
|
|
|
if ($dbBill->getUniqueString() === $localUniqueString) { |
89
|
|
|
continue 2; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
unset($localBills[$i]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $localBills; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|