2019-09-01 14:37:53 +02:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-05 15:46:58 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-09-01 14:37:53 +02:00
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
2019-09-01 14:37:53 +02:00
|
|
|
*
|
2019-11-01 13:40:30 +01:00
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
|
2019-09-01 14:37:53 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2019-09-01 18:52:22 +02:00
|
|
|
use App\Entity\Parts\Part;
|
2019-09-01 14:37:53 +02:00
|
|
|
use App\Entity\PriceInformations\Currency;
|
|
|
|
use App\Entity\PriceInformations\Pricedetail;
|
2020-05-20 22:02:07 +02:00
|
|
|
use Brick\Math\BigDecimal;
|
|
|
|
use Brick\Math\RoundingMode;
|
2019-11-05 17:05:04 +01:00
|
|
|
use Doctrine\ORM\PersistentCollection;
|
2019-09-01 14:37:53 +02:00
|
|
|
use Locale;
|
|
|
|
|
2022-08-14 19:32:53 +02:00
|
|
|
use function count;
|
|
|
|
|
2019-09-01 14:37:53 +02:00
|
|
|
class PricedetailHelper
|
|
|
|
{
|
|
|
|
protected $base_currency;
|
|
|
|
protected $locale;
|
|
|
|
|
|
|
|
public function __construct(string $base_currency)
|
|
|
|
{
|
|
|
|
$this->base_currency = $base_currency;
|
|
|
|
$this->locale = Locale::getDefault();
|
|
|
|
}
|
|
|
|
|
2019-09-01 18:52:22 +02:00
|
|
|
/**
|
|
|
|
* Determines the highest amount, for which you get additional discount.
|
|
|
|
* This function determines the highest min_discount_quantity for the given part.
|
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function getMaxDiscountAmount(Part $part): ?float
|
2019-09-01 18:52:22 +02:00
|
|
|
{
|
|
|
|
$orderdetails = $part->getOrderdetails(true);
|
|
|
|
|
|
|
|
$max = 0;
|
|
|
|
|
|
|
|
foreach ($orderdetails as $orderdetail) {
|
|
|
|
$pricedetails = $orderdetail->getPricedetails();
|
|
|
|
//The orderdetail must have pricedetails, otherwise this will not work!
|
2022-08-14 19:32:53 +02:00
|
|
|
if (0 === count($pricedetails)) {
|
2019-09-01 18:52:22 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-05 17:05:04 +01:00
|
|
|
if ($pricedetails instanceof PersistentCollection) {
|
|
|
|
/* Pricedetails in orderdetails are ordered by min discount quantity,
|
|
|
|
so our first object is our min order amount for the current orderdetail */
|
|
|
|
$max_amount = $pricedetails->last()->getMinDiscountQuantity();
|
|
|
|
} else {
|
|
|
|
// We have to sort the pricedetails manually
|
|
|
|
$array = $pricedetails->map(
|
2020-08-21 22:43:37 +02:00
|
|
|
static function (Pricedetail $pricedetail) {
|
2019-11-05 17:05:04 +01:00
|
|
|
return $pricedetail->getMinDiscountQuantity();
|
|
|
|
}
|
|
|
|
)->toArray();
|
|
|
|
sort($array);
|
|
|
|
$max_amount = end($array);
|
|
|
|
}
|
|
|
|
|
2019-09-01 18:52:22 +02:00
|
|
|
if ($max_amount > $max) {
|
|
|
|
$max = $max_amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 17:05:04 +01:00
|
|
|
if ($max > 0.0) {
|
2019-09-01 18:52:22 +02:00
|
|
|
return $max;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* Determines the minimum amount of the part that can be ordered.
|
|
|
|
*
|
2020-01-04 20:24:09 +01:00
|
|
|
* @param Part $part the part for which the minimum order amount should be determined
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-09-01 18:52:22 +02:00
|
|
|
* @return float
|
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function getMinOrderAmount(Part $part): ?float
|
2019-09-01 18:52:22 +02:00
|
|
|
{
|
|
|
|
$orderdetails = $part->getOrderdetails(true);
|
|
|
|
|
|
|
|
$min = INF;
|
|
|
|
|
|
|
|
foreach ($orderdetails as $orderdetail) {
|
|
|
|
$pricedetails = $orderdetail->getPricedetails();
|
|
|
|
//The orderdetail must have pricedetails, otherwise this will not work!
|
2022-08-14 19:32:53 +02:00
|
|
|
if (0 === count($pricedetails)) {
|
2019-09-01 18:52:22 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pricedetails in orderdetails are ordered by min discount quantity,
|
|
|
|
so our first object is our min order amount for the current orderdetail */
|
|
|
|
$min_amount = $pricedetails[0]->getMinDiscountQuantity();
|
|
|
|
|
|
|
|
if ($min_amount < $min) {
|
|
|
|
$min = $min_amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($min < INF) {
|
|
|
|
return $min;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the average price of a part, when ordering the amount $amount.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-01-04 20:24:09 +01:00
|
|
|
* @param Part $part the part for which the average price should be calculated
|
2019-11-09 00:47:20 +01:00
|
|
|
* @param float $amount The order amount for which the average price should be calculated.
|
|
|
|
* If set to null, the mininmum order amount for the part is used.
|
2019-09-01 18:52:22 +02:00
|
|
|
* @param Currency|null $currency The currency in which the average price should be calculated
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-05-20 22:02:07 +02:00
|
|
|
* @return BigDecimal|null The Average price as bcmath string. Returns null, if it was not possible to calculate the
|
2020-08-21 21:36:22 +02:00
|
|
|
* price for the given
|
2019-09-01 18:52:22 +02:00
|
|
|
*/
|
2020-05-20 22:02:07 +02:00
|
|
|
public function calculateAvgPrice(Part $part, ?float $amount = null, ?Currency $currency = null): ?BigDecimal
|
2019-09-01 18:52:22 +02:00
|
|
|
{
|
2019-11-09 00:47:20 +01:00
|
|
|
if (null === $amount) {
|
2019-09-01 18:52:22 +02:00
|
|
|
$amount = $this->getMinOrderAmount($part);
|
|
|
|
}
|
|
|
|
|
2019-11-09 00:47:20 +01:00
|
|
|
if (null === $amount) {
|
2019-09-01 18:52:22 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$orderdetails = $part->getOrderdetails(true);
|
|
|
|
|
2020-05-20 22:02:07 +02:00
|
|
|
$avg = BigDecimal::zero();
|
2019-09-01 18:52:22 +02:00
|
|
|
$count = 0;
|
|
|
|
|
|
|
|
//Find the price for the amount, for the given
|
|
|
|
foreach ($orderdetails as $orderdetail) {
|
2019-11-09 00:31:42 +01:00
|
|
|
$pricedetail = $orderdetail->findPriceForQty($amount);
|
2019-09-01 18:52:22 +02:00
|
|
|
|
|
|
|
//When we dont have informations about this amount, ignore it
|
2019-11-09 00:47:20 +01:00
|
|
|
if (null === $pricedetail) {
|
2019-09-01 18:52:22 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-04-08 16:06:32 +02:00
|
|
|
$converted = $this->convertMoneyToCurrency($pricedetail->getPricePerUnit(), $pricedetail->getCurrency(), $currency);
|
|
|
|
//Ignore price informations that can not be converted to base currency.
|
2020-04-10 13:05:08 +02:00
|
|
|
if (null !== $converted) {
|
2020-05-20 22:02:07 +02:00
|
|
|
$avg = $avg->plus($converted);
|
2020-04-08 16:06:32 +02:00
|
|
|
++$count;
|
|
|
|
}
|
2019-09-01 18:52:22 +02:00
|
|
|
}
|
|
|
|
|
2019-11-09 00:47:20 +01:00
|
|
|
if (0 === $count) {
|
2019-09-01 18:52:22 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-20 22:02:07 +02:00
|
|
|
return $avg->dividedBy($count)->toScale(Pricedetail::PRICE_PRECISION);
|
2019-09-01 18:52:22 +02:00
|
|
|
}
|
|
|
|
|
2019-09-01 14:37:53 +02:00
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* Converts the given value in origin currency to the choosen target currency.
|
|
|
|
*
|
2019-09-01 14:37:53 +02:00
|
|
|
* @param Currency|null $originCurrency The currency the $value is given in.
|
2019-11-09 00:47:20 +01:00
|
|
|
* Set to null, to use global base currency.
|
2019-09-01 14:37:53 +02:00
|
|
|
* @param Currency|null $targetCurrency The target currency, to which $value should be converted.
|
2019-11-09 00:47:20 +01:00
|
|
|
* Set to null, to use global base currency.
|
|
|
|
*
|
2020-05-20 22:02:07 +02:00
|
|
|
* @return BigDecimal|null The value in $targetCurrency given as bcmath string.
|
2020-08-21 21:36:22 +02:00
|
|
|
* Returns null, if it was not possible to convert between both values (e.g. when the exchange rates are missing)
|
2019-09-01 14:37:53 +02:00
|
|
|
*/
|
2020-05-20 22:02:07 +02:00
|
|
|
public function convertMoneyToCurrency(BigDecimal $value, ?Currency $originCurrency = null, ?Currency $targetCurrency = null): ?BigDecimal
|
2019-09-01 14:37:53 +02:00
|
|
|
{
|
2019-09-01 18:52:22 +02:00
|
|
|
//Skip conversion, if both currencies are same
|
|
|
|
if ($originCurrency === $targetCurrency) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2019-09-01 14:37:53 +02:00
|
|
|
$val_base = $value;
|
2020-05-20 22:02:07 +02:00
|
|
|
//Convert value to base currency
|
2019-11-09 00:47:20 +01:00
|
|
|
if (null !== $originCurrency) {
|
2019-09-01 14:37:53 +02:00
|
|
|
//Without an exchange rate we can not calculate the exchange rate
|
2020-08-21 21:36:22 +02:00
|
|
|
if (null === $originCurrency->getExchangeRate() || $originCurrency->getExchangeRate()->isZero()) {
|
2019-09-01 14:37:53 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-20 22:02:07 +02:00
|
|
|
$val_base = $value->multipliedBy($originCurrency->getExchangeRate());
|
2019-09-01 14:37:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$val_target = $val_base;
|
2020-05-20 22:02:07 +02:00
|
|
|
//Convert value in base currency to target currency
|
2019-11-09 00:47:20 +01:00
|
|
|
if (null !== $targetCurrency) {
|
2019-09-01 14:37:53 +02:00
|
|
|
//Without an exchange rate we can not calculate the exchange rate
|
2019-11-09 00:47:20 +01:00
|
|
|
if (null === $targetCurrency->getExchangeRate()) {
|
2019-09-01 14:37:53 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-20 22:02:07 +02:00
|
|
|
$val_target = $val_base->multipliedBy($targetCurrency->getInverseExchangeRate());
|
2019-09-01 14:37:53 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 22:02:07 +02:00
|
|
|
return $val_target->toScale(Pricedetail::PRICE_PRECISION, RoundingMode::HALF_UP);
|
2019-09-01 14:37:53 +02:00
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
}
|