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

Orderdetail::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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