public void ApproveLoanRequest(OrderLoan orderLoan) { Account buyerAccount = orderLoan.Account; Account sellerAccount = orderLoan.Loan.Account; double orderCost = (double)orderLoan.Commission; double orderAmount = (double)orderLoan.Amount; double orderFee = (double)orderLoan.MonthlyFee; if (buyerAccount.Currency.IsoCode != sellerAccount.Currency.IsoCode) { ExchangeRate rateFromTo = Fixer .Rate(sellerAccount.Currency.IsoCode, buyerAccount.Currency.IsoCode); orderCost = rateFromTo.Convert(orderCost); orderAmount = rateFromTo.Convert(orderAmount); orderFee = rateFromTo.Convert(orderFee); } buyerAccount.Balance -= (decimal)orderCost; buyerAccount.Balance += (decimal)orderAmount; buyerAccount.MonthlyOutcome += (decimal)orderFee; sellerAccount.Balance += orderLoan.Commission; sellerAccount.Balance -= orderLoan.Amount; sellerAccount.MonthlyIncome += orderLoan.MonthlyFee; //TODO: every mounts task transfer orderLoan.CompletedOn = DateTime.UtcNow; orderLoan.Status = OrderStatus.Approved; this.context.SaveChanges(); }
public void Convert_Allows_The_Rate_To_Be_Specified_The_Other_Way_Around() { var rate = new ExchangeRate(EUR, USD, 1.141233m); rate.Convert(100m, EUR).Should().BeApproximately(114.12m, 0.01m); rate.Convert(100m, USD).Should().BeApproximately(87.62m, 0.01m); }
public void ApproveSaveRequest(OrderSave orderedSave) { Account depositorAccount = orderedSave.Account; Account sellerAccount = orderedSave.Save.Account; double depositAmount = (double)orderedSave.Amount; double monthlyIncome = (double)orderedSave.MonthlyFee; if (depositorAccount.Currency.IsoCode != sellerAccount.Currency.IsoCode) { ExchangeRate rateFromTo = Fixer .Rate(sellerAccount.Currency.IsoCode, depositorAccount.Currency.IsoCode); depositAmount = rateFromTo.Convert(depositAmount); monthlyIncome = rateFromTo.Convert(monthlyIncome); } depositorAccount.Balance -= (decimal)depositAmount; depositorAccount.MonthlyIncome += (decimal)monthlyIncome; //TODO every mounts task transfer sellerAccount.Balance += orderedSave.Amount; sellerAccount.MonthlyOutcome += orderedSave.MonthlyFee; orderedSave.CompletedOn = DateTime.UtcNow; orderedSave.Status = OrderStatus.Approved; this.context.SaveChanges(); }
public void WhenConvertingEurosToDollars_ThenConversionShouldBeCorrect() { // Convert €100,99 to $127.156509 (= €100.99 * 1.2591) var converted = _exchangeRate.Convert(Money.Euro(100.99M)); converted.Currency.Should().Be(_dollar); converted.Amount.Should().Be(127.16M); }
public static decimal CalculateOfferPrice(OffersRow offer, CurrenciesRow neededCurrency) { using (var connection = SqlConnections.NewFor <OffersRow>()) { var currencyFields = CurrenciesRow.Fields; var currencyOffer = connection.First <CurrenciesRow>(currencyFields.Id == offer.CurrencyId.Value); if (offer.CurrencyId == neededCurrency.Id) { return(new Money(offer.Price ?? 0, Currency.FromCode(currencyOffer.CurrencyId)).Amount); } else { if (currencyOffer.BaseCurrencyId.HasValue) { ExchangeRate exchangeRateOffer; var baseCurrency = connection.First <CurrenciesRow>( currencyFields.Id == currencyOffer.BaseCurrencyId.Value); exchangeRateOffer = new ExchangeRate(Currency.FromCode(baseCurrency.CurrencyId), Currency.FromCode(currencyOffer.CurrencyId), currencyOffer.Rate ?? 0); if (baseCurrency.Id == neededCurrency.Id) { return(exchangeRateOffer .Convert(new Money(offer.Price ?? 0, Currency.FromCode(currencyOffer.CurrencyId))).Amount); } else { var tempPriceOffer = exchangeRateOffer.Convert( new Money(offer.Price ?? 0, Currency.FromCode(currencyOffer.CurrencyId))); var exchangeRateNeeded = new ExchangeRate(Currency.FromCode(baseCurrency.CurrencyId), Currency.FromCode(neededCurrency.CurrencyId), neededCurrency.Rate ?? 0); return(exchangeRateNeeded.Convert(tempPriceOffer).Amount); } } else { var exchangeRateNeeded = new ExchangeRate(Currency.FromCode(currencyOffer.CurrencyId), Currency.FromCode(neededCurrency.CurrencyId), neededCurrency.Rate ?? 0); return(exchangeRateNeeded .Convert(new Money(offer.Price ?? 0, Currency.FromCode(currencyOffer.CurrencyId))).Amount); } } } }
public void DoTransfer(TransferBankCloudInputModel model, Account grantAccount, Account receiverAccount) { grantAccount.Balance -= model.Amount; if (receiverAccount.CurrencyId != grantAccount.CurrencyId) { ExchangeRate rateFromTo = Fixer .Rate(grantAccount.Currency.IsoCode, receiverAccount.Currency.IsoCode); var convertAmmount = rateFromTo.Convert((double)model.Amount); receiverAccount.Balance += (decimal)convertAmmount; model.ConvertedAmount = (decimal)convertAmmount; } else { receiverAccount.Balance += model.Amount; model.ConvertedAmount = model.Amount; } Transfer transfer = this.mapper.Map <Transfer>(model); transfer.ForeignAccountId = receiverAccount.Id; transfer.Date = DateTime.UtcNow; transfer.Completed = DateTime.UtcNow; transfer.Type = TransferType.BankCloud; transfer.Status = TransferStatus.Approved; transfer.BalanceType = BalanceType.Negative; this.context.Transfers.Add(transfer); this.context.SaveChanges(); }
public IActionResult ChargeBankCloud(string iban) { var chargeData = TempData.Get <AccountsChargeInputModel>("charge"); var bankCloudCharge = this.mapper.Map <ChargeBankCloudInputModel>(chargeData); var grantAccount = this.accountsService.GetAccountByIban(iban); if (grantAccount == null || bankCloudCharge.Id == grantAccount.Id) { this.TempData["error"] = GlobalConstants.MISSING_BANKCLOUD_ACCOUNT; return(this.RedirectToAction("Charge", chargeData)); } Transfer transfer = this.mapper.Map <Transfer>(bankCloudCharge); if (grantAccount.Currency.IsoCode != bankCloudCharge.IsoCode) { ExchangeRate rateFromTo = Fixer .Rate(bankCloudCharge.IsoCode, grantAccount.Currency.IsoCode); var convertedAmount = rateFromTo.Convert((double)bankCloudCharge.Amount); transfer.ConvertedAmount = (decimal)convertedAmount; } else { transfer.ConvertedAmount = bankCloudCharge.Amount; } transfer.ForeignAccountId = grantAccount.Id; transfer.BalanceType = BalanceType.Positive; this.transferService.AddBankCloudTransfer(transfer); return(this.Redirect("/Accounts/Index")); }
public void Convert_Will_Throw_Exception_When_A_Wrong_Currency_Is_Used() { var rate = new ExchangeRate(EUR, USD, 1.141233m); Action action = () => rate.Convert(100, AUD); action.Should().Throw <ArgumentOutOfRangeException>(); }
public void Convert_Will_Throw_Exception_When_The_Currency_Is_Null() { var rate = new ExchangeRate(EUR, USD, 1.141233m); Action action = () => rate.Convert(100, null); action.Should().Throw <ArgumentNullException>(); }
public JsonResult Index(string currencyCode) { if (string.IsNullOrWhiteSpace(currencyCode)) { return(new JsonResult(NotFound())); } currencyCode = currencyCode.ToUpper(); var model = new List <OffersPublicModel>(); using (var connection = SqlConnections.NewFor <OffersRow>()) { var currencyFields = CurrenciesRow.Fields; if (!connection.Exists <CurrenciesRow>(currencyFields.CurrencyId == currencyCode)) { return(new JsonResult(NotFound())); } var offerFields = OffersRow.Fields; var offers = connection.List <OffersRow>(s => s .Select(offerFields.OfferId, offerFields.Name, offerFields.Price, offerFields.CurrencyId) .Where(offerFields.IsActive == 1 && offerFields.IsPublic == 1 && offerFields.Enabled == 1)); foreach (var offer in offers) { var currencyOffer = connection.First <CurrenciesRow>(currencyFields.Id == offer.CurrencyId.Value); var neededCurrency = connection.First <CurrenciesRow>(currencyFields.CurrencyId == currencyCode); if (offer.CurrencyId == neededCurrency.Id) { model.Add(new OffersPublicModel { OfferId = offer.OfferId ?? 0, OfferName = offer.Name, Price = new Money(offer.Price ?? 0, Currency.FromCode(currencyOffer.CurrencyId)).ToString() }); } else { ExchangeRate exchangeRateOffer; if (currencyOffer.BaseCurrencyId.HasValue) { var baseCurrency = connection.First <CurrenciesRow>( currencyFields.Id == currencyOffer.BaseCurrencyId.Value); exchangeRateOffer = new ExchangeRate(Currency.FromCode(baseCurrency.CurrencyId), Currency.FromCode(currencyOffer.CurrencyId), currencyOffer.Rate ?? 0); if (baseCurrency.CurrencyId == currencyCode) { model.Add(new OffersPublicModel { OfferId = offer.OfferId ?? 0, OfferName = offer.Name, Price = exchangeRateOffer .Convert(new Money(offer.Price ?? 0, Currency.FromCode(currencyOffer.CurrencyId))) .ToString() }); } else { var tempPriceOffer = exchangeRateOffer.Convert( new Money(offer.Price ?? 0, Currency.FromCode(currencyOffer.CurrencyId))); var exchangeRateNeeded = new ExchangeRate(Currency.FromCode(baseCurrency.CurrencyId), Currency.FromCode(neededCurrency.CurrencyId), neededCurrency.Rate ?? 0); model.Add(new OffersPublicModel { OfferId = offer.OfferId ?? 0, OfferName = offer.Name, Price = exchangeRateNeeded.Convert(tempPriceOffer).ToString() }); } } else { var exchangeRateNeeded = new ExchangeRate(Currency.FromCode(currencyOffer.CurrencyId), Currency.FromCode(neededCurrency.CurrencyId), neededCurrency.Rate ?? 0); model.Add(new OffersPublicModel { OfferId = offer.OfferId ?? 0, OfferName = offer.Name, Price = exchangeRateNeeded.Convert(new Money(offer.Price ?? 0, Currency.FromCode(currencyOffer.CurrencyId))).ToString() }); } } } } return(new JsonResult(model)); }
public static Money ConvertTo(this Money @this, ExchangeRate rate) => rate.Convert(@this);