Completed
Push — master ( 6bfaa4...9a5e12 )
by Hannes
02:00
created

InvoiceBuilder::setCurrency()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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