Passed
Push — master ( 71fa82...7cf500 )
by Jan
04:35
created

PartLot   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 272
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 44
c 2
b 0
f 0
dl 0
loc 272
rs 10
wmc 24

20 Methods

Rating   Name   Duplication   Size   Complexity  
A isNeedsRefill() 0 3 1
A getComment() 0 3 1
A isExpired() 0 8 2
A getName() 0 3 1
A getExpirationDate() 0 3 1
A setStorageLocation() 0 5 1
A getAmount() 0 7 3
A __clone() 0 6 2
A getIDString() 0 3 1
A isInstockUnknown() 0 3 1
A setInstockUnknown() 0 5 1
A setExpirationDate() 0 5 1
A setNeedsRefill() 0 5 1
A getPart() 0 3 1
A setComment() 0 5 1
A setAmount() 0 5 1
A getDescription() 0 3 1
A setPart() 0 5 1
A getStorageLocation() 0 3 1
A setDescription() 0 5 1
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
declare(strict_types=1);
22
23
/**
24
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
25
 *
26
 * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
27
 *
28
 * This program is free software; you can redistribute it and/or
29
 * modify it under the terms of the GNU General Public License
30
 * as published by the Free Software Foundation; either version 2
31
 * of the License, or (at your option) any later version.
32
 *
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU General Public License
39
 * along with this program; if not, write to the Free Software
40
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
41
 */
42
43
namespace App\Entity\Parts;
44
45
use App\Entity\Base\AbstractDBElement;
46
use App\Entity\Base\TimestampTrait;
47
use App\Entity\Contracts\NamedElementInterface;
48
use App\Entity\Contracts\TimeStampableInterface;
49
use App\Validator\Constraints\Selectable;
50
use App\Validator\Constraints\ValidPartLot;
51
use DateTime;
52
use Doctrine\ORM\Mapping as ORM;
53
use Exception;
54
use Symfony\Component\Validator\Constraints as Assert;
55
56
/**
57
 * This entity describes a lot where parts can be stored.
58
 * It is the connection between a part and its store locations.
59
 *
60
 * @ORM\Entity()
61
 * @ORM\Table(name="part_lots")
62
 * @ORM\HasLifecycleCallbacks()
63
 * @ValidPartLot()
64
 */
65
class PartLot extends AbstractDBElement implements TimeStampableInterface, NamedElementInterface
66
{
67
    use TimestampTrait;
68
69
    /**
70
     * @var string A short description about this lot, shown in table
71
     * @ORM\Column(type="text")
72
     */
73
    protected $description = '';
74
75
    /**
76
     * @var string A comment stored with this lot.
77
     * @ORM\Column(type="text")
78
     */
79
    protected $comment = '';
80
81
    /**
82
     * @var ?DateTime Set a time until when the lot must be used.
83
     *                Set to null, if the lot can be used indefinitely.
84
     * @ORM\Column(type="datetime", name="expiration_date", nullable=true)
85
     */
86
    protected $expiration_date;
87
88
    /**
89
     * @var Storelocation|null The storelocation of this lot
90
     * @ORM\ManyToOne(targetEntity="Storelocation")
91
     * @ORM\JoinColumn(name="id_store_location", referencedColumnName="id", nullable=true)
92
     * @Selectable()
93
     */
94
    protected $storage_location;
95
96
    /**
97
     * @var bool If this is set to true, the instock amount is marked as not known
98
     * @ORM\Column(type="boolean")
99
     */
100
    protected $instock_unknown = false;
101
102
    /**
103
     * @var float For continuous sizes (length, volume, etc.) the instock is saved here.
104
     * @ORM\Column(type="float")
105
     * @Assert\PositiveOrZero()
106
     */
107
    protected $amount = 0;
108
109
    /**
110
     * @var bool Determines if this lot was manually marked for refilling.
111
     * @ORM\Column(type="boolean")
112
     */
113
    protected $needs_refill = false;
114
115
    /**
116
     * @var Part The part that is stored in this lot
117
     * @ORM\ManyToOne(targetEntity="Part", inversedBy="partLots")
118
     * @ORM\JoinColumn(name="id_part", referencedColumnName="id", nullable=false, onDelete="CASCADE")
119
     * @Assert\NotNull()
120
     */
121
    protected $part;
122
123
    public function __clone()
124
    {
125
        if ($this->id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->id of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
126
            $this->addedDate = null;
127
        }
128
        parent::__clone();
129
    }
130
131
    /**
132
     * Returns the ID as an string, defined by the element class.
133
     * This should have a form like P000014, for a part with ID 14.
134
     *
135
     * @return string The ID as a string;
136
     */
137
    public function getIDString(): string
138
    {
139
        return 'PL'.$this->getID();
140
    }
141
142
    /**
143
     * Check if the current part lot is expired.
144
     * This is the case, if the expiration date is greater the the current date.
145
     *
146
     * @return bool|null True, if the part lot is expired. Returns null, if no expiration date was set.
147
     *
148
     * @throws Exception If an error with the DateTime occurs
149
     */
150
    public function isExpired(): ?bool
151
    {
152
        if (null === $this->expiration_date) {
153
            return null;
154
        }
155
156
        //Check if the expiration date is bigger then current time
157
        return $this->expiration_date < new DateTime('now');
158
    }
159
160
    /**
161
     * Gets the description of the part lot. Similar to a "name" of the part lot.
162
     *
163
     * @return string
164
     */
165
    public function getDescription(): string
166
    {
167
        return $this->description;
168
    }
169
170
    /**
171
     * Sets the description of the part lot.
172
     *
173
     * @return PartLot
174
     */
175
    public function setDescription(string $description): self
176
    {
177
        $this->description = $description;
178
179
        return $this;
180
    }
181
182
    /**
183
     * Gets the comment for this part lot.
184
     *
185
     * @return string
186
     */
187
    public function getComment(): string
188
    {
189
        return $this->comment;
190
    }
191
192
    /**
193
     * Sets the comment for this part lot.
194
     *
195
     * @return PartLot
196
     */
197
    public function setComment(string $comment): self
198
    {
199
        $this->comment = $comment;
200
201
        return $this;
202
    }
203
204
    /**
205
     * Gets the expiration date for the part lot. Returns null, if no expiration date was set.
206
     *
207
     * @return DateTime|null
208
     */
209
    public function getExpirationDate(): ?DateTime
210
    {
211
        return $this->expiration_date;
212
    }
213
214
    /**
215
     * Sets the expiration date for the part lot. Set to null, if the part lot does not expires.
216
     *
217
     * @param DateTime $expiration_date
218
     *
219
     * @return PartLot
220
     */
221
    public function setExpirationDate(?DateTime $expiration_date): self
222
    {
223
        $this->expiration_date = $expiration_date;
224
225
        return $this;
226
    }
227
228
    /**
229
     * Gets the storage location, where this part lot is stored.
230
     *
231
     * @return Storelocation|null The store location where this part is stored
232
     */
233
    public function getStorageLocation(): ?Storelocation
234
    {
235
        return $this->storage_location;
236
    }
237
238
    /**
239
     * Sets the storage location, where this part lot is stored.
240
     *
241
     * @return PartLot
242
     */
243
    public function setStorageLocation(?Storelocation $storage_location): self
244
    {
245
        $this->storage_location = $storage_location;
246
247
        return $this;
248
    }
249
250
    /**
251
     * Return the part that is stored in this part lot.
252
     *
253
     * @return Part
254
     */
255
    public function getPart(): Part
256
    {
257
        return $this->part;
258
    }
259
260
    /**
261
     * Sets the part that is stored in this part lot.
262
     *
263
     * @return PartLot
264
     */
265
    public function setPart(Part $part): self
266
    {
267
        $this->part = $part;
268
269
        return $this;
270
    }
271
272
    /**
273
     * Checks if the instock value in the part lot is unknown.
274
     *
275
     * @return bool
276
     */
277
    public function isInstockUnknown(): bool
278
    {
279
        return $this->instock_unknown;
280
    }
281
282
    /**
283
     * Set the unknown instock status of this part lot.
284
     *
285
     * @return PartLot
286
     */
287
    public function setInstockUnknown(bool $instock_unknown): self
288
    {
289
        $this->instock_unknown = $instock_unknown;
290
291
        return $this;
292
    }
293
294
    /**
295
     * @return float
296
     */
297
    public function getAmount(): float
298
    {
299
        if ($this->part instanceof Part && ! $this->part->useFloatAmount()) {
300
            return round($this->amount);
301
        }
302
303
        return (float) $this->amount;
304
    }
305
306
    public function setAmount(float $new_amount): self
307
    {
308
        $this->amount = $new_amount;
309
310
        return $this;
311
    }
312
313
    /**
314
     * @return bool
315
     */
316
    public function isNeedsRefill(): bool
317
    {
318
        return $this->needs_refill;
319
    }
320
321
    /**
322
     * @return PartLot
323
     */
324
    public function setNeedsRefill(bool $needs_refill): self
325
    {
326
        $this->needs_refill = $needs_refill;
327
328
        return $this;
329
    }
330
331
    /**
332
     * @inheritDoc
333
     */
334
    public function getName(): string
335
    {
336
        return $this->description;
337
    }
338
}
339