InvoiceBuilder::setBillDate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace byrokrat\billing;
6
7
use byrokrat\amount\Amount;
8
9
/**
10
 * Create complex invoices
11
 */
12
class InvoiceBuilder
13
{
14
    use AttributesTrait;
15
16
    /**
17
     * @var OcrTools Tools for validating and creating ocr numbers
18
     */
19
    private $ocrTools;
20
21
    /**
22
     * @var string Invoice serial number
23
     */
24
    private $serial;
25
26
    /**
27
     * @var ?AgentInterface Registered seller
28
     */
29
    private $seller;
30
31
    /**
32
     * @var ?AgentInterface Registered buyer
33
     */
34
    private $buyer;
35
36
    /**
37
     * @var string Payment reference number
38
     */
39
    private $ocr;
40
41
    /**
42
     * @var ItemBasket Container of charged items
43
     */
44
    private $itemBasket;
45
46
    /**
47
     * @var \DateTimeInterface Invoice creation date
48
     */
49
    private $billDate;
50
51
    /**
52
     * @var int Number of days before invoice expires
53
     */
54
    private $expiresAfter;
55
56
    /**
57
     * @var ?Amount Prepaid amound to deduct
58
     */
59
    private $deduction;
60
61
    /**
62
     * Reset values at construct
63
     */
64 8
    public function __construct(OcrTools $ocrTools = null)
65
    {
66 8
        $this->ocrTools = $ocrTools ?: new OcrTools;
67 8
        $this->reset();
68 8
    }
69
70
    /**
71
     * Reset builder values
72
     */
73 8
    public function reset(): self
74
    {
75 8
        $this->serial = '';
76 8
        $this->seller = null;
77 8
        $this->buyer = null;
78 8
        $this->ocr = '';
79 8
        $this->itemBasket = new ItemBasket;
80 8
        $this->billDate = new \DateTimeImmutable;
81 8
        $this->expiresAfter = 30;
82 8
        $this->deduction = null;
83 8
        $this->clearAttributes();
84 8
        return $this;
85
    }
86
87
    /**
88
     * Build invoice
89
     */
90 5
    public function buildInvoice(): Invoice
91
    {
92 5
        $invoice = new Invoice(
93 5
            $this->getSerial(),
94 5
            $this->getSeller(),
95 5
            $this->getBuyer(),
96 5
            $this->ocr,
97 5
            $this->itemBasket,
98 5
            $this->billDate,
99 5
            $this->expiresAfter,
100 5
            $this->deduction
101
        );
102
103 5
        foreach ($this->getAttributes() as $key => $value) {
104 1
            $invoice->setAttribute($key, $value);
105
        }
106
107 5
        return $invoice;
108
    }
109
110
    /**
111
     * Set invoice serial number
112
     */
113 8
    public function setSerial(string $serial): self
114
    {
115 8
        $this->serial = $serial;
116 8
        return $this;
117
    }
118
119
    /**
120
     * Get invoice serial number
121
     *
122
     * @throws Exception If serial is not set
123
     */
124 6
    public function getSerial(): string
125
    {
126 6
        if ($this->serial) {
127 5
            return $this->serial;
128
        }
129
130 1
        throw new Exception("Unable to create invoice: serial not set");
131
    }
132
133
    /**
134
     * Set seller
135
     */
136 8
    public function setSeller(AgentInterface $seller): self
137
    {
138 8
        $this->seller = $seller;
139 8
        return $this;
140
    }
141
142
    /**
143
     * Get seller
144
     *
145
     * @throws Exception If seller is not set
146
     */
147 6
    public function getSeller(): AgentInterface
148
    {
149 6
        if (isset($this->seller)) {
150 5
            return $this->seller;
151
        }
152
153 1
        throw new Exception("Unable to create Invoice: seller not set");
154
    }
155
156
    /**
157
     * Set buyer
158
     */
159 8
    public function setBuyer(AgentInterface $buyer): self
160
    {
161 8
        $this->buyer = $buyer;
162 8
        return $this;
163
    }
164
165
    /**
166
     * Get buyer
167
     *
168
     * @throws Exception If buyer is not set
169
     */
170 6
    public function getBuyer(): AgentInterface
171
    {
172 6
        if (isset($this->buyer)) {
173 5
            return $this->buyer;
174
        }
175
176 1
        throw new Exception("Unable to create Invoice: buyer not set");
177
    }
178
179
    /**
180
     * Set invoice reference number
181
     */
182 1
    public function setOcr(string $ocr): self
183
    {
184 1
        $this->ocr = $this->ocrTools->validateOcr($ocr);
185 1
        return $this;
186
    }
187
188
    /**
189
     * Generate invoice reference number from serial number
190
     */
191 1
    public function generateOcr(): self
192
    {
193 1
        $this->ocr = $this->ocrTools->createOcr($this->getSerial());
194 1
        return $this;
195
    }
196
197
    /**
198
     * Add billable to invoice
199
     */
200 1
    public function addItem(Billable $billable): self
201
    {
202 1
        $this->itemBasket->addItem(new ItemEnvelope($billable));
203 1
        return $this;
204
    }
205
206
    /**
207
     * Set date of invoice creation
208
     */
209 1
    public function setBillDate(\DateTimeInterface $date): self
210
    {
211 1
        $this->billDate = $date;
212 1
        return $this;
213
    }
214
215
    /**
216
     * Set number of days before invoice expires
217
     */
218 1
    public function setExpiresAfter(int $nrOfDays): self
219
    {
220 1
        $this->expiresAfter = $nrOfDays;
221 1
        return $this;
222
    }
223
224
    /**
225
     * Set deduction (amount prepaid)
226
     */
227 1
    public function setDeduction(Amount $deduction): self
228
    {
229 1
        $this->deduction = $deduction;
230 1
        return $this;
231
    }
232
}
233