Passed
Push — master ( 08267b...db956f )
by Jan
04:49
created

Orderdetail::setSupplierpartnr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
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
 * part-db version 0.1
25
 * Copyright (C) 2005 Christoph Lechner
26
 * http://www.cl-projects.de/.
27
 *
28
 * part-db version 0.2+
29
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
30
 * http://code.google.com/p/part-db/
31
 *
32
 * Part-DB Version 0.4+
33
 * Copyright (C) 2016 - 2019 Jan Böhmer
34
 * https://github.com/jbtronics
35
 *
36
 * This program is free software; you can redistribute it and/or
37
 * modify it under the terms of the GNU General Public License
38
 * as published by the Free Software Foundation; either version 2
39
 * of the License, or (at your option) any later version.
40
 *
41
 * This program is distributed in the hope that it will be useful,
42
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
44
 * GNU General Public License for more details.
45
 *
46
 * You should have received a copy of the GNU General Public License
47
 * along with this program; if not, write to the Free Software
48
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
49
 */
50
51
namespace App\Entity\PriceInformations;
52
53
use App\Entity\Base\AbstractDBElement;
54
use App\Entity\Base\TimestampTrait;
55
use App\Entity\Contracts\NamedElementInterface;
56
use App\Entity\Contracts\TimeStampableInterface;
57
use App\Entity\Parts\Part;
58
use App\Entity\Parts\Supplier;
59
use DateTime;
60
use Doctrine\Common\Collections\ArrayCollection;
61
use Doctrine\Common\Collections\Collection;
62
use Doctrine\ORM\Mapping as ORM;
63
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
64
use Symfony\Component\Validator\Constraints as Assert;
65
66
/**
67
 * Class Orderdetail.
68
 *
69
 * @ORM\Table("`orderdetails`")
70
 * @ORM\Entity()
71
 * @ORM\HasLifecycleCallbacks()
72
 * @UniqueEntity({"supplierpartnr", "supplier", "part"})
73
 */
74
class Orderdetail extends AbstractDBElement implements TimeStampableInterface, NamedElementInterface
75
{
76
    use TimestampTrait;
77
78
    /**
79
     * @ORM\OneToMany(targetEntity="Pricedetail", mappedBy="orderdetail", cascade={"persist", "remove"}, orphanRemoval=true)
80
     * @Assert\Valid()
81
     * @ORM\OrderBy({"min_discount_quantity" = "ASC"})
82
     */
83
    protected $pricedetails;
84
85
    /**
86
     * @var string
87
     * @ORM\Column(type="string")
88
     */
89
    protected $supplierpartnr = '';
90
91
    /**
92
     * @var bool
93
     * @ORM\Column(type="boolean")
94
     */
95
    protected $obsolete = false;
96
97
    /**
98
     * @var string
99
     * @ORM\Column(type="string")
100
     * @Assert\Url()
101
     */
102
    protected $supplier_product_url = '';
103
104
    /**
105
     * @var Part
106
     * @ORM\ManyToOne(targetEntity="App\Entity\Parts\Part", inversedBy="orderdetails")
107
     * @ORM\JoinColumn(name="part_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
108
     * @Assert\NotNull()
109
     */
110
    protected $part;
111
112
    /**
113
     * @var Supplier
114
     * @ORM\ManyToOne(targetEntity="App\Entity\Parts\Supplier", inversedBy="orderdetails")
115
     * @ORM\JoinColumn(name="id_supplier", referencedColumnName="id")
116
     */
117
    protected $supplier;
118
119
    public function __construct()
120
    {
121
        $this->pricedetails = new ArrayCollection();
122
    }
123
124
    public function __clone()
125
    {
126
        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...
127
            $this->addedDate = null;
128
            $pricedetails = $this->pricedetails;
129
            $this->pricedetails = new ArrayCollection();
130
            //Set master attachment is needed
131
            foreach ($pricedetails as $pricedetail) {
132
                $this->addPricedetail(clone $pricedetail);
133
            }
134
        }
135
        parent::__clone();
136
    }
137
138
    /**
139
     * Helper for updating the timestamp. It is automatically called by doctrine before persisting.
140
     *
141
     * @ORM\PrePersist
142
     * @ORM\PreUpdate
143
     */
144
    public function updateTimestamps(): void
145
    {
146
        $this->lastModified = new DateTime('now');
147
        if (null === $this->addedDate) {
148
            $this->addedDate = new DateTime('now');
149
        }
150
151
        if ($this->part instanceof Part) {
0 ignored issues
show
introduced by
$this->part is always a sub-type of App\Entity\Parts\Part.
Loading history...
152
            $this->part->updateTimestamps();
153
        }
154
    }
155
156
    /********************************************************************************
157
     *
158
     *   Getters
159
     *
160
     *********************************************************************************/
161
162
    /**
163
     * Get the part.
164
     *
165
     * @return Part|null the part of this orderdetails
166
     */
167
    public function getPart(): ?Part
168
    {
169
        return $this->part;
170
    }
171
172
    /**
173
     * Get the supplier.
174
     *
175
     * @return Supplier the supplier of this orderdetails
176
     */
177
    public function getSupplier(): ?Supplier
178
    {
179
        return $this->supplier;
180
    }
181
182
    /**
183
     * Get the supplier part-nr.
184
     *
185
     * @return string the part-nr
186
     */
187
    public function getSupplierPartNr(): string
188
    {
189
        return $this->supplierpartnr;
190
    }
191
192
    /**
193
     * Get if this orderdetails is obsolete.
194
     *
195
     * "Orderdetails is obsolete" means that the part with that supplier-part-nr
196
     * is no longer available from the supplier of that orderdetails.
197
     *
198
     * @return bool * true if this part is obsolete at that supplier
199
     *              * false if this part isn't obsolete at that supplier
200
     */
201
    public function getObsolete(): bool
202
    {
203
        return (bool) $this->obsolete;
204
    }
205
206
    /**
207
     * Get the link to the website of the article on the suppliers website.
208
     *
209
     * @param bool $no_automatic_url Set this to true, if you only want to get the local set product URL for this Orderdetail
210
     *                               and not a automatic generated one, based from the Supplier
211
     *
212
     * @return string the link to the article
213
     */
214
    public function getSupplierProductUrl(bool $no_automatic_url = false): string
215
    {
216
        if ($no_automatic_url || '' !== $this->supplier_product_url) {
217
            return $this->supplier_product_url;
218
        }
219
220
        if (null === $this->getSupplier()) {
221
            return '';
222
        }
223
224
        return $this->getSupplier()->getAutoProductUrl($this->supplierpartnr); // maybe an automatic url is available...
225
    }
226
227
    /**
228
     * Get all pricedetails.
229
     *
230
     * @return Pricedetail[]|Collection all pricedetails as a one-dimensional array of Pricedetails objects,
231
     *                                  sorted by minimum discount quantity
232
     */
233
    public function getPricedetails(): Collection
234
    {
235
        return $this->pricedetails;
236
    }
237
238
    /**
239
     * Adds an pricedetail to this orderdetail.
240
     *
241
     * @param Pricedetail $pricedetail The pricedetail to add
242
     *
243
     * @return Orderdetail
244
     */
245
    public function addPricedetail(Pricedetail $pricedetail): self
246
    {
247
        $pricedetail->setOrderdetail($this);
248
        $this->pricedetails->add($pricedetail);
249
250
        return $this;
251
    }
252
253
    /**
254
     * Removes an pricedetail from this orderdetail.
255
     *
256
     * @return Orderdetail
257
     */
258
    public function removePricedetail(Pricedetail $pricedetail): self
259
    {
260
        $this->pricedetails->removeElement($pricedetail);
261
262
        return $this;
263
    }
264
265
    /**
266
     * Find the pricedetail that is correct for the desired amount (the one with the greatest discount value with a
267
     * minimum order amount of the wished quantity).
268
     *
269
     * @param float $quantity this is the quantity to choose the correct pricedetails
270
     *
271
     * @return Pricedetail|null: the price as a bcmath string. Null if there are no orderdetails for the given quantity
272
     */
273
    public function findPriceForQty(float $quantity = 1.0): ?Pricedetail
274
    {
275
        if ($quantity <= 0) {
276
            return null;
277
        }
278
279
        $all_pricedetails = $this->getPricedetails();
280
281
        $correct_pricedetail = null;
282
        foreach ($all_pricedetails as $pricedetail) {
283
            // choose the correct pricedetails for the chosen quantity ($quantity)
284
            if ($quantity < $pricedetail->getMinDiscountQuantity()) {
285
                break;
286
            }
287
288
            $correct_pricedetail = $pricedetail;
289
        }
290
291
        return $correct_pricedetail;
292
    }
293
294
    /********************************************************************************
295
     *
296
     *   Setters
297
     *
298
     *********************************************************************************/
299
300
    /**
301
     * Sets a new part with which this orderdetail is associated.
302
     *
303
     * @return Orderdetail
304
     */
305
    public function setPart(Part $part): self
306
    {
307
        $this->part = $part;
308
309
        return $this;
310
    }
311
312
    /**
313
     * Sets the new supplier associated with this orderdetail.
314
     *
315
     * @return Orderdetail
316
     */
317
    public function setSupplier(Supplier $new_supplier): self
318
    {
319
        $this->supplier = $new_supplier;
320
321
        return $this;
322
    }
323
324
    /**
325
     * Set the supplier part-nr.
326
     *
327
     * @param string $new_supplierpartnr the new supplier-part-nr
328
     *
329
     * @return Orderdetail
330
     * @return Orderdetail
331
     */
332
    public function setSupplierpartnr(string $new_supplierpartnr): self
333
    {
334
        $this->supplierpartnr = $new_supplierpartnr;
335
336
        return $this;
337
    }
338
339
    /**
340
     * Set if the part is obsolete at the supplier of that orderdetails.
341
     *
342
     * @param bool $new_obsolete true means that this part is obsolete
343
     *
344
     * @return Orderdetail
345
     * @return Orderdetail
346
     */
347
    public function setObsolete(bool $new_obsolete): self
348
    {
349
        $this->obsolete = $new_obsolete;
350
351
        return $this;
352
    }
353
354
    /**
355
     * Sets the custom product supplier URL for this order detail.
356
     * Set this to "", if the function getSupplierProductURL should return the automatic generated URL.
357
     *
358
     * @param string $new_url The new URL for the supplier URL
359
     *
360
     * @return Orderdetail
361
     */
362
    public function setSupplierProductUrl(string $new_url): self
363
    {
364
        //Only change the internal URL if it is not the auto generated one
365
        if ($new_url === $this->supplier->getAutoProductUrl($this->getSupplierPartNr())) {
366
            return $this;
367
        }
368
369
        $this->supplier_product_url = $new_url;
370
371
        return $this;
372
    }
373
374
    public function getName(): string
375
    {
376
        return $this->getSupplierPartNr();
377
    }
378
}
379