Completed
Push — master ( bcdba8...408d98 )
by Jan
03:54
created

Pricedetail::getMinDiscountQuantity()   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
 *
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 Doctrine\ORM\Mapping as ORM;
66
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
67
use Symfony\Component\Validator\Constraints as Assert;
68
69
/**
70
 * Class Pricedetail.
71
 *
72
 * @ORM\Entity()
73
 * @ORM\Table("`pricedetails`")
74
 * @UniqueEntity(fields={"orderdetail", "min_discount_quantity"})
75
 */
76
class Pricedetail extends DBElement
77
{
78
    /**
79
     * @var Orderdetail
80
     * @ORM\ManyToOne(targetEntity="Orderdetail", inversedBy="pricedetails")
81
     * @ORM\JoinColumn(name="orderdetails_id", referencedColumnName="id")
82
     */
83
    protected $orderdetail;
84
85
    /**
86
     * @var float The price related to the detail. (Given in the selected currency)
87
     * @ORM\Column(type="decimal", precision=11, scale=5)
88
     * @Assert\Positive()
89
     */
90
    protected $price;
91
92
    /**
93
     * @var ?Currency The currency used for the current price information.
94
     * If this is null, the global base unit is assumed.
95
     * @ORM\ManyToOne(targetEntity="Currency")
96
     * @ORM\JoinColumn(name="id_currency", referencedColumnName="id", nullable=true)
97
     */
98
    protected $currency;
99
100
    /**
101
     * @var int
102
     * @ORM\Column(type="integer")
103
     * @Assert\Positive()
104
     */
105
    protected $price_related_quantity;
106
107
    /**
108
     * @var int
109
     * @ORM\Column(type="integer")
110
     */
111
    protected $min_discount_quantity;
112
113
    /**
114
     * @var bool
115
     * @ORM\Column(type="boolean")
116
     */
117
    protected $manual_input;
118
119
    /**
120
     * @ORM\Column(type="datetimetz")
121
     */
122
    protected $last_modified;
123
124
    /********************************************************************************
125
     *
126
     *   Getters
127
     *
128
     *********************************************************************************/
129
130
    /**
131
     * Get the orderdetails of this pricedetails.
132
     *
133
     * @return Orderdetail the orderdetails object
134
     */
135
    public function getOrderdetails(): Orderdetail
136
    {
137
        return $this->orderdetail;
138
    }
139
140
    /**
141
     * Returns the price associated with this pricedetail.
142
     * It is given in current currency and for the price related quantity.
143
     * @return float
144
     */
145
    public function getPrice() : float
146
    {
147
        return (float) $this->price;
148
    }
149
150
    /**
151
     * Get the price for a single unit in the currency associated with this price detail.
152
     *
153
     * @param int  $multiplier      The returned price (float or string) will be multiplied
154
     *                              with this multiplier.
155
     *
156
     *     You will get the price for $multiplier parts. If you want the price which is stored
157
     *          in the database, you have to pass the "price_related_quantity" count as $multiplier.
158
     *
159
     * @return float  the price as a float number
160
161
     */
162
    public function getPricePerUnit(int $multiplier = 1) : float
163
    {
164
        return ($this->price * $multiplier) / $this->price_related_quantity;
165
    }
166
167
    /**
168
     *  Get the price related quantity.
169
     *
170
     * This is the quantity, for which the price is valid.
171
     *
172
     * @return int the price related quantity
173
     *
174
     * @see Pricedetail::setPriceRelatedQuantity()
175
     */
176
    public function getPriceRelatedQuantity(): int
177
    {
178
        return $this->price_related_quantity;
179
    }
180
181
    /**
182
     *  Get the minimum discount quantity.
183
     *
184
     * "Minimum discount quantity" means the minimum order quantity for which the price
185
     * of this orderdetails is valid.
186
     *
187
     * @return int the minimum discount quantity
188
     *
189
     * @see Pricedetail::setMinDiscountQuantity()
190
     */
191
    public function getMinDiscountQuantity(): int
192
    {
193
        return $this->min_discount_quantity;
194
    }
195
196
    /**
197
     * Returns the currency associated with this price information.
198
     * Returns null, if no specific currency is selected and the global base currency should be assumed.
199
     * @return Currency|null
200
     */
201
    public function getCurrency(): ?Currency
202
    {
203
        return $this->currency;
204
    }
205
206
    /********************************************************************************
207
     *
208
     *   Setters
209
     *
210
     *********************************************************************************/
211
212
    /**
213
     * Sets the currency associated with the price informations.
214
     * Set to null, to use the global base currency.
215
     * @param Currency|null $currency
216
     * @return Pricedetail
217
     */
218
    public function setCurrency(?Currency $currency): Pricedetail
219
    {
220
        $this->currency = $currency;
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return App\Entity\PriceInformations\Pricedetail. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
221
    }
222
223
    /**
224
     *  Set the price.
225
     *
226
     * @param float $new_price the new price as a float number
227
     *
228
     *      * This is the price for "price_related_quantity" parts!!
229
     *              * Example: if "price_related_quantity" is '10',
230
     *                  you have to set here the price for 10 parts!
231
     *
232
     * @return self
233
     */
234
    public function setPrice(float $new_price): Pricedetail
235
    {
236
        //Assert::natural($new_price, 'The new price must be positive! Got %s!');
237
238
        $this->price = $new_price;
239
240
        return $this;
241
    }
242
243
    /**
244
     *  Set the price related quantity.
245
     *
246
     * This is the quantity, for which the price is valid.
247
     *
248
     * Example:
249
     * If 100pcs costs 20$, you have to set the price to 20$ and the price related
250
     * quantity to 100. The single price (20$/100 = 0.2$) will be calculated automatically.
251
     *
252
     * @param int $new_price_related_quantity the price related quantity
253
     *
254
     * @return self
255
     */
256
    public function setPriceRelatedQuantity(int $new_price_related_quantity): self
257
    {
258
        //Assert::greaterThan($new_price_related_quantity, 0,
259
        //    'The new price related quantity must be greater zero! Got %s.');
260
261
        $this->price_related_quantity = $new_price_related_quantity;
262
263
        return $this;
264
    }
265
266
    /**
267
     *  Set the minimum discount quantity.
268
     *
269
     * "Minimum discount quantity" means the minimum order quantity for which the price
270
     * of this orderdetails is valid. This way, you're able to use different prices
271
     * for different order quantities (quantity discount!).
272
     *
273
     *  Example:
274
     *      - 1-9pcs costs 10$: set price to 10$/pcs and minimum discount quantity to 1
275
     *      - 10-99pcs costs 9$: set price to 9$/pcs and minimum discount quantity to 10
276
     *      - 100pcs or more costs 8$: set price/pcs to 8$ and minimum discount quantity to 100
277
     *
278
     * (Each of this examples would be an own Pricedetails-object.
279
     * So the orderdetails would have three Pricedetails for one supplier.)
280
     *
281
     * @param int $new_min_discount_quantity the minimum discount quantity
282
     *
283
     * @return self
284
     */
285
    public function setMinDiscountQuantity(int $new_min_discount_quantity): self
286
    {
287
        //Assert::greaterThan($new_min_discount_quantity, 0,
288
        //    'The new minimum discount quantity must be greater zero! Got %s.');
289
290
        $this->min_discount_quantity = $new_min_discount_quantity;
291
292
        return $this;
293
    }
294
295
    /**
296
     * Returns the ID as an string, defined by the element class.
297
     * This should have a form like P000014, for a part with ID 14.
298
     *
299
     * @return string The ID as a string;
300
     */
301
    public function getIDString(): string
302
    {
303
        return 'PD'.sprintf('%06d', $this->getID());
304
    }
305
}
306