private void PrepareViewBag(GatePassLineViewModel vm)
 {
     ViewBag.DeliveryUnitList = new UnitService(_unitOfWork).GetUnitList().ToList();
     if (vm != null)
     {
         GatePassHeaderViewModel H = new GatePassHeaderService(_unitOfWork).GetGatePassHeader(vm.GatePassHeaderId);
         ViewBag.DocNo = H.DocTypeName + "-" + H.DocNo;
     }
 }
        public ActionResult _Create(int Id, DateTime?date, bool?IsProdBased)
        {
            GatePassHeader        H = new GatePassHeaderService(_unitOfWork).Find(Id);
            GatePassLineViewModel s = new GatePassLineViewModel();

            s.GatePassHeaderId = H.GatePassHeaderId;
            ViewBag.Status     = H.Status;
            PrepareViewBag(s);
            ViewBag.LineMode = "Create";
            return(PartialView("_Create", s));
        }
        private ActionResult _Delete(int id)
        {
            GatePassLineViewModel temp = _GatePassLineService.GetGatePassLine(id);

            GatePassHeader H = new GatePassHeaderService(_unitOfWork).Find(temp.GatePassHeaderId);

            //Getting Settings

            if (temp == null)
            {
                return(HttpNotFound());
            }
            PrepareViewBag(temp);
            //ViewBag.LineMode = "Delete";

            #region DocTypeTimeLineValidation
            try
            {
                TimePlanValidation = DocumentValidation.ValidateDocumentLine(new DocumentUniqueId {
                    LockReason = null
                }, User.Identity.Name, out ExceptionMsg, out Continue);
            }
            catch (Exception ex)
            {
                string message = _exception.HandleException(ex);
                TempData["CSEXCL"] += message;
                TimePlanValidation  = false;
            }

            if (!TimePlanValidation)
            {
                TempData["CSEXCL"] += ExceptionMsg;
            }
            #endregion

            if ((TimePlanValidation || Continue))
            {
                ViewBag.LineMode = "Delete";
            }



            return(PartialView("_Create", temp));
        }
        private ActionResult _Modify(int id)
        {
            GatePassLineViewModel temp = _GatePassLineService.GetGatePassLine(id);

            GatePassHeader H = new GatePassHeaderService(_unitOfWork).Find(temp.GatePassHeaderId);



            if (temp == null)
            {
                return(HttpNotFound());
            }
            PrepareViewBag(temp);

            //#region DocTypeTimeLineValidation
            //try
            //{

            //    TimePlanValidation = DocumentValidation.ValidateDocumentLine(new DocumentUniqueId { LockReason = temp.LockReason }, User.Identity.Name, out ExceptionMsg, out Continue);

            //}
            //catch (Exception ex)
            //{
            //    string message = _exception.HandleException(ex);
            //    TempData["CSEXCL"] += message;
            //    TimePlanValidation = false;
            //}

            //if (!TimePlanValidation)
            //    TempData["CSEXCL"] += ExceptionMsg;
            //#endregion

            if ((TimePlanValidation || Continue))
            {
                ViewBag.LineMode = "Edit";
            }

            //if (string.IsNullOrEmpty(temp.LockReason))
            //    ViewBag.LineMode = "Edit";
            //else
            //    TempData["CSEXCL"] += temp.LockReason;
            return(PartialView("_Create", temp));
        }
        public ActionResult _CreatePost(GatePassLineViewModel svm)
        {
            List <LogTypeViewModel> LogList = new List <LogTypeViewModel>();
            bool           BeforeSave       = true;
            GatePassHeader temp             = new GatePassHeaderService(_unitOfWork).Find(svm.GatePassHeaderId);
            var            PD = _GatePassLineService.GetGatePassLineListForIndex(svm.GatePassHeaderId).Where(x => x.Product == svm.Product && x.Specification == svm.Specification).ToList();



            #region BeforeSave
            //try
            //{

            //    if (svm.GatePassLineId <= 0)
            //        BeforeSave = GatePassDocEvents.beforeLineSaveEvent(this, new JobEventArgs(svm.GatePassHeaderId, EventModeConstants.Add), ref db);
            //    else
            //        BeforeSave = GatePassDocEvents.beforeLineSaveEvent(this, new JobEventArgs(svm.GatePassHeaderId, EventModeConstants.Edit), ref db);

            //}
            //catch (Exception ex)
            //{
            //    string message = _exception.HandleException(ex);
            //    TempData["CSEXCL"] += message;
            //    EventException = true;
            //}

            //if (!BeforeSave)
            //    ModelState.AddModelError("", "Validation failed before save.");
            #endregion


            if (svm.Product == "" || svm.Product == null)
            {
                ModelState.AddModelError("Product", "The Product is required");
            }

            if (svm.Qty <= 0)
            {
                ModelState.AddModelError("UnitId", "The Qty is required");
            }

            if (svm.UnitId == "" || svm.UnitId == null)
            {
                ModelState.AddModelError("UnitId", "The Unit Name is required");
            }



            if (PD.ToList().Count() != 0)
            {
                ModelState.AddModelError("Product", svm.Product + " " + svm.Specification + " already exist");
            }


            GatePassLine s = Mapper.Map <GatePassLineViewModel, GatePassLine>(svm);

            ViewBag.Status = temp.Status;



            if (svm.GatePassLineId <= 0)
            {
                ViewBag.LineMode = "Create";
            }
            else
            {
                ViewBag.LineMode = "Edit";
            }

            if (ModelState.IsValid && BeforeSave && !EventException)
            {
                if (svm.GatePassLineId <= 0)
                {
                    //Posting in Stock
                    s.CreatedDate  = DateTime.Now;
                    s.ModifiedDate = DateTime.Now;
                    s.CreatedBy    = User.Identity.Name;
                    s.ModifiedBy   = User.Identity.Name;
                    s.ObjectState  = Model.ObjectState.Added;
                    db.GatePassLine.Add(s);

                    if (temp.Status != (int)StatusConstants.Drafted && temp.Status != (int)StatusConstants.Import)
                    {
                        temp.Status       = (int)StatusConstants.Modified;
                        temp.ModifiedDate = DateTime.Now;
                        temp.ModifiedBy   = User.Identity.Name;
                    }

                    temp.ObjectState = Model.ObjectState.Modified;
                    db.GatePassHeader.Add(temp);
                    //try
                    //{
                    //    GatePassDocEvents.onLineSaveEvent(this, new JobEventArgs(s.GatePassHeaderId, s.GatePassLineId, EventModeConstants.Add), ref db);
                    //}
                    //catch (Exception ex)
                    //{
                    //    string message = _exception.HandleException(ex);
                    //    TempData["CSEXCL"] += message;
                    //    EventException = true;
                    //}
                    try
                    {
                        if (EventException)
                        {
                            throw new Exception();
                        }

                        db.SaveChanges();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXCL"] += message;
                        PrepareViewBag(svm);
                        return(PartialView("_Create", svm));
                    }


                    //try
                    //{
                    //    GatePassDocEvents.afterLineSaveEvent(this, new JobEventArgs(s.GatePassHeaderId, s.GatePassHeaderId, EventModeConstants.Add), ref db);
                    //}
                    //catch (Exception ex)
                    //{
                    //    string message = _exception.HandleException(ex);
                    //    TempData["CSEXCL"] += message;
                    //}

                    LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                    {
                        DocTypeId    = temp.DocTypeId,
                        DocId        = temp.GatePassHeaderId,
                        DocLineId    = s.GatePassLineId,
                        ActivityType = (int)ActivityTypeContants.Added,
                        DocNo        = temp.DocNo,
                        DocDate      = temp.DocDate,
                        DocStatus    = temp.Status,
                    }));

                    return(RedirectToAction("_Create", new { id = svm.GatePassHeaderId, IsProdBased = false }));
                    //return RedirectToAction("_Create", new { id = svm.GatePassHeaderId, IsProdBased = (s.ProdOrderLineId == null ? false : true) });
                }
                else
                {
                    GatePassLine templine = (from p in db.GatePassLine
                                             where p.GatePassLineId == s.GatePassLineId
                                             select p).FirstOrDefault();

                    GatePassLine ExTempLine = new GatePassLine();
                    ExTempLine = Mapper.Map <GatePassLine>(templine);

                    templine.GatePassHeaderId = s.GatePassHeaderId;
                    templine.Product          = s.Product;
                    templine.Specification    = s.Specification;
                    templine.Qty          = s.Qty;
                    templine.UnitId       = s.UnitId;
                    templine.ModifiedDate = DateTime.Now;
                    templine.ModifiedBy   = User.Identity.Name;
                    templine.ObjectState  = Model.ObjectState.Modified;
                    db.GatePassLine.Add(templine);
                    int Status = 0;
                    if (temp.Status != (int)StatusConstants.Drafted && temp.Status != (int)StatusConstants.Import)
                    {
                        Status            = temp.Status;
                        temp.Status       = (int)StatusConstants.Modified;
                        temp.ModifiedBy   = User.Identity.Name;
                        temp.ModifiedDate = DateTime.Now;
                    }


                    temp.ObjectState = Model.ObjectState.Modified;
                    db.GatePassHeader.Add(temp);



                    LogList.Add(new LogTypeViewModel
                    {
                        ExObj = ExTempLine,
                        Obj   = templine
                    });



                    XElement Modifications = new ModificationsCheckService().CheckChanges(LogList);

                    //try
                    //{
                    //    GatePassDocEvents.onLineSaveEvent(this, new JobEventArgs(s.GatePassHeaderId, templine.GatePassLineId, EventModeConstants.Edit), ref db);
                    //}
                    //catch (Exception ex)
                    //{
                    //    string message = _exception.HandleException(ex);
                    //    TempData["CSEXCL"] += message;
                    //    EventException = true;
                    //}

                    try
                    {
                        if (EventException)
                        {
                            throw new Exception();
                        }
                        db.SaveChanges();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXCL"] += message;
                        PrepareViewBag(svm);
                        return(PartialView("_Create", svm));
                    }

                    try
                    {
                        GatePassDocEvents.afterLineSaveEvent(this, new JobEventArgs(s.GatePassHeaderId, templine.GatePassLineId, EventModeConstants.Edit), ref db);
                    }
                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXC"] += message;
                    }


                    //Saving the Activity Log

                    LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                    {
                        DocTypeId       = temp.DocTypeId,
                        DocId           = templine.GatePassHeaderId,
                        DocLineId       = templine.GatePassLineId,
                        ActivityType    = (int)ActivityTypeContants.Modified,
                        DocNo           = temp.DocNo,
                        xEModifications = Modifications,
                        DocDate         = temp.DocDate,
                        DocStatus       = temp.Status,
                    }));

                    //End of Saving the Activity Log

                    return(Json(new { success = true }));
                }
            }
            PrepareViewBag(svm);
            return(PartialView("_Create", svm));
        }