Exemplo n.º 1
0
        public static SaveExpenseResponse SubmitExpenseToDatabase(SaveExpenseRequest request)   //Submit New Expense
        {
            var response = new SaveExpenseResponse();

            var expenseItemList = request.ExpenseItemList;

            using (var entities = new ExpenseProjectDBEntities())
            {
                //using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
                //{

                var currentExpenseID = request.ExpenseID;

                Expense.Data.Expense expense = new Data.Expense();
                if (currentExpenseID == 0)  //If Expense doesn't exist, creates a new one and pushes the ID to currentExpenseID
                {
                    expense = new Expense.Data.Expense
                    {
                        TotalAmount  = expenseItemList.Sum(x => x.ExpenseItemAmount), //Holy
                        CreatedDate  = DateTime.Now,
                        Created_User = 1,                                             //Session? Angular?
                        StatusId     = 2
                    };

                    entities.Expenses.Add(expense);     //Creates a new expense in db
                }


                Expense.Data.ExpenseItem expenseItem;

                if (entities.SaveChanges() > 0 || expenseItemList.Count > 0)     //???
                {
                    foreach (var item in expenseItemList)
                    {
                        expenseItem = new ExpenseItem();
                        expenseItem.ExpenseAmount      = item.ExpenseItemAmount;
                        expenseItem.ExpenseDate        = item.ExpenseItemDate;
                        expenseItem.ExpenseDescription = item.ExpenseItemDescription;
                        expenseItem.ExpenseId          = (currentExpenseID == 0) ? expense.ID : currentExpenseID; //This is where you tie the item to the expense

                        if (item.ExpenseItemID == 0)
                        {
                            entities.ExpenseItems.Add(expenseItem);
                        }
                        else
                        {
                            var expenseItemDb = entities.ExpenseItems.Where(e => e.ID == item.ExpenseItemID).First();
                            expenseItemDb.ExpenseDescription = expenseItem.ExpenseDescription;
                            expenseItemDb.ExpenseDate        = expenseItem.ExpenseDate;
                            expenseItemDb.ExpenseAmount      = expenseItem.ExpenseAmount;
                        }
                    }
                    currentExpenseID = (currentExpenseID == 0) ? expense.ID : currentExpenseID;
                    var expenseItemListDB      = entities.ExpenseItems.Where(i => i.ExpenseId == currentExpenseID).ToList();
                    var notMatchedExpenseItems = expenseItemListDB.Where(e => !expenseItemList.Select(i => i.ExpenseItemID).Contains(e.ID)).ToList();

                    foreach (var item in notMatchedExpenseItems)
                    {
                        entities.ExpenseItems.Remove(item);
                    }
                    entities.SaveChanges();
                }

                else
                {
                    response.IsSuccess = false;
                    response.Message   = "There was a problem.";
                }

                SetStatusToWaitingForManager(currentExpenseID);
                entities.SaveChanges();

                //scope.Commit();
            }

            response.IsSuccess = true;
            response.Message   = "Your expense has been successfully created.";

            return(response);
        }
Exemplo n.º 2
0
 public SaveExpenseResponse SaveExpense(SaveExpenseRequest request)
 {
     return(ExpenseHandler.SubmitExpenseToDatabase(request));
 }