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

Orderdetail   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 294
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 53
dl 0
loc 294
rs 10
c 1
b 0
f 0
wmc 26

17 Methods

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