public ActionResult Detail(int id, string transactionType, string IndexType, int?DocLineId)
        {
            if (DocLineId.HasValue)
            {
                ViewBag.DocLineId = DocLineId;
            }

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

            MaterialPlanCancelHeader          pt = _MaterialPlanCancelHeaderService.Find(id);
            MaterialPlanCancelHeaderViewModel vm = AutoMapper.Mapper.Map <MaterialPlanCancelHeader, MaterialPlanCancelHeaderViewModel>(pt);

            if (pt == null)
            {
                return(HttpNotFound());
            }
            //Getting Settings
            var settings = new MaterialPlanSettingsService(_unitOfWork).GetMaterialPlanSettingsForDocument(pt.DocTypeId, vm.DivisionId, vm.SiteId);

            if (settings == null && UserRoles.Contains("SysAdmin"))
            {
                return(RedirectToAction("Create", "MaterialPlanSettings", new { id = pt.DocTypeId }).Warning("Please create Material plan settings"));
            }
            else if (settings == null && !UserRoles.Contains("SysAdmin"))
            {
                return(View("~/Views/Shared/InValidSettings.cshtml"));
            }
            vm.MaterialPlanSettings = Mapper.Map <MaterialPlanSettings, MaterialPlanSettingsViewModel>(settings);
            ViewBag.Name            = new DocumentTypeService(_unitOfWork).Find(pt.DocTypeId).DocumentTypeName;
            ViewBag.id = pt.DocTypeId;

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


            return(View("Create", vm));
        }
        // GET: /MaterialPlanCancelHeaderMaster/Create

        public ActionResult Create(int id)//DocumentTypeID
        {
            MaterialPlanCancelHeaderViewModel vm = new MaterialPlanCancelHeaderViewModel();

            vm.DivisionId  = (int)System.Web.HttpContext.Current.Session["DivisionId"];
            vm.SiteId      = (int)System.Web.HttpContext.Current.Session["SiteId"];
            vm.DocNo       = _MaterialPlanCancelHeaderService.GetMaxDocNo();
            vm.CreatedDate = DateTime.Now;

            //Getting Settings
            var settings = new MaterialPlanSettingsService(_unitOfWork).GetMaterialPlanSettingsForDocument(id, vm.DivisionId, vm.SiteId);

            if (settings == null && UserRoles.Contains("SysAdmin"))
            {
                return(RedirectToAction("Create", "MaterialPlanSettings", new { id = id }).Warning("Please create job order settings"));
            }
            else if (settings == null && !UserRoles.Contains("SysAdmin"))
            {
                return(View("~/Views/Shared/InValidSettings.cshtml"));
            }
            vm.MaterialPlanSettings = Mapper.Map <MaterialPlanSettings, MaterialPlanSettingsViewModel>(settings);

            if (new RolePermissionService(_unitOfWork).IsActionAllowed(UserRoles, id, null, this.ControllerContext.RouteData.Values["controller"].ToString(), "Create") == false)
            {
                return(View("~/Views/Shared/PermissionDenied.cshtml").Warning("You don't have permission to do this task."));
            }

            if (string.IsNullOrEmpty(vm.MaterialPlanSettings.PlanType))
            {
                TempData["CSEXC"] += "Please configure PlanType";
            }

            vm.DocDate   = DateTime.Now;
            vm.DueDate   = DateTime.Now;
            vm.DocTypeId = id;
            vm.DocNo     = new DocumentTypeService(_unitOfWork).FGetNewDocNo("DocNo", ConfigurationManager.AppSettings["DataBaseSchema"] + ".MaterialPlanCancelHeaders", vm.DocTypeId, vm.DocDate, vm.DivisionId, vm.SiteId);
            ViewBag.Name = new DocumentTypeService(_unitOfWork).Find(id).DocumentTypeName;
            ViewBag.id   = id;
            ViewBag.Mode = "Add";
            return(View("Create", vm));
        }
        // GET: /ProductMaster/Edit/5

        private ActionResult Edit(int id, string IndexType)
        {
            ViewBag.IndexStatus = IndexType;
            MaterialPlanCancelHeader          pt = _MaterialPlanCancelHeaderService.Find(id);
            MaterialPlanCancelHeaderViewModel vm = AutoMapper.Mapper.Map <MaterialPlanCancelHeader, MaterialPlanCancelHeaderViewModel>(pt);

            if (pt == null)
            {
                return(HttpNotFound());
            }

            if (new RolePermissionService(_unitOfWork).IsActionAllowed(UserRoles, pt.DocTypeId, null, this.ControllerContext.RouteData.Values["controller"].ToString(), "Edit") == false)
            {
                return(View("~/Views/Shared/PermissionDenied.cshtml").Warning("You don't have permission to do this task."));
            }

            #region DocTypeTimeLineValidation
            try
            {
                TimePlanValidation = DocumentValidation.ValidateDocument(Mapper.Map <DocumentUniqueId>(pt), 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 }));
            }

            //Getting Settings
            var settings = new MaterialPlanSettingsService(_unitOfWork).GetMaterialPlanSettingsForDocument(pt.DocTypeId, vm.DivisionId, vm.SiteId);
            if (settings == null && UserRoles.Contains("SysAdmin"))
            {
                return(RedirectToAction("Create", "MaterialPlanSettings", new { id = pt.DocTypeId }).Warning("Please create Material plan settings"));
            }
            else if (settings == null && !UserRoles.Contains("SysAdmin"))
            {
                return(View("~/Views/Shared/InValidSettings.cshtml"));
            }
            vm.MaterialPlanSettings = Mapper.Map <MaterialPlanSettings, MaterialPlanSettingsViewModel>(settings);
            ViewBag.Mode            = "Edit";
            ViewBag.Name            = new DocumentTypeService(_unitOfWork).Find(pt.DocTypeId).DocumentTypeName;
            ViewBag.id = pt.DocTypeId;

            if (string.IsNullOrEmpty(vm.MaterialPlanSettings.PlanType))
            {
                TempData["CSEXC"] += "Please configure PlanType";
            }

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

            return(View("Create", vm));
        }
        public ActionResult Post(MaterialPlanCancelHeaderViewModel vm)
        {
            MaterialPlanCancelHeader pt = AutoMapper.Mapper.Map <MaterialPlanCancelHeaderViewModel, MaterialPlanCancelHeader>(vm);

            #region DocTypeTimeLineValidation

            try
            {
                if (vm.MaterialPlanCancelHeaderId <= 0)
                {
                    TimePlanValidation = DocumentValidation.ValidateDocument(Mapper.Map <DocumentUniqueId>(vm), DocumentTimePlanTypeConstants.Create, User.Identity.Name, out ExceptionMsg, out Continue);
                }
                else
                {
                    TimePlanValidation = DocumentValidation.ValidateDocument(Mapper.Map <DocumentUniqueId>(vm), 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 && (TimePlanValidation || Continue))
            {
                #region CreateRecord
                if (vm.MaterialPlanCancelHeaderId <= 0)
                {
                    pt.CreatedDate  = DateTime.Now;
                    pt.ModifiedDate = DateTime.Now;
                    pt.CreatedBy    = User.Identity.Name;
                    pt.ModifiedBy   = User.Identity.Name;
                    pt.ObjectState  = Model.ObjectState.Added;
                    _MaterialPlanCancelHeaderService.Create(pt);

                    try
                    {
                        _unitOfWork.Save();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXC"] += message;
                        ViewBag.Mode       = "Add";
                        ViewBag.Name       = new DocumentTypeService(_unitOfWork).Find(vm.DocTypeId).DocumentTypeName;
                        ViewBag.id         = vm.DocTypeId;
                        return(View("Create", vm));
                    }

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

                    return(RedirectToAction("Modify", new { id = pt.MaterialPlanCancelHeaderId }).Success("Data saved successfully"));
                }
                #endregion

                #region EditRecord
                else
                {
                    List <LogTypeViewModel> LogList = new List <LogTypeViewModel>();

                    MaterialPlanCancelHeader temp = _MaterialPlanCancelHeaderService.Find(pt.MaterialPlanCancelHeaderId);

                    MaterialPlanCancelHeader ExRec = Mapper.Map <MaterialPlanCancelHeader>(temp);

                    temp.DocNo        = pt.DocNo;
                    temp.DocDate      = pt.DocDate;
                    temp.Remark       = pt.Remark;
                    temp.BuyerId      = pt.BuyerId;
                    temp.ModifiedDate = DateTime.Now;
                    temp.ModifiedBy   = User.Identity.Name;
                    temp.ObjectState  = Model.ObjectState.Modified;
                    _MaterialPlanCancelHeaderService.Update(temp);

                    LogList.Add(new LogTypeViewModel
                    {
                        ExObj = ExRec,
                        Obj   = temp,
                    });

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

                    try
                    {
                        _unitOfWork.Save();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        TempData["CSEXC"] += message;
                        ViewBag.Mode       = "Edit";
                        ViewBag.Name       = new DocumentTypeService(_unitOfWork).Find(vm.DocTypeId).DocumentTypeName;
                        ViewBag.id         = vm.DocTypeId;
                        return(View("Create", pt));
                    }

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

                    return(RedirectToAction("Index", new { id = vm.DocTypeId }).Success("Data saved successfully"));
                }
                #endregion
            }
            ViewBag.Name = new DocumentTypeService(_unitOfWork).Find(vm.DocTypeId).DocumentTypeName;
            ViewBag.id   = vm.DocTypeId;
            ViewBag.Mode = "Add";
            return(View("Create", vm));
        }