public ActionResult ClosingdayConfirmation()
        {
            decimal             Bal         = 0;
            Opening_ClosingDays openclosOBj = new Opening_ClosingDays();

            foreach (var item in db.Opening_ClosingDays)
            {
                if (item.Date.ToShortDateString().Equals(DateTime.Now.ToShortDateString()) && !item.isClosed)
                {
                    Bal         = item.Opening_Balance;
                    openclosOBj = item;
                    break;
                }
            }
            foreach (var item in db.Transaction.ToList().Where(m => m.Opening_ClosingDays == openclosOBj))
            {
                if (item.status)
                {
                    if (item.Debit > 0)
                    {
                        Bal -= item.Debit;
                    }
                    else if (item.Credit > 0)
                    {
                        Bal += item.Credit;
                    }
                }
            }

            return(View(Bal));
        }
 public ActionResult AddInitialOpenningDay(Opening_ClosingDays obj)
 {
     obj.Closing_Balance = 0;
     obj.Date            = DateTime.Now;
     obj.isClosed        = false;
     obj.status          = true;
     db.Opening_ClosingDays.Add(obj);
     db.SaveChanges();
     return(RedirectToAction("Index", "Factories"));
 }
        public ActionResult ViewTransactionByOpeningDay(int id)
        {
            Opening_ClosingDays opcd = db.Opening_ClosingDays.Find(id);

            if (opcd != null)
            {
                return(View(db.Transaction.Where(m => m.Opening_ClosingDays.Opening_ClosingDays_id == opcd.Opening_ClosingDays_id).ToList()));
            }
            else
            {
                return(HttpNotFound());
            }
        }
        public ActionResult ClosingdayConfirmation(FormCollection form)
        {
            if (form["Yes"] != null)
            {
                decimal             Bal         = 0;
                Opening_ClosingDays openclosOBj = null;
                foreach (var item in db.Opening_ClosingDays)
                {
                    if (item.Date.ToShortDateString().Equals(DateTime.Now.ToShortDateString()) && !item.isClosed)
                    {
                        Bal         = item.Opening_Balance;
                        openclosOBj = item;
                        break;
                    }
                }

                if (openclosOBj != null)
                {
                    foreach (var item in db.Transaction.ToList().Where(m => m.Opening_ClosingDays == openclosOBj))
                    {
                        if (item.status)
                        {
                            if (item.Debit > 0)
                            {
                                Bal -= item.Debit;
                            }
                            else if (item.Credit > 0)
                            {
                                Bal += item.Credit;
                            }
                        }
                    }
                    openclosOBj.Closing_Balance = Bal;
                    openclosOBj.isClosed        = true;
                    db.Entry(openclosOBj).State = EntityState.Modified;
                    db.SaveChanges();

                    Opening_ClosingDays newDay = new Opening_ClosingDays();
                    newDay.Closing_Balance = 0;
                    newDay.Date            = DateTime.Now;
                    newDay.isClosed        = false;
                    newDay.Opening_Balance = Bal;
                    newDay.status          = true;
                    db.Opening_ClosingDays.Add(newDay);
                    db.SaveChanges();
                }
            }
            return(RedirectToAction("ViewToday", "Transaction"));
        }
        public ActionResult ViewToday()
        {
            var transactions = new List <Transaction>();
            Opening_ClosingDays openclosOBj = new Opening_ClosingDays();

            foreach (var item in db.Opening_ClosingDays)
            {
                if (item.Date.ToShortDateString().Equals(DateTime.Now.ToShortDateString()) && !item.isClosed)
                {
                    openclosOBj = item;
                    break;
                }
            }
            foreach (var item in db.Transaction.ToList().Where(m => m.Opening_ClosingDays == openclosOBj).OrderByDescending(m => m.Transaction_DateTime))
            {
                if (item.status)
                {
                    transactions.Add(item);
                }
            }
            return(View(transactions));
        }
        public JsonResult CheckIntialOpening()
        {
            var Opening_ClosingDays = db.Opening_ClosingDays.ToList();

            if (Opening_ClosingDays.Count() == 0)
            {
                return(Json(1, JsonRequestBehavior.AllowGet));
            }


            bool day_det = false;

            foreach (var item in Opening_ClosingDays)
            {
                if (item.Date.ToShortDateString() == DateTime.Now.ToShortDateString() && !item.isClosed)
                {
                    day_det = true;
                    break;
                }
            }
            if (!day_det)
            {
                decimal             Bal         = 0;
                Opening_ClosingDays openclosOBj = db.Opening_ClosingDays.Find(Opening_ClosingDays.Max(m => m.Opening_ClosingDays_id));
                if (openclosOBj != null && !openclosOBj.isClosed)
                {
                    Bal = openclosOBj.Opening_Balance;
                    foreach (var item in db.Transaction.ToList().Where(m => m.Opening_ClosingDays == openclosOBj))
                    {
                        if (item.status)
                        {
                            if (item.Debit > 0)
                            {
                                Bal -= item.Debit;
                            }
                            else if (item.Credit > 0)
                            {
                                Bal += item.Credit;
                            }
                        }
                    }
                    openclosOBj.Closing_Balance = Bal;
                    openclosOBj.isClosed        = true;
                    db.Entry(openclosOBj).State = EntityState.Modified;
                    db.SaveChanges();

                    Opening_ClosingDays newDay = new Opening_ClosingDays();
                    newDay.Closing_Balance = 0;
                    newDay.Date            = DateTime.Now;
                    newDay.isClosed        = false;
                    newDay.Opening_Balance = Bal;
                    newDay.status          = true;
                    db.Opening_ClosingDays.Add(newDay);
                    db.SaveChanges();
                }
                else
                {
                    return(Json(1, JsonRequestBehavior.AllowGet));
                }
            }
            return(Json(0, JsonRequestBehavior.AllowGet));
        }