|
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\tools; |
|
12
|
|
|
|
|
13
|
|
|
use hiqdev\php\billing\bill\Bill; |
|
14
|
|
|
use hiqdev\php\billing\bill\BillInterface; |
|
15
|
|
|
use hiqdev\php\billing\bill\BillRepositoryInterface; |
|
16
|
|
|
use hiqdev\php\billing\charge\ChargeInterface; |
|
17
|
|
|
use hiqdev\php\billing\charge\GeneralizerInterface; |
|
18
|
|
|
use hiqdev\php\units\QuantityInterface; |
|
19
|
|
|
use Money\Money; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Andrii Vasyliev <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class DbMergingAggregator extends Aggregator |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var BillRepositoryInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $billRepository; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var MergerInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $merger; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct( |
|
37
|
|
|
GeneralizerInterface $generalizer, |
|
38
|
|
|
BillRepositoryInterface $billRepository, |
|
39
|
|
|
MergerInterface $merger |
|
40
|
|
|
) { |
|
41
|
|
|
parent::__construct($generalizer); |
|
42
|
|
|
$this->billRepository = $billRepository; |
|
43
|
|
|
$this->merger = $merger; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Aggregates given Charges to Bills. |
|
48
|
|
|
* Then merges them with Bills from DB. |
|
49
|
|
|
* @param ChargeInterface[]|ChargeInterface[][] $charges |
|
50
|
|
|
* @return BillInterface[] |
|
51
|
|
|
*/ |
|
52
|
|
|
public function aggregateCharges(array $charges): array |
|
53
|
|
|
{ |
|
54
|
|
|
$bills = parent::aggregateCharges($charges); |
|
55
|
|
|
$ids = $this->billRepository->findIds($bills); |
|
56
|
|
|
$fromdb = $this->billRepository->findByIds($ids); |
|
57
|
|
|
|
|
58
|
|
|
return $this->merger->mergeBills(array_merge($bills, $fromdb)); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|