InvoiceBuilder::addVatToTotal()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (C) 2020-2025 Iain Cambridge
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by
10
 * the Free Software Foundation, either version 2.1 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace Parthenon\Invoice;
23
24
use Brick\Money\Money;
25
use Parthenon\Common\Address;
26
27
final class InvoiceBuilder
28
{
29
    /**
30
     * @var ItemInterface[]
31
     */
32
    private array $items = [];
33
34
    private string $vatNumber;
35
36
    private CountryRules $countryRules;
37
38
    private Address $billerAddress;
39
40
    private float $vatRate = 0.0;
41
42
    private bool $addVat = true;
43
44
    private \DateTimeInterface $createdAt;
45
46
    private string $currency = 'EUR';
47
48
    private InvoiceNumberGeneratorInterface $invoiceNumberGenerator;
49
50
    private $invoiceNumber;
51
52
    private string $deliveryAddress;
53
54
    private string $paymentDetails;
55
56
    public function setInvoiceNumberGenerator(InvoiceNumberGeneratorInterface $invoiceNumberGenerator): void
57
    {
58
        $this->invoiceNumberGenerator = $invoiceNumberGenerator;
59
    }
60
61
    public function setInvoiceNumber($invoiceNumber): void
62
    {
63
        $this->invoiceNumber = $invoiceNumber;
64
    }
65
66
    public function addVatToTotal(): self
67
    {
68
        $this->addVat = true;
69
70
        return $this;
71
    }
72
73
    public function deductVatFromTotal(): self
74
    {
75
        $this->addVat = false;
76
77
        return $this;
78
    }
79
80
    public function addItem(string $name, Money $value, array $options = []): self
81
    {
82
        $quantity = $options['quantity'] ?? 1;
83
        $vat = $options['vat'] ?? $this->vatRate;
84
        $type = $options['type'] ?? '';
85
        $description = $options['description'] ?? '';
86
87
        $item = new Item($name, $description, $value, $quantity, $vat, $this->currency);
88
        $item->setType($type);
89
90
        $this->items[] = $item;
91
92
        return $this;
93
    }
94
95
    public function build(): Invoice
96
    {
97
        $invoice = new Invoice($this->addVat);
98
        $invoice->setCurrency($this->currency);
99
100
        if (isset($this->deliveryAddress)) {
101
            $invoice->setDeliveryAddress($this->deliveryAddress);
102
        }
103
104
        if (isset($this->billerAddress)) {
105
            $invoice->setBillerAddress($this->billerAddress);
106
        }
107
108
        if (isset($this->createdAt)) {
109
            $invoice->setCreatedAt($this->createdAt);
110
        }
111
112
        if (isset($this->vatNumber)) {
113
            $invoice->setVatNumber($this->vatNumber);
114
        }
115
116
        if (isset($this->paymentDetails)) {
117
            $invoice->setPaymentDetails($this->paymentDetails);
118
        }
119
120
        foreach ($this->items as $item) {
121
            if (isset($this->billerAddress) && isset($this->countryRules)) {
122
                $this->countryRules->handleRules($item, $this->billerAddress);
123
            }
124
            $invoice->addItem($item);
125
        }
126
127
        if (isset($this->invoiceNumber)) {
128
            $invoice->setInvoiceNumber($this->invoiceNumber);
129
        } elseif (isset($this->invoiceNumberGenerator)) {
130
            $invoice->setInvoiceNumber($this->invoiceNumberGenerator->generateNumber($invoice));
131
        }
132
133
        return $invoice;
134
    }
135
136
    public function addVatNumber(string $vatNumber)
137
    {
138
        $this->vatNumber = $vatNumber;
139
    }
140
141
    public function setBillerAddress(Address $address)
142
    {
143
        $this->billerAddress = $address;
144
    }
145
146
    public function setCountryRules(CountryRules $countryRules): void
147
    {
148
        $this->countryRules = $countryRules;
149
    }
150
151
    public function setCreatedAt(\DateTimeInterface $createdAt): void
152
    {
153
        $this->createdAt = $createdAt;
154
    }
155
156
    public function setCurrency(string $currency): void
157
    {
158
        $this->currency = $currency;
159
    }
160
161
    public function setDeliveryAddress(string $deliveryAddress): void
162
    {
163
        $this->deliveryAddress = $deliveryAddress;
164
    }
165
166
    public function setPaymentDetails(string $paymentDetails): void
167
    {
168
        $this->paymentDetails = $paymentDetails;
169
    }
170
}
171