Passed
Branch master (350f1b)
by Jan
04:53
created

Orderdetail::getObsolete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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 Doctrine\Common\Collections\ArrayCollection;
60
use Doctrine\Common\Collections\Collection;
61
use Doctrine\ORM\Mapping as ORM;
62
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
63
use Symfony\Component\Validator\Constraints as Assert;
64
65
/**
66
 * Class Orderdetail.
67
 *
68
 * @ORM\Table("`orderdetails`")
69
 * @ORM\Entity()
70
 * @ORM\HasLifecycleCallbacks()
71
 * @UniqueEntity({"supplierpartnr", "supplier", "part"})
72
 */
73
class Orderdetail extends AbstractDBElement implements TimeStampableInterface, NamedElementInterface
74
{
75
    use TimestampTrait;
76
77
    /**
78
     * @ORM\OneToMany(targetEntity="Pricedetail", mappedBy="orderdetail", cascade={"persist", "remove"}, orphanRemoval=true)
79
     * @Assert\Valid()
80
     * @ORM\OrderBy({"min_discount_quantity" = "ASC"})
81
     */
82
    protected $pricedetails;
83
84
    /**
85
     * @var string
86
     * @ORM\Column(type="string")
87
     */
88
    protected $supplierpartnr = '';
89
90
    /**
91
     * @var bool
92
     * @ORM\Column(type="boolean")
93
     */
94
    protected $obsolete = false;
95
96
    /**
97
     * @var string
98
     * @ORM\Column(type="string")
99
     * @Assert\Url()
100
     */
101
    protected $supplier_product_url = '';
102
103
    /**
104
     * @var Part
105
     * @ORM\ManyToOne(targetEntity="App\Entity\Parts\Part", inversedBy="orderdetails")
106
     * @ORM\JoinColumn(name="part_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
107
     * @Assert\NotNull()
108
     */
109
    protected $part;
110
111
    /**
112
     * @var Supplier
113
     * @ORM\ManyToOne(targetEntity="App\Entity\Parts\Supplier", inversedBy="orderdetails")
114
     * @ORM\JoinColumn(name="id_supplier", referencedColumnName="id")
115
     */
116
    protected $supplier;
117
118
    public function __construct()
119
    {
120
        $this->pricedetails = new ArrayCollection();
121
    }
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
            $pricedetails = $this->pricedetails;
128
            $this->pricedetails = new ArrayCollection();
129
            //Set master attachment is needed
130
            foreach ($pricedetails as $pricedetail) {
131
                $this->addPricedetail(clone $pricedetail);
132
            }
133
        }
134
        parent::__clone();
135
    }
136
137
    /********************************************************************************
138
     *
139
     *   Getters
140
     *
141
     *********************************************************************************/
142
143
    /**
144
     * Get the part.
145
     *
146
     * @return Part|null the part of this orderdetails
147
     */
148
    public function getPart(): ?Part
149
    {
150
        return $this->part;
151
    }
152
153
    /**
154
     * Get the supplier.
155
     *
156
     * @return Supplier the supplier of this orderdetails
157
     */
158
    public function getSupplier(): ?Supplier
159
    {
160
        return $this->supplier;
161
    }
162
163
    /**
164
     * Get the supplier part-nr.
165
     *
166
     * @return string the part-nr
167
     */
168
    public function getSupplierPartNr(): string
169
    {
170
        return $this->supplierpartnr;
171
    }
172
173
    /**
174
     * Get if this orderdetails is obsolete.
175
     *
176
     * "Orderdetails is obsolete" means that the part with that supplier-part-nr
177
     * is no longer available from the supplier of that orderdetails.
178
     *
179
     * @return bool * true if this part is obsolete at that supplier
180
     *              * false if this part isn't obsolete at that supplier
181
     */
182
    public function getObsolete(): bool
183
    {
184
        return (bool) $this->obsolete;
185
    }
186
187
    /**
188
     * Get the link to the website of the article on the suppliers website.
189
     *
190
     * @param bool $no_automatic_url Set this to true, if you only want to get the local set product URL for this Orderdetail
191
     *                               and not a automatic generated one, based from the Supplier
192
     *
193
     * @return string the link to the article
194
     */
195
    public function getSupplierProductUrl(bool $no_automatic_url = false): string
196
    {
197
        if ($no_automatic_url || '' !== $this->supplier_product_url) {
198
            return $this->supplier_product_url;
199
        }
200
201
        if (null === $this->getSupplier()) {
202
            return '';
203
        }
204
205
        return $this->getSupplier()->getAutoProductUrl($this->supplierpartnr); // maybe an automatic url is available...
206
    }
207
208
    /**
209
     * Get all pricedetails.
210
     *
211
     * @return Pricedetail[]|Collection all pricedetails as a one-dimensional array of Pricedetails objects,
212
     *                                  sorted by minimum discount quantity
213
     */
214
    public function getPricedetails(): Collection
215
    {
216
        return $this->pricedetails;
217
    }
218
219
    /**
220
     * Adds an pricedetail to this orderdetail.
221
     *
222
     * @param Pricedetail $pricedetail The pricedetail to add
223
     *
224
     * @return Orderdetail
225
     */
226
    public function addPricedetail(Pricedetail $pricedetail): self
227
    {
228
        $pricedetail->setOrderdetail($this);
229
        $this->pricedetails->add($pricedetail);
230
231
        return $this;
232
    }
233
234
    /**
235
     * Removes an pricedetail from this orderdetail.
236
     *
237
     * @return Orderdetail
238
     */
239
    public function removePricedetail(Pricedetail $pricedetail): self
240
    {
241
        $this->pricedetails->removeElement($pricedetail);
242
243
        return $this;
244
    }
245
246
    /**
247
     * Find the pricedetail that is correct for the desired amount (the one with the greatest discount value with a
248
     * minimum order amount of the wished quantity).
249
     *
250
     * @param float $quantity this is the quantity to choose the correct pricedetails
251
     *
252
     * @return Pricedetail|null: the price as a bcmath string. Null if there are no orderdetails for the given quantity
253
     */
254
    public function findPriceForQty(float $quantity = 1.0): ?Pricedetail
255
    {
256
        if ($quantity <= 0) {
257
            return null;
258
        }
259
260
        $all_pricedetails = $this->getPricedetails();
261
262
        $correct_pricedetail = null;
263
        foreach ($all_pricedetails as $pricedetail) {
264
            // choose the correct pricedetails for the chosen quantity ($quantity)
265
            if ($quantity < $pricedetail->getMinDiscountQuantity()) {
266
                break;
267
            }
268
269
            $correct_pricedetail = $pricedetail;
270
        }
271
272
        return $correct_pricedetail;
273
    }
274
275
    /********************************************************************************
276
     *
277
     *   Setters
278
     *
279
     *********************************************************************************/
280
281
    /**
282
     * Sets a new part with which this orderdetail is associated.
283
     *
284
     * @return Orderdetail
285
     */
286
    public function setPart(Part $part): self
287
    {
288
        $this->part = $part;
289
290
        return $this;
291
    }
292
293
    /**
294
     * Sets the new supplier associated with this orderdetail.
295
     *
296
     * @return Orderdetail
297
     */
298
    public function setSupplier(Supplier $new_supplier): self
299
    {
300
        $this->supplier = $new_supplier;
301
302
        return $this;
303
    }
304
305
    /**
306
     * Set the supplier part-nr.
307
     *
308
     * @param string $new_supplierpartnr the new supplier-part-nr
309
     *
310
     * @return Orderdetail
311
     * @return Orderdetail
312
     */
313
    public function setSupplierpartnr(string $new_supplierpartnr): self
314
    {
315
        $this->supplierpartnr = $new_supplierpartnr;
316
317
        return $this;
318
    }
319
320
    /**
321
     * Set if the part is obsolete at the supplier of that orderdetails.
322
     *
323
     * @param bool $new_obsolete true means that this part is obsolete
324
     *
325
     * @return Orderdetail
326
     * @return Orderdetail
327
     */
328
    public function setObsolete(bool $new_obsolete): self
329
    {
330
        $this->obsolete = $new_obsolete;
331
332
        return $this;
333
    }
334
335
    /**
336
     * Sets the custom product supplier URL for this order detail.
337
     * Set this to "", if the function getSupplierProductURL should return the automatic generated URL.
338
     *
339
     * @param string $new_url The new URL for the supplier URL
340
     *
341
     * @return Orderdetail
342
     */
343
    public function setSupplierProductUrl(string $new_url): self
344
    {
345
        //Only change the internal URL if it is not the auto generated one
346
        if ($new_url === $this->supplier->getAutoProductUrl($this->getSupplierPartNr())) {
347
            return $this;
348
        }
349
350
        $this->supplier_product_url = $new_url;
351
352
        return $this;
353
    }
354
355
    public function getName(): string
356
    {
357
        return $this->getSupplierPartNr();
358
    }
359
}
360