Passed
Push — master ( ccdac1...a3c626 )
by Jan
04:20
created

PartLot::__clone()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
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\TimeStampableInterface;
48
use App\Validator\Constraints\Selectable;
49
use App\Validator\Constraints\ValidPartLot;
50
use DateTime;
51
use Doctrine\ORM\Mapping as ORM;
52
use Exception;
53
use Symfony\Component\Validator\Constraints as Assert;
54
55
/**
56
 * This entity describes a lot where parts can be stored.
57
 * It is the connection between a part and its store locations.
58
 *
59
 * @ORM\Entity()
60
 * @ORM\Table(name="part_lots")
61
 * @ORM\HasLifecycleCallbacks()
62
 * @ValidPartLot()
63
 */
64
class PartLot extends AbstractDBElement implements TimeStampableInterface
65
{
66
    use TimestampTrait;
67
68
    /**
69
     * @var string A short description about this lot, shown in table
70
     * @ORM\Column(type="text")
71
     */
72
    protected $description = '';
73
74
    /**
75
     * @var string A comment stored with this lot.
76
     * @ORM\Column(type="text")
77
     */
78
    protected $comment = '';
79
80
    /**
81
     * @var ?DateTime Set a time until when the lot must be used.
82
     *                Set to null, if the lot can be used indefinitely.
83
     * @ORM\Column(type="datetime", name="expiration_date", nullable=true)
84
     */
85
    protected $expiration_date;
86
87
    /**
88
     * @var Storelocation|null The storelocation of this lot
89
     * @ORM\ManyToOne(targetEntity="Storelocation")
90
     * @ORM\JoinColumn(name="id_store_location", referencedColumnName="id", nullable=true)
91
     * @Selectable()
92
     */
93
    protected $storage_location;
94
95
    /**
96
     * @var bool If this is set to true, the instock amount is marked as not known
97
     * @ORM\Column(type="boolean")
98
     */
99
    protected $instock_unknown = false;
100
101
    /**
102
     * @var float For continuous sizes (length, volume, etc.) the instock is saved here.
103
     * @ORM\Column(type="float")
104
     * @Assert\PositiveOrZero()
105
     */
106
    protected $amount = 0;
107
108
    /**
109
     * @var bool Determines if this lot was manually marked for refilling.
110
     * @ORM\Column(type="boolean")
111
     */
112
    protected $needs_refill = false;
113
114
    /**
115
     * @var Part The part that is stored in this lot
116
     * @ORM\ManyToOne(targetEntity="Part", inversedBy="partLots")
117
     * @ORM\JoinColumn(name="id_part", referencedColumnName="id", nullable=false, onDelete="CASCADE")
118
     * @Assert\NotNull()
119
     */
120
    protected $part;
121
122
    /**
123
     * Returns the ID as an string, defined by the element class.
124
     * This should have a form like P000014, for a part with ID 14.
125
     *
126
     * @return string The ID as a string;
127
     */
128
    public function getIDString(): string
129
    {
130
        return 'PL'.$this->getID();
131
    }
132
133
    /**
134
     * Check if the current part lot is expired.
135
     * This is the case, if the expiration date is greater the the current date.
136
     *
137
     * @return bool|null True, if the part lot is expired. Returns null, if no expiration date was set.
138
     *
139
     * @throws Exception If an error with the DateTime occurs
140
     */
141
    public function isExpired(): ?bool
142
    {
143
        if (null === $this->expiration_date) {
144
            return null;
145
        }
146
147
        //Check if the expiration date is bigger then current time
148
        return $this->expiration_date < new DateTime('now');
149
    }
150
151
    /**
152
     * Gets the description of the part lot. Similar to a "name" of the part lot.
153
     *
154
     * @return string
155
     */
156
    public function getDescription(): string
157
    {
158
        return $this->description;
159
    }
160
161
    /**
162
     * Sets the description of the part lot.
163
     *
164
     * @param  string  $description
165
     * @return PartLot
166
     */
167
    public function setDescription(string $description): self
168
    {
169
        $this->description = $description;
170
171
        return $this;
172
    }
173
174
    /**
175
     * Gets the comment for this part lot.
176
     *
177
     * @return string
178
     */
179
    public function getComment(): string
180
    {
181
        return $this->comment;
182
    }
183
184
    /**
185
     * Sets the comment for this part lot.
186
     *
187
     * @param  string  $comment
188
     * @return PartLot
189
     */
190
    public function setComment(string $comment): self
191
    {
192
        $this->comment = $comment;
193
194
        return $this;
195
    }
196
197
    /**
198
     * Gets the expiration date for the part lot. Returns null, if no expiration date was set.
199
     *
200
     * @return DateTime|null
201
     */
202
    public function getExpirationDate(): ?DateTime
203
    {
204
        return $this->expiration_date;
205
    }
206
207
    /**
208
     * Sets the expiration date for the part lot. Set to null, if the part lot does not expires.
209
     *
210
     * @param DateTime $expiration_date
211
     *
212
     * @return PartLot
213
     */
214
    public function setExpirationDate(?DateTime $expiration_date): self
215
    {
216
        $this->expiration_date = $expiration_date;
217
218
        return $this;
219
    }
220
221
    /**
222
     * Gets the storage location, where this part lot is stored.
223
     *
224
     * @return Storelocation|null The store location where this part is stored
225
     */
226
    public function getStorageLocation(): ?Storelocation
227
    {
228
        return $this->storage_location;
229
    }
230
231
    /**
232
     * Sets the storage location, where this part lot is stored.
233
     *
234
     * @param  Storelocation|null  $storage_location
235
     * @return PartLot
236
     */
237
    public function setStorageLocation(?Storelocation $storage_location): self
238
    {
239
        $this->storage_location = $storage_location;
240
241
        return $this;
242
    }
243
244
    /**
245
     * Return the part that is stored in this part lot.
246
     *
247
     * @return Part
248
     */
249
    public function getPart(): Part
250
    {
251
        return $this->part;
252
    }
253
254
    /**
255
     * Sets the part that is stored in this part lot.
256
     *
257
     * @param Part $part
258
     *
259
     * @return PartLot
260
     */
261
    public function setPart(Part $part): self
262
    {
263
        $this->part = $part;
264
265
        return $this;
266
    }
267
268
    /**
269
     * Checks if the instock value in the part lot is unknown.
270
     *
271
     * @return bool
272
     */
273
    public function isInstockUnknown(): bool
274
    {
275
        return $this->instock_unknown;
276
    }
277
278
    /**
279
     * Set the unknown instock status of this part lot.
280
     *
281
     * @param  bool  $instock_unknown
282
     * @return PartLot
283
     */
284
    public function setInstockUnknown(bool $instock_unknown): self
285
    {
286
        $this->instock_unknown = $instock_unknown;
287
288
        return $this;
289
    }
290
291
    /**
292
     * @return float
293
     */
294
    public function getAmount(): float
295
    {
296
        if ($this->part instanceof Part && ! $this->part->useFloatAmount()) {
297
            return round($this->amount);
298
        }
299
300
        return (float) $this->amount;
301
    }
302
303
    public function setAmount(float $new_amount): self
304
    {
305
        $this->amount = $new_amount;
306
307
        return $this;
308
    }
309
310
    /**
311
     * @return bool
312
     */
313
    public function isNeedsRefill(): bool
314
    {
315
        return $this->needs_refill;
316
    }
317
318
    /**
319
     * @param  bool  $needs_refill
320
     * @return PartLot
321
     */
322
    public function setNeedsRefill(bool $needs_refill): self
323
    {
324
        $this->needs_refill = $needs_refill;
325
326
        return $this;
327
    }
328
329
    public function __clone()
330
    {
331
        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...
332
            $this->addedDate = null;
333
        }
334
        parent::__clone();
335
    }
336
}
337