// GET: /ProductMaster/Create

        public ActionResult Create()
        {
            DocumentTypeTimeExtensionViewModel vm = new DocumentTypeTimeExtensionViewModel();

            vm.SiteId     = (int)System.Web.HttpContext.Current.Session["SiteId"];
            vm.DivisionId = (int)System.Web.HttpContext.Current.Session["DivisionId"];
            vm.ExpiryDate = DateTime.Now;
            vm.DocDate    = DateTime.Now;
            PrepareViewBag();
            return(View("Create", vm));
        }
        // GET: /ProductMaster/Edit/5

        public ActionResult Edit(int id)
        {
            DocumentTypeTimeExtension pt = _DocumentTypeTimeExtensionService.Find(id);

            DocumentTypeTimeExtensionViewModel Temp = Mapper.Map <DocumentTypeTimeExtension, DocumentTypeTimeExtensionViewModel>(pt);

            PrepareViewBag();
            if (pt == null)
            {
                return(HttpNotFound());
            }
            return(View("Create", Temp));
        }
        public ActionResult Post(DocumentTypeTimeExtensionViewModel vm)
        {
            DocumentTypeTimeExtension pt = Mapper.Map <DocumentTypeTimeExtensionViewModel, DocumentTypeTimeExtension>(vm);

            if (ModelState.IsValid)
            {
                var DocType = db.DocumentType.Where(m => m.DocumentTypeName == MasterDocTypeConstants.DocumentTypeTimeExtension).FirstOrDefault();

                if (vm.DocumentTypeTimeExtensionId <= 0)
                {
                    _DocumentTypeTimeExtensionService.Create(pt, User.Identity.Name);

                    try
                    {
                        db.SaveChanges();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        ModelState.AddModelError("", message);
                        PrepareViewBag();
                        return(View("Create", vm));
                    }

                    LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                    {
                        DocTypeId    = DocType.DocumentTypeId,
                        DocId        = pt.DocumentTypeTimeExtensionId,
                        ActivityType = (int)ActivityTypeContants.Added,
                    }));

                    NotifyUser(pt.DocumentTypeTimeExtensionId);

                    return(RedirectToAction("Create").Success("Data saved successfully"));
                }
                else
                {
                    List <LogTypeViewModel> LogList = new List <LogTypeViewModel>();

                    DocumentTypeTimeExtension temp = _DocumentTypeTimeExtensionService.Find(pt.DocumentTypeTimeExtensionId);

                    DocumentTypeTimeExtension ExRec = Mapper.Map <DocumentTypeTimeExtension>(pt);

                    temp.DocTypeId   = pt.DocTypeId;
                    temp.Type        = pt.Type;
                    temp.ExpiryDate  = pt.ExpiryDate;
                    temp.UserName    = pt.UserName;
                    temp.Reason      = pt.Reason;
                    temp.NoOfRecords = pt.NoOfRecords;
                    temp.DocDate     = pt.DocDate;

                    _DocumentTypeTimeExtensionService.Update(temp, User.Identity.Name);

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

                    try
                    {
                        db.SaveChanges();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        ModelState.AddModelError("", message);
                        PrepareViewBag();
                        return(View("Create", pt));
                    }

                    LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
                    {
                        DocTypeId       = DocType.DocumentTypeId,
                        DocId           = temp.DocumentTypeTimeExtensionId,
                        ActivityType    = (int)ActivityTypeContants.Modified,
                        xEModifications = Modifications,
                    }));

                    return(RedirectToAction("Index").Success("Data saved successfully"));
                }
            }
            PrepareViewBag();
            return(View("Create", vm));
        }