public FrayteResult AddZoneCountryPostCode(FrayteZoneCountryPostCode zonepostcode) { FrayteResult result = new FrayteResult(); try { if (zonepostcode != null) { int countryId = dbContext.Countries.Where(p => p.CountryName == zonepostcode.CountryName).Select(p => p.CountryId).FirstOrDefault(); if (countryId > 0) { if (zonepostcode.LogisticZoneCountryPostCodeId > 0) { var postcode = dbContext.LogisticZoneCountryPostCodes.Where(p => p.LogisticZoneCountryPostCodeId == zonepostcode.LogisticZoneCountryPostCodeId).FirstOrDefault(); if (postcode != null) { if (UtilityRepository.LogisticZoneCountryId(zonepostcode.OperationZoneId, countryId, zonepostcode.LogisticCompany, zonepostcode.LogisticType, zonepostcode.RateType, zonepostcode.Zone) > 0) { postcode.LogisticZoneCountryId = UtilityRepository.LogisticZoneCountryId(zonepostcode.OperationZoneId, countryId, zonepostcode.LogisticCompany, zonepostcode.LogisticType, zonepostcode.RateType, zonepostcode.Zone); postcode.FromPostCode = zonepostcode.FromPostCode; postcode.ToPostCode = zonepostcode.ToPostCode; dbContext.Entry(postcode).State = System.Data.Entity.EntityState.Modified; dbContext.SaveChanges(); result.Status = true; } else { if (postcode.FromPostCode == zonepostcode.FromPostCode && postcode.ToPostCode == zonepostcode.ToPostCode) { result.Status = false; } else { var servicezone = (from ls in dbContext.LogisticServices join lsz in dbContext.LogisticServiceZones on ls.LogisticServiceId equals lsz.LogisticServiceId where ls.OperationZoneId == zonepostcode.OperationZoneId && ls.LogisticCompany == zonepostcode.LogisticCompany && ls.LogisticType == zonepostcode.LogisticType && ls.RateType == (zonepostcode.RateType == null ? null : zonepostcode.RateType) && lsz.ZoneName == zonepostcode.Zone select new { lsz.LogisticServiceZoneId }).FirstOrDefault(); LogisticServiceZoneCountry lszc = new LogisticServiceZoneCountry(); lszc.OperationZoneId = zonepostcode.OperationZoneId; lszc.LogisticServiceZoneId = servicezone.LogisticServiceZoneId; lszc.CountryId = countryId; dbContext.LogisticServiceZoneCountries.Add(lszc); if (lszc != null) { dbContext.SaveChanges(); } LogisticZoneCountryPostCode lzcpc = new LogisticZoneCountryPostCode(); lzcpc.LogisticZoneCountryId = lszc.LogisticZoneCountryId; lzcpc.FromPostCode = zonepostcode.FromPostCode; lzcpc.ToPostCode = zonepostcode.ToPostCode; dbContext.LogisticZoneCountryPostCodes.Add(lzcpc); if (lzcpc != null) { dbContext.SaveChanges(); } result.Status = true; } } } } else { var servicezone = (from ls in dbContext.LogisticServices join lsz in dbContext.LogisticServiceZones on ls.LogisticServiceId equals lsz.LogisticServiceId join lszc in dbContext.LogisticServiceZoneCountries on lsz.LogisticServiceZoneId equals lszc.LogisticServiceZoneId where ls.OperationZoneId == zonepostcode.OperationZoneId && ls.LogisticCompany == zonepostcode.LogisticCompany && ls.LogisticType == zonepostcode.LogisticType && ls.RateType == (zonepostcode.RateType == null ? null : zonepostcode.RateType) && lsz.ZoneName == zonepostcode.Zone && lszc.CountryId == countryId select new { lszc.LogisticZoneCountryId }).FirstOrDefault(); if (servicezone != null) { LogisticZoneCountryPostCode lzcpc = new LogisticZoneCountryPostCode(); lzcpc.LogisticZoneCountryId = servicezone.LogisticZoneCountryId; lzcpc.FromPostCode = zonepostcode.FromPostCode; lzcpc.ToPostCode = zonepostcode.ToPostCode; dbContext.LogisticZoneCountryPostCodes.Add(lzcpc); if (lzcpc != null) { dbContext.SaveChanges(); } result.Status = true; } else { LogisticServiceZoneCountry lszc = new LogisticServiceZoneCountry(); lszc.OperationZoneId = zonepostcode.OperationZoneId; lszc.LogisticServiceZoneId = UtilityRepository.LogisticServiceZoneId(zonepostcode.OperationZoneId, zonepostcode.LogisticCompany, zonepostcode.LogisticType, zonepostcode.RateType, zonepostcode.Zone); lszc.CountryId = countryId; dbContext.LogisticServiceZoneCountries.Add(lszc); if (lszc != null) { dbContext.SaveChanges(); } LogisticZoneCountryPostCode lzcpc = new LogisticZoneCountryPostCode(); lzcpc.LogisticZoneCountryId = lszc.LogisticZoneCountryId; lzcpc.FromPostCode = zonepostcode.FromPostCode; lzcpc.ToPostCode = zonepostcode.ToPostCode; dbContext.LogisticZoneCountryPostCodes.Add(lzcpc); if (lzcpc != null) { dbContext.SaveChanges(); } result.Status = true; } } } } return(result); } catch (Exception ex) { return(result); } }
public void AddCountryZone(FrayteZoneCountry zone) { try { if (zone != null) { //Step 1: Get the list of all the countries related to the zone from db. var list = (from zoneCount in dbContext.LogisticServiceZoneCountries where zoneCount.LogisticServiceZoneId == zone.ZoneId select zoneCount).ToList(); //Step 2: Remove Countries, for this need to find the country which is in db list but not in UI list var result = list.Where(p => !zone.Fraytezonezountry.Any(p2 => p2.CountryId == p.CountryId)).ToList(); foreach (var removeObj in result) { LogisticServiceZoneCountry removeData = dbContext.LogisticServiceZoneCountries.Where(p => p.CountryId == removeObj.CountryId && p.LogisticServiceZoneId == removeObj.LogisticServiceZoneId).FirstOrDefault(); if (removeData != null) { dbContext.LogisticServiceZoneCountries.Remove(removeData); dbContext.SaveChanges(); } } //Step 3: Add Countries, for this need to find the countries which is in UI list but not in db list var result1 = zone.Fraytezonezountry.Where(p => !list.Any(p2 => p2.CountryId == p.CountryId)).ToList(); foreach (var addObj in result1) { LogisticServiceZoneCountry newZoneCountry = new LogisticServiceZoneCountry(); newZoneCountry.LogisticServiceZoneId = zone.ZoneId; newZoneCountry.CountryId = addObj.CountryId; newZoneCountry.OperationZoneId = zone.OperationZoneId; newZoneCountry.TransitTime = addObj.TransitTime; dbContext.LogisticServiceZoneCountries.Add(newZoneCountry); dbContext.SaveChanges(); } //Step 4: Update Transit Time For Zone Country var transitlist = (from zoneCount in dbContext.LogisticServiceZoneCountries where zoneCount.LogisticServiceZoneId == zone.ZoneId select zoneCount).ToList(); if (transitlist.Count > 0) { int j = 0; foreach (var ll in transitlist) { if (ll.LogisticZoneCountryId > 0) { var transit = dbContext.LogisticServiceZoneCountries.Where(p => p.LogisticZoneCountryId == ll.LogisticZoneCountryId).FirstOrDefault(); if (transit != null) { transit.LogisticZoneCountryId = ll.LogisticZoneCountryId; transit.TransitTime = zone.Fraytezonezountry[j].TransitTime; dbContext.Entry(transit).State = System.Data.Entity.EntityState.Modified; dbContext.SaveChanges(); } j++; } } } } } catch (DbEntityValidationException ex) { foreach (var entityValidationErrors in ex.EntityValidationErrors) { foreach (var validationError in entityValidationErrors.ValidationErrors) { string ss = "Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage; } } } }