Ejemplo n.º 1
0
 public void AddNewCar(int userId, SpendingReport.Service.Models.Cars.CarAttributes carAttributes)
 {
     try
     {
         using (var context = new SpendingReportEntities())
         {
             var currentUser = UserHelpers.GetUserByIDWithCars(userId);
             if (currentUser == null)
                 throw new Exception("Používateľ nebol nájdený!");
             var newCar = new Car
             {
                 Name = carAttributes.Name,
                 TankSize = carAttributes.TankSize
             };
             currentUser.Cars.Add(newCar);
             context.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         throw new Exception("Nastaka chyba pri pridavani noveho auta!", ex);
     }
 }
        public bool AddTransactionCategory(CategoryModel model)
        {
            try
            {
                using (var context = new SpendingReportEntities())
                {
                    TransactionCategory category = null;
                    var name = new CategoryName()
                    {
                        Description = model.Description
                    };
                    context.CategoryNames.Add(name);
                    if (model.Id != 0)
                    {
                        category = context.TransactionCategories.FirstOrDefault(i => i.Id == model.Id);
                    }
                    if (category == null)
                    {
                        category = context.TransactionCategories.FirstOrDefault(i => i.Name == model.Name) ??
                                      CreateTransactionCategory(context);
                    }
                    category.Name = model.Name;

                    category.CategoryNames.Add(name);
                    context.SaveChanges();

                    //todo: async
                    UpdateCategories(model.Description, category.Id);
                }

                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("Error occured in add transaction description!", ex);
            }
        }
Ejemplo n.º 3
0
        public Import SaveData(Import bankPayments, int UserId)
        {
            Import ImportWithPocessedTransactions;

            try
            {
                using (var context = new SpendingReportEntities())
                {
                    //if (!bankPayments.Bank.BankID.HasValue)
                    //{
                    //    return -1;
                    //}

                    //entity.Bank bank = context.Banks.FirstOrDefault(t => t.BankCode == bankPayments.Bank.BankID);
                    var currentUser = Helpers.UserHelpers.GetUserByID(UserId);
                    entity.BankAccount SourceAccount;
                    SourceAccount = currentUser.BankAccounts.FirstOrDefault(t => t.IBAN == bankPayments.Account.IBan);
                    if (SourceAccount == null)
                    {
                        SourceAccount = new entity.BankAccount
                        {
                            IBAN = bankPayments.Account.IBan,
                            AccountNumber = (long)bankPayments.Account.AccountID
                        };
                        SourceAccount.Bank =
                            context.Banks.FirstOrDefault(t => t.BankCode == bankPayments.Account.Bank.BankID) ?? new entity.Bank
                            {
                                BankCode = (short)bankPayments.Account.Bank.BankID,
                                Name=String.Empty
                            };
                        currentUser.BankAccounts.Add(SourceAccount);
                        context.BankAccounts.Add(SourceAccount);
                    }

                    ImportWithPocessedTransactions = new Import
                    {
                        Account = new data.ImportedBankAccount
                        {
                            Bank = bankPayments.Account.Bank,
                            AccountID = bankPayments.Account.AccountID,
                            IBan = bankPayments.Account.IBan,
                            Payments = new List<ImportedPayment>()
                        },//bankPayments.Account,
                        From = bankPayments.From,
                        To = bankPayments.To
                    };

                    //entity.Bank bank = context.Banks.FirstOrDefault(t => t.BankCode == bankPayments.Account.Bank.BankID);
                    using (var dbTransaction = context.Database.BeginTransaction())
                    {
                        foreach (Payment transaction in bankPayments.Account.Payments)
                        {

                            if (IsTransactionExist(context, transaction))
                            {
                                //ImportedPayment tr = new ImportedPayment(false);
                                //tr.VariableSymbol = transaction.VariableSymbol;
                                //ImportWithPocessedTransactions.Account.Payments.Add(tr);
                                ImportedPayment tr = transaction as ImportedPayment;
                                tr.Imported = false;
                                ImportWithPocessedTransactions.Account.Payments.Add(tr);
                                continue;
                            }
                            entity.Entry newTransaction = new entity.Entry
                            {
                                ConstantSymbol = transaction.ConstantSymbol,
                                VariableSymbol = transaction.VariableSymbol,
                                SpecificSymbol = transaction.SpecificSymbol,
                                Reference = transaction.Reference,
                                DateAvailable = transaction.DateAvailable,
                                DatePosted = transaction.DatePosted,
                                Memo = transaction.Description,
                                Name = transaction.TransactionName,
                                DateAdded = DateTime.Now,
                                AmountInfo = new entity.AmountInfo
                                {
                                    Amount = Math.Abs(transaction.TransactionAmount.Amount),
                                    Currency = transaction.TransactionAmount.Currency
                                },
                                //Bank = GetBank(context, transaction.BankName.Localise())
                            };
                            var account =
                                context.BankAccounts.FirstOrDefault(t => t.IBAN == transaction.BankAccount.IBan);
                            if (account != null)
                            {
                                newTransaction.DestinationAccountId = account.Id;
                            }
                            else
                            {
                                newTransaction.DestinationAccount = new entity.BankAccount
                                {
                                    AccountNumber = (long?) transaction.BankAccount.AccountID,
                                    //BankCode = (short?) transaction.BankAccount.BankID,
                                    IBAN = transaction.BankAccount.IBan,
                                    Bank =
                                        (transaction?.BankAccount?.Bank?.BankID.HasValue != null &&
                                         transaction.BankAccount.Bank.BankID.HasValue)
                                            ? GetBank(context, transaction.BankAccount.Bank.BankID.Value)
                                            : null,
                                    UserId = null
                                };
                            }
                            if (transaction.TransactionAmount.Type != AmountType.NotDefined)
                            {
                                newTransaction.AmountInfo.Type = transaction.TransactionAmount.Type == AmountType.Credit
                                    ? GetTransactionType(context, "Credit")
                                    : GetTransactionType(context, "Debit");
                            }
                            else
                            {
                                newTransaction.AmountInfo.Type = transaction.TransactionAmount.Amount < 0
                                    ? GetTransactionType(context, "Debit")
                                    : GetTransactionType(context, "Credit");
                            }
                            SourceAccount.Entries.Add(newTransaction);
                            ImportedPayment importedTr = transaction as ImportedPayment; //new ImportedPayment(true);
                            importedTr.Imported = true;

                            //importedTr.VariableSymbol = transaction.VariableSymbol;
                            ImportWithPocessedTransactions.Account.Payments.Add(importedTr);
                            //Payment tran = new Payment();
                            //tran.VariableSymbol = transaction.VariableSymbol;
                            //ImportWithPocessedTransactions.Account.Payments.Add(tran);
                            //break;
                            context.SaveChanges();
                        }
                        dbTransaction.Commit();
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception("Nastala chyba pri ukladani importu!", e);
            }
            return ImportWithPocessedTransactions;
        }
        private void UpdateCategories(string Description, int transactionCategoryID)
        {
            using (var context = new SpendingReportEntities())
            {
                TransactionCategory transactionCategory;
                try
                {
                    transactionCategory =
                        context.TransactionCategories.SingleOrDefault(i => i.Id == transactionCategoryID);
                }
                catch (Exception ex)
                {
                    throw new Exception("There are more transaction descriptions with id: " + transactionCategoryID, ex);
                }
                var transactions = GetTransactionsByCategory(Description, context);
                foreach (var transaction in transactions)
                {
                    if (!transaction.TransactionCategories.Contains(transactionCategory))
                    {
                        transaction.TransactionCategories.Add(transactionCategory);
                    }

                }
                context.SaveChanges();
            }
        }