-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItem.php
More file actions
100 lines (87 loc) · 2.23 KB
/
Item.php
File metadata and controls
100 lines (87 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace RockInvoiceItems;
use HTMLPurifier_Exception;
use ProcessWire\WireData;
use ProcessWire\WireException;
use ProcessWire\WirePermissionException;
use RockMoney\Money;
use function ProcessWire\rockmoney;
use function ProcessWire\wire;
class Item extends WireData
{
public string $text;
public Money $net;
public float $vat;
public float $quantity;
public Money $totalNet;
public Money $totalVat;
public Money $totalGross;
public function __construct(array $data = [])
{
$this->text = '';
$this->net = rockmoney()->parse(0);
$this->vat = 0;
$this->quantity = 0;
$this->importArray($data);
}
public function __debugInfo()
{
return [
'text' => $this->text,
'net' => $this->net,
'vat' => $this->vat,
'quantity' => $this->quantity,
'totalNet' => $this->totalNet,
'totalVat' => $this->totalVat,
'totalGross' => $this->totalGross,
];
}
public function float($data): float
{
return round((float) $data, 2);
}
/**
* Get array ready for json encodeing
* @return array
*/
public function getJsonArray(): array
{
return [
'text' => (string)$this->text,
'net' => $this->net->getFloat(),
'vat' => $this->float($this->vat),
'quantity' => $this->float($this->quantity),
];
}
/**
* Import plain array data
*/
private function importArray(array $data): void
{
foreach ($data as $key => $value) {
if ($key === 'text') $this->text = wire()->sanitizer->purify($value);
if ($key === 'net') $this->net = rockmoney($value);
if ($key === 'vat') $this->vat = $this->float($value);
if ($key === 'quantity') $this->quantity = $this->float($value);
}
$this->setTotals();
}
public function setTotals(): void
{
$this->totalNet = $this->net->times($this->quantity);
$this->totalVat = $this->net->times($this->quantity)->times($this->vatRate());
$this->totalGross = $this->net->times($this->quantity)->times(1 + $this->vatRate());
}
public function toJSON(): string
{
return json_encode($this->getJsonArray());
}
public function __toString()
{
return $this->toJSON();
}
public function vatRate(): float
{
return $this->vat / 100;
}
}