private void PrepareViewBag(QAGroupLineViewModel vm)
        {
            ViewBag.DeliveryUnitList = new UnitService(_unitOfWork).GetUnitList().ToList();

            if (vm != null)
            {
                QAGroupViewModel H = new QAGroupService(_unitOfWork).GetQAGroup(vm.QAGroupId);
                ViewBag.DocNo = H.DocTypeName + "-" + H.QaGroupName;
            }
        }
        public ActionResult _Create(int Id, DateTime?date, bool?IsProdBased)
        {
            QAGroup H = new QAGroupService(_unitOfWork).Find(Id);
            QAGroupLineViewModel s = new QAGroupLineViewModel();

            s.IsActive     = true;
            s.QAGroupId    = H.QAGroupId;
            ViewBag.Status = H.Status;
            PrepareViewBag(s);
            ViewBag.LineMode = "Create";
            return(PartialView("_Create", s));
        }
        private ActionResult _Delete(int id)
        {
            QAGroupLineViewModel temp = _QAGroupLineService.GetQAGroupLine(id);

            QAGroup H = new QAGroupService(_unitOfWork).Find(temp.QAGroupId);

            //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)
        {
            QAGroupLineViewModel temp = _QAGroupLineService.GetQAGroupLine(id);

            QAGroup H = new QAGroupService(_unitOfWork).Find(temp.QAGroupId);



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



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

            return(PartialView("_Create", temp));
        }
        public ActionResult _CreatePost(QAGroupLineViewModel svm)
        {
            List <LogTypeViewModel> LogList = new List <LogTypeViewModel>();
            bool    BeforeSave = true;
            QAGroup temp       = new QAGroupService(_unitOfWork).Find(svm.QAGroupId);
            int     i          = 0;

            if (temp.QAGroupId <= 0)
            {
                i = _QAGroupLineService.GetQAGroupLineListForIndex(svm.QAGroupId).Where(x => x.Name == svm.Name).ToList().Count();
            }
            else
            {
                i = _QAGroupLineService.GetQAGroupLineListForIndex(svm.QAGroupId).Where(x => x.Name == svm.Name && x.QAGroupLineId != svm.QAGroupLineId).ToList().Count();
            }
            #region BeforeSave
            //try
            //{

            //    if (svm.GatePassLineId <= 0)
            //        BeforeSave = JobOrderDocEvents.beforeLineSaveEvent(this, new JobEventArgs(svm.GatePassHeaderId, EventModeConstants.Add), ref db);
            //    else
            //        BeforeSave = JobOrderDocEvents.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.Name == "" || svm.Name == null)
            {
                ModelState.AddModelError("Name", "The Name is required");
            }

            if (i != 0)
            {
                ModelState.AddModelError("Name", svm.Name + " already exist");
            }


            QAGroupLine s = Mapper.Map <QAGroupLineViewModel, QAGroupLine>(svm);

            ViewBag.Status = temp.Status;



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

            if (ModelState.IsValid && BeforeSave && !EventException)
            {
                if (svm.QAGroupLineId <= 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.QAGroupLine.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.QAGroup.Add(temp);
                    //try
                    //{
                    //    JobOrderDocEvents.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));
                    }



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

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

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

                    templine.QAGroupId = s.QAGroupId;

                    /*templine.Name = s.Name;
                     * templine.IsMandatory = s.IsMandatory;
                     * templine.DataType = s.DataType;
                     * templine.ListItem = s.ListItem;
                     * templine.DefaultValue = s.DefaultValue;
                     * templine.IsActive = s.IsActive;*/
                    templine.ModifiedDate = DateTime.Now;
                    templine.ModifiedBy   = User.Identity.Name;
                    templine.ObjectState  = Model.ObjectState.Modified;
                    db.QAGroupLine.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.QAGroup.Add(temp);



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



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



                    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
                    {
                        JobOrderDocEvents.afterLineSaveEvent(this, new JobEventArgs(s.QAGroupId, templine.QAGroupLineId, 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.QAGroupId,
                        DocLineId       = templine.QAGroupLineId,
                        ActivityType    = (int)ActivityTypeContants.Modified,
                        DocNo           = temp.QaGroupName,
                        xEModifications = Modifications,
                        DocDate         = temp.CreatedDate,
                        DocStatus       = temp.Status,
                    }));

                    //End of Saving the Activity Log

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