Ejemplo n.º 1
0
        public AccountRecord FetchRecord(DateTime start, DateTime end)
        {
            if (actions.Count == 0)
            {
                return(null);
            }
            var currency        = new AccountCurrency(actions[0].Currency.Type);
            var includedActions = new List <AccountAction>();
            var conclusion      = new AccountRecord(includedActions, currency, start, end);

            foreach (var item in actions)
            {
                if (item.Timestamp >= start && item.Timestamp <= end)
                {
                    switch (item.Type)
                    {
                    case AccountActionType.Expense: {
                        conclusion.Result.Amount -= item.Currency.Amount;
                        break;
                    }

                    case AccountActionType.Income: {
                        conclusion.Result.Amount += item.Currency.Amount;
                        break;
                    }
                    }
                    includedActions.Add(item);
                }
            }
            return(conclusion);
        }
Ejemplo n.º 2
0
 public AccountRecord(IReadOnlyList <AccountAction> actions, AccountCurrency result, DateTime startDate, DateTime endDate)
 {
     Result    = result;
     StartDate = startDate;
     EndDate   = endDate;
     Actions   = actions;
 }
Ejemplo n.º 3
0
 public AccountAction(AccountActionType type, AccountCurrency currency, string category, string note)
 {
     Timestamp = DateTime.Now;
     Type      = type;
     Note      = note;
     Category  = category;
     Currency  = currency;
 }
Ejemplo n.º 4
0
 public Account(AccountCurrency currency, string type, string name)
 {
     Name     = name;
     Currency = currency;
     Type     = type;
     AccountCreatedTimestamp = DateTime.Now;
     Id = Guid.NewGuid();
 }
Ejemplo n.º 5
0
        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}"
            });
        }
Ejemplo n.º 6
0
 public void AddAccount(AccountCurrency currency, string type, string name)
 {
     if (Data is List <Account> dataList)
     {
         var acc = new Account(currency, type, name);
         dataList.Add(acc);
         AccountAdded?.Invoke(this, new AccountManagerEventArgs(acc, AccountManagerEventType.AccountAdded));
     }
 }
Ejemplo n.º 7
0
 public static ApplicationResult ModifyAccount(string newAccountCurrencyType = null, string newAccountType = null)
 {
     if (CurrentAccount == null)
     {
         return(new ApplicationResult {
             Success = false,
             Message = "No active accounts"
         });
     }
     if (string.IsNullOrEmpty(newAccountCurrencyType) && string.IsNullOrEmpty(newAccountType))
     {
         return(new ApplicationResult {
             Success = false,
             Message = "At least one argument must be non-null"
         });
     }
     if (newAccountCurrencyType != null && newAccountCurrencyType != CurrentAccount.Currency.Type)
     {
         if (!AccountCurrency.Exists(newAccountCurrencyType))
         {
             return(new ApplicationResult {
                 Success = false,
                 Message = $"Currency {newAccountCurrencyType} is not supported"
             });
         }
         CurrentAccount.Currency.Type = newAccountCurrencyType;
         foreach (var item in AccountActionManager.Data)
         {
             item.Currency.Type = newAccountCurrencyType;
         }
         AccountActionManager.UpdateData();
     }
     if (newAccountType != null && newAccountType != CurrentAccount.Type)
     {
         if (AccountManager.Data.Exists(acc => acc.Type == newAccountType))
         {
             return(new ApplicationResult {
                 Success = false,
                 Message = $"Account {newAccountType} already exists"
             });
         }
         CurrentAccount.Type = newAccountType;
     }
     AccountManager.UpdateData();
     return(new ApplicationResult {
         Success = true,
         Message = "Successfully modified the account"
     });
 }
Ejemplo n.º 8
0
        private static void DoIncomeOrExpenseOperation(AccountActionType actionType, AccountCurrency targetCurrency, decimal finalAmount)
        {
            switch (actionType)
            {
            case AccountActionType.Expense: {
                targetCurrency.Substract(finalAmount);
                break;
            }

            case AccountActionType.Income: {
                targetCurrency.Add(finalAmount);
                break;
            }
            }
        }
Ejemplo n.º 9
0
        public static decimal Convert(AccountCurrency sourceCurrency, AccountCurrency targetCurrency, decimal?amount = null)
        {
            if (amount == null)
            {
                amount = targetCurrency.Amount;
            }
            if (targetCurrency.Type == sourceCurrency.Type)
            {
                return(targetCurrency.Amount);
            }
            string[] lines           = File.ReadAllLines(FilePath);
            int?     convertIndexCol = null;
            int?     convertIndexRow = null;
            decimal  mod;

            try {
                var types = lines[0].Split(',');
                for (int i = 1; i < types.Length; i++)
                {
                    if (types[i] == sourceCurrency.Type)
                    {
                        convertIndexRow = i;
                        break;
                    }
                }
                if (convertIndexRow == null)
                {
                    throw new Exception($"{sourceCurrency.Type} -> {targetCurrency.Type}");
                }
                for (int i = 1; i < lines.Length; i++)
                {
                    var data = lines[i].Split(',');
                    if (data[0] == targetCurrency.Type)
                    {
                        convertIndexCol = i;
                    }
                }
                if (convertIndexCol == null)
                {
                    throw new Exception($"{sourceCurrency.Type} -> {targetCurrency.Type}");
                }
                mod = decimal.Parse(lines[(int)convertIndexCol].Split(',')[(int)convertIndexRow]);
            } catch (Exception ex) {
                throw new BadConversionException($"Bad converison: {ex.Message}");
            }
            return((decimal)amount * mod);
        }
Ejemplo n.º 10
0
        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"
            });
        }
Ejemplo n.º 11
0
 public void EditAccountAction(AccountAction accountAction, AccountCurrency currency = null, string category = null, string note = null)
 {
     if (currency == null && category == null && note == null)
     {
         return;
     }
     if (currency != null)
     {
         accountAction.Currency.Amount = currency.Amount;
         accountAction.Currency.Type   = currency.Type;
     }
     if (category != null)
     {
         accountAction.Category = category;
     }
     if (note != null)
     {
         accountAction.Note = note;
     }
 }
Ejemplo n.º 12
0
 public static ApplicationResult CreateAccount(string alias, string currencyType, string accountType, decimal amount = 0)
 {
     if (AccountManager.Data.Exists(account => account.Type == accountType))
     {
         return(new ApplicationResult {
             Success = false,
             Message = "Account with such name already exists"
         });
     }
     if (!AccountCurrency.Exists(currencyType))
     {
         return(new ApplicationResult {
             Success = false,
             Message = $"Currency {currencyType} is not supported"
         });
     }
     AccountManager.AddAccount(new AccountCurrency(currencyType, amount), accountType, alias);
     AccountManager.UpdateData();
     return(new ApplicationResult {
         Success = true,
         Message = $"Account successfully created"
     });
 }