Passed
Push — master ( 0663a0...89258b )
by Jan
04:32
created

Orderdetail::getPricedetails()   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 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (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 General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
20
 *
21
 */
22
23
declare(strict_types=1);
24
25
/**
26
 * part-db version 0.1
27
 * Copyright (C) 2005 Christoph Lechner
28
 * http://www.cl-projects.de/.
29
 *
30
 * part-db version 0.2+
31
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
32
 * http://code.google.com/p/part-db/
33
 *
34
 * Part-DB Version 0.4+
35
 * Copyright (C) 2016 - 2019 Jan Böhmer
36
 * https://github.com/jbtronics
37
 *
38
 * This program is free software; you can redistribute it and/or
39
 * modify it under the terms of the GNU General Public License
40
 * as published by the Free Software Foundation; either version 2
41
 * of the License, or (at your option) any later version.
42
 *
43
 * This program is distributed in the hope that it will be useful,
44
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
45
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
46
 * GNU General Public License for more details.
47
 *
48
 * You should have received a copy of the GNU General Public License
49
 * along with this program; if not, write to the Free Software
50
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
51
 */
52
53
namespace App\Entity\PriceInformations;
54
55
use App\Entity\Base\DBElement;
56
use App\Entity\Base\TimestampTrait;
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 DBElement
72
{
73
    use TimestampTrait;
74
75
    /**
76
     * @var Part
77
     * @ORM\ManyToOne(targetEntity="App\Entity\Parts\Part", inversedBy="orderdetails")
78
     * @ORM\JoinColumn(name="part_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
79
     * @Assert\NotNull()
80
     */
81
    protected $part;
82
83
    /**
84
     * @var Supplier
85
     * @ORM\ManyToOne(targetEntity="App\Entity\Parts\Supplier", inversedBy="orderdetails")
86
     * @ORM\JoinColumn(name="id_supplier", referencedColumnName="id")
87
     */
88
    protected $supplier;
89
90
    /**
91
     * @ORM\OneToMany(targetEntity="Pricedetail", mappedBy="orderdetail", cascade={"persist", "remove"}, orphanRemoval=true)
92
     * @Assert\Valid()
93
     * @ORM\OrderBy({"min_discount_quantity" = "ASC"})
94
     */
95
    protected $pricedetails;
96
97
    /**
98
     * @var string
99
     * @ORM\Column(type="string")
100
     */
101
    protected $supplierpartnr = '';
102
103
    /**
104
     * @var bool
105
     * @ORM\Column(type="boolean")
106
     */
107
    protected $obsolete = false;
108
109
    /**
110
     * @var string
111
     * @ORM\Column(type="string")
112
     * @Assert\Url()
113
     */
114
    protected $supplier_product_url = '';
115
116
    public function __construct()
117
    {
118
        $this->pricedetails = new ArrayCollection();
119
    }
120
121
    /**
122
     * Returns the ID as an string, defined by the element class.
123
     * This should have a form like P000014, for a part with ID 14.
124
     *
125
     * @return string The ID as a string;
126
     */
127
    public function getIDString(): string
128
    {
129
        return 'O'.sprintf('%06d', $this->getID());
130
    }
131
132
    /********************************************************************************
133
     *
134
     *   Getters
135
     *
136
     *********************************************************************************/
137
138
    /**
139
     * Get the part.
140
     *
141
     * @return Part|null the part of this orderdetails
142
     */
143
    public function getPart(): ?Part
144
    {
145
        return $this->part;
146
    }
147
148
    /**
149
     * Get the supplier.
150
     *
151
     * @return Supplier the supplier of this orderdetails
152
     */
153
    public function getSupplier(): ?Supplier
154
    {
155
        return $this->supplier;
156
    }
157
158
    /**
159
     * Get the supplier part-nr.
160
     *
161
     * @return string the part-nr.
162
     */
163
    public function getSupplierPartNr(): string
164
    {
165
        return $this->supplierpartnr;
166
    }
167
168
    /**
169
     * Get if this orderdetails is obsolete.
170
     *
171
     * "Orderdetails is obsolete" means that the part with that supplier-part-nr
172
     * is no longer available from the supplier of that orderdetails.
173
     *
174
     * @return bool * true if this part is obsolete at that supplier
175
     *              * false if this part isn't obsolete at that supplier
176
     */
177
    public function getObsolete(): bool
178
    {
179
        return (bool) $this->obsolete;
180
    }
181
182
    /**
183
     * Get the link to the website of the article on the suppliers website.
184
     *
185
     * @param $no_automatic_url bool Set this to true, if you only want to get the local set product URL for this Orderdetail
186
     * and not a automatic generated one, based from the Supplier
187
     *
188
     * @return string the link to the article
189
     */
190
    public function getSupplierProductUrl(bool $no_automatic_url = false): string
191
    {
192
        if ($no_automatic_url || '' !== $this->supplier_product_url) {
193
            return $this->supplier_product_url;
194
        }
195
196
        if ($this->getSupplier() === null) {
197
            return '';
198
        }
199
200
        return $this->getSupplier()->getAutoProductUrl($this->supplierpartnr); // maybe an automatic url is available...
201
    }
202
203
    /**
204
     * Get all pricedetails.
205
     *
206
     * @return Pricedetail[]|Collection all pricedetails as a one-dimensional array of Pricedetails objects,
207
     *                        sorted by minimum discount quantity
208
     */
209
    public function getPricedetails(): Collection
210
    {
211
        return $this->pricedetails;
212
    }
213
214
    /**
215
     * Adds an pricedetail to this orderdetail
216
     * @param Pricedetail $pricedetail The pricedetail to add
217
     * @return Orderdetail
218
     */
219
    public function addPricedetail(Pricedetail $pricedetail) : Orderdetail
220
    {
221
        $pricedetail->setOrderdetail($this);
222
        $this->pricedetails->add($pricedetail);
223
        return $this;
224
    }
225
226
    /**
227
     * Removes an pricedetail from this orderdetail
228
     * @param Pricedetail $pricedetail
229
     * @return Orderdetail
230
     */
231
    public function removePricedetail(Pricedetail $pricedetail) : Orderdetail
232
    {
233
        $this->pricedetails->removeElement($pricedetail);
234
        return $this;
235
    }
236
237
    /**
238
     * Find the pricedetail that is correct for the desired amount (the one with the greatest discount value with a
239
     * minimum order amount of the wished quantity)
240
     * @param float     $quantity        this is the quantity to choose the correct pricedetails
241
     *
242
     * @return Pricedetail|null: the price as a bcmath string. Null if there are no orderdetails for the given quantity
243
     */
244
    public function findPriceForQty(float $quantity = 1) : ?Pricedetail
245
    {
246
        if ($quantity <= 0) {
247
            return null;
248
        }
249
250
        $all_pricedetails = $this->getPricedetails();
251
252
        $correct_pricedetail = null;
253
        foreach ($all_pricedetails as $pricedetail) {
254
            // choose the correct pricedetails for the choosed quantity ($quantity)
255
            if ($quantity < $pricedetail->getMinDiscountQuantity()) {
256
                break;
257
            }
258
259
            $correct_pricedetail = $pricedetail;
260
        }
261
262
        return $correct_pricedetail;
263
    }
264
265
    /********************************************************************************
266
     *
267
     *   Setters
268
     *
269
     *********************************************************************************/
270
271
    /**
272
     * Sets a new part with which this orderdetail is associated
273
     * @param Part $part
274
     * @return Orderdetail
275
     */
276
    public function setPart(Part $part) : Orderdetail
277
    {
278
        $this->part = $part;
279
        return $this;
280
    }
281
282
    /**
283
     * Sets the new supplier associated with this orderdetail.
284
     * @param Supplier $new_supplier
285
     * @return Orderdetail
286
     */
287
    public function setSupplier(Supplier $new_supplier) : Orderdetail
288
    {
289
        $this->supplier = $new_supplier;
290
        return $this;
291
    }
292
293
    /**
294
     * Set the supplier part-nr.
295
     * @param string $new_supplierpartnr the new supplier-part-nr
296
     * @return Orderdetail
297
     * @return Orderdetail
298
     */
299
    public function setSupplierpartnr(string $new_supplierpartnr): self
300
    {
301
        $this->supplierpartnr = $new_supplierpartnr;
302
303
        return $this;
304
    }
305
306
    /**
307
     * Set if the part is obsolete at the supplier of that orderdetails.
308
     * @param bool $new_obsolete true means that this part is obsolete
309
     * @return Orderdetail
310
     * @return Orderdetail
311
     */
312
    public function setObsolete(bool $new_obsolete): self
313
    {
314
        $this->obsolete = $new_obsolete;
315
316
        return $this;
317
    }
318
319
    /**
320
     * Sets the custom product supplier URL for this order detail.
321
     * Set this to "", if the function getSupplierProductURL should return the automatic generated URL.
322
     * @param $new_url string The new URL for the supplier URL.
323
     * @return Orderdetail
324
     */
325
    public function setSupplierProductUrl(string $new_url) : Orderdetail
326
    {
327
        //Only change the internal URL if it is not the auto generated one
328
        if ($new_url === $this->supplier->getAutoProductUrl($this->getSupplierPartNr())) {
329
            return $this;
330
        }
331
332
        $this->supplier_product_url = $new_url;
333
334
        return $this;
335
    }
336
}
337