private void SaveTransaction()
        {
            decimal oldAmount = 0;

            if (EditMode)
            {
                oldAmount = transaction.Amount;
            }
            Account accountFrom = listOfAccounts.ElementAt(spinnerTransactionAccount.SelectedItemPosition);
            Account accountTo   = listOfAccounts.ElementAt(spinnerTransactionAccountTransfer.SelectedItemPosition);

            transaction.Name         = edtTransactionName.Text;
            transaction.Amount       = Convert.ToDecimal(txtTransactionAmount.Text);
            transaction.Account      = accountFrom.Id;
            transaction.PaymentType  = spinnerTransactionPaymentType.SelectedItem.ToString();
            transaction.Type         = Type;
            transaction.CurrencyRate = 1;

            var date     = Convert.ToDateTime(txtTransactionDate.Text);
            var datetime = new DateTime(date.Year, date.Month, date.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);

            transaction.CreatedDate = datetime;

            if (Type == "Transfer")
            {
                transaction.AccountTransfer = accountTo.Id;
                transaction.Category        = SilverCoinsManager.GetCategoryByName("Transfer").Id;

                if (accountFrom.Currency != accountTo.Currency)
                {
                    transaction.CurrencyRate = Convert.ToDecimal(edtCurrencyRate.Text);
                }
                else
                {
                    transaction.CurrencyRate = 1;
                }
            }
            else if (Type == "Income")
            {
                transaction.Category = listOfIncomeCategories.ElementAt(spinnerTransactionCategory.SelectedItemPosition).Id;
            }
            else
            {
                transaction.Category = listOfExpenseCategories.ElementAt(spinnerTransactionCategory.SelectedItemPosition).Id;
            }

            SilverCoinsManager.SaveTransaction(transaction);

            if (EditMode)
            {
                AccountCalculations.AccountCalculations.UpdateBalanceAfterTransactionUpdate(accountFrom, accountTo, transaction, oldAmount);
            }
            else
            {
                AccountCalculations.AccountCalculations.UpdateBalanceAfterTransactionCreate(accountFrom, accountTo, transaction);
            }
            Finish();
        }
Example #2
0
        internal static void ImportTransactionsFromList(IEnumerable <TransactionImportExport> transactionsForImport)
        {
            if (transactionsForImport.Any())
            {
                // Get list of accounts and categories from document
                var accountsForImport = transactionsForImport.Select(x => new Account
                {
                    Name        = x.Account,
                    Currency    = x.Currency,
                    Balance     = Convert.ToDecimal(x.Balance),
                    CreatedDate = DateTime.Today,
                    Description = string.Empty,
                    Icon        = Resource.Drawable.cash
                });

                var categoriesForImport = transactionsForImport.Select(x => new Category
                {
                    Name        = x.Category,
                    Type        = x.Type == Category.CategoryTypes.Expense.ToString() ? Category.CategoryTypes.Expense : Category.CategoryTypes.Income,
                    Description = string.Empty,
                    CreatedDate = DateTime.Today,
                    Visible     = 1,
                    Icon        = Resource.Drawable.salary
                });

                var accounts   = SilverCoinsManager.GetAccounts();
                var categories = SilverCoinsManager.GetCategories();

                // Find accounts and categories that do not exist in database
                var newAccounts   = accountsForImport.Where(x => !accounts.Any(s => s.Name == x.Name));
                var newCategories = categoriesForImport.Where(x => !categories.Any(s => s.Name == x.Name));

                // Insert missing accounts and categories
                if (newAccounts.Any())
                {
                    foreach (var account in newAccounts)
                    {
                        SilverCoinsManager.SaveAccount(account);
                    }
                }

                if (newCategories.Any())
                {
                    foreach (var category in newCategories)
                    {
                        SilverCoinsManager.SaveCategory(category);
                    }
                }

                foreach (var transaction in transactionsForImport)
                {
                    SilverCoinsManager.SaveTransaction(new Transaction
                    {
                        Name        = transaction.Name,
                        Account     = SilverCoinsManager.GetAccountByName(transaction.Account).Id,
                        Category    = SilverCoinsManager.GetCategoryByName(transaction.Category).Id,
                        Amount      = Convert.ToDecimal(transaction.Amount),
                        PaymentType = transaction.PaymentType,
                        Type        = transaction.Type,
                        CreatedDate = DateTime.Now
                    });
                }
            }
        }