Use BigDecimal for Pricedetail price field.

This commit is contained in:
Jan Böhmer 2020-05-20 22:02:07 +02:00
parent db4d7dc5fc
commit b1db89e3b4
8 changed files with 116 additions and 49 deletions

View file

@ -53,7 +53,10 @@ namespace App\Entity\PriceInformations;
use App\Entity\Base\AbstractDBElement; use App\Entity\Base\AbstractDBElement;
use App\Entity\Base\TimestampTrait; use App\Entity\Base\TimestampTrait;
use App\Entity\Contracts\TimeStampableInterface; use App\Entity\Contracts\TimeStampableInterface;
use App\Validator\Constraints\BigDecimal\BigDecimalPositive;
use App\Validator\Constraints\Selectable; use App\Validator\Constraints\Selectable;
use Brick\Math\BigDecimal;
use Brick\Math\RoundingMode;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
@ -74,10 +77,10 @@ class Pricedetail extends AbstractDBElement implements TimeStampableInterface
/** /**
* @var string The price related to the detail. (Given in the selected currency) * @var string The price related to the detail. (Given in the selected currency)
* @ORM\Column(type="decimal", precision=11, scale=5) * @ORM\Column(type="big_decimal", precision=11, scale=5)
* @Assert\Positive() * @BigDecimalPositive()
*/ */
protected $price = '0.0'; protected $price;
/** /**
* @var ?Currency The currency used for the current price information. * @var ?Currency The currency used for the current price information.
@ -118,7 +121,7 @@ class Pricedetail extends AbstractDBElement implements TimeStampableInterface
public function __construct() public function __construct()
{ {
bcscale(static::PRICE_PRECISION); $this->price = BigDecimal::zero()->toScale(self::PRICE_PRECISION);
} }
public function __clone() public function __clone()
@ -149,9 +152,9 @@ class Pricedetail extends AbstractDBElement implements TimeStampableInterface
* Returns the price associated with this pricedetail. * Returns the price associated with this pricedetail.
* It is given in current currency and for the price related quantity. * It is given in current currency and for the price related quantity.
* *
* @return string the price as string, like returned raw from DB * @return BigDecimal the price as BigDecimal object, like returned raw from DB
*/ */
public function getPrice(): string public function getPrice(): BigDecimal
{ {
return $this->price; return $this->price;
} }
@ -159,23 +162,20 @@ class Pricedetail extends AbstractDBElement implements TimeStampableInterface
/** /**
* Get the price for a single unit in the currency associated with this price detail. * Get the price for a single unit in the currency associated with this price detail.
* *
* @param float|string $multiplier The returned price (float or string) will be multiplied * @param float|string|BigDecimal $multiplier The returned price (float or string) will be multiplied
* with this multiplier. * with this multiplier.
* *
* You will get the price for $multiplier parts. If you want the price which is stored * You will get the price for $multiplier parts. If you want the price which is stored
* in the database, you have to pass the "price_related_quantity" count as $multiplier. * in the database, you have to pass the "price_related_quantity" count as $multiplier.
* @param float|string $multiplier The returned price (float or string) will be multiplied
* with this multiplier.
* *
* @return string|null the price as a bcmath string * @return BigDecimal the price as a bcmath string
*/ */
public function getPricePerUnit($multiplier = 1.0): ?string public function getPricePerUnit($multiplier = 1.0): BigDecimal
{ {
$multiplier = (string) $multiplier; $tmp = BigDecimal::of($multiplier);
$tmp = bcmul($this->price, $multiplier, static::PRICE_PRECISION); $tmp = $tmp->multipliedBy($this->price);
return bcdiv($tmp, (string) $this->price_related_quantity, static::PRICE_PRECISION); return $tmp->dividedBy($this->price_related_quantity, static::PRICE_PRECISION, RoundingMode::HALF_UP);
//return ($this->price * $multiplier) / $this->price_related_quantity;
} }
/** /**
@ -265,7 +265,7 @@ class Pricedetail extends AbstractDBElement implements TimeStampableInterface
/** /**
* Set the price. * Set the price.
* *
* @param string $new_price the new price as a float number * @param BigDecimal $new_price the new price as a float number
* *
* * This is the price for "price_related_quantity" parts!! * * This is the price for "price_related_quantity" parts!!
* * Example: if "price_related_quantity" is '10', * * Example: if "price_related_quantity" is '10',
@ -273,15 +273,9 @@ class Pricedetail extends AbstractDBElement implements TimeStampableInterface
* *
* @return $this * @return $this
*/ */
public function setPrice(string $new_price): self public function setPrice(BigDecimal $new_price): self
{ {
//Assert::natural($new_price, 'The new price must be positive! Got %s!'); $this->price = $new_price->toScale(self::PRICE_PRECISION, RoundingMode::HALF_UP);
/* Just a little hack to ensure that price has 5 digits after decimal point,
so that DB does not detect changes, when something like 0.4 is passed
Third parameter must have the scale value of decimal column. */
$this->price = bcmul($new_price, '1.0', static::PRICE_PRECISION);
return $this; return $this;
} }

View file

@ -44,10 +44,11 @@ namespace App\Form\Part;
use App\Entity\Parts\MeasurementUnit; use App\Entity\Parts\MeasurementUnit;
use App\Entity\PriceInformations\Pricedetail; use App\Entity\PriceInformations\Pricedetail;
use App\Form\Type\BigDecimalMoneyType;
use App\Form\Type\BigDecimalNumberType;
use App\Form\Type\CurrencyEntityType; use App\Form\Type\CurrencyEntityType;
use App\Form\Type\SIUnitType; use App\Form\Type\SIUnitType;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
@ -70,7 +71,7 @@ class PricedetailType extends AbstractType
'class' => 'form-control-sm', 'class' => 'form-control-sm',
], ],
]); ]);
$builder->add('price', NumberType::class, [ $builder->add('price', BigDecimalNumberType::class, [
'label' => false, 'label' => false,
'scale' => 5, 'scale' => 5,
'html5' => true, 'html5' => true,

View file

@ -0,0 +1,63 @@
<?php
/**
* 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/>.
*/
namespace App\Form\Type;
use Brick\Math\BigDecimal;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormBuilderInterface;
class BigDecimalNumberType extends AbstractType implements DataTransformerInterface
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addModelTransformer($this);
}
public function getParent()
{
return NumberType::class;
}
public function transform($value)
{
if ($value === null) {
return null;
}
if ($value instanceof BigDecimal) {
return (string) $value;
}
return $value;
}
public function reverseTransform($value)
{
if ($value === null) {
return null;
}
return BigDecimal::of($value);
}
}

View file

@ -45,6 +45,8 @@ namespace App\Services;
use App\Entity\Parts\Part; use App\Entity\Parts\Part;
use App\Entity\PriceInformations\Currency; use App\Entity\PriceInformations\Currency;
use App\Entity\PriceInformations\Pricedetail; use App\Entity\PriceInformations\Pricedetail;
use Brick\Math\BigDecimal;
use Brick\Math\RoundingMode;
use Doctrine\ORM\PersistentCollection; use Doctrine\ORM\PersistentCollection;
use Locale; use Locale;
@ -147,10 +149,10 @@ class PricedetailHelper
* If set to null, the mininmum order amount for the part is used. * If set to null, the mininmum order amount for the part is used.
* @param Currency|null $currency The currency in which the average price should be calculated * @param Currency|null $currency The currency in which the average price should be calculated
* *
* @return string|null The Average price as bcmath string. Returns null, if it was not possible to calculate the * @return BigDecimal|null The Average price as bcmath string. Returns null, if it was not possible to calculate the
* price for the given * price for the given
*/ */
public function calculateAvgPrice(Part $part, ?float $amount = null, ?Currency $currency = null): ?string public function calculateAvgPrice(Part $part, ?float $amount = null, ?Currency $currency = null): ?BigDecimal
{ {
if (null === $amount) { if (null === $amount) {
$amount = $this->getMinOrderAmount($part); $amount = $this->getMinOrderAmount($part);
@ -162,7 +164,7 @@ class PricedetailHelper
$orderdetails = $part->getOrderdetails(true); $orderdetails = $part->getOrderdetails(true);
$avg = '0'; $avg = BigDecimal::zero();
$count = 0; $count = 0;
//Find the price for the amount, for the given //Find the price for the amount, for the given
@ -177,7 +179,7 @@ class PricedetailHelper
$converted = $this->convertMoneyToCurrency($pricedetail->getPricePerUnit(), $pricedetail->getCurrency(), $currency); $converted = $this->convertMoneyToCurrency($pricedetail->getPricePerUnit(), $pricedetail->getCurrency(), $currency);
//Ignore price informations that can not be converted to base currency. //Ignore price informations that can not be converted to base currency.
if (null !== $converted) { if (null !== $converted) {
$avg = bcadd($avg, $converted, Pricedetail::PRICE_PRECISION); $avg = $avg->plus($converted);
++$count; ++$count;
} }
} }
@ -186,7 +188,7 @@ class PricedetailHelper
return null; return null;
} }
return bcdiv($avg, (string) $count, Pricedetail::PRICE_PRECISION); return $avg->dividedBy($count)->toScale(Pricedetail::PRICE_PRECISION);
} }
/** /**
@ -197,40 +199,38 @@ class PricedetailHelper
* @param Currency|null $targetCurrency The target currency, to which $value should be converted. * @param Currency|null $targetCurrency The target currency, to which $value should be converted.
* Set to null, to use global base currency. * Set to null, to use global base currency.
* *
* @return string|null The value in $targetCurrency given as bcmath string. * @return BigDecimal|null The value in $targetCurrency given as bcmath string.
* Returns null, if it was not possible to convert between both values (e.g. when the exchange rates are missing) * Returns null, if it was not possible to convert between both values (e.g. when the exchange rates are missing)
*/ */
public function convertMoneyToCurrency($value, ?Currency $originCurrency = null, ?Currency $targetCurrency = null): ?string public function convertMoneyToCurrency(BigDecimal $value, ?Currency $originCurrency = null, ?Currency $targetCurrency = null): ?BigDecimal
{ {
//Skip conversion, if both currencies are same //Skip conversion, if both currencies are same
if ($originCurrency === $targetCurrency) { if ($originCurrency === $targetCurrency) {
return $value; return $value;
} }
$value = (string) $value;
//Convert value to base currency
$val_base = $value; $val_base = $value;
//Convert value to base currency
if (null !== $originCurrency) { if (null !== $originCurrency) {
//Without an exchange rate we can not calculate the exchange rate //Without an exchange rate we can not calculate the exchange rate
if ($originCurrency->getExchangeRate() === null || $originCurrency->getExchangeRate()->isZero()) { if ($originCurrency->getExchangeRate() === null || $originCurrency->getExchangeRate()->isZero()) {
return null; return null;
} }
$val_base = bcmul($value, (string) $originCurrency->getExchangeRate(), Pricedetail::PRICE_PRECISION); $val_base = $value->multipliedBy($originCurrency->getExchangeRate());
} }
//Convert value in base currency to target currency
$val_target = $val_base; $val_target = $val_base;
//Convert value in base currency to target currency
if (null !== $targetCurrency) { if (null !== $targetCurrency) {
//Without an exchange rate we can not calculate the exchange rate //Without an exchange rate we can not calculate the exchange rate
if (null === $targetCurrency->getExchangeRate()) { if (null === $targetCurrency->getExchangeRate()) {
return null; return null;
} }
$val_target = bcmul($val_base, (string) $targetCurrency->getInverseExchangeRate(), Pricedetail::PRICE_PRECISION); $val_target = $val_base->multipliedBy($targetCurrency->getInverseExchangeRate());
} }
return $val_target; return $val_target->toScale(Pricedetail::PRICE_PRECISION, RoundingMode::HALF_UP);
} }
} }

View file

@ -53,6 +53,7 @@ use App\Services\MarkdownParser;
use App\Services\MoneyFormatter; use App\Services\MoneyFormatter;
use App\Services\SIFormatter; use App\Services\SIFormatter;
use App\Services\Trees\TreeViewGenerator; use App\Services\Trees\TreeViewGenerator;
use Brick\Math\BigDecimal;
use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Extension\AbstractExtension; use Twig\Extension\AbstractExtension;
@ -153,6 +154,10 @@ class AppExtension extends AbstractExtension
public function formatCurrency($amount, ?Currency $currency = null, int $decimals = 5) public function formatCurrency($amount, ?Currency $currency = null, int $decimals = 5)
{ {
if ($amount instanceof BigDecimal) {
$amount = (string) $amount;
}
return $this->moneyFormatter->format($amount, $currency, $decimals); return $this->moneyFormatter->format($amount, $currency, $decimals);
} }

View file

@ -38,18 +38,21 @@
<tbody> <tbody>
{% for detail in order.pricedetails %} {% for detail in order.pricedetails %}
<tr> <tr>
<td> <td>
{{ detail.MinDiscountQuantity | amountFormat(part.partUnit) }} {{ detail.MinDiscountQuantity | amountFormat(part.partUnit) }}
</td> </td>
<td> <td>
{{ detail.price | moneyFormat(detail.currency) }} / {{ detail.PriceRelatedQuantity | amountFormat(part.partUnit) }} {{ detail.price | moneyFormat(detail.currency) }} / {{ detail.PriceRelatedQuantity | amountFormat(part.partUnit) }}
{% if detail.currency != (app.user.currency ?? null) and pricedetail_helper.convertMoneyToCurrency(detail.price, detail.currency) > 0 %} {% set tmp = pricedetail_helper.convertMoneyToCurrency(detail.price, detail.currency) %}
{% if detail.currency != (app.user.currency ?? null) and tmp is not null and tmp.GreaterThan(0) %}
<span class="text-muted">({{ pricedetail_helper.convertMoneyToCurrency(detail.price, detail.currency, app.user.currency ?? null) | moneyFormat(app.user.currency ?? null) }})</span> <span class="text-muted">({{ pricedetail_helper.convertMoneyToCurrency(detail.price, detail.currency, app.user.currency ?? null) | moneyFormat(app.user.currency ?? null) }})</span>
{% endif %} {% endif %}
</td> </td>
<td> <td>
{{ detail.PricePerUnit | moneyFormat(detail.currency) }} {{ detail.PricePerUnit | moneyFormat(detail.currency) }}
{% if detail.currency != (app.user.currency ?? null) and pricedetail_helper.convertMoneyToCurrency(detail.PricePerUnit, detail.currency) > 0 %} {% set tmp = pricedetail_helper.convertMoneyToCurrency(detail.PricePerUnit, detail.currency) %}
{% if detail.currency != (app.user.currency ?? null) and tmp is not null and tmp.GreaterThan(0) %}
<span class="text-muted">({{ pricedetail_helper.convertMoneyToCurrency(detail.PricePerUnit, detail.currency, app.user.currency ?? null) | moneyFormat(app.user.currency ?? null) }})</span> <span class="text-muted">({{ pricedetail_helper.convertMoneyToCurrency(detail.PricePerUnit, detail.currency, app.user.currency ?? null) | moneyFormat(app.user.currency ?? null) }})</span>
{% endif %} {% endif %}
</td> </td>

View file

@ -59,6 +59,6 @@ class CurrencyTest extends TestCase
$this->assertNull($currency->getInverseExchangeRate()); $this->assertNull($currency->getInverseExchangeRate());
$currency->setExchangeRate(BigDecimal::of('1.45643')); $currency->setExchangeRate(BigDecimal::of('1.45643'));
$this->assertSame(BigDecimal::of('0.68661'), $currency->getInverseExchangeRate()); $this->assertSame((string) BigDecimal::of('0.68661'), (string) $currency->getInverseExchangeRate());
} }
} }

View file

@ -45,6 +45,7 @@ namespace App\Tests\Entity\PriceSystem;
use App\Entity\Parts\Part; use App\Entity\Parts\Part;
use App\Entity\PriceInformations\Orderdetail; use App\Entity\PriceInformations\Orderdetail;
use App\Entity\PriceInformations\Pricedetail; use App\Entity\PriceInformations\Pricedetail;
use Brick\Math\BigDecimal;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
class PricedetailTest extends TestCase class PricedetailTest extends TestCase
@ -52,17 +53,17 @@ class PricedetailTest extends TestCase
public function testGetPricePerUnit(): void public function testGetPricePerUnit(): void
{ {
$pricedetail = new Pricedetail(); $pricedetail = new Pricedetail();
$pricedetail->setPrice('100.234'); $pricedetail->setPrice(BigDecimal::of('100.234'));
$this->assertSame('100.23400', $pricedetail->getPricePerUnit()); $this->assertSame('100.23400', (string) $pricedetail->getPricePerUnit());
$pricedetail->setPriceRelatedQuantity(2.3); $pricedetail->setPriceRelatedQuantity(2.3);
$this->assertSame('43.58000', $pricedetail->getPricePerUnit()); $this->assertSame('43.58000', (string) $pricedetail->getPricePerUnit());
$this->assertSame('139.45600', $pricedetail->getPricePerUnit('3.2')); $this->assertSame('139.45600', (string) $pricedetail->getPricePerUnit('3.2'));
$pricedetail->setPrice('10000000.2345'); //Ten million $pricedetail->setPrice(BigDecimal::of('10000000.2345')); //Ten million
$pricedetail->setPriceRelatedQuantity(1.234e9); //100 billion $pricedetail->setPriceRelatedQuantity(1.234e9); //100 billion
$this->assertSame('0.00810', $pricedetail->getPricePerUnit()); $this->assertSame('0.00810', (string) $pricedetail->getPricePerUnit());
} }
public function testGetPriceRelatedQuantity(): void public function testGetPriceRelatedQuantity(): void