Passed
Push — master ( 8e2362...7608d5 )
by Jan
04:12
created

Orderdetail::setSupplier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\Parts\Part;
66
use App\Entity\Parts\Supplier;
67
use Doctrine\ORM\Mapping as ORM;
68
use Doctrine\ORM\PersistentCollection;
69
use Exception;
70
71
/**
72
 * Class Orderdetail.
73
 *
74
 * @ORM\Table("`orderdetails`")
75
 * @ORM\Entity()
76
 */
77
class Orderdetail extends DBElement
78
{
79
    /**
80
     * @var Part
81
     * @ORM\ManyToOne(targetEntity="App\Entity\Parts\Part", inversedBy="orderdetails")
82
     * @ORM\JoinColumn(name="part_id", referencedColumnName="id")
83
     */
84
    protected $part;
85
86
    /**
87
     * @var Supplier
88
     * @ORM\ManyToOne(targetEntity="App\Entity\Parts\Supplier", inversedBy="orderdetails")
89
     * @ORM\JoinColumn(name="id_supplier", referencedColumnName="id")
90
     */
91
    protected $supplier;
92
93
    /**
94
     * @ORM\OneToMany(targetEntity="Pricedetail", mappedBy="orderdetail")
95
     */
96
    protected $pricedetails;
97
98
    /**
99
     * @var string
100
     * @ORM\Column(type="string")
101
     */
102
    protected $supplierpartnr;
103
104
    /**
105
     * @var bool
106
     * @ORM\Column(type="boolean")
107
     */
108
    protected $obsolete;
109
110
    /**
111
     * @var string
112
     * @ORM\Column(type="string")
113
     */
114
    protected $supplier_product_url;
115
116
    /**
117
     * @var \DateTime The date when this element was created.
118
     * @ORM\Column(type="datetimetz", name="datetime_added")
119
     */
120
    protected $addedDate;
121
122
    /**
123
     * Returns the ID as an string, defined by the element class.
124
     * This should have a form like P000014, for a part with ID 14.
125
     *
126
     * @return string The ID as a string;
127
     */
128
    public function getIDString(): string
129
    {
130
        return 'O'.sprintf('%06d', $this->getID());
131
    }
132
133
    /********************************************************************************
134
     *
135
     *   Getters
136
     *
137
     *********************************************************************************/
138
139
    /**
140
     * Get the part.
141
     *
142
     * @return Part the part of this orderdetails
143
     */
144
    public function getPart(): Part
145
    {
146
        return $this->part;
147
    }
148
149
    /**
150
     * Get the supplier.
151
     *
152
     * @return Supplier the supplier of this orderdetails
153
     */
154
    public function getSupplier(): Supplier
155
    {
156
        return $this->supplier;
157
    }
158
159
    /**
160
     * Get the supplier part-nr.
161
     *
162
     * @return string the part-nr.
163
     */
164
    public function getSupplierPartNr(): string
165
    {
166
        return $this->supplierpartnr;
167
    }
168
169
    /**
170
     * Get if this orderdetails is obsolete.
171
     *
172
     * "Orderdetails is obsolete" means that the part with that supplier-part-nr
173
     * is no longer available from the supplier of that orderdetails.
174
     *
175
     * @return bool * true if this part is obsolete at that supplier
176
     *              * false if this part isn't obsolete at that supplier
177
     */
178
    public function getObsolete(): bool
179
    {
180
        return (bool) $this->obsolete;
181
    }
182
183
    /**
184
     * Returns the date/time when the element was created.
185
     * Returns null if the element was not yet saved to DB yet.
186
     *
187
     * @return \DateTime|null The creation time of the part.
188
     */
189
    public function getAddedDate(): ?\DateTime
190
    {
191
        return $this->addedDate;
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
        return $this->getSupplier()->getAutoProductUrl($this->supplierpartnr); // maybe an automatic url is available...
209
    }
210
211
    /**
212
     * Get all pricedetails.
213
     *
214
     * @return Pricedetail[] all pricedetails as a one-dimensional array of Pricedetails objects,
215
     *                        sorted by minimum discount quantity
216
     *
217
     * @throws Exception if there was an error
218
     */
219
    public function getPricedetails(): PersistentCollection
220
    {
221
        return $this->pricedetails;
222
    }
223
224
    /**
225
     * Get the price for a specific quantity.
226
     * @param int      $quantity        this is the quantity to choose the correct pricedetails
227
     * @param int|null $multiplier      * This is the multiplier which will be applied to every single price
228
     *                                  * If you pass NULL, the number from $quantity will be used
229
     *
230
     * @return float float: the price as a float number (if "$as_money_string == false")
231
     *
232
     * @throws Exception if there are no pricedetails for the choosed quantity
233
     *                   (for example, there are only one pricedetails with the minimum discount quantity '10',
234
     *                   but the choosed quantity is '5' --> the price for 5 parts is not defined!)
235
     * @throws Exception if there was an error
236
     */
237
    public function getPrice(int $quantity = 1, $multiplier = null) : ?float
238
    {
239
240
        if (($quantity === 0) && ($multiplier === null)) {
241
                return 0.0;
242
        }
243
244
        $all_pricedetails = $this->getPricedetails();
245
246
        if (count($all_pricedetails) == 0) {
247
                return null;
248
        }
249
250
251
        $correct_pricedetails = null;
252
        foreach ($all_pricedetails as $pricedetails) {
253
            // choose the correct pricedetails for the choosed quantity ($quantity)
254
            if ($quantity < $pricedetails->getMinDiscountQuantity()) {
255
                break;
256
            }
257
258
            $correct_pricedetails = $pricedetails;
259
        }
260
261
        if ($correct_pricedetails === null) {
0 ignored issues
show
introduced by
The condition $correct_pricedetails === null is always true.
Loading history...
262
            return null;
263
        }
264
265
        if ($multiplier === null) {
266
            $multiplier = $quantity;
267
        }
268
269
        return $correct_pricedetails->getPricePerUnit($multiplier);
270
    }
271
272
    /********************************************************************************
273
     *
274
     *   Setters
275
     *
276
     *********************************************************************************/
277
278
    /**
279
     * Sets the new supplier associated with this orderdetail.
280
     * @param Supplier $new_supplier
281
     * @return Orderdetail
282
     */
283
    public function setSupplier(Supplier $new_supplier) : Orderdetail
284
    {
285
        $this->supplier = $new_supplier;
286
        return $this;
287
    }
288
289
    /**
290
     * Set the supplier part-nr.
291
     * @param string $new_supplierpartnr the new supplier-part-nr
292
     * @return Orderdetail
293
     * @return Orderdetail
294
     */
295
    public function setSupplierpartnr(string $new_supplierpartnr): self
296
    {
297
        $this->supplierpartnr = $new_supplierpartnr;
298
299
        return $this;
300
    }
301
302
    /**
303
     * Set if the part is obsolete at the supplier of that orderdetails.
304
     * @param bool $new_obsolete true means that this part is obsolete
305
     * @return Orderdetail
306
     * @return Orderdetail
307
     */
308
    public function setObsolete(bool $new_obsolete): self
309
    {
310
        $this->obsolete = $new_obsolete;
311
312
        return $this;
313
    }
314
315
    /**
316
     * Sets the custom product supplier URL for this order detail.
317
     * Set this to "", if the function getSupplierProductURL should return the automatic generated URL.
318
     * @param $new_url string The new URL for the supplier URL.
319
     * @return Orderdetail
320
     * @return Orderdetail
321
     */
322
    public function setSupplierProductUrl(string $new_url)
323
    {
324
        $this->supplier_product_url = $new_url;
325
326
        return $this;
327
    }
328
}
329