Completed
Push — master ( 855eac...c2b4d1 )
by Jan
04:06
created

Orderdetail::getSupplierProductUrl()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 7
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
     *
197
     * @param bool     $as_money_string * if true, this method returns a money string incl. currency
198
     *                                  * if false, this method returns the price as float
199
     * @param int      $quantity        this is the quantity to choose the correct pricedetails
200
     * @param int|null $multiplier      * This is the multiplier which will be applied to every single price
201
     *                                  * If you pass NULL, the number from $quantity will be used
202
     *
203
     * @return float|string|null float: the price as a float number (if "$as_money_string == false")
204
     *                           * null: if there are no prices and "$as_money_string == false"
205
     *                           * string:   the price as a string incl. currency (if "$as_money_string == true")
206
     *
207
     * @throws Exception if there are no pricedetails for the choosed quantity
208
     *                   (for example, there are only one pricedetails with the minimum discount quantity '10',
209
     *                   but the choosed quantity is '5' --> the price for 5 parts is not defined!)
210
     * @throws Exception if there was an error
211
     *
212
     * @see floatToMoneyString()
213
     */
214
    public function getPrice(bool $as_money_string = false, int $quantity = 1, $multiplier = null)
0 ignored issues
show
Unused Code introduced by
The parameter $multiplier 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

214
    public function getPrice(bool $as_money_string = false, int $quantity = 1, /** @scrutinizer ignore-unused */ $multiplier = null)

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...
Unused Code introduced by
The parameter $as_money_string 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

214
    public function getPrice(/** @scrutinizer ignore-unused */ bool $as_money_string = false, int $quantity = 1, $multiplier = null)

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...
Unused Code introduced by
The parameter $quantity 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

214
    public function getPrice(bool $as_money_string = false, /** @scrutinizer ignore-unused */ int $quantity = 1, $multiplier = null)

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...
215
    {
216
        /*
217
        if (($quantity == 0) && ($multiplier === null)) {
218
            if ($as_money_string) {
219
                return floatToMoneyString(0);
220
            } else {
221
                return 0;
222
            }
223
        }
224
225
        $all_pricedetails = $this->getPricedetails();
226
227
        if (count($all_pricedetails) == 0) {
228
            if ($as_money_string) {
229
                return floatToMoneyString(null);
230
            } else {
231
                return null;
232
            }
233
        }
234
235
        foreach ($all_pricedetails as $pricedetails) {
236
            // choose the correct pricedetails for the choosed quantity ($quantity)
237
            if ($quantity < $pricedetails->getMinDiscountQuantity()) {
238
                break;
239
            }
240
241
            $correct_pricedetails = $pricedetails;
242
        }
243
244
        if (! isset($correct_pricedetails) || (! \is_object($correct_pricedetails))) {
245
            throw new Exception(_('Es sind keine Preisinformationen für die angegebene Bestellmenge vorhanden!'));
246
        }
247
248
        if ($multiplier === null) {
249
            $multiplier = $quantity;
250
        }
251
252
        return $correct_pricedetails->getPrice($as_money_string, $multiplier);
253
         * */
254
        //TODO
255
        throw new \Exception('Not implemented yet...');
256
    }
257
258
    /********************************************************************************
259
     *
260
     *   Setters
261
     *
262
     *********************************************************************************/
263
264
    /**
265
     * Set the supplier ID.
266
     *
267
     * @param int $new_supplier_id the ID of the new supplier
268
     */
269
    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

269
    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...
270
    {
271
        throw new \Exception('Not implemented yet!');
272
        //TODO;
273
274
        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...
275
    }
276
277
    /**
278
     * Set the supplier part-nr.
279
     *
280
     * @param string $new_supplierpartnr the new supplier-part-nr
281
     */
282
    public function setSupplierpartnr(string $new_supplierpartnr): self
283
    {
284
        $this->supplierpartnr = $new_supplierpartnr;
285
286
        return $this;
287
    }
288
289
    /**
290
     * Set if the part is obsolete at the supplier of that orderdetails.
291
     *
292
     * @param bool $new_obsolete true means that this part is obsolete
293
     */
294
    public function setObsolete(bool $new_obsolete): self
295
    {
296
        $this->obsolete = $new_obsolete;
297
298
        return $this;
299
    }
300
301
    /**
302
     * Sets the custom product supplier URL for this order detail.
303
     * Set this to "", if the function getSupplierProductURL should return the automatic generated URL.
304
     *
305
     * @param $new_url string The new URL for the supplier URL.
306
     */
307
    public function setSupplierProductUrl(string $new_url)
308
    {
309
        $this->supplier_product_url = $new_url;
310
311
        return $this;
312
    }
313
}
314