1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Del\Expenses\Entity; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Del\Expenses\Value\Category; |
7
|
|
|
use LogicException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @Entity(repositoryClass="Del\Expenses\Repository\EntryRepository") |
11
|
|
|
* @InheritanceType("SINGLE_TABLE") |
12
|
|
|
* @DiscriminatorColumn(name="type", type="string") |
13
|
|
|
* @DiscriminatorMap({"IN" = "Del\Expenses\Entity\Income", "OUT" = "Del\Expenses\Entity\Expenditure"}) |
14
|
|
|
*/ |
15
|
|
|
abstract class Entry implements EntryInterface |
16
|
|
|
{ |
17
|
|
|
const VAT_NONE = 0; |
18
|
|
|
const VAT_EXC = 1; |
19
|
|
|
const VAT_INC = 2; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @Id |
23
|
|
|
* @Column(type="integer") |
24
|
|
|
* @GeneratedValue |
25
|
|
|
*/ |
26
|
|
|
private $id; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @Column(type="integer") |
30
|
|
|
*/ |
31
|
|
|
private $userId; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @Column(type="datetime") |
35
|
|
|
* @var DateTime |
36
|
|
|
*/ |
37
|
|
|
private $date; |
38
|
|
|
|
39
|
|
|
/** @Column(type="decimal",precision=11,scale=2) */ |
40
|
|
|
private $amount; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @Column(type="decimal",precision=11,scale=2) |
44
|
|
|
* @var float $vatRate |
45
|
|
|
*/ |
46
|
|
|
private $vatRate; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @Column(type="decimal",precision=11,scale=2) |
50
|
|
|
* @var float |
51
|
|
|
*/ |
52
|
|
|
private $vat; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @Column(type="decimal",precision=11,scale=2) |
56
|
|
|
* @var float |
57
|
|
|
*/ |
58
|
|
|
private $total; |
59
|
|
|
|
60
|
|
|
/** @Column(type="string",length=50,nullable=true) */ |
61
|
|
|
private $category; |
62
|
|
|
|
63
|
|
|
/** @Column(type="string",length=50,nullable=true) */ |
64
|
|
|
private $description; |
65
|
|
|
|
66
|
|
|
/** @Column(type="string",length=255,nullable=true) */ |
67
|
|
|
private $note; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Entry constructor. |
71
|
|
|
* @param int $vatRate |
72
|
|
|
*/ |
73
|
16 |
|
public function __construct($vatRate = 0) |
74
|
|
|
{ |
75
|
16 |
|
$this->vatRate = $vatRate; |
|
|
|
|
76
|
16 |
|
$this->amount = 0; |
77
|
16 |
|
$this->vat = 0; |
|
|
|
|
78
|
16 |
|
$this->total = 0; |
|
|
|
|
79
|
16 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return int |
83
|
|
|
*/ |
84
|
5 |
|
public function getId() |
85
|
|
|
{ |
86
|
5 |
|
return $this->id; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param int $id |
91
|
|
|
* @return Entry |
92
|
|
|
*/ |
93
|
4 |
|
public function setId($id) |
94
|
|
|
{ |
95
|
4 |
|
$this->id = (int) $id; |
96
|
4 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
5 |
|
public function getUserId() |
103
|
|
|
{ |
104
|
5 |
|
return $this->userId; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param mixed $userId |
109
|
|
|
* @return Entry |
110
|
|
|
*/ |
111
|
7 |
|
public function setUserId($userId) |
112
|
|
|
{ |
113
|
7 |
|
$this->userId = $userId; |
114
|
7 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return DateTime |
119
|
|
|
*/ |
120
|
3 |
|
public function getDate() |
121
|
|
|
{ |
122
|
3 |
|
return $this->date; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param DateTime $date |
127
|
|
|
* @return Entry |
128
|
|
|
*/ |
129
|
7 |
|
public function setDate(DateTime $date) |
130
|
|
|
{ |
131
|
7 |
|
$this->date = $date; |
132
|
7 |
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return float |
137
|
|
|
*/ |
138
|
7 |
|
public function getAmount() |
139
|
|
|
{ |
140
|
7 |
|
return $this->amount; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param float $amount |
145
|
|
|
* @return Entry |
146
|
|
|
*/ |
147
|
10 |
|
public function setAmount($amount, $vatPaid = self::VAT_NONE) |
148
|
|
|
{ |
149
|
10 |
|
if ($vatPaid != self::VAT_NONE && $this->vatRate == 0) { |
150
|
|
|
|
151
|
|
|
throw new LogicException('Set a VAT rate before setting the amount, if inc/ex VAT'); |
152
|
|
|
|
153
|
10 |
|
} elseif ($vatPaid == self::VAT_INC) { |
154
|
|
|
|
155
|
1 |
|
$fraction = 100 / $this->vatRate; |
156
|
1 |
|
$fraction ++ ; |
157
|
1 |
|
$vatAmount = $amount / $fraction; |
158
|
1 |
|
$total = $amount; |
159
|
1 |
|
$amount = $amount - $vatAmount; |
160
|
|
|
|
161
|
9 |
|
} elseif ($vatPaid == self::VAT_EXC) { |
162
|
|
|
|
163
|
3 |
|
$vatAmount = ($amount / 100) * $this->vatRate; |
164
|
3 |
|
$total = $amount + $vatAmount; |
165
|
|
|
|
166
|
|
|
} else { |
167
|
|
|
|
168
|
6 |
|
$vatAmount = 0; |
169
|
6 |
|
$total = $amount; |
170
|
6 |
|
$this->vatRate = 0; |
|
|
|
|
171
|
|
|
|
172
|
|
|
} |
173
|
10 |
|
$this->amount = $amount; |
174
|
10 |
|
$this->vat = $vatAmount; |
|
|
|
|
175
|
10 |
|
$this->total = $total; |
176
|
10 |
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return Category |
181
|
|
|
*/ |
182
|
3 |
|
public function getCategory() |
183
|
|
|
{ |
184
|
3 |
|
return new Category($this->category); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param Category $category |
189
|
|
|
* @return Entry |
190
|
|
|
*/ |
191
|
7 |
|
public function setCategory(Category $category) |
192
|
|
|
{ |
193
|
7 |
|
$this->category = $category->getValue(); |
194
|
7 |
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
3 |
|
public function getDescription() |
201
|
|
|
{ |
202
|
3 |
|
return $this->description; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param string $description |
207
|
|
|
* @return Entry |
208
|
|
|
*/ |
209
|
7 |
|
public function setDescription($description) |
210
|
|
|
{ |
211
|
7 |
|
$this->description = $description; |
212
|
7 |
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return string |
217
|
|
|
*/ |
218
|
3 |
|
public function getNote() |
219
|
|
|
{ |
220
|
3 |
|
return $this->note; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param string $note |
225
|
|
|
* @return Entry |
226
|
|
|
*/ |
227
|
7 |
|
public function setNote($note) |
228
|
|
|
{ |
229
|
7 |
|
$this->note = $note; |
230
|
7 |
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return float |
235
|
|
|
*/ |
236
|
5 |
|
public function getVatRate() |
237
|
|
|
{ |
238
|
5 |
|
return $this->vatRate; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @param float $vatRate |
243
|
|
|
* @return Entry |
244
|
|
|
*/ |
245
|
6 |
|
public function setVatRate($vatRate) |
246
|
|
|
{ |
247
|
6 |
|
$this->vatRate = $vatRate; |
248
|
6 |
|
return $this; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @return float |
253
|
|
|
*/ |
254
|
6 |
|
public function getVat() |
255
|
|
|
{ |
256
|
6 |
|
return $this->vat; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @return float |
261
|
|
|
*/ |
262
|
6 |
|
public function getTotal() |
263
|
|
|
{ |
264
|
6 |
|
return $this->total; |
265
|
|
|
} |
266
|
|
|
} |
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.