public static void SetStatusToWaitingForManager(int ID) { var expense = new ExpenseDomain(); using (var entities = new ExpenseProjectDBEntities()) { expense = (from e in entities.Expenses where (e.ID == ID) select new ExpenseDomain { StatusID = 4, } ).FirstOrDefault(); entities.SaveChanges(); } }
public static SaveExpenseResponse ManageExpenses(ManageExpenseRequest request) //Evaluate an Expense { using (var entities = new ExpenseProjectDBEntities()) { try //Correct? { var expense = entities.Expenses.SingleOrDefault(e => e.ID == request.ExpenseID); if (request.Approved) { if (expense.StatusId == 2) { expense.StatusId = 3; //Status of the expense updates from waiting mng approval to waiting acc approval } else if (expense.StatusId == 3) { expense.StatusId = 5; //What to do with Approved Expenses? } } else { expense.StatusId = 4; expense.RejectDescription = request.RejectDescription; } entities.SaveChanges(); var response = new SaveExpenseResponse(); response.IsSuccess = true; response.Message = "The expense's status has been successfully updated."; return(response); } catch (NullReferenceException) { var response = new SaveExpenseResponse(); response.IsSuccess = false; response.Message = "The expense you're trying to evaluate doesn't exist."; return(response); } } }
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); }