// GET: /JobOrderHeader/Create

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

            p.DocDate     = DateTime.Now;
            p.DueDate     = DateTime.Now;
            p.CreatedDate = DateTime.Now;
            p.DivisionId  = (int)System.Web.HttpContext.Current.Session["DivisionId"];
            p.SiteId      = (int)System.Web.HttpContext.Current.Session["SiteId"];

            //Getting Settings
            var settings = _jobOrderSettingsServie.GetJobOrderSettingsForDocument(id, p.DivisionId, p.SiteId);

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

            p.DocTypeId = id;
            List <PerkViewModel> Perks = new List <PerkViewModel>();

            //Perks
            if (p.JobOrderSettings.Perks != null)
            {
                foreach (var item in p.JobOrderSettings.Perks.Split(',').ToList())
                {
                    PerkViewModel temp        = Mapper.Map <Perk, PerkViewModel>(_perkService.Find(Convert.ToInt32(item)));
                    var           DocTypePerk = _perkDocumentTypeService.GetPerkDocumentTypeForPerk(id, temp.PerkId, p.SiteId, p.DivisionId);
                    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);
                }
            }

            if (p.JobOrderSettings.isVisibleCostCenter)
            {
                p.CostCenterName = _JobOrderHeaderService.FGetJobOrderCostCenter(p.DocTypeId, p.DocDate, p.DivisionId, p.SiteId);
            }

            p.PerkViewModel       = Perks;
            p.UnitConversionForId = settings.UnitConversionForId;
            p.ProcessId           = settings.ProcessId;

            PrepareViewBag(id);

            //TODO
            //p.OrderById = new EmployeeService(_unitOfWork).GetEmloyeeForUser(User.Identity.GetUserId());

            if (settings.isVisibleGodown == true && System.Web.HttpContext.Current.Session["DefaultGodownId"] != null)
            {
                p.GodownId = (int)System.Web.HttpContext.Current.Session["DefaultGodownId"];
            }

            if (settings.DueDays.HasValue)
            {
                p.DueDate = _JobOrderHeaderService.AddDueDate(DateTime.Now, settings.DueDays.Value);
            }
            else
            {
                p.DueDate = DateTime.Now;
            }
            p.DocNo      = _documentTypeService.FGetNewDocNo("DocNo", ConfigurationManager.AppSettings["DataBaseSchema"] + ".JobOrderHeaders", p.DocTypeId, p.DocDate, p.DivisionId, p.SiteId);
            ViewBag.Mode = "Add";
            ViewBag.Name = _documentTypeService.Find(id).DocumentTypeName;
            ViewBag.id   = id;
            return(View(p));
        }