public function testShouldFail() { $a = new Money(9); //$b = $a->negate(); //$this->assertEquals(-9, $b->getAmount()); $this->assertEquals(-8, $a->getAmount()); }
/** @return Money */ public function convert(Money $money) { if (!$money->getCurrency()->equals($this->counterCurrency)) { throw new InvalidArgumentException("The Money has the wrong currency"); } // @todo add rounding mode? return new Money((int) round($money->getAmount() * $this->ratio), $this->baseCurrency); }
public function testCanSetAmount() { // Arrange $a = new Money(1); // Act $a->setAmount(123); // Assert $this->assertEquals(123, $a->getAmount()); }
public function testAdd() { // Arrange $a = new Money(600); // Act $a->add(66); // Assert $this->assertEquals(666, $a->getAmount()); }
/** * Compares this moneys amount with the given ones and returns 0 if they are equal, 1 if this amount is larger than * the given ones, -1 otherwise. This method explicitly disregards the Currency! * * @param Money $money the money to compare with * * @return integer */ public function compare(Money $money) { return bccomp($this->getAmount(), $money->getAmount(), 2); }
/** * Returns the order value of all positions with a tax rate > 0 without any * fees and charges. * * @param boolean $includeChargesForProducts Indicates wether to include charges and * discounts for products * @param boolean $includeChargesForTotal Indicates wether to include charges and * discounts for the shopping cart total * * @return Money */ public function getTaxableAmountNetWithoutFees($includeChargesForProducts = false, $includeChargesForTotal = false) { $priceNet = new Money(); $priceNet->setAmount(0); $priceNet->setCurrency(SilvercartConfig::DefaultCurrency()); if ($includeChargesForTotal == 'false') { $includeChargesForTotal = false; } if ($includeChargesForProducts == 'false') { $includeChargesForProducts = false; } foreach ($this->SilvercartOrderPositions() as $position) { if (!$includeChargesForProducts && $position->isChargeOrDiscount && $position->chargeOrDiscountModificationImpact == 'productValue' || !$includeChargesForTotal && $position->isChargeOrDiscount && $position->chargeOrDiscountModificationImpact == 'totalValue') { continue; } if ($position->TaxRate > 0 || $position->IsNonTaxable) { $priceNet->setAmount($priceNet->getAmount() + $position->PriceTotal->getAmount()); } } return new DataObject(array('Amount' => $priceNet)); }
function testSetValueAsMoney() { $m1 = new Money(); $m1->setValue(array('Currency' => 'EUR', 'Amount' => 3.44)); $m2 = new Money(); $m2->setValue($m1); $this->assertEquals($m2->getCurrency(), 'EUR'); $this->assertEquals($m2->getAmount(), 3.44); }
/** * Returns the amount. * * @return float */ public function getAmount() { $amount = parent::getAmount(); $this->extend('updateAmount', $amount); return $amount; }
/** * Try to publish a product with amount changed */ function testChangeProductAmount() { $this->loginAs('admin'); $productA = $this->objFromFixture('Product', 'productA'); $productID = $productA->ID; //Publish $productA->doPublish(); $this->assertTrue($productA->isPublished()); $versions = DB::query('SELECT * FROM "Product_versions" WHERE "RecordID" = ' . $productID); $versionsAfterPublished = array(); foreach ($versions as $versionRow) { $versionsAfterPublished[] = $versionRow; } $originalAmount = $productA->Amount; $newAmount = new Money(); $newAmount->setAmount($originalAmount->getAmount() + 50); $newAmount->setCurrency($originalAmount->getCurrency()); $this->assertTrue($newAmount->Amount != $originalAmount->Amount); //Update price and publish $productA->Amount = $newAmount; $productA->doPublish(); $versions = DB::query('SELECT * FROM "Product_versions" WHERE "RecordID" = ' . $productID); $versionsAfterPriceChange = array(); foreach ($versions as $versionRow) { $versionsAfterPriceChange[] = $versionRow; } $this->assertTrue(count($versionsAfterPublished) + 1 == count($versionsAfterPriceChange)); $this->assertEquals($versionsAfterPriceChange[2]['AmountAmount'], $newAmount->getAmount()); }
public function testCheckCurrent() { $money = new Money(2); $this->assertEquals(2, $money->getAmount()); }
/** * Compare if the amount is equal to the given amount. * * @param Money $money * @return bool * @throws \InvalidArgumentException */ public function equalTo(Money $money) { if ($this->getCurrency() !== $money->getCurrency()) { throw new \InvalidArgumentException("Argument one must be of same currency"); } return $this->getAmount() === $money->getAmount(); }
public function testPlus() { $number = new Money(100); $number->plus(28); $this->assertEquals(128, $number->getAmount()); }
/** * 等価性確認 * * @param Money $money マネーオブジェクト * @return bool 等価性(trueで等価) */ public function equals(Money $money) { return $this->_amount === $money->getAmount() && $this->_currency === $money->getCurrency(); }
/** * Compares this instance with an other instance. * * @param Money $b The instance to which this instance is to be compared. * * @return int -1, 0 or 1 as this instance is less than, equal to, or greater than $b * * @throws \InvalidArgumentException When the currencies do not match */ public function compareTo(Money $b) { if ($this->getCurrency() != $b->getCurrency()) { throw new \InvalidArgumentException('Can not compare different currencies'); } if ($this->getAmount() < $b->getAmount()) { return -1; } elseif ($this->getAmount() == $b->getAmount()) { return 0; } else { return 1; } }
/** * Subtratcs the given money from this one (immutable) and returns the result. * * @param Money $money the money to subtract * * @return Money * * @throws CurrencyMismatchException if the Currencies do not match */ public function subtract(Money $money) { if (!$this->currency->equals($money->getCurrency())) { throw new CurrencyMismatchException($this . ' does not match ' . $money); } $amount = bcsub($this->amount, $money->getAmount(), self::BCSCALE); return self::valueOf($amount, $this->currency); }
/** * @covers \SebastianBergmann\Money\Money::multiply * @covers \SebastianBergmann\Money\Money::newMoney * @covers \SebastianBergmann\Money\Money::castToInt * @uses \SebastianBergmann\Money\Money::__construct * @uses \SebastianBergmann\Money\Money::handleCurrencyArgument * @uses \SebastianBergmann\Money\Money::getAmount * @uses \SebastianBergmann\Money\Money::assertInsideIntegerBounds * @uses \SebastianBergmann\Money\Currency */ public function testCanBeMultipliedByAFactor() { $a = new Money(1, new Currency('EUR')); $b = $a->multiply(2); $this->assertEquals(1, $a->getAmount()); $this->assertEquals(2, $b->getAmount()); }
/** * Returns the handling costs for the chosen shipping method. * * @return Money * * @author Roland Lehmann <*****@*****.**>, * Sebastian Diel <*****@*****.**> * @since 16.11.2013 */ public function HandlingCostShipment() { $handlingCostShipment = 0; $selectedShippingMethod = $this->getShippingMethod(); if ($selectedShippingMethod) { $handlingCostShipmentObj = $selectedShippingMethod->getShippingFee()->getCalculatedPrice(); } else { $handlingCostShipmentObj = new Money(); $handlingCostShipmentObj->setAmount($handlingCostShipment); $handlingCostShipmentObj->setCurrency(SilvercartConfig::DefaultCurrency()); } if (SilvercartConfig::PriceType() == 'net') { $taxRate = $this->getMostValuableTaxRate(); if ($taxRate !== false) { $handlingCostShipment = round($handlingCostShipmentObj->getAmount() / (100 + $taxRate->Rate) * 100, 2); } $handlingCostShipmentObj->setAmount($handlingCostShipment); } return $handlingCostShipmentObj; }