public HttpResponseMessage FarmYieldsFromSamplings([FromBody] FarmYieldDTO cqDTO)
        {
            string key;
            var aur = new AppUserRepository();
            var companyId = 0;
            var userId = aur.ValidateUser(cqDTO.Key, out key, ref companyId);
            if (userId > 0)
            {
                var ur = new FarmYieldRepository();
                var u = new FarmYield();
                if (cqDTO.YieldDate != null)
                {
                    cqDTO.Start_YieldDate = DateTime.Parse(cqDTO.YieldDate).ToString();
                    cqDTO.End_YieldDate = DateTime.Parse(cqDTO.YieldDate).AddDays(1).ToString();
                }
                else
                {
                    int sm = int.Parse(cqDTO.StartDateMonth);
                    if (sm == 1)
                    {
                        cqDTO.Start_YieldDate = DateTime.Parse("12/23/" + (int.Parse(cqDTO.StartDateYear) - 1).ToString()).ToString();
                        cqDTO.End_YieldDate = DateTime.Parse("2/14/" + cqDTO.StartDateYear).ToString();
                    }
                    else if (sm == 12)
                    {
                        cqDTO.Start_YieldDate = DateTime.Parse("11/23/" + cqDTO.StartDateYear).ToString();
                        cqDTO.End_YieldDate = DateTime.Parse("1/14/" + (int.Parse(cqDTO.StartDateYear) + 1).ToString()).ToString();
                    }
                    else
                    {
                        cqDTO.Start_YieldDate = DateTime.Parse((int.Parse(cqDTO.StartDateMonth) - 1).ToString() + "/23/" + cqDTO.StartDateYear).ToString();
                        cqDTO.End_YieldDate = DateTime.Parse((int.Parse(cqDTO.StartDateMonth) + 1).ToString() + "/14/" + cqDTO.StartDateYear).ToString();
                    }

                    cqDTO.StartDateMonth = null;
                    cqDTO.StartDateYear = null;
                }
                SGApp.DTOs.GenericDTO dto = new GenericDTO();
                dto.StartDate = DateTime.Parse(cqDTO.Start_YieldDate);
                dto.EndDate = DateTime.Parse(cqDTO.End_YieldDate);
                List<Sampling> samplingResults = new List<Sampling>();
                PondRepository pr = new PondRepository();
                var client = new HttpClient
                {
                    //BaseAddress = new Uri("http://323-booth-svr2:3030/")
                    BaseAddress = new Uri("http://64.139.95.243:7846/")
                    //BaseAddress = new Uri(baseAddress)
                };
                try
                {
                    //var response = client.PostAsJsonAsync("api/Remote/GetKeithsData", dto).Result;
                    //response.EnsureSuccessStatusCode();
                    JavaScriptSerializer json_serializer = new JavaScriptSerializer();
                    //Sampling[] samplingResultsArray = json_serializer.Deserialize<Sampling[]>(response.Content.ReadAsStringAsync().Result); // new List<Sampling>();
                    //Sampling[] samplingResultsArray = response.Content.ReadAsAsync<Sampling[]>().Result;
                    //samplingResults = samplingResultsArray.ToList();
                    //JavaScriptSerializer json_serializer = new JavaScriptSerializer();
                    Sampling[] samplingResultsArray = json_serializer.Deserialize<Sampling[]>(Constants.testdata);
                    samplingResults = samplingResultsArray.ToList();
                    samplingResults = samplingResults.GroupBy(x => x.farmPond).Select(group => group.First()).ToList();
                    //var result = response.Content.ReadAsStringAsync().Result;

                    //return Request.CreateResponse(HttpStatusCode.OK, result);
                }
                catch (Exception e)
                {
                    throw new HttpException("Error occurred: " + e.Message);
                }
                var predicate = ur.GetPredicate(cqDTO, u, companyId);
                var data = ur.GetByPredicate(predicate);
                var col = new Collection<Dictionary<string, string>>();
                data = data.OrderBy(x => x.YieldDate).ToList();

                foreach (Sampling sam in samplingResults)
                {

                    FarmYield fy = data.Where(x => x.Pond.InnovaName == sam.farmPond).FirstOrDefault();
                    Pond pd = pr.GetPondFromInnovaName(sam.farmPond);
                    var dic = new Dictionary<string, string>();
                    if (fy != null)
                    {
                        dic.Add("YieldId", fy.YieldID.ToString());
                        dic.Add("PondID", fy.PondID.ToString());
                        dic.Add("PondName", sam.farmPond);
                        dic.Add("FarmID", fy.Pond.FarmId.ToString());
                        dic.Add("YieldDate", fy.YieldDate.ToShortDateString());
                        dic.Add("PoundsYielded", fy.PoundsYielded.ToString());
                        dic.Add("PoundsPlant", fy.PoundsPlant.ToString());
                        dic.Add("PoundsHeaded", fy.PoundsHeaded.ToString());
                        dic.Add("PercentYield", fy.PercentYield.ToString());
                        dic.Add("PercentYield2", fy.PercentYield2.ToString());
                    }
                    else {
                        dic.Add("YieldId", "-1");
                        dic.Add("PondID", pd.PondId.ToString() != null ? pd.PondId.ToString() : "");
                        dic.Add("PondName", sam.farmPond != null ? sam.farmPond : "");
                        dic.Add("FarmID", pd.FarmId.ToString() != null ? pd.FarmId.ToString() : "");
                        dic.Add("YieldDate", cqDTO.Start_YieldDate);
                        dic.Add("PoundsYielded", "");
                        dic.Add("PoundsPlant", "");
                        dic.Add("PoundsHeaded", "");
                        dic.Add("PercentYield", "");
                        dic.Add("PercentYield2", "");

                    }

                    col.Add(dic);

                }
                foreach (FarmYield fy in data)
                {

                    Sampling samp = samplingResults.Where(x => x.farmPond == fy.Pond.InnovaName).FirstOrDefault();
                    var dic = new Dictionary<string, string>();
                    if (samp == null)
                    {
                        dic.Add("YieldId", fy.YieldID.ToString());
                        dic.Add("PondID", fy.PondID.ToString());
                        dic.Add("PondName", fy.Pond.InnovaName != null ? fy.Pond.InnovaName : fy.Pond.PondName);
                        dic.Add("FarmID", fy.Pond.FarmId.ToString());
                        dic.Add("YieldDate", fy.YieldDate.ToShortDateString());
                        dic.Add("PoundsYielded", fy.PoundsYielded.ToString());
                        dic.Add("PoundsPlant", fy.PoundsPlant.ToString());
                        dic.Add("PoundsHeaded", fy.PoundsHeaded.ToString());
                        dic.Add("PercentYield", fy.PercentYield.ToString());
                        dic.Add("PercentYield2", fy.PercentYield2.ToString());
                        col.Add(dic);
                    }

                }

                var retVal = new GenericDTO
                {
                    Key = key,
                    ReturnData = col
                };
                return Request.CreateResponse(HttpStatusCode.OK, retVal);
            }
            var message = "validation failed";
            return Request.CreateResponse(HttpStatusCode.NotFound, message);
        }
        internal HttpResponseMessage FarmYieldDates(HttpRequestMessage request, FarmYieldDTO cqDTO)
        {
            string key;
            var    aur       = new AppUserRepository();
            var    companyId = 0;
            var    userId    = aur.ValidateUser(cqDTO.Key, out key, ref companyId);

            if (userId > 0)
            {
                var ur = new FarmYieldRepository();
                var u  = new FarmYield();
                if (cqDTO.YieldDate != null)
                {
                    cqDTO.Start_YieldDate = DateTime.Parse(cqDTO.YieldDate).ToString();
                    cqDTO.End_YieldDate   = DateTime.Parse(cqDTO.YieldDate).AddDays(1).ToString();
                }
                else
                {
                    int sm = int.Parse(cqDTO.StartDateMonth);
                    if (sm == 1)
                    {
                        cqDTO.Start_YieldDate = DateTime.Parse("12/23/" + (int.Parse(cqDTO.StartDateYear) - 1).ToString()).ToString();
                        cqDTO.End_YieldDate   = DateTime.Parse("2/14/" + cqDTO.StartDateYear).ToString();
                    }
                    else if (sm == 12)
                    {
                        cqDTO.Start_YieldDate = DateTime.Parse("11/23/" + cqDTO.StartDateYear).ToString();
                        cqDTO.End_YieldDate   = DateTime.Parse("1/14/" + (int.Parse(cqDTO.StartDateYear) + 1).ToString()).ToString();
                    }
                    else
                    {
                        cqDTO.Start_YieldDate = DateTime.Parse((int.Parse(cqDTO.StartDateMonth) - 1).ToString() + "/23/" + cqDTO.StartDateYear).ToString();
                        cqDTO.End_YieldDate   = DateTime.Parse((int.Parse(cqDTO.StartDateMonth) + 1).ToString() + "/14/" + cqDTO.StartDateYear).ToString();
                    }

                    cqDTO.StartDateMonth = null;
                    cqDTO.StartDateYear  = null;
                }
                var predicate = ur.GetPredicate(cqDTO, u, companyId);
                var data      = ur.GetByPredicate(predicate);
                var col       = new Collection <Dictionary <string, string> >();
                data = data.GroupBy(x => x.YieldDate).Select(x => x.First()).OrderBy(x => x.YieldDate).ToList();
                foreach (var item in data)
                {
                    var dic = new Dictionary <string, string>();


                    dic.Add("YieldDate", item.YieldDate.ToShortDateString());

                    col.Add(dic);
                    var ufdic = new Dictionary <string, string>();
                }

                var retVal = new GenericDTO
                {
                    Key        = key,
                    ReturnData = col
                };
                return(Request.CreateResponse(HttpStatusCode.OK, retVal));
            }
            var message = "validation failed";

            return(request.CreateResponse(HttpStatusCode.NotFound, message));
        }
        internal HttpResponseMessage FarmYields(HttpRequestMessage request, FarmYieldDTO cqDTO)
        {
            string key;
            var aur = new AppUserRepository();
            var companyId = 0;
            var userId = aur.ValidateUser(cqDTO.Key, out key, ref companyId);
            if (userId > 0)
            {
                var ur = new FarmYieldRepository();
                var u = new FarmYield();
                if (cqDTO.YieldDate != null)
                {
                    cqDTO.Start_YieldDate = DateTime.Parse(cqDTO.YieldDate).ToString();
                    cqDTO.End_YieldDate = DateTime.Parse(cqDTO.YieldDate).AddDays(1).ToString();
                }
                else
                {
                    int sm = int.Parse(cqDTO.StartDateMonth);
                    if (sm == 1)
                    {
                        cqDTO.Start_YieldDate = DateTime.Parse("12/23/" + (int.Parse(cqDTO.StartDateYear) - 1).ToString()).ToString();
                        cqDTO.End_YieldDate = DateTime.Parse("2/14/" + cqDTO.StartDateYear).ToString();
                    }
                    else if (sm == 12)
                    {
                        cqDTO.Start_YieldDate = DateTime.Parse("11/23/" + cqDTO.StartDateYear).ToString();
                        cqDTO.End_YieldDate = DateTime.Parse("1/14/" + (int.Parse(cqDTO.StartDateYear) + 1).ToString()).ToString();
                    }
                    else
                    {
                        cqDTO.Start_YieldDate = DateTime.Parse((int.Parse(cqDTO.StartDateMonth) - 1).ToString() + "/23/" + cqDTO.StartDateYear).ToString();
                        cqDTO.End_YieldDate = DateTime.Parse((int.Parse(cqDTO.StartDateMonth) + 1).ToString() + "/14/" + cqDTO.StartDateYear).ToString();
                    }

                    cqDTO.StartDateMonth = null;
                    cqDTO.StartDateYear = null;
                }

                var predicate = ur.GetPredicate(cqDTO, u, companyId);
                var data = ur.GetByPredicate(predicate);
                var col = new Collection<Dictionary<string, string>>();
                data = data.OrderBy(x => x.YieldDate).ToList();
                foreach (var item in data)
                {

                    var dic = new Dictionary<string, string>();

                    dic.Add("YieldId", item.YieldID.ToString());
                    dic.Add("PondID", item.PondID.ToString());
                    dic.Add("PondName", item.Pond.PondName);
                    dic.Add("FarmID", item.Pond.FarmId.ToString());
                    dic.Add("YieldDate", item.YieldDate.ToShortDateString());
                    dic.Add("PoundsYielded", item.PoundsYielded.ToString());
                    dic.Add("PoundsPlant", item.PoundsPlant.ToString());
                    dic.Add("PoundsHeaded", item.PoundsHeaded.ToString());
                    dic.Add("PercentYield", item.PercentYield.ToString());
                    dic.Add("PercentYield2", item.PercentYield2.ToString());
                    col.Add(dic);
                    var ufdic = new Dictionary<string, string>();

                }

                var retVal = new GenericDTO
                {
                    Key = key,
                    ReturnData = col
                };
                return Request.CreateResponse(HttpStatusCode.OK, retVal);
            }
            var message = "validation failed";
            return request.CreateResponse(HttpStatusCode.NotFound, message);
        }
        public HttpResponseMessage FarmYieldsFromSamplings([FromBody] FarmYieldDTO cqDTO)
        {
            string key;
            var    aur       = new AppUserRepository();
            var    companyId = 0;
            var    userId    = aur.ValidateUser(cqDTO.Key, out key, ref companyId);

            if (userId > 0)
            {
                var ur = new FarmYieldRepository();
                var u  = new FarmYield();
                if (cqDTO.YieldDate != null)
                {
                    cqDTO.Start_YieldDate = DateTime.Parse(cqDTO.YieldDate).ToString();
                    cqDTO.End_YieldDate   = DateTime.Parse(cqDTO.YieldDate).AddDays(1).ToString();
                }
                else
                {
                    int sm = int.Parse(cqDTO.StartDateMonth);
                    if (sm == 1)
                    {
                        cqDTO.Start_YieldDate = DateTime.Parse("12/23/" + (int.Parse(cqDTO.StartDateYear) - 1).ToString()).ToString();
                        cqDTO.End_YieldDate   = DateTime.Parse("2/14/" + cqDTO.StartDateYear).ToString();
                    }
                    else if (sm == 12)
                    {
                        cqDTO.Start_YieldDate = DateTime.Parse("11/23/" + cqDTO.StartDateYear).ToString();
                        cqDTO.End_YieldDate   = DateTime.Parse("1/14/" + (int.Parse(cqDTO.StartDateYear) + 1).ToString()).ToString();
                    }
                    else
                    {
                        cqDTO.Start_YieldDate = DateTime.Parse((int.Parse(cqDTO.StartDateMonth) - 1).ToString() + "/23/" + cqDTO.StartDateYear).ToString();
                        cqDTO.End_YieldDate   = DateTime.Parse((int.Parse(cqDTO.StartDateMonth) + 1).ToString() + "/14/" + cqDTO.StartDateYear).ToString();
                    }

                    cqDTO.StartDateMonth = null;
                    cqDTO.StartDateYear  = null;
                }
                SGApp.DTOs.GenericDTO dto = new GenericDTO();
                dto.StartDate = DateTime.Parse(cqDTO.Start_YieldDate);
                dto.EndDate   = DateTime.Parse(cqDTO.End_YieldDate);
                List <DTOs.Sampling> samplingResults = new List <DTOs.Sampling>();
                PondRepository       pr = new PondRepository();
                var client = new HttpClient
                {
                    //BaseAddress = new Uri("http://323-booth-svr2:3030/")
                    BaseAddress = new Uri("http://64.139.95.243:7846/")
                                  //BaseAddress = new Uri(baseAddress)
                };
                try
                {
                    //var response = client.PostAsJsonAsync("api/Remote/GetKeithsData", dto).Result;
                    //response.EnsureSuccessStatusCode();
                    JavaScriptSerializer json_serializer = new JavaScriptSerializer();
                    //Sampling[] samplingResultsArray = json_serializer.Deserialize<Sampling[]>(response.Content.ReadAsStringAsync().Result); // new List<Sampling>();
                    //Sampling[] samplingResultsArray = response.Content.ReadAsAsync<Sampling[]>().Result;
                    //samplingResults = samplingResultsArray.ToList();
                    //JavaScriptSerializer json_serializer = new JavaScriptSerializer();
                    DTOs.Sampling[] samplingResultsArray = json_serializer.Deserialize <DTOs.Sampling[]>(Constants.testdata);
                    samplingResults = samplingResultsArray.ToList();
                    samplingResults = samplingResults.GroupBy(x => x.farmPond).Select(group => group.First()).ToList();
                    //var result = response.Content.ReadAsStringAsync().Result;

                    //return Request.CreateResponse(HttpStatusCode.OK, result);
                }
                catch (Exception e)
                {
                    throw new HttpException("Error occurred: " + e.Message);
                }
                var predicate = ur.GetPredicate(cqDTO, u, companyId);
                var data      = ur.GetByPredicate(predicate);
                var col       = new Collection <Dictionary <string, string> >();
                data = data.OrderBy(x => x.YieldDate).ToList();

                foreach (DTOs.Sampling sam in samplingResults)
                {
                    FarmYield fy  = data.Where(x => x.Pond.InnovaName == sam.farmPond).FirstOrDefault();
                    Pond      pd  = pr.GetPondFromInnovaName(sam.farmPond);
                    var       dic = new Dictionary <string, string>();
                    if (fy != null)
                    {
                        dic.Add("YieldId", fy.YieldID.ToString());
                        dic.Add("PondID", fy.PondID.ToString());
                        dic.Add("PondName", sam.farmPond);
                        dic.Add("FarmID", fy.Pond.FarmId.ToString());
                        dic.Add("YieldDate", fy.YieldDate.ToShortDateString());
                        dic.Add("PoundsYielded", fy.PoundsYielded.ToString());
                        dic.Add("PoundsPlant", fy.PoundsPlant.ToString());
                        dic.Add("PoundsHeaded", fy.PoundsHeaded.ToString());
                        dic.Add("PercentYield", fy.PercentYield.ToString());
                        dic.Add("PercentYield2", fy.PercentYield2.ToString());
                    }
                    else
                    {
                        dic.Add("YieldId", "-1");
                        dic.Add("PondID", pd.PondId.ToString() != null ? pd.PondId.ToString() : "");
                        dic.Add("PondName", sam.farmPond != null ? sam.farmPond : "");
                        dic.Add("FarmID", pd.FarmId.ToString() != null ? pd.FarmId.ToString() : "");
                        dic.Add("YieldDate", cqDTO.Start_YieldDate);
                        dic.Add("PoundsYielded", "");
                        dic.Add("PoundsPlant", "");
                        dic.Add("PoundsHeaded", "");
                        dic.Add("PercentYield", "");
                        dic.Add("PercentYield2", "");
                    }

                    col.Add(dic);
                }
                foreach (FarmYield fy in data)
                {
                    DTOs.Sampling samp = samplingResults.Where(x => x.farmPond == fy.Pond.InnovaName).FirstOrDefault();
                    var           dic  = new Dictionary <string, string>();
                    if (samp == null)
                    {
                        dic.Add("YieldId", fy.YieldID.ToString());
                        dic.Add("PondID", fy.PondID.ToString());
                        dic.Add("PondName", fy.Pond.InnovaName != null ? fy.Pond.InnovaName : fy.Pond.PondName);
                        dic.Add("FarmID", fy.Pond.FarmId.ToString());
                        dic.Add("YieldDate", fy.YieldDate.ToShortDateString());
                        dic.Add("PoundsYielded", fy.PoundsYielded.ToString());
                        dic.Add("PoundsPlant", fy.PoundsPlant.ToString());
                        dic.Add("PoundsHeaded", fy.PoundsHeaded.ToString());
                        dic.Add("PercentYield", fy.PercentYield.ToString());
                        dic.Add("PercentYield2", fy.PercentYield2.ToString());
                        col.Add(dic);
                    }
                }

                var retVal = new GenericDTO
                {
                    Key        = key,
                    ReturnData = col
                };
                return(Request.CreateResponse(HttpStatusCode.OK, retVal));
            }
            var message = "validation failed";

            return(Request.CreateResponse(HttpStatusCode.NotFound, message));
        }