Completed
Push — master ( d79f58...e0b171 )
by Jan
03:56 queued 10s
created

Orderdetail::getPrice()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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