Ejemplo n.º 1
0
 /**
  * @return float|string
  */
 public function getPrice()
 {
     if (null == $this->unitPrice) {
         return 'N\\A';
     }
     return round($this->unitPrice->getAmount() / 100, 2);
 }
 public function getAmountInBaseUnits(Money $money)
 {
     $iso = $this->iso4217->getByAlpha3($money->getCurrency()->getName());
     $decimals = $iso['exp'];
     $dividend = pow(10, $decimals);
     return $money->getAmount() / $dividend;
 }
Ejemplo n.º 3
0
 /**
  * Decrease this account current balance
  *
  * @param Money $amount
  * @throws InsufficientFunds
  *     A member cannot withdraw more than it's account current balance
  */
 public function withdraw(Money $amount)
 {
     if ($amount->greaterThan($this->balance)) {
         throw new InsufficientFunds("Cannot withdraw {$amount->getAmount()}");
     }
     $this->balance = $this->balance->subtract($amount);
 }
Ejemplo n.º 4
0
 /**
  * @param \Money\Money $money
  * @param        $rounding_mode
  * @return \Money\Money
  * @expectedException InvalidArgumentException
  */
 public function convert(Money $money, RoundingMode $rounding_mode = null)
 {
     if (!$money->getCurrency()->equals($this->baseCurrency)) {
         throw new InvalidArgumentException("The Money has the wrong currency");
     }
     $rounding_mode = $rounding_mode ?: RoundingMode::halfUp();
     return new Money((int) round($money->getAmount() * $this->ratio, 0, $rounding_mode->getRoundingMode()), $this->counterCurrency);
 }
 public function testHydratorHydratesAsExpected()
 {
     $hydrator = new MoneyHydrator();
     $data = ['amount' => 500, 'currency' => 'BRL'];
     $money = new Money(500, new Currency('BRL'));
     $object = $hydrator->hydrate($data, new \stdClass());
     $this->assertEquals($money->getAmount(), $object->getAmount());
     $this->assertEquals($money->getCurrency(), $object->getCurrency());
 }
Ejemplo n.º 6
0
 function it_converts_money_using_swap(SwapInterface $swap, Money $money, Money $converted, Currency $from, Currency $to)
 {
     $money->getAmount()->willReturn(100);
     $money->getCurrency()->willReturn($from);
     $money->multiply(120.3971)->willReturn($converted);
     $converted->getAmount()->willReturn(12039);
     $converted->getCurrency()->willReturn($to);
     $from->__toString()->willReturn('EUR');
     $to->__toString()->willReturn('RSD');
     $rate = new Rate(120.3971);
     $swap->quote('EUR/RSD')->shouldBeCalled()->willReturn($rate);
     $this->convert($money, $to)->getAmount()->shouldBe(12039);
 }
Ejemplo n.º 7
0
 /**
  * @covers Xoops\Core\Kernel\Dtype\DtypeMoney::getVar
  * @covers Xoops\Core\Kernel\Dtype\DtypeMoney::cleanVar
  */
 public function testGetVarCleanVar()
 {
     $testValue = new Money(10000, new Currency('USD'));
     $key = 'money_test';
     $this->xObject[$key] = $testValue;
     $this->xObject[$key] = $this->object->cleanVar($this->xObject, $key);
     $value = $this->xObject->getVar($key, Dtype::FORMAT_NONE);
     $this->assertInstanceOf('\\Money\\Money', $value);
     $this->assertEquals($testValue->getAmount(), $value->getAmount());
     $this->assertEquals($testValue->getCurrency(), $value->getCurrency());
     $this->assertNotSame($value, $testValue);
     $value2 = $this->xObject->getVar($key, Dtype::FORMAT_SHOW);
     $this->assertInstanceOf('\\Money\\Money', $value2);
     $this->assertEquals($testValue->getAmount(), $value2->getAmount());
     $this->assertEquals($testValue->getCurrency(), $value2->getCurrency());
     $this->assertNotSame($value, $value2);
 }
Ejemplo n.º 8
0
 /**
  * {@inheritdoc}
  */
 public function format(Money $money)
 {
     $valueBase = (string) $money->getAmount();
     $negative = false;
     if (substr($valueBase, 0, 1) === '-') {
         $negative = true;
         $valueBase = substr($valueBase, 1);
     }
     $fractionDigits = $this->formatter->getAttribute(\NumberFormatter::FRACTION_DIGITS);
     $valueLength = strlen($valueBase);
     if ($valueLength > $fractionDigits) {
         $subunits = substr($valueBase, 0, $valueLength - $fractionDigits) . '.';
         $subunits .= substr($valueBase, $valueLength - $fractionDigits);
     } else {
         $subunits = '0.' . str_pad('', $fractionDigits - $valueLength, '0') . $valueBase;
     }
     if ($negative === true) {
         $subunits = '-' . $subunits;
     }
     return $this->formatter->formatCurrency($subunits, $money->getCurrency()->getCode());
 }
Ejemplo n.º 9
0
 /**
  * @param Product $product
  *
  * @return Money
  */
 public function discount(Product $product)
 {
     $discountTotal = $this->money($product);
     // "Global" Basket Discount
     foreach ($product->discounts as $discount) {
         $discountTotal = $discountTotal->add($discount->product($product)->multiply($product->quantity));
     }
     // Coupons
     foreach ($product->coupons as $coupon) {
         // Each discount of an coupon
         foreach ($coupon->discounts() as $productDiscount) {
             // Calculate the amount of discount
             $discount = $productDiscount->product($product);
             $discount = $discount->multiply($product->quantity);
             // Add the discount to the discount total
             $discount = new Money($discount->getAmount(), $product->price->getCurrency());
             $discountTotal = $discountTotal->add($discount);
         }
     }
     return $discountTotal;
 }
Ejemplo n.º 10
0
 /**
  * {@inheritdoc}
  */
 public function format(Money $money)
 {
     $valueBase = $money->getAmount();
     $negative = false;
     if ($valueBase[0] === '-') {
         $negative = true;
         $valueBase = substr($valueBase, 1);
     }
     $subunit = $this->currencies->subunitFor($money->getCurrency());
     $valueLength = strlen($valueBase);
     if ($valueLength > $subunit) {
         $formatted = substr($valueBase, 0, $valueLength - $subunit) . '.';
         $formatted .= substr($valueBase, $valueLength - $subunit);
     } else {
         $formatted = '0.' . str_pad('', $subunit - $valueLength, '0') . $valueBase;
     }
     if ($negative === true) {
         $formatted = '-' . $formatted;
     }
     return $formatted;
 }
Ejemplo n.º 11
0
 /**
  * {@inheritdoc}
  */
 public function format(Money $money)
 {
     if (BitcoinCurrencies::CODE !== $money->getCurrency()->getCode()) {
         throw new FormatterException('Bitcoin Formatter can only format Bitcoin currency');
     }
     $valueBase = $money->getAmount();
     $negative = false;
     if ('-' === $valueBase[0]) {
         $negative = true;
         $valueBase = substr($valueBase, 1);
     }
     $subunit = $this->currencies->subunitFor($money->getCurrency());
     $valueBase = Number::roundMoneyValue($valueBase, $this->fractionDigits, $subunit);
     $valueLength = strlen($valueBase);
     if ($valueLength > $subunit) {
         $formatted = substr($valueBase, 0, $valueLength - $subunit);
         if ($subunit) {
             $formatted .= '.';
             $formatted .= substr($valueBase, $valueLength - $subunit);
         }
     } else {
         $formatted = '0.' . str_pad('', $subunit - $valueLength, '0') . $valueBase;
     }
     if ($this->fractionDigits === 0) {
         $formatted = substr($formatted, 0, strpos($formatted, '.'));
     } elseif ($this->fractionDigits > $subunit) {
         $formatted .= str_pad('', $this->fractionDigits - $subunit, '0');
     } elseif ($this->fractionDigits < $subunit) {
         $lastDigit = strpos($formatted, '.') + $this->fractionDigits + 1;
         $formatted = substr($formatted, 0, $lastDigit);
     }
     $formatted = BitcoinCurrencies::SYMBOL . $formatted;
     if (true === $negative) {
         $formatted = '-' . BitcoinCurrencies::SYMBOL . $formatted;
     }
     return $formatted;
 }
Ejemplo n.º 12
0
 public function testGetters()
 {
     $m = new Money(100, $euro = new Currency('EUR'));
     $this->assertEquals(100, $m->getAmount());
     $this->assertEquals($euro, $m->getCurrency());
 }
Ejemplo n.º 13
0
 /**
  * Deposit the given amount to the beneficiary's account
  *
  * @param Money $amount
  * @throws InvalidTransferAmount
  *     A member cannot transfer a negative amount to another member
  */
 protected function applyDeposit(Money $amount)
 {
     if ($amount->isNegative()) {
         throw new InvalidTransferAmount("Cannot transfer negative amount {$amount->getAmount()}");
     }
     $this->account->deposit($amount);
 }
Ejemplo n.º 14
0
 /**
  * @param Money $amount
  *
  * @return float
  */
 public function toFloat(Money $amount)
 {
     return (double) $amount->getAmount() / pow(10, 2);
 }
Ejemplo n.º 15
0
 /**
  * @param Money $money
  * @return bool
  */
 public function notGreaterThan(Money $money)
 {
     return $this->getAmount() <= $money->getAmount();
 }
Ejemplo n.º 16
0
 /**
  * @param Money $money
  * @return string
  */
 public function formatMoney(Money $money)
 {
     return "\${$this->formatMoneyAmount(round($money->getAmount() / 100, 2))} {$money->getCurrency()}";
 }
Ejemplo n.º 17
0
 /**
  * Returns the amount for the given Money object as simple float
  *
  * @param Money $money
  * @return float
  */
 public function asFloat(Money $money)
 {
     $amount = $money->getAmount();
     $amount = (double) $amount;
     $amount = $amount / pow(10, $this->decimals);
     return $amount;
 }
Ejemplo n.º 18
0
 /**
  * Set price.
  *
  * @param MoneyObject $price
  *
  * @return $this
  */
 public function setAmount(MoneyObject $price)
 {
     $this->priceAmount = $price->getAmount();
     $this->priceCurrency = $price->getCurrency()->getName();
     return $this;
 }
Ejemplo n.º 19
0
 /**
  * Format money as a string - using this format as requirements are as a pound/pence
  * value with two decimal places - a float ill get reformatted as a JSON number (eg. 1.50 as 1.5).
  */
 public function __toString()
 {
     return number_format($this->money->getAmount() / 100, 2, '.', '');
 }
Ejemplo n.º 20
0
 /**
  * @param \JMS\Serializer\VisitorInterface $visitor
  * @param Money $money
  * @param mixed[] $type
  * @param \JMS\Serializer\Context $context
  * @return string
  */
 public function serializeMoney(VisitorInterface $visitor, Money $money, array $type, Context $context)
 {
     return (string) $money->getAmount() . ' ' . $money->getCurrency()->getName();
 }
 /**
  * Returns a string representation of the object.
  *
  * @return string
  */
 public function toString()
 {
     return "amount is not lower than {$this->upperLimit->getAmount()}";
 }
Ejemplo n.º 22
0
 /**
  * Returns money amount
  *
  * @return Integer
  */
 public function getAmount()
 {
     $amount = new Integer($this->money->getAmount());
     return $amount;
 }
Ejemplo n.º 23
0
 /**
  * @return mixed
  */
 public function __toString()
 {
     return $this->value->getAmount();
 }
Ejemplo n.º 24
0
 /**
  * @param Money $cost
  *
  * @return $this
  */
 public function willCosts(Money $cost)
 {
     $this->amount = $cost->getAmount();
     $this->currency = $cost->getCurrency()->getName();
     return $this;
 }
Ejemplo n.º 25
0
 /**
  * @param Money  $money
  * @param bool   $showDecimals
  * @param string $locale
  * @param string $pattern
  *
  * @return string
  */
 public function __invoke(Money $money, $showDecimals = null, $locale = null, $pattern = null)
 {
     $currencyFormat = $this->getView()->plugin('currencyFormat');
     return $currencyFormat($money->getAmount() / 100, $money->getCurrency(), $showDecimals, $locale, $pattern);
 }
Ejemplo n.º 26
0
 /**
  * @param Money $money
  * @param $divisor
  *
  * @return string
  */
 private function convert(Money $money, $divisor)
 {
     return number_format($money->getAmount() / $divisor, 2, '.', '');
 }
Ejemplo n.º 27
0
 /**
  * @return float
  */
 public function asFloat() : float
 {
     return (double) ($this->money->getAmount() / 100);
 }