Upgraded brick/math to latest version

This commit is contained in:
Jan Böhmer 2026-05-19 21:17:45 +02:00
parent 506d5f8173
commit 527c42c227
11 changed files with 58 additions and 56 deletions

View file

@ -37,6 +37,7 @@ use App\Services\EntityURLGenerator;
use App\Services\Formatters\AmountFormatter;
use App\Services\Formatters\MoneyFormatter;
use App\Services\ProjectSystem\ProjectBuildHelper;
use Brick\Math\BigDecimal;
use Brick\Math\RoundingMode;
use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\Query;
@ -93,14 +94,14 @@ class ProjectBomEntriesDataTable implements DataTableTypeInterface
return htmlspecialchars($this->amountFormatter->format($context->getQuantity(), $context->getPart()->getPartUnit()));
},
])
->add('partId', TextColumn::class, [
'label' => $this->translator->trans('project.bom.part_id'),
'visible' => true,
'orderField' => 'part.id',
'render' => function ($value, ProjectBOMEntry $context) {
return $context->getPart() instanceof Part ? (string) $context->getPart()->getId() : '';
},
])
->add('partId', TextColumn::class, [
'label' => $this->translator->trans('project.bom.part_id'),
'visible' => true,
'orderField' => 'part.id',
'render' => function ($value, ProjectBOMEntry $context) {
return $context->getPart() instanceof Part ? (string) $context->getPart()->getId() : '';
},
])
->add('name', TextColumn::class, [
'label' => $this->translator->trans('part.table.name'),
'orderField' => 'NATSORT(part.name)',
@ -161,7 +162,7 @@ class ProjectBomEntriesDataTable implements DataTableTypeInterface
'label' => $this->translator->trans('part.table.manufacturingStatus'),
'data' => static fn(ProjectBOMEntry $context): ?ManufacturingStatus => $context->getPart()?->getManufacturingStatus(),
'orderField' => 'part.manufacturing_status',
'class' => ManufacturingStatus::class,
'class' => ManufacturingStatus::class,
'render' => function (?ManufacturingStatus $status, ProjectBOMEntry $context): string {
if ($status === null) {
return '';
@ -212,7 +213,7 @@ class ProjectBomEntriesDataTable implements DataTableTypeInterface
'visible' => false,
'render' => function ($value, ProjectBOMEntry $context) {
$price = $this->projectBuildHelper->getEntryUnitPrice($context);
return $this->moneyFormatter->format($price->toScale(2, RoundingMode::UP)->toFloat(), null, 2, true);
return $this->moneyFormatter->format($price->toScale(2, RoundingMode::Up)->toFloat(), null, 2, true);
},
])
->add('ext_price', TextColumn::class, [
@ -221,7 +222,8 @@ class ProjectBomEntriesDataTable implements DataTableTypeInterface
'render' => function ($value, ProjectBOMEntry $context) {
$price = $this->projectBuildHelper->getEntryUnitPrice($context);
return $this->moneyFormatter->format(
$price->multipliedBy($context->getQuantity())->toScale(2, RoundingMode::UP)->toFloat(),
$price->multipliedBy(BigDecimal::fromFloatShortest($context->getQuantity()))
->toScale(2, RoundingMode::Up)->toFloat(),
null,
2,
true

View file

@ -44,7 +44,7 @@ class BigDecimalType extends Type
return BigDecimal::of($value);
return BigDecimal::of(is_float($value) ? BigDecimal::fromFloatShortest($value) : $value);
}
/**

View file

@ -204,7 +204,7 @@ class Currency extends AbstractStructuralDBElement
return null;
}
return BigDecimal::one()->dividedBy($tmp, $tmp->getScale(), RoundingMode::HALF_UP);
return BigDecimal::one()->dividedBy($tmp, $tmp->getScale(), RoundingMode::HalfUp);
}
/**

View file

@ -195,10 +195,10 @@ class Pricedetail extends AbstractDBElement implements TimeStampableInterface
#[SerializedName('price_per_unit')]
public function getPricePerUnit(float|string|BigDecimal $multiplier = 1.0): BigDecimal
{
$tmp = BigDecimal::of($multiplier);
$tmp = is_float($multiplier) ? BigDecimal::fromFloatShortest($multiplier) : BigDecimal::of($multiplier);
$tmp = $tmp->multipliedBy($this->price);
return $tmp->dividedBy($this->price_related_quantity, static::PRICE_PRECISION, RoundingMode::HALF_UP);
return $tmp->dividedBy(BigDecimal::fromFloatShortest($this->price_related_quantity), static::PRICE_PRECISION, RoundingMode::HalfUp);
}
/**
@ -317,7 +317,7 @@ class Pricedetail extends AbstractDBElement implements TimeStampableInterface
*/
public function setPrice(BigDecimal $new_price): self
{
$tmp = $new_price->toScale(self::PRICE_PRECISION, RoundingMode::HALF_UP);
$tmp = $new_price->toScale(self::PRICE_PRECISION, RoundingMode::HalfUp);
//Only change the object, if the value changes, so that doctrine does not detect it as changed.
if ((string) $tmp !== (string) $this->price) {
$this->price = $tmp;

View file

@ -286,7 +286,7 @@ class PKPartImporter
//Partkeepr stores the price per item, we need to convert it to the price per packaging unit
$price_per_item = BigDecimal::of($partdistributor['price']);
$packaging_unit = (float) ($partdistributor['packagingUnit'] ?? 1);
$pricedetail->setPrice($price_per_item->multipliedBy($packaging_unit));
$pricedetail->setPrice($price_per_item->multipliedBy(BigDecimal::fromFloatShortest($packaging_unit)));
$pricedetail->setPriceRelatedQuantity($packaging_unit);
//We have to set the minimum discount quantity to the packaging unit (PartKeepr does not know this concept)
//But in Part-DB the minimum discount qty have to be unique across a orderdetail

View file

@ -222,7 +222,7 @@ class TimeTravel
if (isset($metadata->fieldMappings[$field])) {
//We need to convert the string to a BigDecimal first
if (!$data instanceof BigDecimal && ('big_decimal' === $metadata->getFieldMapping($field)->type)) {
$data = BigDecimal::of($data);
$data = is_float($data) ? BigDecimal::fromFloatShortest($data) : BigDecimal::of($data);
}
if (!$data instanceof \DateTimeInterface

View file

@ -170,7 +170,7 @@ class PricedetailHelper
return null;
}
return $avg->dividedBy($count, Pricedetail::PRICE_PRECISION, RoundingMode::HALF_UP);
return $avg->dividedBy($count, Pricedetail::PRICE_PRECISION, RoundingMode::HalfUp);
}
/**
@ -213,6 +213,6 @@ class PricedetailHelper
$val_target = $val_base->multipliedBy($targetCurrency->getInverseExchangeRate());
}
return $val_target->toScale(Pricedetail::PRICE_PRECISION, RoundingMode::HALF_UP);
return $val_target->toScale(Pricedetail::PRICE_PRECISION, RoundingMode::HalfUp);
}
}

View file

@ -190,7 +190,7 @@ final readonly class ProjectBuildHelper
continue;
}
$has_price = true;
$total = $total->plus($unit_price->multipliedBy($entry->getQuantity())->multipliedBy($number_of_builds));
$total = $total->plus($unit_price->multipliedBy(BigDecimal::fromFloatShortest($entry->getQuantity()))->multipliedBy($number_of_builds));
}
return $has_price ? $total : null;
@ -206,7 +206,7 @@ final readonly class ProjectBuildHelper
if ($total === null) {
return null;
}
return $total->dividedBy($number_of_builds, 10, RoundingMode::HALF_UP);
return $total->dividedBy($number_of_builds, 10, RoundingMode::HalfUp);
}
/**
@ -215,7 +215,7 @@ final readonly class ProjectBuildHelper
public function roundedTotalBuildPrice(Project $project, int $number_of_builds = 1, ?Currency $currency = null): ?BigDecimal
{
return $this->calculateTotalBuildPrice($project, $number_of_builds, $currency)
?->toScale(2, RoundingMode::UP);
?->toScale(2, RoundingMode::Up);
}
/**
@ -224,7 +224,7 @@ final readonly class ProjectBuildHelper
public function roundedUnitBuildPrice(Project $project, int $number_of_builds = 1, ?Currency $currency = null): ?BigDecimal
{
return $this->calculateUnitBuildPrice($project, $number_of_builds, $currency)
?->toScale(2, RoundingMode::UP);
?->toScale(2, RoundingMode::Up);
}
/**

View file

@ -44,15 +44,15 @@ class ExchangeRateUpdater
try {
//Try it in the direction QUOTE/BASE first, as most providers provide rates in this direction
$rate = $this->swap->latest($currency->getIsoCode().'/'.$this->localizationSettings->baseCurrency);
$effective_rate = BigDecimal::of($rate->getValue());
$effective_rate = BigDecimal::fromFloatShortest($rate->getValue());
} catch (UnsupportedCurrencyPairException|UnsupportedExchangeQueryException $exception) {
//Otherwise try to get it inverse and calculate it ourselfes, from the format "BASE/QUOTE"
$rate = $this->swap->latest($this->localizationSettings->baseCurrency.'/'.$currency->getIsoCode());
//The rate says how many quote units are worth one base unit
//So we need to invert it to get the exchange rate
$rate_bd = BigDecimal::of($rate->getValue());
$effective_rate = BigDecimal::one()->dividedBy($rate_bd, Currency::PRICE_SCALE, RoundingMode::HALF_UP);
$rate_bd = BigDecimal::fromFloatShortest($rate->getValue());
$effective_rate = BigDecimal::one()->dividedBy($rate_bd, Currency::PRICE_SCALE, RoundingMode::HalfUp);
}
$currency->setExchangeRate($effective_rate);