public ActionResult Expense(ExpenseSalesView expense, string time) { #region Check Cache var saleCache = CacheSale; if (IsCookieTimeEqual(time)) { MessageSetter.SetWarning("Sorry , previous transaction is not saved to the database."); if (saleCache != null) { if (IsToday(saleCache)) { // today return(RedirectToActionPermanent("Today")); } else { return(DetermineSaleDay(saleCache)); } } } #endregion int SaleID = expense.SaleID; const string dailyExpense = "Daily Expense"; if (expense.Amount < 0) { ModelState.AddModelError("Amount", "Amount can't be negative."); return(DetermineSaleDay(SaleID)); } SaleDayView day = new SaleDayView(SaleID); var userAccExtention = new UserAccountsExtension(); var accountOf = userAccExtention.GetExpenseAccount(); var accountBalance = new AccountBalance() { AccountOf = accountOf.UserID, AddBy = userinfo.GetUserID(), Amount = expense.Amount * (-1), Dated = DateTime.Now, CategoryProduct = expense.ExpenseCategoryID, Note = expense.Note, IsExpense = true, IsAddedMoney = false, IsBoughtProduct = false, IsVerified = false }; db.AccountBalances.Add(accountBalance); MessageSetter.SetPositive("Expense of " + expense.Amount + " added successfully."); db.SaveChanges(); return(DetermineSaleDay(SaleID)); }
public ActionResult DiscardItem(int SaleID, int CategoryID, string time) { #region Check Cache var saleCache = CacheSale; if (IsCookieTimeEqual(time)) { if (saleCache != null) { if (IsToday(saleCache)) { // today return(RedirectToActionPermanent("Today")); } else { return(DetermineSaleDay(saleCache)); } } return(DetermineSaleDay(SaleID)); } #endregion var sale = GetDatedSale(SaleID); if (sale == null) { return(RedirectToActionPermanent("Today")); } if (IsTransactionRejected(sale)) { return(View("Error2", new Exception("Sorry you have no rights to change previous sales information."))); } var inventory = db.Inventories.Include(n => n.Category).FirstOrDefault(n => n.CategoryID == CategoryID); var lastSold = db.DetailedSales.OrderByDescending(n => n.Time) .FirstOrDefault(n => n.SaleID == SaleID && n.CategoryID == CategoryID && !n.IsDiscard && !n.IsMarkedDiscard); var existRecord = sale != null && inventory != null; var saleView = new SaleDayView(SaleID) { Sale = sale }; ///means item still sold and can be discarded bool soldOut = false; var summary = saleView.GetSummary(); var specificCategory = summary.FirstOrDefault(n => n.SaleID == SaleID && n.CategoryID == CategoryID); if (specificCategory != null) { // if any summary of that item exist. // if any item is still sold from this category. soldOut = !(specificCategory.SoldQuantity > 0); //if still one sold then it will be false. } if (existRecord && !soldOut) { //first check if one product is sold or not. //if not then can't discard. var saleItem = new DB.DetailedSale() { CategoryID = CategoryID, SaleID = SaleID, SellingPrice = (int)inventory.SoldPrice, SoldAt = lastSold.SoldAt * -1, Time = DateTime.Now, IsDiscard = true, UserID = userinfo.GetUserID() }; // do some inventory in // not implemented yet... InventoryIn(sale, inventory); sale.TotalAcheived += saleItem.SoldAt; //because sold at is - sale.TotalSold += saleItem.SoldAt; //fixing discount money if (lastSold.Discount != null && lastSold.Discount > 0) { sale.Discount -= lastSold.Discount; } //tracking discard sales sale.DiscardSales++; if (sale.DiscardSales > 20) { sale.AnyProblem = true; } lastSold.IsMarkedDiscard = true;//when discarded, marked it. db.DetailedSales.Add(saleItem); string categoryDisplay = inventory.Category.Category1; MessageSetter.SetPositive(categoryDisplay + " -1."); //decreasing quantity of sale DecreaseSaleQuantity(saleItem); //remove category wise sale RemoveCategoryWiseSellingInformation(saleItem); db.SaveChanges(); CacheSale = sale; return(DetermineSaleDay(sale)); } else if (existRecord && soldOut) { // do nothing , send to the view. if (IsToday(sale)) { return(RedirectToActionPermanent("Today")); } return(DetermineSaleDay(sale)); } else { throw new Exception("Information about discarding not found."); } }