Ejemplo n.º 1
0
        public IActionResult AddContract(Contract model)
        {
            if (HttpContext.Session.GetString("LoginID") == null)
            {
                return(RedirectToAction("Login", "Users"));
            }
            if (!(HttpContext.Session.GetString("AdminValidity") == "Admin" || HttpContext.Session.GetString("AdminValidity") == "Super Admin"))
            {
                return(RedirectToAction("Error", "Admin"));
            }
            List <SelectListItem> ContractTypeList = new List <SelectListItem>
            {
                new SelectListItem {
                    Value = "Bundled hours with product", Text = "Bundled hours with product"
                },
                new SelectListItem {
                    Value = "Maintenance Contract", Text = "Maintenance Contract"
                }
            };

            ViewData["ContractType"] = ContractTypeList;

            CentricsContext       context         = HttpContext.RequestServices.GetService(typeof(Centrics.Models.CentricsContext)) as CentricsContext;
            List <ClientAddress>  clientAddresses = context.getAllClientAddress();
            List <SelectListItem> companynames    = new List <SelectListItem>();

            for (int i = 0; i < clientAddresses.Count; i++)
            {
                companynames.Add(new SelectListItem {
                    Value = clientAddresses[i].ClientCompany, Text = clientAddresses[i].ClientCompany
                });
            }

            ViewData["Company"] = companynames;

            if (ModelState.IsValid)
            {
                if (!DateComparer(model.StartValid, model.EndValid))
                {
                    ModelState.AddModelError("", "End Date should be set to a date after the start date. ");
                    return(View(model));
                }
                if (!(model.EndValid.CompareTo(DateTime.Today) > 0))
                {
                    ModelState.AddModelError("", "The Contract you are trying to add has an End Date that expired already");
                    return(View(model));
                }

                context.LogAction("Contract", "New " + model.ContractType + " contract has been created for " + model.ClientCompany + " lasting from " + model.StartValid + " to " + model.EndValid + ".", context.GetUser(Convert.ToInt32(HttpContext.Session.GetString("LoginID"))));
                context.AddContract(model);
                return(RedirectToAction("ViewContract"));
            }
            return(View(model));
        }