Issues (113)

src/bill/Bill.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * PHP Billing Library
4
 *
5
 * @link      https://github.com/hiqdev/php-billing
6
 * @package   php-billing
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2020, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\bill;
12
13
use DateTimeImmutable;
14
use hiqdev\php\billing\action\UsageInterval;
15
use hiqdev\php\billing\charge\ChargeInterface;
16
use hiqdev\php\billing\customer\CustomerInterface;
17
use hiqdev\php\billing\Exception\CannotReassignException;
18
use hiqdev\php\billing\plan\PlanInterface;
19
use hiqdev\php\billing\target\TargetInterface;
20
use hiqdev\php\billing\type\TypeInterface;
0 ignored issues
show
The type hiqdev\php\billing\type\TypeInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use hiqdev\php\units\QuantityInterface;
22
use Money\Money;
23
24
/**
25
 * Bill.
26
 *
27
 * @author Andrii Vasyliev <[email protected]>
28
 */
29
class Bill implements BillInterface
30
{
31
    /** @var int|string */
32
    protected $id;
33
34
    /** @var TypeInterface */
35
    protected $type;
36
37
    /** @var DateTimeImmutable */
38
    protected $time;
39
40
    /** @var Money */
41
    protected $sum;
42
43
    /** @var QuantityInterface */
44
    protected $quantity;
45
46
    /** @var CustomerInterface */
47
    protected $customer;
48
49
    /** @var TargetInterface */
50
    protected $target;
51
52
    /** @var BillRequisite */
53
    protected $requisite;
54
55
    /** @var PlanInterface */
56
    protected $plan;
57
58
    /** @var ChargeInterface[] */
59
    protected $charges = [];
60
61
    /** @var BillState */
62
    protected $state;
63 3
64
    /** @var string */
65
    protected $comment;
66
67
    /** @var UsageInterval */
68
    protected $usageInterval;
69
70
    public function __construct(
71
                            $id,
72
        TypeInterface $type,
73
        DateTimeImmutable $time,
74
        Money $sum,
75 3
        QuantityInterface $quantity,
76 3
        CustomerInterface $customer,
77 3
        TargetInterface $target = null,
78 3
        PlanInterface $plan = null,
79 3
        array $charges = [],
80 3
        BillState $state = null
81 3
    ) {
82 3
        $this->id           = $id;
83 3
        $this->type         = $type;
84 3
        $this->time         = $time;
85 3
        $this->sum          = $sum;
86
        $this->quantity     = $quantity;
87
        $this->customer     = $customer;
88
        $this->target       = $target;
89
        $this->plan         = $plan;
90
        $this->charges      = $charges;
91 1
        $this->state        = $state;
92
    }
93
94 1
    /**
95 1
     * Provides unique string.
96 1
     * Can be used to compare or aggregate bills.
97 1
     */
98 1
    public function getUniqueString(): string
99
    {
100
        $parts = [
101 1
            'currency'  => $this->sum->getCurrency()->getCode(),
102
            'buyer'     => $this->customer->getUniqueId(),
103
            'target'    => $this->target ? $this->target->getUniqueId() : null,
104
            'type'      => $this->type->getUniqueId(),
105
            'time'      => $this->time->format('c'),
106
        ];
107
108
        return implode('-', $parts);
109
    }
110
111
    public function getUsageInterval(): UsageInterval
112
    {
113
        if ($this->usageInterval === null) {
114 3
            $this->initializeWholeMonthUsageInterval();
115
        }
116 3
117
        return $this->usageInterval;
118
    }
119
120
    private function initializeWholeMonthUsageInterval(): void
121
    {
122
        $this->setUsageInterval(UsageInterval::wholeMonth($this->time));
123
    }
124
125
    public function calculatePrice()
126
    {
127
        $quantity = $this->quantity->getQuantity();
128
129
        return $quantity ? $this->sum->divide(sprintf('%.14F', $quantity)) : $this->sum;
130 1
    }
131
132 1
    /**
133
     * @return int|string
134
     */
135 3
    public function getId()
136
    {
137 3
        return $this->id;
138
    }
139
140
    public function setId($id)
141
    {
142
        if ($this->id === $id) {
143 3
            return;
144
        }
145 3
        if ($this->id !== null) {
146
            throw new CannotReassignException('bill id');
147
        }
148
        $this->id = $id;
149
    }
150
151 3
    public function getType(): TypeInterface
152
    {
153 3
        return $this->type;
154
    }
155
156
    public function getTime(): DateTimeImmutable
157
    {
158
        return $this->time;
159 3
    }
160
161 3
    public function getTarget(): ?TargetInterface
162
    {
163
        return $this->target;
164
    }
165
166
    public function getRequisite(): ?BillRequisite
167
    {
168
        return $this->requisite;
169
    }
170
171
    public function getCustomer(): CustomerInterface
172
    {
173
        return $this->customer;
174 3
    }
175
176 3
    public function getQuantity(): QuantityInterface
177
    {
178
        return $this->quantity;
179
    }
180
181
    public function setQuantity(QuantityInterface $quantity): BillInterface
182 3
    {
183
        $this->quantity = $quantity;
184 3
185
        return $this;
186
    }
187
188
    public function getSum(): Money
189
    {
190
        return $this->sum;
191
    }
192
193
    public function getPlan(): ?PlanInterface
194
    {
195 1
        return $this->plan;
196
    }
197 1
198
    public function hasCharges(): bool
199
    {
200
        return $this->charges !== [];
201
    }
202
203
    /**
204
     * @return ChargeInterface[]
205
     */
206
    public function getCharges(): array
207
    {
208
        return $this->charges;
209
    }
210
211
    /**
212
     * @param ChargeInterface[] $prices
213
     * @throws \Exception
214
     */
215
    public function setCharges(array $charges): self
216
    {
217
        if ($this->hasCharges()) {
218
            throw new CannotReassignException('bill charges');
219
        }
220
        $this->charges = $charges;
221
222
        return $this;
223
    }
224
225
    public function getState(): ?BillState
226
    {
227
        return $this->state;
228
    }
229
230
    public function setFinished(): void
231
    {
232
        $this->state = BillState::finished();
233
    }
234
235
    public function isFinished(): ?bool
236
    {
237
        return $this->state === null ? null : $this->state->isFinished();
238
    }
239
240
    public function getComment()
241
    {
242
        return $this->comment;
243
    }
244
245
    public function setComment(string $comment)
246
    {
247
        $this->comment = $comment;
248
    }
249
250
    public function jsonSerialize(): array
251
    {
252
        return array_filter(get_object_vars($this));
253
    }
254
255
    public function setUsageInterval(UsageInterval $usageInterval): void
256
    {
257
        $this->usageInterval = $usageInterval;
258
    }
259
}
260