using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using HealthchoiceMVC.Models; using HealthchoiceMVC.Services; using System.Threading.Tasks; using System.Text.RegularExpressions; namespace HealthchoiceMVC.Controllers { public class ProviderRegistrationController : Controller { CommonController common = new CommonController(); ApplicationUser user = new ApplicationUser(); HCLocalService service = new HCLocalService(); private HCDBEntities db = new HCDBEntities(); private ApplicationDbContext userDb = new ApplicationDbContext(); // GET: ProviderRegistration public ActionResult Index() { return View(); } // GET: ProviderRegistration/Details/5 public ActionResult Details(int id) { return View(); } private void PopulateViewbag() { //ViewBag.CountryList = service.GetListOfCountries(); //ViewBag.CountryListAll = service.GetListOfCountriesAll(); //ViewBag.GenderList = service.GetLookUpDropDown("gender"); ViewBag.TitleList = service.GetLookUpDropDown("title"); //ViewBag.PDIStatus = service.GetLookUpDropDown("PDI"); ViewBag.Disciplines = service.GetDisciplines(); List SubDisciplines = new List(); SubDisciplines.Add(new SelectListItem { Text = "-Select discipline first-", Value = "" }); ViewBag.SubDisciplines = SubDisciplines; ViewBag.ProvSpecialityTypeList = service.GetLookUpDropDown("provider_speciality_type"); var SubDisciplineKeys = new string[] { "", "18" }; ViewBag.SubDisciplineList = service.GetSubDisciplinesByDisciplineCode(14).Where(s => SubDisciplineKeys.Contains(s.Value)); //JQuery needs to be added. } // GET: ProviderRegistration/Create public ActionResult Create() { provider provider = new provider(); user = service.GetCurrentUserByName(User.Identity.Name); provider.prov_first_name = user.FirstName; provider.prov_surname = user.LastName; provider.prov_cell_no = user.ContactNumber; provider.prov_email = user.Email; provider.user_guid = user.Id; //remove this after testing provider.prov_cell_no = "0727977277"; provider.prov_tel_no = "0215320937"; provider.prov_discipline_code = 14; provider.prov_sub_discipline_key = 18; provider.prov_hpcsa_no = "1234567"; provider.prov_middle_name = "Etienne"; provider.prov_title = "Dr"; provider.prov_id_no = "6606305051086"; PopulateViewbag(); return View(provider); } private string CleanPhone(string phone) { Regex digitsOnly = new Regex(@"[^\d]"); return digitsOnly.Replace(phone, ""); } private void PageValidation(provider provider) { provider.prov_country_code = "BA";// just temporarily to get basic registration done if (provider.prov_country_code.Equals("ZA") && provider.prov_id_no.Length != 13) { ModelState.AddModelError(String.Empty, "Your South African ID Number should be 13 digits."); } if (provider.prov_disp_lic_ind == true && provider.prov_disp_lic_no == null) { ModelState.AddModelError(String.Empty, "Please include your dispensing license number."); } if (provider.prov_disp_lic_ind == false && provider.prov_disp_lic_no != null) { ModelState.AddModelError(String.Empty, "Please select the can dispense option as well as your dispensing license number."); } string telNumeric = CleanPhone(provider.prov_tel_no); string mobileNumeric = CleanPhone(provider.prov_cell_no); if (telNumeric.Length < 10) ModelState.AddModelError("prov_tel_no", "Telephone number should contain at least 10 numeric characters"); if (mobileNumeric.Length < 10) ModelState.AddModelError("prov_cell_no", "Alternative number should contain at least 10 numeric characters"); if ((provider.prov_tel_no != null && provider.prov_cell_no != null) && (telNumeric.Length >= 9 && mobileNumeric.Length >= 9)) { if (telNumeric.Substring(telNumeric.Length - 9) == mobileNumeric.Substring(mobileNumeric.Length - 9)) { ModelState.AddModelError("prov_tel_no", "Telephone and alternative number cannot be the same"); } } } // POST: ProviderRegistration/Create [HttpPost] [ValidateAntiForgeryToken] public async Task Create([Bind(Include = "prov_key,prov_title,prov_initials,prov_first_name,prov_middle_name,prov_surname,prov_country_code,prov_id_no,prov_id_type_key,prov_gender_code,prov_vat_no,prov_pdi_status_code,prov_discipline_code,prov_sub_discipline_key,prov_hpcsa_no,prov_email,prov_tel_dialling_code,prov_tel_no,prov_fax_no,prov_cell_no,prov_speciality_code,prov_disp_lic_ind,prov_disp_lic_no,prov_no_of_locations,prov_registered,prov_deregistered,modified_dt,modified_by_key,user_guid,registration_complete")] provider provider) { PageValidation(provider); var errors = ViewData.ModelState.Where(n => n.Value.Errors.Count > 0).ToList(); if (ModelState.IsValid) { if (provider.prov_initials == null) { string initials = ""; initials = provider.prov_first_name.ElementAt(0).ToString().ToUpper(); if (provider.prov_middle_name != null) { initials += "" + provider.prov_middle_name.ElementAt(0).ToString().ToUpper(); } provider.prov_initials = initials; } //add the provider record provider.prov_registered = DateTime.Now; db.provider.Add(provider); await db.SaveChangesAsync(); //add the user_link record user = service.GetCurrentUserByName(User.Identity.Name); user_link user_link = new user_link(); user_link.entity_key = 4; user_link.entity_type = "Provider"; user_link.UserId = user.Id; user_link.modified_by_key = user.Id; user_link.modified_dt = DateTime.Now; db.user_link.Add(user_link); await db.SaveChangesAsync(); //add the prov_user_link record prov_user_link prov_user_link = new prov_user_link(); prov_user_link.prov_key = provider.prov_key; prov_user_link.UserId = user.Id; db.prov_user_link.Add(prov_user_link); await db.SaveChangesAsync(); //Add provider role var result = new CommonController(); await result.AddUserRole(user.Id, "3"); //Provider await result.AddUserRole(user.Id, "7"); //ProviderAdmin return RedirectToAction("Index", "Home"); } PopulateViewbag(); return View(provider); } // GET: ProviderRegistration/Edit/5 public ActionResult Edit(int id) { return View(); } // POST: ProviderRegistration/Edit/5 [HttpPost] public ActionResult Edit(int id, FormCollection collection) { try { // TODO: Add update logic here return RedirectToAction("Index"); } catch { return View(); } } // GET: ProviderRegistration/Delete/5 public ActionResult Delete(int id) { return View(); } // POST: ProviderRegistration/Delete/5 [HttpPost] public ActionResult Delete(int id, FormCollection collection) { try { // TODO: Add delete logic here return RedirectToAction("Index"); } catch { return View(); } } } }