public ActionResult Create(int id)//DocumentTypeId
        {
            QAGroupViewModel p = new QAGroupViewModel();

            p.CreatedDate = DateTime.Now;
            p.DocTypeId   = id;
            PrepareViewBag(id);
            ViewBag.Mode = "Add";
            ViewBag.Name = new DocumentTypeService(_unitOfWork).Find(id).DocumentTypeName;
            ViewBag.id   = id;
            return(View(p));
        }
        public ActionResult Detail(int id, string IndexType, string transactionType, int?DocLineId)
        {
            if (DocLineId.HasValue)
            {
                ViewBag.DocLineId = DocLineId;
            }
            //Saving ViewBag Data::

            ViewBag.transactionType = transactionType;
            ViewBag.IndexStatus     = IndexType;

            QAGroupViewModel s = _QAGroupService.GetQAGroup(id);

            //Getting Settings



            ////Perks


            PrepareViewBag(s.DocTypeId);
            if (s == null)
            {
                return(HttpNotFound());
            }

            if (String.IsNullOrEmpty(transactionType) || transactionType == "detail")
            {
                LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                {
                    DocTypeId    = s.DocTypeId,
                    DocId        = s.QAGroupId,
                    ActivityType = (int)ActivityTypeContants.Detail,
                    DocNo        = s.QaGroupName,
                    DocDate      = s.CreatedDate,
                    DocStatus    = s.Status,
                }));
            }


            return(View("Create", s));
        }
        // GET: /JobOrderHeader/Edit/5
        private ActionResult Edit(int id, string IndexType)
        {
            ViewBag.IndexStatus = IndexType;
            QAGroupViewModel s = _QAGroupService.GetQAGroup(id);

            #region DocTypeTimeLineValidation
            //try
            //{

            //    TimePlanValidation = DocumentValidation.ValidateDocument(Mapper.Map<DocumentUniqueId>(s), DocumentTimePlanTypeConstants.Modify, User.Identity.Name, out ExceptionMsg, out Continue);

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

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

            //if ((!TimePlanValidation && !Continue))
            //{
            //    return RedirectToAction("DetailInformation", new { id = id, IndexType = IndexType });
            //}

            //Job Order Settings


            // s.JobOrderSettings = Mapper.Map<JobOrderSettings, JobOrderSettingsViewModel>(settings);

            ////Perks
            // s.PerkViewModel = new PerkService(_unitOfWork).GetPerkListForDocumentTypeForEdit(id).ToList();

            //if (s.PerkViewModel.Count == 0)
            //{
            //    List<PerkViewModel> Perks = new List<PerkViewModel>();
            //    if (s.JobOrderSettings.Perks != null)
            //        foreach (var item in s.JobOrderSettings.Perks.Split(',').ToList())
            //        {
            //            PerkViewModel temp = Mapper.Map<Perk, PerkViewModel>(new PerkService(_unitOfWork).Find(Convert.ToInt32(item)));

            //            var DocTypePerk = (from p2 in context.PerkDocumentType
            //                               where p2.DocTypeId == s.DocTypeId && p2.PerkId == temp.PerkId && p2.SiteId == s.SiteId && p2.DivisionId == s.DivisionId
            //                               select p2).FirstOrDefault();
            //            if (DocTypePerk != null)
            //            {
            //                temp.Base = DocTypePerk.Base;
            //                temp.Worth = DocTypePerk.Worth;
            //                temp.CostConversionMultiplier = DocTypePerk.CostConversionMultiplier;
            //                temp.IsEditableRate = DocTypePerk.IsEditableRate;
            //            }
            //            else
            //            {
            //                temp.Base = 0;
            //                temp.Worth = 0;
            //                temp.CostConversionMultiplier = 0;
            //                temp.IsEditableRate = true;
            //            }

            //            Perks.Add(temp);
            //        }
            //    s.PerkViewModel = Perks;
            //}
            PrepareViewBag(s.DocTypeId);
            if (s == null)
            {
                return(HttpNotFound());
            }

            //ViewBag.transactionType = "detail";

            ViewBag.Mode            = "Edit";
            ViewBag.transactionType = "";

            ViewBag.Name = new DocumentTypeService(_unitOfWork).Find(s.DocTypeId).DocumentTypeName;
            ViewBag.id   = s.DocTypeId;

            if (!(System.Web.HttpContext.Current.Request.UrlReferrer.PathAndQuery).Contains("Create"))
            {
                LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                {
                    DocTypeId    = s.DocTypeId,
                    DocId        = s.QAGroupId,
                    ActivityType = (int)ActivityTypeContants.Detail,
                    DocNo        = s.QaGroupName,
                    DocDate      = s.CreatedDate,
                    DocStatus    = s.Status,
                }));
            }

            return(View("Create", s));
        }
        public ActionResult Post(QAGroupViewModel svm)
        {
            bool BeforeSave          = true;
            bool CostCenterGenerated = false;

            QAGroup s = Mapper.Map <QAGroupViewModel, QAGroup>(svm);

            #region BeforeSaveEvents
            try
            {
                if (svm.QAGroupId <= 0)
                {
                    BeforeSave = JobOrderDocEvents.beforeHeaderSaveEvent(this, new JobEventArgs(svm.QAGroupId, EventModeConstants.Add), ref context);
                }
                else
                {
                    BeforeSave = JobOrderDocEvents.beforeHeaderSaveEvent(this, new JobEventArgs(svm.QAGroupId, EventModeConstants.Edit), ref context);
                }
            }
            catch (Exception ex)
            {
                string message = _exception.HandleException(ex);
                TempData["CSEXC"] += message;
                EventException     = true;
            }

            if (!BeforeSave)
            {
                TempData["CSEXC"] += "Failed validation before save";
            }
            #endregion

            #region DocTypeTimeLineValidation

            try
            {
                if (svm.QAGroupId <= 0)
                {
                    TimePlanValidation = DocumentValidation.ValidateDocument(Mapper.Map <DocumentUniqueId>(svm), DocumentTimePlanTypeConstants.Create, User.Identity.Name, out ExceptionMsg, out Continue);
                }
                else
                {
                    TimePlanValidation = DocumentValidation.ValidateDocument(Mapper.Map <DocumentUniqueId>(svm), DocumentTimePlanTypeConstants.Modify, User.Identity.Name, out ExceptionMsg, out Continue);
                }
            }
            catch (Exception ex)
            {
                string message = _exception.HandleException(ex);
                TempData["CSEXC"] += message;
                TimePlanValidation = false;
            }

            if (!TimePlanValidation)
            {
                TempData["CSEXC"] += ExceptionMsg;
            }

            #endregion

            if (ModelState.IsValid && BeforeSave && !EventException && (TimePlanValidation || Continue))
            {
                //CreateLogic
                #region CreateRecord
                if (svm.QAGroupId <= 0)
                {
                    // s.GodownId= (int)System.Web.HttpContext.Current.Session["DefaultGodownId"];
                    s.CreatedDate  = DateTime.Now;
                    s.ModifiedDate = DateTime.Now;
                    s.CreatedBy    = User.Identity.Name;
                    s.ModifiedBy   = User.Identity.Name;
                    s.Status       = (int)StatusConstants.Drafted;
                    s.ObjectState  = Model.ObjectState.Added;
                    context.QAGroup.Add(s);
                    try
                    {
                        JobOrderDocEvents.onHeaderSaveEvent(this, new JobEventArgs(s.QAGroupId, EventModeConstants.Add), ref context);
                    }
                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXC"] += message;
                        EventException     = true;
                    }


                    try
                    {
                        if (EventException)
                        {
                            throw new Exception();
                        }
                        //_unitOfWork.Save();
                        context.SaveChanges();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXC"] += message;
                        PrepareViewBag(svm.DocTypeId);
                        ViewBag.Mode = "Add";
                        return(View("Create", svm));
                    }



                    try
                    {
                        JobOrderDocEvents.afterHeaderSaveEvent(this, new JobEventArgs(s.QAGroupId, EventModeConstants.Add), ref context);
                    }
                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXC"] += message;
                    }

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

                    //Update DocId in COstCenter


                    return(RedirectToAction("Modify", "QAGroup", new { Id = s.QAGroupId }).Success("Data saved successfully"));
                }
                #endregion


                //EditLogic
                #region EditRecord

                else
                {
                    bool GodownChanged = false;
                    List <LogTypeViewModel> LogList = new List <LogTypeViewModel>();

                    QAGroup temp  = context.QAGroup.Find(s.QAGroupId);
                    QAGroup ExRec = Mapper.Map <QAGroup>(temp);

                    int status = temp.Status;

                    if (temp.Status != (int)StatusConstants.Drafted && temp.Status != (int)StatusConstants.Import)
                    {
                        temp.Status = (int)StatusConstants.Modified;
                    }

                    temp.QaGroupName  = s.QaGroupName;
                    temp.Description  = s.Description;
                    temp.ModifiedDate = DateTime.Now;
                    temp.ModifiedBy   = User.Identity.Name;
                    temp.ObjectState  = Model.ObjectState.Modified;
                    context.QAGroup.Add(temp);
                    LogList.Add(new LogTypeViewModel
                    {
                        ExObj = ExRec,
                        Obj   = temp,
                    });

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

                    try
                    {
                        JobOrderDocEvents.onHeaderSaveEvent(this, new JobEventArgs(s.QAGroupId, EventModeConstants.Edit), ref context);
                    }
                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXC"] += message;
                        EventException     = true;
                    }

                    try
                    {
                        if (EventException)
                        {
                            throw new Exception();
                        }

                        context.SaveChanges();
                        //_unitOfWork.Save();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);

                        PrepareViewBag(svm.DocTypeId);
                        TempData["CSEXC"] += message;
                        ViewBag.id         = svm.DocTypeId;
                        ViewBag.Mode       = "Edit";
                        return(View("Create", svm));
                    }

                    try
                    {
                        JobOrderDocEvents.afterHeaderSaveEvent(this, new JobEventArgs(s.QAGroupId, EventModeConstants.Edit), ref context);
                    }
                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXC"] += message;
                    }

                    LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                    {
                        DocTypeId       = temp.DocTypeId,
                        DocId           = temp.QAGroupId,
                        ActivityType    = (int)ActivityTypeContants.Modified,
                        DocNo           = temp.QaGroupName,
                        xEModifications = Modifications,
                        DocDate         = temp.CreatedDate,
                        DocStatus       = temp.Status,
                    }));

                    return(RedirectToAction("Index", new { id = svm.DocTypeId }).Success("Data saved successfully"));
                }
                #endregion
            }
            PrepareViewBag(svm.DocTypeId);
            ViewBag.Mode = "Add";
            return(View("Create", svm));
        }