Esempio n. 1
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
                    });
                }
            }
        }