public ActionResult CreateAssistantCompany()
        {
            AssistantCompanyCreateVM assistantCreateVM = new AssistantCompanyCreateVM();

            assistantCreateVM.HolidayPayRate          = "12,00";
            assistantCreateVM.PayrollTaxRate          = "31,42";
            assistantCreateVM.PensionAndInsuranceRate = "6,00";

            List <SelectListItem> careCompanies = new List <SelectListItem>();

            careCompanies = new ApplicationDbContext().CareCompanies.Where(c => c.IsActive == true).ToList().ConvertAll(c => new SelectListItem
            {
                Value = $"{c.Id}",
                Text  = c.CompanyName
            });

            assistantCreateVM.Companies = new SelectList(careCompanies, "Value", "Text");
            return(View(assistantCreateVM));
        }
        public ActionResult CreateAssistantCompany([Bind(Include = "SelectedCompanyId,FirstName,LastName,AssistantSSN,Email,PhoneNumber,HourlySalary,HolidayPayRate,PayrollTaxRate,PensionAndInsuranceRate")] AssistantCompanyCreateVM assistantCreateVM)
        {
            bool errorFound = false;

            //Check that the SSN has the correct format
            if (!string.IsNullOrWhiteSpace(assistantCreateVM.AssistantSSN))
            {
                assistantCreateVM.AssistantSSN = assistantCreateVM.AssistantSSN.Trim();
                Regex regex = new Regex(@"^([1-9][0-9]{3})(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[12][0-9])))[-]?\d{4}$");
                Match match = regex.Match(assistantCreateVM.AssistantSSN);
                if (!match.Success)
                {
                    ModelState.AddModelError("AssistantSSN", "Ej giltigt personnummer. Formaten YYYYMMDD-NNNN och YYYYMMDDNNNN är giltiga.");
                    errorFound = true;
                }
            }
            else
            {
                errorFound = true;
            }

            //Check that the assistant is born in the 20th or 21st century
            if (!errorFound)
            {
                if (int.Parse(assistantCreateVM.AssistantSSN.Substring(0, 2)) != 19 && int.Parse(assistantCreateVM.AssistantSSN.Substring(0, 2)) != 20)
                {
                    ModelState.AddModelError("AssistantSSN", "Assistenten måste vara född på 1900- eller 2000-talet.");
                    errorFound = true;
                }
            }

            //Check that the assistant is at least 18 years old and was not born in the future:-)
            if (!errorFound)
            {
                DateTime assistantBirthday = new DateTime(int.Parse(assistantCreateVM.AssistantSSN.Substring(0, 4)), int.Parse(assistantCreateVM.AssistantSSN.Substring(4, 2)), int.Parse(assistantCreateVM.AssistantSSN.Substring(6, 2)));
                if (assistantBirthday.Date > DateTime.Now.Date)
                {
                    ModelState.AddModelError("AssistantSSN", "Födelsedatumet får inte vara senare än idag.");
                    errorFound = true;
                }
                else if (assistantBirthday > DateTime.Now.AddYears(-18))
                {
                    ModelState.AddModelError("AssistantSSN", "Assistenten måste vara minst 18 år.");
                    errorFound = true;
                }
            }

            //Check if there is an assistant with the same SSN already in the company. The same assistant is allowed in another company.
            if (!errorFound)
            {
                var twinAssistant = db.Assistants.Where(a => (a.AssistantSSN == assistantCreateVM.AssistantSSN) && (a.CareCompanyId == assistantCreateVM.SelectedCompanyId)).FirstOrDefault();
                if (twinAssistant != null)
                {
                    ModelState.AddModelError("AssistantSSN", "Det finns redan en assistent med detta personnummer");
                    errorFound = true;
                }
            }

            if (!errorFound)
            {
                if (assistantCreateVM.AssistantSSN.Length == 12)
                {
                    assistantCreateVM.AssistantSSN = assistantCreateVM.AssistantSSN.Insert(8, "-");
                }
            }

            if (ModelState.IsValid)
            {
                Assistant assistant = new Assistant();
                assistant.FirstName               = assistantCreateVM.FirstName;
                assistant.LastName                = assistantCreateVM.LastName;
                assistant.AssistantSSN            = assistantCreateVM.AssistantSSN;
                assistant.PhoneNumber             = assistantCreateVM.PhoneNumber;
                assistant.Email                   = assistantCreateVM.Email;
                assistant.HourlySalary            = assistantCreateVM.HourlySalary;
                assistant.HolidayPayRate          = assistantCreateVM.HolidayPayRate;
                assistant.PayrollTaxRate          = assistantCreateVM.PayrollTaxRate;
                assistant.PensionAndInsuranceRate = assistantCreateVM.PensionAndInsuranceRate;
                assistant.CareCompanyId           = (int)assistantCreateVM.SelectedCompanyId;

                db.Assistants.Add(assistant);
                db.SaveChanges();
                return(RedirectToAction("IndexAllCompanies"));
            }

            List <SelectListItem> careCompanies = new List <SelectListItem>();

            careCompanies = new ApplicationDbContext().CareCompanies.Where(c => c.IsActive == true).ToList().ConvertAll(c => new SelectListItem
            {
                Value = $"{c.Id}",
                Text  = c.CompanyName
            });

            assistantCreateVM.Companies = new SelectList(careCompanies, "Value", "Text");
            return(View(assistantCreateVM));
        }