Newer
Older
<?php
/**
* This file contains QUI\ERP\Money\Price
*/
namespace QUI\ERP\Money;
use QUI;
use QUI\ERP\Currency\Currency;
use QUI\ERP\Discount\Discount;
use QUI\Interfaces\Users\User;
use function mb_strpos;
use function mb_substr;
use function preg_replace;
use function round;
use function str_replace;
use function trim;
/**
* Class Price
* @package QUI\ERP\Products\Price
*/
class Price
{
/**
* Netto Price
* @var float|int
/**
* Price currency
* @var QUI\ERP\Currency\Currency
*/
/**
* Flag for Price from
* @var bool
*/
/**
* @var array
*/
/**
* User
* @var ?QUI\Interfaces\Users\User
protected ?QUI\Interfaces\Users\User $User = null;
/**
* Price constructor.
*
* @param Currency $Currency
* @param User|null $User - optional, if no user, session user are used
public function __construct(
QUI\ERP\Currency\Currency $Currency,
null | QUI\Interfaces\Users\User $User = null
$this->Currency = $Currency;
if (!QUI::getUsers()->isUser($User)) {
$this->User = QUI::getUserBySession();
}
}
/**
* Return the price as array notation
* @return array
*/
public function toArray(): array
'price' => $this->value(),
'currency' => $this->getCurrency()->getCode(),
'display' => $this->getDisplayPrice(),
}
/**
* @return float|int|null
public function getPrice(): float | int | null
* @return float|int|null
public function value(): float | int | null
* @return float|int|null
public function getValue(): float | int | null
/**
* Return the price for the view / displaying
*
* @return string
*/
public function getDisplayPrice(): string
return $this->Currency->format($this->getPrice());
}
/**
* Add a discount to the price
*
* @param QUI\ERP\Discount\Discount $Discount
* @throws QUI\Exception
*/
public function addDiscount(Discount $Discount): void
{
/* @var $Disc Discount */
foreach ($this->discounts as $Disc) {
// der gleiche discount kann nur einmal enthalten sein
if ($Disc->getId() == $Discount->getId()) {
return;
}
if ($Disc->canCombinedWith($Discount) === false) {
'quiqqer/products',
'exception.discount.not.combinable',
'id1' => $Disc->getId(),
'id2' => $Discount->getId()
}
}
$this->discounts[] = $Discount;
}
/**
* Return the assigned discounts
*
* @return array [Discount, Discount, Discount]
*/
public function getDiscounts(): array
{
return $this->discounts;
}
/**
* Return the currency from the price
*
* @return QUI\ERP\Currency\Currency
*/
public function getCurrency(): QUI\ERP\Currency\Currency
{
return $this->Currency;
}
/**
* calculation
*/
/**
* Validates a price value
*
* @param QUI\Locale|null $Locale - based locale, in which the price is
* @return float|int|null
public static function validatePrice(mixed $value, null | QUI\Locale $Locale = null): float | int | null
if (is_float($value) || is_int($value)) {
return round($value, QUI\ERP\Defaults::getPrecision());
if ($value instanceof Price) {
$value = $value->getPrice();
}
$isNegative = str_starts_with($value, '-');
return null;
}
if ($Locale === null) {
$Locale = QUI::getSystemLocale();
}
$negativeTurn = 1;
if ($isNegative) {
$negativeTurn = -1;
}
$thousandSeparator = $Locale->getGroupingSeparator();
$thousands = mb_strpos($value, $thousandSeparator);
if ($thousands === false && $decimal === false) {
return round(floatval($value), 4) * $negativeTurn;
if (mb_substr($value, -4, 1) === $thousandSeparator) {
$value = str_replace($thousandSeparator, '', $value);
}
}
if ($thousands === false && $decimal !== false) {
$value = str_replace($decimalSeparator, '.', $value);
$value = str_replace($thousandSeparator, '', $value);
$value = str_replace($decimalSeparator, '.', $value);
$value = floatval($value);
$value = round($value, QUI\ERP\Defaults::getPrecision());
return $value * $negativeTurn;
* Return if the price is minimal price and higher prices exists
*
* @return bool
*/
public function isMinimalPrice(): bool
{
return $this->isMinimalPrice;
}
/**
* enables the minimal price
* -> price from
* -> ab
*/
public function enableMinimalPrice(): void
{
$this->isMinimalPrice = true;
}
/**
* enables the minimal price
* -> price from
* -> ab
*/
public function disableMinimalPrice(): void
{
$this->isMinimalPrice = false;
}