public ExecutingAgencyLookupItem CreateNewExecutingAgency(participatingorg org) { var nOrg = aimsDAL.CreateNewExecutingAgency(org, Sessions.UserId); Sessions.iOrgs.ExecutingAgencies.Add(nOrg); if (org.ExecutingAgencyTypeId == (int)ExecutingAgencyType.DP) Sessions.iOrgs.FundSources.Add(nOrg); return nOrg; }
protected void GuessAgency(participatingorg org, bool isFilterByType) { var IsNotFoundInAims = true; var exAgencies = Sessions.iOrgs.ExecutingAgencies; //var exAgencies = isFilterByType ? $filter('filter')($scope.ExecutingAgencies, { ExecutingAgencyTypeId: org.ExecutingAgencyTypeId }) : $scope.ExecutingAgencies; if (org.@ref != null) { var exa = exAgencies.Find(f => f.IATICode == org.@ref); if (exa != null) { org.AllID = exa.AllID; IsNotFoundInAims = false; if (!isFilterByType) org.ExecutingAgencyTypeId = exa.ExecutingAgencyTypeId; } } if (IsNotFoundInAims) { //try to set executing agency ExecutingAgencyLookupItem agencyGuessed = null; var minDistance = 1000; for (var i = 0; i < exAgencies.Count; i++) { var distance = getEditDistance(org.Name.ToLower(), exAgencies[i].Name.ToLower()); if (minDistance > distance) { minDistance = distance; agencyGuessed = exAgencies[i]; } } if (agencyGuessed != null) { var tolaratedDistance = isFilterByType ? (org.Name.Length + agencyGuessed.Name.Length) / 2 : ((org.Name.Length + agencyGuessed.Name.Length) / 2) * 50 / 100; if (minDistance < tolaratedDistance) { org.AllID = agencyGuessed.AllID; if (!isFilterByType) org.ExecutingAgencyTypeId = agencyGuessed.ExecutingAgencyTypeId; } } else { org.ExecutingAgencyTypeId = 4; } } }
public ExecutingAgencyLookupItem CreateNewExecutingAgency(participatingorg org, string userId) { ExecutingAgencyLookupItem returnAgency = null; if (org.ExecutingAgencyTypeId == (int)ExecutingAgencyType.DP) { //http://iatistandard.org/202/codelists/OrganisationType/ var fundsourceCategory = GetOtherFundSourceCategory(userId); var cur = dbContext.tblCurrencies.FirstOrDefault(f => f.IATICode == "USD"); var ent = dbContext.tblFundSources.FirstOrDefault(f => f.FundSourceName == org.Name || f.IATICode == org.@ref); if (ent == null) { ent = new tblFundSource { FundSourceCategoryId = fundsourceCategory.Id, CurrencyId = cur.Id, FundSourceName = org.Name, IATICode = org.@ref, IDate = DateTime.Now, IUser = userId, }; dbContext.tblFundSources.Add(ent); dbContext.SaveChanges(); } returnAgency = new ExecutingAgencyLookupItem { ExecutingAgencyTypeId = (int)ExecutingAgencyType.DP, ExecutingAgencyOrganizationTypeId = ent.FundSourceCategoryId, ExecutingAgencyOrganizationId = ent.Id, Name = ent.FundSourceName, }; } else if (org.ExecutingAgencyTypeId == (int)ExecutingAgencyType.Government) { tblMinistry ministry = GetNAMinistry(userId); var ent = dbContext.tblMinistryAgencies.FirstOrDefault(f => f.AgencyName == org.Name); if (ent == null) { ent = new tblMinistryAgency { MinistryId = ministry.Id, AgencyName = org.Name, IDate = DateTime.Now, IUser = userId, }; dbContext.tblMinistryAgencies.Add(ent); dbContext.SaveChanges(); } returnAgency = new ExecutingAgencyLookupItem { ExecutingAgencyTypeId = (int)ExecutingAgencyType.Government, ExecutingAgencyOrganizationTypeId = ent.MinistryId, ExecutingAgencyOrganizationId = ent.Id, Name = ent.AgencyName, }; } else if (org.ExecutingAgencyTypeId == (int)ExecutingAgencyType.NGO) { var ent = dbContext.tblNGOCSOes.FirstOrDefault(f => f.NGOOrganizationName == org.Name); if (ent == null) { ent = new tblNGOCSO { NGOOrganizationName = org.Name, NGOOrganizationTypeId = dbContext.tblNGOOrganizationTypes.FirstOrDefault().Id, IUser = userId, IDate = DateTime.Now }; dbContext.tblNGOCSOes.Add(ent); dbContext.SaveChanges(); } returnAgency = new ExecutingAgencyLookupItem { ExecutingAgencyTypeId = (int)ExecutingAgencyType.NGO, ExecutingAgencyOrganizationTypeId = ent.NGOOrganizationTypeId, ExecutingAgencyOrganizationId = ent.Id, Name = ent.NGOOrganizationName, }; } return returnAgency; }