public function getAmountInBaseUnits(Money $money) { $iso = $this->iso4217->getByAlpha3($money->getCurrency()->getName()); $decimals = $iso['exp']; $dividend = pow(10, $decimals); return $money->getAmount() / $dividend; }
/** * @return float|string */ public function getPrice() { if (null == $this->unitPrice) { return 'N\\A'; } return round($this->unitPrice->getAmount() / 100, 2); }
/** * 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); }
/** * @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 convert(Money $money, $target) { if (!$target instanceof MoneyCurrency) { throw new InvalidArgumentException(sprintf('Second argument must be Currency, %s given.', gettype($target))); } $rate = $this->swap->quote(new CurrencyPair($money->getCurrency(), $target)); return new Money($money->multiply((double) $rate->getValue())->getAmount(), $target); }
/** * @param Quote|Invoice $object * @param Money $total * * @return mixed */ private function setDiscount($object, Money $total) { if (null !== $object->getDiscount()) { $discount = $total->multiply($object->getDiscount()); $total = $total->subtract($discount); return $total; } return $total; }
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()); }
/** * {@inheritdoc} */ public function format(Money $money) { $currencyCode = $money->getCurrency()->getCode(); if (isset($this->formatters[$currencyCode])) { return $this->formatters[$currencyCode]->format($money); } if (isset($this->formatters['*'])) { return $this->formatters['*']->format($money); } throw new FormatterException('No formatter found for currency ' . $currencyCode); }
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); }
/** * @param string $fromMemberId * @param string $amount * @param string $toMemberId * @param string $occurredOn */ public function __construct($fromMemberId, $amount, $toMemberId, $occurredOn) { $this->occurredOn = DateTime::createFromFormat('Y-m-d H:i:s', $occurredOn); $this->fromMemberId = Identifier::fromString($fromMemberId); $this->amount = Money::MXN($amount); $this->toMemberId = Identifier::fromString($toMemberId); }
/** @test */ function it_should_serialize_a_domain_event_to_json() { $serializer = new JsonSerializer(); $anEvent = new InstantaneousEvent(Identifier::fromString('abc'), Money::MXN(10000), new DateTime('2015-10-24 12:39:51')); $json = $serializer->serialize($anEvent); $this->assertEquals('{"occurred_on":"2015-10-24 12:39:51","member_id":"abc","amount":10000}', $json, 'JSON format for serialized event is incorrect'); }
/** * @param integer $amount */ public function __construct($amount, Currency $currency = null) { if ($currency === null) { $currency = new Currency(self::DEFAULT_CURRENCY_NAME); } parent::__construct($amount, $currency); }
public function testOverrideCurrency() { \Locale::setDefault("fr_FR"); $form = $this->factory->create($this->simpleMoneyTypeClass, null, ["currency" => "USD"]); $form->submit(array("tbbc_amount" => '1 252,5')); $this->assertEquals(Money::USD(125250), $form->getData()); }
/** * @return MoneyFieldset */ public function __invoke() { $moneyFieldset = new MoneyFieldset(); $moneyFieldset->setHydrator(new MoneyHydrator()); $moneyFieldset->setObject(Money::BRL(0)); return $moneyFieldset; }
/** * @param SimpleXMLElement $statementXml * @param Statement $statement */ private function addEntriesToStatement(SimpleXMLElement $statementXml, Statement $statement) { $index = 0; $entriesXml = $statementXml->Ntry; foreach ($entriesXml as $entryXml) { $amount = Money::stringToUnits((string) $entryXml->Amt); $currency = (string) $entryXml->Amt['Ccy']; $bookingDate = (string) $entryXml->BookgDt->Dt; $valueDate = (string) $entryXml->ValDt->Dt; if ((string) $entryXml->CdtDbtInd === 'DBIT') { $amount = $amount * -1; } $entry = new Entry($statement, $index, new Money($amount, new Currency($currency)), new DateTimeImmutable($bookingDate), new DateTimeImmutable($valueDate)); if (isset($entryXml->RvslInd) && (string) $entryXml->RvslInd === 'true') { $entry->setReversalIndicator(true); } if (isset($entryXml->NtryRef) && (string) $entryXml->NtryRef) { $entry->setReference((string) $entryXml->NtryRef); } if (isset($entryXml->NtryDtls->Btch->PmtInfId) && (string) $entryXml->NtryDtls->Btch->PmtInfId) { $entry->setBatchPaymentId((string) $entryXml->NtryDtls->Btch->PmtInfId); } $this->addTransactionDetailsToEntry($entryXml, $entry); $statement->addEntry($entry); $index++; } }
/** * @param PlanDefinition $definition * @param Money $amountToPay * @param PlanParameters $parameters * @return PaymentPlan */ public function getPlan(PlanDefinition $definition, Money $amountToPay, PlanParameters $parameters) { /** @var array $paymentProportions */ $paymentProportions = $definition->getAttribute('payments'); //The values of the payments array is a ratios array /** @var Money[] $amounts */ $amounts = $amountToPay->allocate(array_values($paymentProportions)); $payments = array_map(function ($rawDate, $amount) { //rawDate is either 'immediate' or a 'Y-m-d' string, or invalid. if ($rawDate === 'immediate') { return PlannedPayment::immediate($amount); } return PlannedPayment::withDueDate($amount, DueDate::fromString($rawDate)); }, array_keys($paymentProportions), $amounts); return new PaymentPlan($payments, $definition->getAttribute('short_description'), $definition->getAttribute('long_description')); }
/** @test */ function it_should_update_the_information_of_a_registered_member() { $member = $this->members->with(Identifier::fromString('wxyz')); $member->transfer(Money::MXN(500), $this->existingMember); $this->members->update($this->existingMember); $this->assertBalanceAmounts(3500, $this->existingMember, "Current member balance should be 3500"); }
/** * {@inheritdoc} * * @throws InvalidArgumentException * * @return int|null */ public function filter($value) { if (null === $value || is_string($value) && strlen($value) === 0) { return null; } return Money::stringToUnits($value); }
/** * @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); }
public function testSetData() { \Locale::setDefault("fr_FR"); $form = $this->factory->create($this->moneyTypeClass, null, array("currency_type" => $this->currencyTypeClass)); $form->setData(Money::EUR(120)); $formView = $form->createView(); $this->assertEquals("1,20", $formView->children["tbbc_amount"]->vars["value"]); }
public function widthdrawl($amount) { $originalfunds = Money::USD((int) $this->fundsavailable); $addedFunds = Money::USD((int) $amount); $fundsavailable = $originalfunds->subtract($addedFunds); $this->fundsavailable = $fundsavailable->getAmount(); $this->save(); }
public function testSetData() { \Locale::setDefault("fr_FR"); $moneyType = new SimpleMoneyType($this->pairManager, 2); $form = $this->factory->create($moneyType, null, array()); $form->setData(Money::EUR(120)); $formView = $form->createView(); $this->assertEquals("1,20", $formView->children["tbbc_amount"]->vars["value"]); }
/** @test */ function beneficiary_balance_should_increase_after_funds_have_been_transferred() { $this->forAll(Generator\int(1, 10000))->then(function ($amount) { $fromMember = A::member()->withBalance(10000)->build(); $toMember = A::member()->withBalance(5000)->build(); $fromMember->transfer(Money::MXN($amount), $toMember); $this->assertBalanceIsGreaterThan(5000, $toMember, "Transferring {$amount} increased balance of receiver member"); }); }
function it_should_spread_the_bursary_when_the_plan_spans_multiple_years($baseCalculator, PaymentPlan $planBeforeBursary, PaymentPlan $finalPlan) { $baseCalculator->getPlan(Def::withName('Foo'), Money::GBP(250), Argument::any())->willReturn($planBeforeBursary); $planBeforeBursary->getPlannedPayments()->willReturn([PlannedPayment::immediate(Money::GBP(50)), PlannedPayment::withDueDate(Money::GBP(50), DueDate::fromString('2014-01-01')), PlannedPayment::withDueDate(Money::GBP(50), DueDate::fromString('2014-06-01')), PlannedPayment::withDueDate(Money::GBP(50), DueDate::fromString('2015-01-01')), PlannedPayment::withDueDate(Money::GBP(50), DueDate::fromString('2015-06-01'))]); $planBeforeBursary->getShortDescription()->willReturn('foo'); $planBeforeBursary->getLongDescription()->willReturn('foofoo'); $finalPlan = new PaymentPlan([PlannedPayment::immediate(Money::GBP(50)), PlannedPayment::withDueDate(Money::GBP(50), DueDate::fromString('2014-01-01')), PlannedPayment::withDueDate(Money::GBP(40), DueDate::fromString('2014-06-01')), PlannedPayment::withDueDate(Money::GBP(50), DueDate::fromString('2015-01-01')), PlannedPayment::withDueDate(Money::GBP(40), DueDate::fromString('2015-06-01'))], 'foo', 'foofoo'); $this->getPlan(Def::withName('Foo'), Money::GBP(230), Params::fromArray(['bursary_total_deduction' => 20]))->shouldBeLike($finalPlan); }
/** @test */ public function ConvertsEurToUsdAndBack() { $eur = Money::EUR(100); $pair = new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.25); $usd = $pair->convert($eur); $this->assertEquals(Money::USD(125), $usd); $pair = new CurrencyPair(new Currency('USD'), new Currency('EUR'), 0.8); $eur = $pair->convert($usd); $this->assertEquals(Money::EUR(100), $eur); }
/** * @return float */ public function calculateTotal() { /** @var Money $total */ $total = Money::GBP(0); foreach ($this->products as $product) { $unitPrice = $product->getUnitPrice() ?: Money::GBP(0); $total = $total->add($unitPrice); } return round($total->getAmount() / 100, 2); }
function it_should_use_a_registered_modifier_when_it_is_part_of_the_plan_definition($basePlan, PlanModifierInterface $modifier, PaymentPlan $planProvidedByModifier, PaymentPlan $unmodifiedPlan) { $definition = PlanDefinition::withNameAndAttributesAsArray('Foo', ['modifiers' => ['mymodifier']]); $this->registerModifier('mymodifier', $modifier); $modifier->setBaseCalculator($basePlan)->shouldBeCalled(); $modifier->getPlan($definition, Money::GBP(100), PlanParameters::none())->shouldBeCalled()->willReturn($planProvidedByModifier); $basePlan->getPlan(PlanDefinition::withName('Foo'), Money::GBP(100), PlanParameters::none())->willReturn($unmodifiedPlan); $this->getPlan($definition, Money::GBP(100), PlanParameters::none())->shouldReturn($planProvidedByModifier); $this->getPlan(PlanDefinition::withName('Foo'), Money::GBP(100), PlanParameters::none())->shouldReturn($unmodifiedPlan); }
/** @test */ public function ConvertsEurToUsdWithModes() { $eur = Money::Euro(10); $pair = new CurrencyPair(CurrencyProxy::determine('EUR'), CurrencyProxy::determine('USD'), 1.25); $usd = $pair->convert($eur); $this->assertEquals(Money::USD(13), $usd); $pair = new CurrencyPair(CurrencyProxy::determine('EUR'), CurrencyProxy::determine('USD'), 1.25); $usd = $pair->convert($eur, new RoundingMode(RoundingMode::ROUND_HALF_DOWN)); $this->assertEquals(Money::USD(12), $usd); }
/** @test */ function it_should_send_deposit_received_email() { $template = Mockery::mock(TemplateEngine::class); $template->shouldReceive('render')->once()->with(Mockery::type('string'), Mockery::type('array')); $transport = new InMemory(); $sender = new TransferFundsZendMailSender($template, $transport); $sender->sendDepositReceivedEmail(A::member()->build()->information(), A::member()->withEmail('*****@*****.**')->build()->information(), Money::MXN(500), new DateTime()); $this->assertEquals('*****@*****.**', $transport->getLastMessage()->getTo()->current()->getEmail(), 'Address doesn\'t belong to the member receiving the deposit'); $this->assertRegExp('/received.*deposit/', $transport->getLastMessage()->getSubject(), 'Email\'s subject is wrong'); }
/** @test */ function it_should_create_an_stored_event_from_a_given_domain_event() { $event = new InstantaneousEvent(Identifier::fromString('abc'), Money::MXN(500000), new DateTime('2015-10-25 19:59:00')); $factory = new StoredEventFactory(new JsonSerializer()); $storedEvent = $factory->from($event); // Stored events get an ID after being persisted $this->assertEquals(null, $storedEvent->id()); $this->assertEquals('{"occurred_on":"2015-10-25 19:59:00","member_id":"abc","amount":500000}', $storedEvent->body()); $this->assertEquals(InstantaneousEvent::class, $storedEvent->type()); $this->assertEquals('2015-10-25 19:59:00', $storedEvent->occurredOn()->format('Y-m-d H:i:s')); }