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 |
|
isset($data['id']) ? $entry->setId($data['id']) : null; |
|
|
|
|
47
|
6 |
|
$entry->setUserId($data['userId']) |
48
|
6 |
|
->setCategory(new Category($data['category'])) |
49
|
6 |
|
->setVatRate($data['vatRate']) |
50
|
6 |
|
->setAmount($data['amount'], $data['vat']) |
51
|
6 |
|
->setDate($data['date']) |
52
|
6 |
|
->setDescription($data['description']) |
53
|
6 |
|
->setNote($data['note']); |
54
|
6 |
|
return $entry; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return Expenditure |
59
|
|
|
*/ |
60
|
4 |
|
public function createExpenditureFromArray(array $data) |
61
|
|
|
{ |
62
|
4 |
|
$expenditure = new Expenditure(); |
63
|
|
|
/** @var Expenditure $expenditure */ |
64
|
4 |
|
$expenditure = $this->setFromArray($data, $expenditure); |
65
|
4 |
|
return $expenditure; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return Income |
70
|
|
|
*/ |
71
|
3 |
|
public function createIncomeFromArray(array $data) |
72
|
|
|
{ |
73
|
3 |
|
$income = new Income(); |
74
|
|
|
/** @var Income $income */ |
75
|
3 |
|
$income = $this->setFromArray($data, $income); |
76
|
3 |
|
return $income; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Pass an Income, Expenditure, or Expense Claim |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
1 |
|
public function toArray(EntryInterface $entry) |
84
|
|
|
{ |
85
|
|
|
return [ |
86
|
1 |
|
'id' => $entry->getId(), |
87
|
1 |
|
'userId' => $entry->getUserId(), |
88
|
1 |
|
'date' => $entry->getDate(), |
89
|
1 |
|
'amount' => $entry->getAmount(), |
90
|
1 |
|
'vatRate' => $entry->getVatRate(), |
91
|
1 |
|
'vat' => $entry->getVat(), |
92
|
1 |
|
'total' => $entry->getTotal(), |
93
|
1 |
|
'description' => $entry->getDescription(), |
94
|
1 |
|
'category' => $entry->getCategory()->getValue(), |
95
|
1 |
|
'note' => $entry->getNote(), |
96
|
1 |
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param Income $income |
101
|
|
|
* @return Income |
102
|
|
|
*/ |
103
|
2 |
|
public function saveIncome(Income $income) |
104
|
|
|
{ |
105
|
2 |
|
return $this->getRepository()->save($income); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param Income $income |
110
|
|
|
*/ |
111
|
2 |
|
public function deleteIncome(Income $income) |
112
|
|
|
{ |
113
|
2 |
|
$this->deleteEntry($income); |
114
|
2 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param $id |
118
|
|
|
* @return Income |
119
|
|
|
*/ |
120
|
1 |
|
public function findIncomeById($id) |
121
|
|
|
{ |
122
|
1 |
|
$criteria = new EntryCriteria(); |
123
|
1 |
|
$criteria->setId($id); |
124
|
1 |
|
return $this->getRepository()->findOneByCriteria($criteria); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param Expenditure $expenditure |
129
|
|
|
* @return Expenditure |
130
|
|
|
*/ |
131
|
2 |
|
public function saveExpenditure(Expenditure $expenditure) |
132
|
|
|
{ |
133
|
2 |
|
return $this->getRepository()->save($expenditure); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param Expenditure $expenditure |
138
|
|
|
*/ |
139
|
2 |
|
public function deleteExpenditure(Expenditure $expenditure) |
140
|
|
|
{ |
141
|
2 |
|
$this->deleteEntry($expenditure); |
142
|
2 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param $id |
146
|
|
|
* @return Expenditure |
147
|
|
|
*/ |
148
|
1 |
|
public function findExpenditureById($id) |
149
|
|
|
{ |
150
|
1 |
|
$criteria = new EntryCriteria(); |
151
|
1 |
|
$criteria->setId($id); |
152
|
1 |
|
return $this->getRepository()->findOneByCriteria($criteria); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param EntryCriteria $criteria |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
1 |
|
public function findByCriteria(EntryCriteria $criteria) |
160
|
|
|
{ |
161
|
1 |
|
return $this->getRepository()->findByCriteria($criteria); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param EntryInterface $entry |
166
|
|
|
*/ |
167
|
3 |
|
public function deleteEntry(EntryInterface $entry) |
168
|
|
|
{ |
169
|
3 |
|
$this->getRepository()->delete($entry); |
170
|
3 |
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return float |
174
|
|
|
*/ |
175
|
|
|
public function getVatRate() |
176
|
|
|
{ |
177
|
|
|
return $this->vatRate; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param float $vatRate |
182
|
|
|
* @return ExpensesService |
183
|
|
|
*/ |
184
|
|
|
public function setVatRate($vatRate) |
185
|
|
|
{ |
186
|
|
|
$this->vatRate = $vatRate; |
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
} |
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.