Passed
Push — master ( da6b74...a51687 )
by Jan
07:57
created

PricedetailHelper::getMaxDiscountAmount()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 39
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 20
nc 12
nop 1
dl 0
loc 39
rs 8.9777
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony)
4
 *
5
 * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
20
 *
21
 */
22
23
namespace App\Services;
24
25
use App\Entity\Parts\Part;
26
use App\Entity\PriceInformations\Currency;
27
use App\Entity\PriceInformations\Pricedetail;
28
use Doctrine\Common\Collections\ArrayCollection;
29
use Doctrine\ORM\PersistentCollection;
30
use Locale;
31
32
class PricedetailHelper
33
{
34
    protected $base_currency;
35
    protected $locale;
36
37
    public function __construct(string $base_currency)
38
    {
39
        $this->base_currency = $base_currency;
40
        $this->locale = Locale::getDefault();
41
    }
42
43
    /**
44
     * Determines the highest amount, for which you get additional discount.
45
     * This function determines the highest min_discount_quantity for the given part.
46
     * @param Part $part
47
     * @return float|null
48
     */
49
    public function getMaxDiscountAmount(Part $part) : ?float
50
    {
51
        $orderdetails = $part->getOrderdetails(true);
52
53
        $max = 0;
54
55
        foreach ($orderdetails as $orderdetail) {
56
            $pricedetails = $orderdetail->getPricedetails();
57
            //The orderdetail must have pricedetails, otherwise this will not work!
58
            if (count($pricedetails) === 0) {
59
                continue;
60
            }
61
62
            if ($pricedetails instanceof PersistentCollection) {
63
                /* Pricedetails in orderdetails are ordered by min discount quantity,
64
                    so our first object is our min order amount for the current orderdetail */
65
                $max_amount = $pricedetails->last()->getMinDiscountQuantity();
66
            } else {
67
                // We have to sort the pricedetails manually
68
                $array = $pricedetails->map(
69
                    function (Pricedetail $pricedetail) {
70
                        return $pricedetail->getMinDiscountQuantity();
71
                    }
72
                )->toArray();
73
                sort($array);
74
                $max_amount = end($array);
75
            }
76
77
78
            if ($max_amount > $max) {
79
                $max = $max_amount;
80
            }
81
        }
82
83
        if ($max > 0.0) {
84
            return $max;
85
        }
86
87
        return null;
88
    }
89
90
    /**
91
     * Determines the minimum amount of the part that can be ordered
92
     * @param Part $part The part for which the minimum order amount should be determined.
93
     * @return float
94
     */
95
    public function getMinOrderAmount(Part $part) : ?float
96
    {
97
        $orderdetails = $part->getOrderdetails(true);
98
99
        $min = INF;
100
101
        foreach ($orderdetails as $orderdetail) {
102
            $pricedetails = $orderdetail->getPricedetails();
103
            //The orderdetail must have pricedetails, otherwise this will not work!
104
            if (count($pricedetails) === 0) {
105
                continue;
106
            }
107
108
            /* Pricedetails in orderdetails are ordered by min discount quantity,
109
                so our first object is our min order amount for the current orderdetail */
110
            $min_amount = $pricedetails[0]->getMinDiscountQuantity();
111
112
            if ($min_amount < $min) {
113
                $min = $min_amount;
114
            }
115
        }
116
117
        if ($min < INF) {
118
            return $min;
119
        }
120
121
        return null;
122
    }
123
124
    /**
125
     * Calculates the average price of a part, when ordering the amount $amount.
126
     * @param Part $part The part for which the average price should be calculated.
127
     * @param float $amount The order amount for which the average price should be calculated.
128
     * If set to null, the mininmum order amount for the part is used.
129
     * @param Currency|null $currency The currency in which the average price should be calculated
130
     * @return string|null The Average price as bcmath string. Returns null, if it was not possible to calculate the
131
     * price for the given
132
     */
133
    public function calculateAvgPrice(Part $part, ?float $amount = null, ?Currency $currency = null) : ?string
134
    {
135
        if ($amount === null) {
136
            $amount = $this->getMinOrderAmount($part);
137
        }
138
139
        if ($amount === null) {
140
            return null;
141
        }
142
143
        $orderdetails = $part->getOrderdetails(true);
144
145
        $avg = "0";
146
        $count = 0;
147
148
        //Find the price for the amount, for the given
149
        foreach ($orderdetails as $orderdetail) {
150
            $pricedetail = $orderdetail->getPrice($amount);
151
152
            //When we dont have informations about this amount, ignore it
153
            if ($pricedetail === null) {
154
                continue;
155
            }
156
157
            $avg = bcadd($avg, $this->convertMoneyToCurrency($pricedetail->getPricePerUnit(), $pricedetail->getCurrency(), $currency), Pricedetail::PRICE_PRECISION);
158
            $count++;
159
        }
160
161
        if ($count === 0) {
162
            return null;
163
        }
164
165
        return bcdiv($avg, (string) $count, Pricedetail::PRICE_PRECISION);
166
    }
167
168
    /**
169
     * Converts the given value in origin currency to the choosen target currency
170
     * @param $value float|string The value that should be converted
171
     * @param Currency|null $originCurrency The currency the $value is given in.
172
     * Set to null, to use global base currency.
173
     * @param Currency|null $targetCurrency The target currency, to which $value should be converted.
174
     * Set to null, to use global base currency.
175
     * @return string|null The value in $targetCurrency given as bcmath string.
176
     * Returns null, if it was not possible to convert between both values (e.g. when the exchange rates are missing)
177
     */
178
    public function convertMoneyToCurrency($value, ?Currency $originCurrency = null, ?Currency $targetCurrency = null) : ?string
179
    {
180
        //Skip conversion, if both currencies are same
181
        if ($originCurrency === $targetCurrency) {
182
            return $value;
183
        }
184
185
        $value = (string) $value;
186
187
        //Convert value to base currency
188
        $val_base = $value;
189
        if ($originCurrency !== null) {
190
            //Without an exchange rate we can not calculate the exchange rate
191
            if ((float) $originCurrency->getExchangeRate() === 0) {
0 ignored issues
show
introduced by
The condition (double)$originCurrency->getExchangeRate() === 0 is always false.
Loading history...
192
                return null;
193
            }
194
195
            $val_base = bcmul($value, $originCurrency->getExchangeRate(), Pricedetail::PRICE_PRECISION);
196
        }
197
198
        //Convert value in base currency to target currency
199
        $val_target = $val_base;
200
        if ($targetCurrency !== null) {
201
            //Without an exchange rate we can not calculate the exchange rate
202
            if ($targetCurrency->getExchangeRate() === null) {
203
                return null;
204
            }
205
206
            $val_target = bcmul($val_base, $targetCurrency->getInverseExchangeRate(), Pricedetail::PRICE_PRECISION);
207
        }
208
209
        return $val_target;
210
    }
211
}