using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using HealthchoiceMVC.Models; using Microsoft.AspNet.Identity.EntityFramework; using Microsoft.AspNet.Identity; using System.Web.Services.Description; using System.Collections; using HealthchoiceMVC.NonDbModels; namespace HealthchoiceMVC.Services { public class HCLocalService { HCDBEntities entities = new HCDBEntities(); HCWebService webService = new HCWebService(); //Generic method for a SelectList from the lookup table. public List GetLookUpDropDown(string lookuptype) { List LookUpList = new List(); var lookups = from l in entities.lookup_table select l; lookups = lookups.Where(l => l.lookup_type.ToUpper().Contains(lookuptype.ToUpper()) && l.active == true); List sortedList = lookups.OrderBy(o => o.lookup_display_order).ToList(); LookUpList.Add(new SelectListItem { Text = "-Please select-", Value = "" }); foreach (lookup_table lt in sortedList) { LookUpList.Add(new SelectListItem { Text = lt.lookup_dropdown_description, Value = lt.lookup_dropdown_value }); } return LookUpList; } //Generic Method for getting a lookup as a standard list. public List GetLookups(string lookuptype) { var lookups = from l in entities.lookup_table select l; lookups = lookups.Where(l => l.lookup_type.ToUpper().Contains(lookuptype.ToUpper()) && l.active == true); List sortedList = lookups.OrderBy(o => o.lookup_display_order).ToList(); return sortedList; } //Generic Method for getting a lookup as a standard list. public List GetLookupValues(string lookuptype) { return entities.lookup_table.Where(l => l.lookup_type.ToUpper().Contains(lookuptype.ToUpper()) && l.active == true).Select(i=> i.lookup_dropdown_value).ToList(); } //Generic Method To Get Lookup Text public string GetLookupText(string dropdown_value, string lookup_type) { var lookuptext = from l in entities.lookup_table where l.lookup_dropdown_value == dropdown_value && l.lookup_type == lookup_type select l.lookup_dropdown_description; return lookuptext.FirstOrDefault(); } public List GetUserTypeList() { List UserTypeList = new List(); var UserTypes = entities.user_type.ToList(); UserTypeList.Add(new SelectListItem { Text = "-Please select-", Value = "" }); foreach (user_type ut in UserTypes) { UserTypeList.Add(new SelectListItem { Text = ut.user_type_desc, Value = ut.user_type_key.ToString() }); } return UserTypeList; } public List GetNetworkList() { List NetworkList = new List(); var NetworkLists = entities.NetworkAffiliations.ToList(); NetworkList.Add(new SelectListItem { Text = "-Please select-", Value = "" }); foreach (NetworkAffiliation n in NetworkLists) { NetworkList.Add(new SelectListItem { Text = n.NetworkName, Value = n.NetworkAffiliationId.ToString() }); } return NetworkList; } public List GetUserRolesByType(int user_type) { List UserRoleList = new List(); var UserRoles = (from ur in entities.user_role join ut in entities.user_type on ur.user_type_key equals ut.user_type_key where ut.user_type_key==user_type select ur); UserRoleList.Add(new SelectListItem { Text = "-Please select-", Value = "" }); foreach (var ur in UserRoles) { UserRoleList.Add(new SelectListItem { Text = ur.user_role_desc, Value = ur.user_role_key.ToString() }); } return UserRoleList; } public List GetUserRolesByRoleArray(IList Roles) { List UserRoleList = new List(); var UserRoles = (from ur in entities.user_role join urr in entities.user_role_by_role_permission on ur.user_role_key equals urr.user_role_key join roles in Roles on urr.user_role_manager equals roles select ur).Distinct().ToList(); UserRoleList.Add(new SelectListItem { Text = "-Please select-", Value = "" }); foreach (var ur in UserRoles) { UserRoleList.Add(new SelectListItem { Text = ur.user_role_desc, Value = ur.user_role_key.ToString() }); } return UserRoleList; } public string GetUserRoleValueByKey(int role_key) { var role = entities.user_role.Find(role_key) ; return role.user_role_value; } public string GetUserRoleDescByValue(string roleValue) { var role = (from ur in entities.user_role where ur.user_role_value == roleValue select ur).FirstOrDefault(); return role.user_role_desc; } public int GetUserRoleKeyByValue(string roleValue) { var role = (from ur in entities.user_role where ur.user_role_value == roleValue select ur).FirstOrDefault(); return role.user_role_key; } public ApplicationUser GetCurrentUserByName(string name) { using (var context = new ApplicationDbContext()) { var userStore = new UserStore(context); var userManager = new UserManager(userStore); ApplicationUser user = userManager.FindByName(name); return user; } } public ApplicationUser GetCurrentUserByEmail(string email) { using (var context = new ApplicationDbContext()) { var user = context.Users.Where(i => i.Email == email).FirstOrDefault(); return user; } } public ApplicationUser GetCurrentUserById(string Id) { using (var context = new ApplicationDbContext()) { var userStore = new UserStore(context); var userManager = new UserManager(userStore); ApplicationUser user = userManager.FindById(Id); return user; } } public List GetDisciplines() { List DisciplineList = new List(); DisciplineList.Add(new SelectListItem { Text = "-Please select-", Value = "" }); //List diciplineCodes = entities.prov_discipline.Where(o => o.prov_discipline_code == 14).ToList(); var inListDisciplines = GetLookups("alloweddisciplines"); List inListDisciplineIDs = new List(); foreach (var item in inListDisciplines) { inListDisciplineIDs.Add(Convert.ToInt32(item.lookup_dropdown_value)); } //List disciplineCodes = webService.GetDisciplines();//.Where(i => inListDisciplineIDs.Contains(i.prov_discipline_code)); IEnumerable disciplineCodes = webService.GetDisciplines().Where(i => inListDisciplineIDs.Contains(i.prov_discipline_code)); foreach (prov_discipline_basic pd in disciplineCodes) { DisciplineList.Add(new SelectListItem { Text = pd.prov_discipline_desc, Value = pd.prov_discipline_code.ToString() }); } return DisciplineList; } public int GetPracticeIdFromCode(string practiceNumber) { using (HCDBEntities entities = new HCDBEntities()) { var practiceId = entities.Practices.Where(i => i.PracticeNumber == practiceNumber.Trim() && i.ActiveIndic == true).Select(i => i.PracticeId).FirstOrDefault(); return practiceId; } } public List GetSubDisciplinesByDisciplineCode(int prov_discipline_code) { List SubDisciplineList = new List(); SubDisciplineList.Add(new SelectListItem { Text = "-Please select-", Value = "" }); //List subDiciplineCodes = entities.prov_sub_discipline.Where(o => o.prov_discipline_code == 14 && o.prov_sub_discipline1 == 0).ToList(); List SubDisciplineCodes = webService.GetSubDisciplinesByDisciplineCode(prov_discipline_code); foreach (prov_sub_discipline sd in SubDisciplineCodes) { SubDisciplineList.Add(new SelectListItem { Text = sd.prov_sub_discipline_desc, Value = sd.prov_sub_discipline_key.ToString() }); } return SubDisciplineList; } } public class IdentityInfo { public IdentityInfo(string identityNumber) { this.Initialize(identityNumber); } public string IdentityNumber { get; private set; } public DateTime BirthDate { get; private set; } public String Gender { get; private set; } public int Age { get; private set; } public string AgeToLongString { get; private set; } public bool IsSouthAfrican { get; private set; } public bool IsValid { get; private set; } private void Initialize(string identityNumber) { this.IdentityNumber = (identityNumber ?? string.Empty).Replace(" ", ""); if (this.IdentityNumber.Length == 13) { var digits = new int[13]; for (int i = 0; i < 13; i++) { digits[i] = int.Parse(this.IdentityNumber.Substring(i, 1)); } int control1 = digits.Where((v, i) => i % 2 == 0 && i < 12).Sum(); string second = string.Empty; digits.Where((v, i) => i % 2 != 0 && i < 12).ToList().ForEach(v => second += v.ToString()); var string2 = (int.Parse(second) * 2).ToString(); int control2 = 0; for (int i = 0; i < string2.Length; i++) { control2 += int.Parse(string2.Substring(i, 1)); } var control = (10 - ((control1 + control2) % 10)) % 10; if (digits[12] == control) { this.BirthDate = DateTime.ParseExact(this.IdentityNumber .Substring(0, 6), "yyMMdd", null); this.Gender = digits[6] < 5 ? "Female" : "Male"; this.IsSouthAfrican = digits[10] == 0; this.Age = CalculateAge(BirthDate); this.AgeToLongString = CalculateAgeToLongString(BirthDate); this.IsValid = true; } } } private int CalculateAge(DateTime birthDay) { DateTime today = DateTime.Today; int age = today.Year - birthDay.Year; if (birthDay > today.AddYears(age)) age--; return age; } private string CalculateAgeToLongString(DateTime birthDay) { TimeSpan difference = DateTime.Now.Subtract(birthDay); DateTime currentAge = DateTime.MinValue + difference; int years = currentAge.Year - 1; int months = currentAge.Month - 1; int days = currentAge.Day - 1; return String.Format("{0} years, {1} months and {2} days.", years, months, days); } } }