Completed
Push — master ( baa046...a40d4d )
by Jan
04:49
created

Orderdetail::getSupplierPartNr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * part-db version 0.1
7
 * Copyright (C) 2005 Christoph Lechner
8
 * http://www.cl-projects.de/.
9
 *
10
 * part-db version 0.2+
11
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
12
 * http://code.google.com/p/part-db/
13
 *
14
 * Part-DB Version 0.4+
15
 * Copyright (C) 2016 - 2019 Jan Böhmer
16
 * https://github.com/jbtronics
17
 *
18
 * This program is free software; you can redistribute it and/or
19
 * modify it under the terms of the GNU General Public License
20
 * as published by the Free Software Foundation; either version 2
21
 * of the License, or (at your option) any later version.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 * GNU General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU General Public License
29
 * along with this program; if not, write to the Free Software
30
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
31
 */
32
33
namespace App\Entity;
34
35
use Doctrine\ORM\Mapping as ORM;
36
use Doctrine\ORM\PersistentCollection;
37
use Exception;
38
39
/**
40
 * Class Orderdetail.
41
 *
42
 * @ORM\Table("orderdetails")
43
 * @ORM\Entity()
44
 */
45
class Orderdetail extends DBElement
46
{
47
    /**
48
     * @var Part
49
     * @ORM\ManyToOne(targetEntity="Part", inversedBy="orderdetails")
50
     * @ORM\JoinColumn(name="part_id", referencedColumnName="id")
51
     */
52
    protected $part;
53
54
    /**
55
     * @var Supplier
56
     * @ORM\ManyToOne(targetEntity="Supplier", inversedBy="orderdetails")
57
     * @ORM\JoinColumn(name="id_supplier", referencedColumnName="id")
58
     */
59
    protected $supplier;
60
61
    /**
62
     * @ORM\OneToMany(targetEntity="Pricedetail", mappedBy="orderdetail")
63
     */
64
    protected $pricedetails;
65
66
    /**
67
     * @var string
68
     * @ORM\Column(type="string")
69
     */
70
    protected $supplierpartnr;
71
72
    /**
73
     * @var bool
74
     * @ORM\Column(type="boolean")
75
     */
76
    protected $obsolete;
77
78
    /**
79
     * @var string
80
     * @ORM\Column(type="string")
81
     */
82
    protected $supplier_product_url;
83
84
    /**
85
     * @var \DateTime The date when this element was created.
86
     * @ORM\Column(type="datetimetz", name="datetime_added")
87
     */
88
    protected $addedDate;
89
90
    /**
91
     * Returns the ID as an string, defined by the element class.
92
     * This should have a form like P000014, for a part with ID 14.
93
     *
94
     * @return string The ID as a string;
95
     */
96
    public function getIDString(): string
97
    {
98
        return 'O'.sprintf('%06d', $this->getID());
99
    }
100
101
    /********************************************************************************
102
     *
103
     *   Getters
104
     *
105
     *********************************************************************************/
106
107
    /**
108
     * Get the part.
109
     *
110
     * @return Part the part of this orderdetails
111
     */
112
    public function getPart(): Part
113
    {
114
        return $this->part;
115
    }
116
117
    /**
118
     * Get the supplier.
119
     *
120
     * @return Supplier the supplier of this orderdetails
121
     *
122
     * @throws DatabaseException if there was an error
123
     */
124
    public function getSupplier(): Supplier
125
    {
126
        return $this->supplier;
127
    }
128
129
    /**
130
     * Get the supplier part-nr.
131
     *
132
     * @return string the part-nr.
133
     */
134
    public function getSupplierPartNr(): string
135
    {
136
        return $this->supplierpartnr;
137
    }
138
139
    /**
140
     * Get if this orderdetails is obsolete.
141
     *
142
     * "Orderdetails is obsolete" means that the part with that supplier-part-nr
143
     * is no longer available from the supplier of that orderdetails.
144
     *
145
     * @return bool * true if this part is obsolete at that supplier
146
     *              * false if this part isn't obsolete at that supplier
147
     */
148
    public function getObsolete(): bool
149
    {
150
        return (bool) $this->obsolete;
151
    }
152
153
    /**
154
     * Returns the date/time when the element was created.
155
     * Returns null if the element was not yet saved to DB yet.
156
     *
157
     * @return \DateTime|null The creation time of the part.
158
     */
159
    public function getAddedDate(): ?\DateTime
160
    {
161
        return $this->addedDate;
162
    }
163
164
    /**
165
     * Get the link to the website of the article on the suppliers website.
166
     *
167
     * @param $no_automatic_url bool Set this to true, if you only want to get the local set product URL for this Orderdetail
168
     * and not a automatic generated one, based from the Supplier
169
     *
170
     * @return string the link to the article
171
     */
172
    public function getSupplierProductUrl(bool $no_automatic_url = false): string
173
    {
174
        if ($no_automatic_url || '' !== $this->supplier_product_url) {
175
            return $this->supplier_product_url;
176
        }
177
178
        return $this->getSupplier()->getAutoProductUrl($this->supplierpartnr); // maybe an automatic url is available...
179
    }
180
181
    /**
182
     * Get all pricedetails.
183
     *
184
     * @return Pricedetail[] all pricedetails as a one-dimensional array of Pricedetails objects,
185
     *                        sorted by minimum discount quantity
186
     *
187
     * @throws Exception if there was an error
188
     */
189
    public function getPricedetails(): PersistentCollection
190
    {
191
        return $this->pricedetails;
192
    }
193
194
    /**
195
     * Get the price for a specific quantity.
196
     * @param int      $quantity        this is the quantity to choose the correct pricedetails
197
     * @param int|null $multiplier      * This is the multiplier which will be applied to every single price
198
     *                                  * If you pass NULL, the number from $quantity will be used
199
     *
200
     * @return float float: the price as a float number (if "$as_money_string == false")
201
     *
202
     * @throws Exception if there are no pricedetails for the choosed quantity
203
     *                   (for example, there are only one pricedetails with the minimum discount quantity '10',
204
     *                   but the choosed quantity is '5' --> the price for 5 parts is not defined!)
205
     * @throws Exception if there was an error
206
     */
207
    public function getPrice(int $quantity = 1, $multiplier = null) : ?float
208
    {
209
210
        if (($quantity == 0) && ($multiplier === null)) {
211
                return 0.0;
212
        }
213
214
        $all_pricedetails = $this->getPricedetails();
215
216
        if (count($all_pricedetails) == 0) {
217
                return null;
218
        }
219
220
221
        $correct_pricedetails = null;
222
        foreach ($all_pricedetails as $pricedetails) {
223
            // choose the correct pricedetails for the choosed quantity ($quantity)
224
            if ($quantity < $pricedetails->getMinDiscountQuantity()) {
225
                break;
226
            }
227
228
            $correct_pricedetails = $pricedetails;
229
        }
230
231
        if ($correct_pricedetails == null) {
0 ignored issues
show
introduced by
The condition $correct_pricedetails == null is always true.
Loading history...
232
            return null;
233
        }
234
235
        if ($multiplier === null) {
236
            $multiplier = $quantity;
237
        }
238
239
        return $correct_pricedetails->getPricePerUnit($multiplier);
240
    }
241
242
    /********************************************************************************
243
     *
244
     *   Setters
245
     *
246
     *********************************************************************************/
247
248
    /**
249
     * Set the supplier ID.
250
     *
251
     * @param int $new_supplier_id the ID of the new supplier
252
     */
253
    public function setSupplierId(int $new_supplier_id): self
0 ignored issues
show
Unused Code introduced by
The parameter $new_supplier_id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

253
    public function setSupplierId(/** @scrutinizer ignore-unused */ int $new_supplier_id): self

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
254
    {
255
        throw new \Exception('Not implemented yet!');
256
        //TODO;
257
258
        return $this;
0 ignored issues
show
Unused Code introduced by
return $this is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
259
    }
260
261
    /**
262
     * Set the supplier part-nr.
263
     *
264
     * @param string $new_supplierpartnr the new supplier-part-nr
265
     */
266
    public function setSupplierpartnr(string $new_supplierpartnr): self
267
    {
268
        $this->supplierpartnr = $new_supplierpartnr;
269
270
        return $this;
271
    }
272
273
    /**
274
     * Set if the part is obsolete at the supplier of that orderdetails.
275
     *
276
     * @param bool $new_obsolete true means that this part is obsolete
277
     */
278
    public function setObsolete(bool $new_obsolete): self
279
    {
280
        $this->obsolete = $new_obsolete;
281
282
        return $this;
283
    }
284
285
    /**
286
     * Sets the custom product supplier URL for this order detail.
287
     * Set this to "", if the function getSupplierProductURL should return the automatic generated URL.
288
     *
289
     * @param $new_url string The new URL for the supplier URL.
290
     */
291
    public function setSupplierProductUrl(string $new_url)
292
    {
293
        $this->supplier_product_url = $new_url;
294
295
        return $this;
296
    }
297
}
298