1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Awin\ReportTask\Bundle\ReportBundle\Service; |
4
|
|
|
|
5
|
|
|
use Awin\ReportTask\Bundle\ReportBundle\Model\Merchant; |
6
|
|
|
use Awin\ReportTask\Bundle\ReportBundle\Model\TransactionTable; |
7
|
|
|
use Awin\ReportTask\Bundle\ReportBundle\Utility\Helper; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* The merchant transaction aggregation service, it filter the transaction data with merchant_id |
11
|
|
|
* or date, and prepare the data for the report service class |
12
|
|
|
* |
13
|
|
|
* @date 24/06/2017 |
14
|
|
|
* @time 19:17 |
15
|
|
|
* @author Peng Yue <[email protected]> |
16
|
|
|
* @copyright 2004-2017 Peng Yue |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
class MerchantTransactionService implements MerchantTransactionServiceInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var TransactionTable |
23
|
|
|
*/ |
24
|
|
|
private $transactionRepository; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Merchant |
28
|
|
|
*/ |
29
|
|
|
private $merchantRepository; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The data to be report after filtered by merchant_id, date, if either exists. |
33
|
|
|
* each item has the filter csv data, and intersect all the items to get final filter data |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $data = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param TransactionTable $transactionRepository |
41
|
|
|
* |
42
|
|
|
* @return $this |
43
|
|
|
*/ |
44
|
3 |
|
public function setTransactionRepository($transactionRepository) |
45
|
|
|
{ |
46
|
3 |
|
$this->transactionRepository = $transactionRepository; |
47
|
|
|
|
48
|
3 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param Merchant $merchantRepository |
53
|
|
|
* |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
2 |
|
public function setMerchantRepository($merchantRepository) |
57
|
|
|
{ |
58
|
2 |
|
$this->merchantRepository = $merchantRepository; |
59
|
|
|
|
60
|
2 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get all the transactions by merchant id. |
65
|
|
|
* If merchant id is null, then the transaction on all the merchants will be returned |
66
|
|
|
* |
67
|
|
|
* @param int|null $merchantId |
68
|
|
|
* |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
3 |
|
public function filterTransactionsByMerchantId($merchantId = null) |
72
|
|
|
{ |
73
|
3 |
|
$this->data[] = (null === $merchantId) |
74
|
1 |
|
? $this->transactionRepository->getTransactions() |
75
|
2 |
|
: $this->data[] = $this->merchantRepository |
76
|
2 |
|
->setTransactionRepository($this->transactionRepository) |
77
|
2 |
|
->getTransactionsByMerchantId($merchantId); |
78
|
|
|
|
79
|
3 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the transactions by the date, |
84
|
|
|
* If the date is null, then all the transactions will be return |
85
|
|
|
* |
86
|
|
|
* @param string|null $date |
87
|
|
|
* |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
1 |
|
public function filterTransactionsByDate($date = null) |
91
|
|
|
{ |
92
|
1 |
|
$this->data[] = $this->transactionRepository->getTransactionsByDate($date); |
93
|
|
|
|
94
|
1 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Intersect and keep the same values transactions on each filter data set |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public function getReportData() |
103
|
|
|
{ |
104
|
|
|
//remove the empty array for the intersection later on |
105
|
3 |
|
$nonEmptyResults = array_filter($this->data, function($item) { |
106
|
3 |
|
return !empty($item); |
107
|
3 |
|
}); |
108
|
|
|
|
109
|
|
|
//get the first array from list and intersect with the rest items |
110
|
3 |
|
$result = array_shift($nonEmptyResults); |
111
|
|
|
|
112
|
3 |
|
foreach ($nonEmptyResults as $item) { |
113
|
2 |
|
$result = Helper::array_intersect_recursive($result, $item); |
114
|
|
|
} |
115
|
|
|
|
116
|
3 |
|
return $result; |
117
|
|
|
} |
118
|
|
|
} |