public static ApplicationResult TransferToAccount(string accountType, decimal amount) { if (CurrentAccount.Type == accountType) { return(new ApplicationResult { Success = false, Message = "Cannot transfer to yourself" }); } if (CurrentAccount.Currency.Amount <= 0) { return(new ApplicationResult { Success = false, Message = "Insuffisent amount on the account" }); } if (amount <= 0) { return(new ApplicationResult { Success = false, Message = "Amount cannot be zero or less than zero" }); } var result = AccountManager.Data.FirstOrDefault(acc => acc.Type == accountType); if (result == null) { return(new ApplicationResult { Success = false, Message = "Specified account type does not exist" }); } if (CurrentAccount.Currency.Type != result.Currency.Type) { try { var converted = AccountCurrency.Convert(CurrentAccount.Currency, result.Currency, amount); CurrentAccount.Currency.Amount -= amount; result.Currency.Amount += converted; AccountManager.UpdateData(); } catch (BadConversionException ex) { return(new ApplicationResult { Success = false, Message = ex.Message }); } return(new ApplicationResult { Success = true, Message = $"Successfully converted and transfered to {result.Type}" }); } CurrentAccount.Currency.Amount -= amount; result.Currency.Amount += amount; AccountManager.UpdateData(); return(new ApplicationResult { Success = true, Message = $"Successfully transfered to {result.Type}" }); }
public static ApplicationResult AddIncomeOrExpense(AccountActionType actionType, string currencyType, decimal amount, string category, string note) { if (amount <= 0) { return(new ApplicationResult { Success = false, Message = "Amount cannot be zero or less than zero" }); } if (currencyType.ToLower() == "auto") { currencyType = CurrentAccount.Currency.Type; } var currentAccountCurrency = CurrentAccount.Currency; if (currencyType != CurrentAccount.Currency.Type) { try { var result = AccountCurrency.Convert(currencyType, CurrentAccount.Currency.Type, amount); DoIncomeOrExpenseOperation(actionType, currentAccountCurrency, result); AccountActionManager.AddAccountAction(new AccountAction(actionType, new AccountCurrency(CurrentAccount.Currency.Type, result), category, note)); AccountActionManager.UpdateData(); AccountManager.UpdateData(); } catch (BadConversionException ex) { return(new ApplicationResult { Success = false, Message = ex.Message }); } return(new ApplicationResult { Success = true, Message = "Successfully converted and added" }); } DoIncomeOrExpenseOperation(actionType, currentAccountCurrency, amount); AccountActionManager.AddAccountAction(new AccountAction(actionType, new AccountCurrency(currencyType, amount), category, note)); AccountActionManager.UpdateData(); AccountManager.UpdateData(); return(new ApplicationResult { Success = true, Message = "Successfully added" }); }