Skip to content
Code-Schnipsel Gruppen Projekte
CalculationValue.php 2,34 KiB
Newer Older
  • Learn to ignore specific revisions
  •  * This file contains QUI\ERP\Accounting\CalculationValue
    
     */
    
    namespace QUI\ERP\Accounting;
    
    use QUI;
    
    
    Henning Leutz's avatar
    Henning Leutz committed
    use function is_numeric;
    use function round;
    
    
    /**
     * Class CalculationValue
     * - represent a number for the calculations
     *
     * @package QUI\ERP\Accounting
     */
    class CalculationValue
    {
    
    Henning Leutz's avatar
    Henning Leutz committed
        protected QUI\ERP\Currency\Currency $Currency;
    
        protected int | float $number = 0;
    
        protected int | float $precision = 8;
    
    
        /**
         * CalculationValue constructor.
         *
         * @param $number
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @param QUI\ERP\Currency\Currency|null $Currency
    
         * @param bool|int $precision - The optional number of decimal digits to round to.
    
        public function __construct(
            $number,
            null | QUI\ERP\Currency\Currency $Currency = null,
            bool | int $precision = false
        ) {
    
    Henning Leutz's avatar
    Henning Leutz committed
            if (!is_numeric($number)) {
    
                return;
            }
    
            $this->number = $number;
    
    Henning Leutz's avatar
    Henning Leutz committed
            if (is_numeric($precision)) {
    
                $this->precision = $precision;
    
                $this->precision = QUI\ERP\Defaults::getPrecision();
    
            }
    
            // currency
            if ($Currency instanceof QUI\ERP\Currency\Currency) {
                $this->Currency = $Currency;
    
                return;
            }
    
            $this->Currency = QUI\ERP\Defaults::getCurrency();
        }
    
    
        public function value(): float | int
    
        /**
         * Return the CalculationValue with the wanted precision
         *
    
         * @param bool|int $precision
    
         * @return CalculationValue
         */
    
        public function precision(bool | int $precision = false): CalculationValue
    
        {
            if ($precision === false) {
                return $this;
            }
    
            return new CalculationValue(
                $this->number,
                $this->Currency,
                $precision
            );
        }
    
        /**
         * Return the formatted number
         *
    
         * @param null|QUI\Locale $Locale - optional, Locale object for the formatting
    
        public function formatted(null | QUI\Locale $Locale = null): string
    
            return $this->Currency->format($this->number, $Locale);
    
        }
    
        /**
         * Return the un-formatted number
         *
    
    Henning Leutz's avatar
    Henning Leutz committed
         * @return float
    
    Henning Leutz's avatar
    Henning Leutz committed
        public function get(): float
    
    Henning Leutz's avatar
    Henning Leutz committed
            return round($this->number, $this->precision);