private void setCarholderInfo(CardHolderInfo cardHolderInfo, DepartmentInfo departmentInfo, CadreInfo cadreInfo, CrewInfo crewInfo, DesignationInfo designationInfo, SectionInfo sectionInfo, CompanyInfo companyInfo) { if (departmentInfo == null) { cardHolderInfo.DepartmentId = null; } else if (departmentInfo.DepartmentId > 0) { cardHolderInfo.DepartmentId = departmentInfo.DepartmentId; } else { cardHolderInfo.Department = departmentInfo; } if (cadreInfo == null) { cardHolderInfo.CadreId = null; } else if (cadreInfo.CadreId > 0) { cardHolderInfo.CadreId = cadreInfo.CadreId; } else { cardHolderInfo.Cadre = cadreInfo; } if (crewInfo == null) { cardHolderInfo.CrewId = null; } else if (crewInfo.CrewId > 0) { cardHolderInfo.CrewId = crewInfo.CrewId; } else { cardHolderInfo.Crew = crewInfo; } if (designationInfo == null) { cardHolderInfo.DesignationId = null; } else if (designationInfo.DesignationId > 0) { cardHolderInfo.DesignationId = designationInfo.DesignationId; } else { cardHolderInfo.Designation = designationInfo; } if (sectionInfo == null) { cardHolderInfo.SectionId = null; } else if (sectionInfo.SectionId > 0) { cardHolderInfo.SectionId = sectionInfo.SectionId; } else { cardHolderInfo.Section = sectionInfo; } if (companyInfo == null) { cardHolderInfo.CompanyId = null; } else if (companyInfo.CompanyId > 0) { cardHolderInfo.CompanyId = companyInfo.CompanyId; } else { cardHolderInfo.Company = companyInfo; } }
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { DataGridViewRow row = this.dgvCompanys.Rows[e.RowIndex]; string companyName = row.Cells[1].Value as string; if (row == null || string.IsNullOrEmpty(companyName)) { this.dgvCompanys.CancelEdit(); return; } if (!string.IsNullOrEmpty(companyName)) { companyName = companyName.Trim().ToLower(); } List <CompanyInfo> companies = EFERTDbUtility.mEFERTDb.Companies.ToList(); bool companyAlradyExist = companies.Exists(c => c.CompanyName.Trim().ToLower() == companyName); if (companyAlradyExist) { this.dgvCompanys.CancelEdit(); return; } CompanyInfo company = null; if (row.Tag == null) { company = new CompanyInfo() { CompanyName = row.Cells[1].Value as String }; EFERTDbUtility.mEFERTDb.Companies.Add(company); } else { company = row.Tag as CompanyInfo; company.CompanyName = row.Cells[1].Value as String; EFERTDbUtility.mEFERTDb.Entry(company).State = System.Data.Entity.EntityState.Modified; } try { EFERTDbUtility.mEFERTDb.SaveChanges(); if (row.Tag == null) { //EFERTDbUtility.mVisitingLocations.Add(company); row.Tag = company; } else { //EFERTDbUtility.mVisitingLocations[EFERTDbUtility.mVisitingLocations.IndexOf(company)] = company; } } catch (Exception ex) { EFERTDbUtility.RollBack(); this.dgvCompanys.CancelEdit(); MessageBox.Show(this, "Some error occurred in updating visiting locations.\n\n" + EFERTDbUtility.GetInnerExceptionMessage(ex)); } }
private void SearchCardHolderCore(string searchString, bool isNicNumber, bool isTempCard = false, bool isVisitorCard = false) { EFERTDbUtility.InitializeDatabases(false); CCFTCentral ccftCentral = EFERTDbUtility.mCCFTCentral; Cardholder cardHolder = null; CardHolderInfo cardHolderInfo = null; VisitorCardHolder visitor = null; DailyCardHolder dailyCardHolder = null; bool updatedCardExist = true; if (isNicNumber) { Task <Cardholder> cardHolderByNicTask = new Task <Cardholder>(() => { Cardholder cardHolderByNic = (from pds in ccftCentral.PersonalDataStrings where pds != null && pds.PersonalDataFieldID == 5051 && pds.Value != null && pds.Value == searchString select pds.Cardholder).FirstOrDefault(); return(cardHolderByNic); }); cardHolderByNicTask.Start(); cardHolderInfo = (from card in EFERTDbUtility.mEFERTDb.CardHolders where card != null && card.CNICNumber == searchString select card).FirstOrDefault(); if (cardHolderInfo == null) { cardHolder = cardHolderByNicTask.Result; if (cardHolder == null) { dailyCardHolder = (from daily in EFERTDbUtility.mEFERTDb.DailyCardHolders where daily != null && daily.CNICNumber == searchString select daily).FirstOrDefault(); if (dailyCardHolder == null) { visitor = (from visit in EFERTDbUtility.mEFERTDb.Visitors where visit != null && visit.CNICNumber == searchString select visit).FirstOrDefault(); } } } else { if (cardHolderInfo.IsTemp) { cardHolder = cardHolderByNicTask.Result; if (cardHolder == null) { updatedCardExist = false; } } } } else { Task <Cardholder> cardHolderByCardNumberTask = new Task <Cardholder>(() => { Cardholder cardHolderByCardNumber = (from c in ccftCentral.Cardholders where c != null && c.LastName == searchString select c).FirstOrDefault(); return(cardHolderByCardNumber); }); cardHolderByCardNumberTask.Start(); cardHolderInfo = (from card in EFERTDbUtility.mEFERTDb.CardHolders where card != null && card.CardNumber == searchString select card).FirstOrDefault(); if (cardHolderInfo == null) { CheckInAndOutInfo cardIssued = (from checkIn in EFERTDbUtility.mEFERTDb.CheckedInInfos where checkIn != null && checkIn.CheckedIn && checkIn.CardNumber == searchString select checkIn).FirstOrDefault(); if (cardIssued != null) { dailyCardHolder = cardIssued.DailyCardHolders; if (dailyCardHolder == null) { visitor = cardIssued.Visitors; if (visitor == null) { cardHolderInfo = cardIssued.CardHolderInfos; if (cardHolderInfo != null && cardHolderInfo.IsTemp) { cardHolder = (from pds in ccftCentral.PersonalDataStrings where pds != null && pds.PersonalDataFieldID == 5051 && pds.Value != null && pds.Value == cardIssued.CNICNumber select pds.Cardholder).FirstOrDefault(); if (cardHolder != null) { updatedCardExist = true; } else { updatedCardExist = false; } } } } } if (visitor == null && dailyCardHolder == null && cardHolderInfo == null) { if (!isTempCard && !isVisitorCard) { cardHolder = cardHolderByCardNumberTask.Result; } } if (visitor == null && dailyCardHolder == null && cardHolder == null && cardHolderInfo == null) { if (Form.ActiveForm != null) { bool found = false; if (Form.ActiveForm is VisitorForm) { found = true; (Form.ActiveForm as VisitorForm).SetCardNumber(searchString); } else if (Form.ActiveForm is PermanentChForm) { found = true; (Form.ActiveForm as PermanentChForm).SetCardNumber(searchString); } else if (Form.ActiveForm is ContractorChForm) { found = true; (Form.ActiveForm as ContractorChForm).SetCardNumber(searchString); } if (found) { return; } } if (isTempCard) { MessageBox.Show(this, "This temporary card is not issued to any person."); } else if (isVisitorCard) { MessageBox.Show(this, "This visitor card is not issued to any visitor."); } else { MessageBox.Show(this, "Cardholder with " + searchString + " card number is not found."); } return; } } //else //{ // if (!cardHolderInfo.GallagherCardHolder) // { // cardHolder = cardHolderByNicTask.Result; // if (cardHolder == null) // { // updatedCardExist = false; // } // } //} //bool isDigitOnly = this.IsDigitsOnly(searchString); //if (isDigitOnly) //{ //cardHolder = (from c in ccftCentral.Cardholders // where c != null && c.LastName == searchString // select c).FirstOrDefault(); //} //else //{ // cardHolder = (from c in ccftCentral.Cardholders // where c != null && c.FirstName == searchString // select c).FirstOrDefault(); //} } if (cardHolder == null && cardHolderInfo == null && visitor == null && dailyCardHolder == null) { ContractorChForm npchf = new ContractorChForm(searchString); npchf.ShowDialog(this); } else { if (cardHolderInfo != null && !cardHolderInfo.IsTemp) { string cadre = cardHolderInfo.Cadre == null ? "" : cardHolderInfo.Cadre.CadreName; bool isPermanent = cadre.ToLower() == "nmpt" || cadre.ToLower() == "mpt"; if (isPermanent) { PermanentChForm permanentForm = new PermanentChForm(cardHolderInfo); permanentForm.Show(); } else { ContractorChForm contractorForm = new ContractorChForm(cardHolderInfo); contractorForm.Show(); } } else if (cardHolder != null) { Dictionary <int, string> chPds = new Dictionary <int, string>(); foreach (PersonalDataString pds in cardHolder.PersonalDataStrings) { if (pds != null) { chPds.Add(pds.PersonalDataFieldID, pds.Value); } } string cadre = (from c in chPds where c.Key == 12952 && c.Value != null select c.Value).FirstOrDefault(); if (string.IsNullOrEmpty(cadre)) { MessageBox.Show(this, "No Cadre found."); } else { bool isPermanent = cadre.ToLower() == "nmpt" || cadre.ToLower() == "mpt"; if (isPermanent) { int? pNumber = cardHolder.PersonalDataIntegers == null || cardHolder.PersonalDataIntegers.Count == 0 ? null : cardHolder.PersonalDataIntegers.ElementAt(0).Value; string strPNumber = pNumber == null ? "P-Number not found." : pNumber.ToString(); DateTime?dateOfBirth = cardHolder.PersonalDataDates == null || cardHolder.PersonalDataDates.Count == 0 ? null : cardHolder.PersonalDataDates.ElementAt(0).Value; string strDOB = dateOfBirth == null ? "Date of birth not found." : dateOfBirth.ToString(); string bloodGroup = chPds.ContainsKey(5047) && chPds[5047] != null ? chPds[5047] : string.Empty; string CNICNumber = chPds.ContainsKey(5051) && chPds[5051] != null ? chPds[5051] : string.Empty; string crew = chPds.ContainsKey(12869) && chPds[12869] != null ? chPds[12869] : string.Empty; string department = chPds.ContainsKey(5043) && chPds[5043] != null ? chPds[5043] : string.Empty; string designation = chPds.ContainsKey(5042) && chPds[5042] != null ? chPds[5042] : string.Empty; string contactNumber = chPds.ContainsKey(5053) && chPds[5053] != null ? chPds[5053] : string.Empty; string section = chPds.ContainsKey(12951) && chPds[12951] != null ? chPds[12951] : string.Empty; int cardHolderId = cardHolder.FTItemID; string companyName = chPds.ContainsKey(5059) && chPds[5059] != null ? chPds[5059] : string.Empty; CadreInfo cadreInfo = (from c in EFERTDbUtility.mEFERTDb.Cadres where c != null && c.CadreName == cadre select c).FirstOrDefault() ?? new CadreInfo() { CadreName = cadre }; CrewInfo crewInfo = string.IsNullOrEmpty(crew) ? null : ((from c in EFERTDbUtility.mEFERTDb.Crews where c != null && c.CrewName == crew select c).FirstOrDefault() ?? new CrewInfo() { CrewName = crew }); DepartmentInfo departmentInfo = string.IsNullOrEmpty(department) ? null : ((from c in EFERTDbUtility.mEFERTDb.Departments where c != null && c.DepartmentName == department select c).FirstOrDefault() ?? new DepartmentInfo() { DepartmentName = department }); DesignationInfo designationInfo = string.IsNullOrEmpty(designation) ? null : ((from c in EFERTDbUtility.mEFERTDb.Designations where c != null && c.Designation == designation select c).FirstOrDefault() ?? new DesignationInfo() { Designation = designation }); SectionInfo sectionInfo = string.IsNullOrEmpty(section) ? null : ((from c in EFERTDbUtility.mEFERTDb.Sections where c != null && c.SectionName == section select c).FirstOrDefault() ?? new SectionInfo() { SectionName = section }); CompanyInfo companyInfo = string.IsNullOrEmpty(companyName) ? null : ((from c in EFERTDbUtility.mEFERTDb.Companies where c != null && c.CompanyName == companyName select c).FirstOrDefault() ?? new CompanyInfo() { CompanyName = companyName }); if (cardHolderInfo != null && cardHolderInfo.IsTemp) { cardHolderInfo.FTItemId = cardHolderId; cardHolderInfo.FirstName = cardHolder.FirstName; cardHolderInfo.LastName = cardHolder.LastName; cardHolderInfo.BloodGroup = string.IsNullOrEmpty(bloodGroup) ? null : bloodGroup; cardHolderInfo.CardNumber = cardHolder.LastName; cardHolderInfo.CNICNumber = string.IsNullOrEmpty(CNICNumber) ? null : CNICNumber; cardHolderInfo.EmergancyContactNumber = string.IsNullOrEmpty(contactNumber) ? null : contactNumber; cardHolderInfo.PNumber = pNumber == null ? null : pNumber.ToString(); cardHolderInfo.DateOfBirth = dateOfBirth == null ? null : dateOfBirth.ToString(); cardHolderInfo.IsTemp = false; setCarholderInfo(cardHolderInfo, departmentInfo, cadreInfo, crewInfo, designationInfo, sectionInfo, companyInfo); EFERTDbUtility.mEFERTDb.Entry(cardHolderInfo).State = System.Data.Entity.EntityState.Modified; } else { cardHolderInfo = new CardHolderInfo() { FTItemId = cardHolderId, FirstName = cardHolder.FirstName, LastName = cardHolder.LastName, BloodGroup = string.IsNullOrEmpty(bloodGroup) ? null : bloodGroup, CardNumber = cardHolder.LastName, CNICNumber = string.IsNullOrEmpty(CNICNumber) ? null : CNICNumber, EmergancyContactNumber = string.IsNullOrEmpty(contactNumber) ? null : contactNumber, PNumber = pNumber == null ? null : pNumber.ToString(), DateOfBirth = dateOfBirth == null ? null : dateOfBirth.ToString(), IsTemp = false }; setCarholderInfo(cardHolderInfo, departmentInfo, cadreInfo, crewInfo, designationInfo, sectionInfo, companyInfo); EFERTDbUtility.mEFERTDb.CardHolders.Add(cardHolderInfo); } EFERTDbUtility.mEFERTDb.SaveChanges(); PermanentChForm permanentForm = new PermanentChForm(cardHolderInfo); permanentForm.Show(); } else { string companyName = chPds.ContainsKey(5059) && chPds[5059] != null ? chPds[5059] : string.Empty; string CNICNumber = chPds.ContainsKey(5051) && chPds[5051] != null ? chPds[5051] : string.Empty; string department = chPds.ContainsKey(5043) && chPds[5043] != null ? chPds[5043] : string.Empty; string designation = chPds.ContainsKey(5042) && chPds[5042] != null ? chPds[5042] : string.Empty; string emergancyContactNumber = chPds.ContainsKey(5053) && chPds[5053] != null ? chPds[5053] : string.Empty; string section = chPds.ContainsKey(12951) && chPds[12951] != null ? chPds[12951] : string.Empty; string wONumber = chPds.ContainsKey(5344) && chPds[5344] != null ? chPds[5344] : string.Empty; int cardHolderId = cardHolder.FTItemID; CadreInfo cadreInfo = (from c in EFERTDbUtility.mEFERTDb.Cadres where c != null && c.CadreName == cadre select c).FirstOrDefault() ?? new CadreInfo() { CadreName = cadre }; CrewInfo crewInfo = null; DepartmentInfo departmentInfo = string.IsNullOrEmpty(department) ? null : ((from c in EFERTDbUtility.mEFERTDb.Departments where c != null && c.DepartmentName == department select c).FirstOrDefault() ?? new DepartmentInfo() { DepartmentName = department }); DesignationInfo designationInfo = string.IsNullOrEmpty(designation) ? null : ((from c in EFERTDbUtility.mEFERTDb.Designations where c != null && c.Designation == designation select c).FirstOrDefault() ?? new DesignationInfo() { Designation = designation }); SectionInfo sectionInfo = string.IsNullOrEmpty(section) ? null : ((from c in EFERTDbUtility.mEFERTDb.Sections where c != null && c.SectionName == section select c).FirstOrDefault() ?? new SectionInfo() { SectionName = section }); CompanyInfo companyInfo = string.IsNullOrEmpty(companyName) ? null : ((from c in EFERTDbUtility.mEFERTDb.Companies where c != null && c.CompanyName == companyName select c).FirstOrDefault() ?? new CompanyInfo() { CompanyName = companyName }); if (cardHolderInfo != null && cardHolderInfo.IsTemp) { cardHolderInfo.FTItemId = cardHolderId; cardHolderInfo.FirstName = cardHolder.FirstName; cardHolderInfo.LastName = cardHolder.LastName; cardHolderInfo.CardNumber = cardHolder.LastName; cardHolderInfo.CNICNumber = string.IsNullOrEmpty(CNICNumber) ? null : CNICNumber; cardHolderInfo.EmergancyContactNumber = string.IsNullOrEmpty(emergancyContactNumber) ? null : emergancyContactNumber; cardHolderInfo.WONumber = string.IsNullOrEmpty(wONumber) ? null : wONumber; cardHolderInfo.IsTemp = false; setCarholderInfo(cardHolderInfo, departmentInfo, cadreInfo, crewInfo, designationInfo, sectionInfo, companyInfo); EFERTDbUtility.mEFERTDb.Entry(cardHolderInfo).State = System.Data.Entity.EntityState.Modified; } else { cardHolderInfo = new CardHolderInfo() { FTItemId = cardHolderId, FirstName = cardHolder.FirstName, LastName = cardHolder.LastName, CardNumber = cardHolder.LastName, CNICNumber = string.IsNullOrEmpty(CNICNumber) ? null : CNICNumber, EmergancyContactNumber = string.IsNullOrEmpty(emergancyContactNumber) ? null : emergancyContactNumber, WONumber = string.IsNullOrEmpty(wONumber) ? null : wONumber, IsTemp = false }; setCarholderInfo(cardHolderInfo, departmentInfo, cadreInfo, crewInfo, designationInfo, sectionInfo, companyInfo); EFERTDbUtility.mEFERTDb.CardHolders.Add(cardHolderInfo); } EFERTDbUtility.mEFERTDb.SaveChanges(); ContractorChForm contractorForm = new ContractorChForm(cardHolderInfo); contractorForm.Show(); } } } else if (!updatedCardExist) { ContractorChForm contractorForm = new ContractorChForm(cardHolderInfo, true); contractorForm.Show(); } else if (visitor != null) { VisitorForm vistorForm = new VisitorForm(visitor); vistorForm.Show(); } else if (dailyCardHolder != null) { ContractorChForm contractorForm = new ContractorChForm(dailyCardHolder); contractorForm.Show(); } } }