public JsonResult GetUserOrderStatus(string email, decimal?lat, decimal?lng) { ClientOrderStatusDto result = svc.GetUserOrderStatus(email, lat, lng); if (result.Client != null) { result.Client.Location = null; result.Client.Estate = null; } return(Json(result, JsonRequestBehavior.AllowGet)); }
public ClientOrderStatusDto GetUserOrderStatus(string email, decimal?latitude, decimal?longitude) { db.Configuration.ProxyCreationEnabled = false; var response = new ClientOrderStatusDto(); var user = db.Users.Include(u => u.Location).Include(u => u.Zone).FirstOrDefault(u => u.Email == email); if (user != null) { ZoneSync.ProcessUser(user.Id); response.UserAllowedToOrder = user.Zone.AllowOrder; var latestOrder = db.Orders.Where(o => o.ClientId == user.Id && o.Status != OrderStatus.Canceled).OrderByDescending(o => o.CreatedDate).FirstOrDefault(); response.UserHasOrder = latestOrder != null; if ((user.Location != null && !user.Location.AllowOrder) && (user.Zone != null && !user.Zone.AllowOrder)) { response.UserAllowedToOrder = false; } response.Client = user; response.Client.Orders = null; //for circular json serialization var zone = user.Zone; if (zone != null && zone.AllowSpecial) { //check if zone special applies switch (zone.Status) { case TrenchingStatus.Undefined: response.Special = db.Specials.First(s => s.SpecialType == SpecialType.EarlyBird); break; case TrenchingStatus.HasDates: response.Special = db.Specials.First(s => s.SpecialType == SpecialType.JustInTime); break; } } } else if (latitude != null && longitude != null) { var zones = db.Zones.Where(z => !z.IsDeleted); string lat = latitude.ToString().Replace(",", ".").Replace("-", ""); string lng = longitude.ToString().Replace(",", ".").Replace("-", ""); const string url = "http://maps.frogfoot.net/ftth/check"; using (var client = new WebClient()) { string result = client.DownloadString(url + string.Format("?ll=-{0},{1}", lat, lng)); //if there is a zone that matches the zone code of the returned result the assign that zone to the user then save. dynamic data = JObject.Parse(result); string precinctCode = data["precinct-zone"]; if (data["possible"] == true && !string.IsNullOrEmpty(precinctCode)) { var zone = zones.FirstOrDefault(z => z.Code == precinctCode); if (zone != null) { response.UserAllowedToOrder = zone.AllowOrder; //check if zone special applies if (zone.AllowSpecial) { switch (zone.Status) { case TrenchingStatus.Undefined: response.Special = db.Specials.First(s => s.SpecialType == SpecialType.EarlyBird); break; case TrenchingStatus.HasDates: response.Special = db.Specials.First(s => s.SpecialType == SpecialType.JustInTime); break; } } } } } } return(response); }