1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Del\Expenses\Service; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Del\Expenses\Criteria\EntryCriteria; |
7
|
|
|
use Del\Expenses\Entity\EntryInterface; |
8
|
|
|
use Del\Expenses\Entity\Expenditure; |
9
|
|
|
use Del\Expenses\Entity\Income; |
10
|
|
|
use Del\Expenses\Repository\EntryRepository; |
11
|
|
|
use Del\Expenses\Value\Category; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class ExpensesService |
15
|
|
|
{ |
16
|
|
|
/** @var float $vatRate */ |
17
|
|
|
private $vatRate; |
18
|
|
|
|
19
|
|
|
/** @var EntryRepository $repository */ |
20
|
|
|
protected $repository; |
21
|
|
|
|
22
|
6 |
|
public function __construct(EntryRepository $repository, $vatRate = 20) |
23
|
|
|
{ |
24
|
6 |
|
$this->vatRate = $vatRate; |
|
|
|
|
25
|
6 |
|
$this->repository = $repository; |
26
|
6 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return EntryRepository |
30
|
|
|
*/ |
31
|
3 |
|
private function getRepository() |
32
|
|
|
{ |
33
|
3 |
|
return $this->repository; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array $data |
38
|
|
|
* @param EntryInterface $entry |
39
|
|
|
* @return EntryInterface |
40
|
|
|
*/ |
41
|
6 |
|
private function setFromArray(array $data, EntryInterface $entry) |
42
|
|
|
{ |
43
|
6 |
|
if(!$data['date'] instanceof DateTime) { |
44
|
2 |
|
$data['date'] = new DateTime($data['date']); |
45
|
2 |
|
} |
46
|
6 |
|
$entry->setId($data['userId']) |
47
|
6 |
|
->setUserId($data['userId']) |
48
|
6 |
|
->setCategory(new Category($data['category'])) |
49
|
6 |
|
->setAmount($data['amount']) |
50
|
6 |
|
->setDate($data['date']) |
51
|
6 |
|
->setDescription($data['description']) |
52
|
6 |
|
->setNote($data['note']); |
53
|
6 |
|
return $entry; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return Expenditure |
58
|
|
|
*/ |
59
|
4 |
|
public function createExpenditureFromArray(array $data) |
60
|
|
|
{ |
61
|
4 |
|
$expenditure = new Expenditure(); |
62
|
|
|
/** @var Expenditure $expenditure */ |
63
|
4 |
|
$expenditure = $this->setFromArray($data, $expenditure); |
64
|
4 |
|
return $expenditure; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return Income |
69
|
|
|
*/ |
70
|
3 |
|
public function createIncomeFromArray(array $data) |
71
|
|
|
{ |
72
|
3 |
|
$income = new Income(); |
73
|
|
|
/** @var Income $income */ |
74
|
3 |
|
$income = $this->setFromArray($data, $income); |
75
|
3 |
|
return $income; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Pass an Income, Expenditure, or Expense Claim |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
1 |
|
public function toArray(EntryInterface $entry) |
83
|
|
|
{ |
84
|
|
|
return [ |
85
|
1 |
|
'id' => $entry->getId(), |
86
|
1 |
|
'userId' => $entry->getUserId(), |
87
|
1 |
|
'date' => $entry->getDate(), |
88
|
1 |
|
'amount' => $entry->getAmount(), |
89
|
1 |
|
'vatRate' => $entry->getVatRate(), |
90
|
1 |
|
'vat' => $entry->getVat(), |
91
|
1 |
|
'total' => $entry->getTotal(), |
92
|
1 |
|
'description' => $entry->getDescription(), |
93
|
1 |
|
'category' => $entry->getCategory()->getValue(), |
94
|
1 |
|
'note' => $entry->getNote(), |
95
|
1 |
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Income $income |
100
|
|
|
* @return Income |
101
|
|
|
*/ |
102
|
2 |
|
public function saveIncome(Income $income) |
103
|
|
|
{ |
104
|
2 |
|
return $this->getRepository()->save($income); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param Income $income |
109
|
|
|
*/ |
110
|
2 |
|
public function deleteIncome(Income $income) |
111
|
|
|
{ |
112
|
2 |
|
$this->deleteEntry($income); |
113
|
2 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param $id |
117
|
|
|
* @return Income |
118
|
|
|
*/ |
119
|
1 |
|
public function findIncomeById($id) |
120
|
|
|
{ |
121
|
1 |
|
$criteria = new EntryCriteria(); |
122
|
1 |
|
$criteria->setId($id); |
123
|
1 |
|
return $this->getRepository()->findOneByCriteria($criteria); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param Expenditure $expenditure |
128
|
|
|
* @return Expenditure |
129
|
|
|
*/ |
130
|
2 |
|
public function saveExpenditure(Expenditure $expenditure) |
131
|
|
|
{ |
132
|
2 |
|
return $this->getRepository()->save($expenditure); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param Expenditure $expenditure |
137
|
|
|
*/ |
138
|
2 |
|
public function deleteExpenditure(Expenditure $expenditure) |
139
|
|
|
{ |
140
|
2 |
|
$this->deleteEntry($expenditure); |
141
|
2 |
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param $id |
145
|
|
|
* @return Expenditure |
146
|
|
|
*/ |
147
|
1 |
|
public function findExpenditureById($id) |
148
|
|
|
{ |
149
|
1 |
|
$criteria = new EntryCriteria(); |
150
|
1 |
|
$criteria->setId($id); |
151
|
1 |
|
return $this->getRepository()->findOneByCriteria($criteria); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param EntryCriteria $criteria |
156
|
|
|
* @return array |
157
|
|
|
*/ |
158
|
1 |
|
public function findByCriteria(EntryCriteria $criteria) |
159
|
|
|
{ |
160
|
1 |
|
return $this->getRepository()->findByCriteria($criteria); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param EntryInterface $entry |
165
|
|
|
*/ |
166
|
3 |
|
public function deleteEntry(EntryInterface $entry) |
167
|
|
|
{ |
168
|
3 |
|
$this->getRepository()->delete($entry); |
169
|
3 |
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return float |
173
|
|
|
*/ |
174
|
|
|
public function getVatRate() |
175
|
|
|
{ |
176
|
|
|
return $this->vatRate; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param float $vatRate |
181
|
|
|
* @return ExpensesService |
182
|
|
|
*/ |
183
|
|
|
public function setVatRate($vatRate) |
184
|
|
|
{ |
185
|
|
|
$this->vatRate = $vatRate; |
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
} |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.