public HttpResponseMessage ProductionTotals([FromBody] ProductionTotalDTO 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 ProductionTotalRepository();
                var u = new ProductionTotal();
                if (cqDTO.ProductionDate != null)
                {
                    cqDTO.Start_ProductionDate = DateTime.Parse(cqDTO.ProductionDate).ToString();
                    cqDTO.End_ProductionDate = DateTime.Parse(cqDTO.ProductionDate).AddDays(1).ToString();
                }
                else
                {
                    int sm = int.Parse(cqDTO.StartDateMonth);
                    if (sm == 1)
                    {
                        cqDTO.Start_ProductionDate = DateTime.Parse("12/23/" + (int.Parse(cqDTO.StartDateYear) - 1).ToString()).ToString();
                        cqDTO.End_ProductionDate = DateTime.Parse("2/14/" + cqDTO.StartDateYear).ToString();
                    }
                    else if (sm == 12)
                    {
                        cqDTO.Start_ProductionDate = DateTime.Parse("11/23/" + cqDTO.StartDateYear).ToString();
                        cqDTO.End_ProductionDate = DateTime.Parse("1/14/" + (int.Parse(cqDTO.StartDateYear) + 1).ToString()).ToString();
                    }
                    else
                    {
                        cqDTO.Start_ProductionDate = DateTime.Parse((int.Parse(cqDTO.StartDateMonth) - 1).ToString() + "/23/" + cqDTO.StartDateYear).ToString();
                        cqDTO.End_ProductionDate = 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_ProductionDate);
                dto.EndDate = DateTime.Parse(cqDTO.End_ProductionDate);
                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);
                    Sampling[] samplingResultsArray = json_serializer.Deserialize<Sampling[]>(response.Content.ReadAsStringAsync().Result);
                    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.ProductionDate).ToList();

                foreach (Sampling sam in samplingResults)
                {

                    ProductionTotal 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)
                    {
                        var wb = fy.WeighBacks != null ? fy.WeighBacks : 0;
                        dic.Add("ProductionTotalId", fy.ProductionTotalID.ToString());
                        dic.Add("PondID", fy.PondId.ToString());
                        dic.Add("PondName", sam.farmPond);
                        dic.Add("FarmID", fy.Pond.FarmId.ToString());
                        dic.Add("ProductionDate", fy.ProductionDate.ToShortDateString());
                        dic.Add("PlantWeight", fy.PlantWeight != null ? fy.PlantWeight.ToString() : "---");
                        dic.Add("PondWeight", fy.PondWeight != null ? fy.PondWeight.ToString() : "---");
                        dic.Add("WeighBacks", fy.WeighBacks != null ? fy.WeighBacks.ToString() : "---");
                        dic.Add("AverageYield", fy.AverageYield != null ? fy.AverageYield.ToString() : "---");
                        dic.Add("HeadedWeight", fy.AverageYield != null && fy.PlantWeight != null ? String.Format("{0:0.00}", ((fy.AverageYield / 100) * (fy.PlantWeight - wb))) : "---");
                    }
                    else
                    {
                        dic.Add("ProductionTotalId", "-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("ProductionDate", cqDTO.ProductionDate);
                        dic.Add("PlantWeight", "---");
                        dic.Add("PondWeight", "---");
                        dic.Add("WeighBacks", "---");
                        dic.Add("AverageYield", "---");
                        dic.Add("HeadedWeight", "---");

                    }

                    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 ProductionDates(HttpRequestMessage request, ProductionTotalDTO 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 ProductionTotalRepository();
                var u = new ProductionTotal();
                if (cqDTO.ProductionDate != null)
                {
                    cqDTO.Start_ProductionDate = DateTime.Parse(cqDTO.ProductionDate).ToString();
                    cqDTO.End_ProductionDate = DateTime.Parse(cqDTO.ProductionDate).AddDays(1).ToString();
                }
                else
                {
                    int sm = int.Parse(cqDTO.StartDateMonth);
                    if (sm == 1)
                    {
                        cqDTO.Start_ProductionDate = DateTime.Parse("12/23/" + (int.Parse(cqDTO.StartDateYear) - 1).ToString()).ToString();
                        cqDTO.End_ProductionDate = DateTime.Parse("2/14/" + cqDTO.StartDateYear).ToString();
                    }
                    else if (sm == 12)
                    {
                        cqDTO.Start_ProductionDate = DateTime.Parse("11/23/" + cqDTO.StartDateYear).ToString();
                        cqDTO.End_ProductionDate = DateTime.Parse("1/14/" + (int.Parse(cqDTO.StartDateYear) + 1).ToString()).ToString();
                    }
                    else
                    {
                        cqDTO.Start_ProductionDate = DateTime.Parse((int.Parse(cqDTO.StartDateMonth) - 1).ToString() + "/23/" + cqDTO.StartDateYear).ToString();
                        cqDTO.End_ProductionDate = 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>>();
                foreach (var item in data)
                {

                    var dic = new Dictionary<string, string>();
                    dic.Add("ProductionDate", item.ProductionDate.ToShortDateString());
                    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);
        }
 private void UpdateProductionTotalYield(FarmYieldDTO uDto)
 {
     var pr = new ProductionTotalRepository();
     var prod = new ProductionTotal();
     var prodexists = pr.GetByDateAndPond(DateTime.Parse(uDto.YieldDate), int.Parse(uDto.PondID));
     decimal ay = 0;
     var y1 = uDto.PercentYield == null ? 0 : decimal.Parse(uDto.PercentYield);
     var y2 = uDto.PercentYield2 == null ? 0 : decimal.Parse(uDto.PercentYield2);
     if (y1 > 0 || y2 > 0)
     {
         if (y2 > 0)
         {
             if (y1 > 0)
             {
                 ay = (y1 + y2) / 2;
             }
             else
             {
                 ay = y2;
             }
         }
         else
         {
             ay = y1;
         }
     }
     if (prodexists == null)
     {
         prod.PondId = int.Parse(uDto.PondID);
         prod.ProductionDate = DateTime.Parse(uDto.YieldDate);
         prod.AverageYield = ay;
         pr.Save(prod);
     }
     else
     {
         prod = prodexists;
         var fyr = new FarmYieldRepository();
         List<FarmYield> fyl = fyr.GetByDateAndPond(DateTime.Parse(uDto.YieldDate), int.Parse(uDto.PondID));
         int fycount = fyl.Where(x => x.PercentYield != null).Count();
         int fycount2 = fyl.Where(x => x.PercentYield2 != null).Count();
         decimal fysum1 = fyl.Where(x => x.PercentYield != null).Sum(x => x.PercentYield).Value;
         decimal fysum2 = fyl.Where(x => x.PercentYield2 != null).Sum(x => x.PercentYield2).Value;
         prod.AverageYield = (fysum1 + fysum2) / (fycount + fycount2);
         //if (prod.AverageYield == null)
         //{
         //    prod.AverageYield = ay;
         //}
         //else
         //{
         //    prod.AverageYield = (prod.AverageYield + ay) / 2;
         //}
         pr.Save(prod);
     }
 }
        private void UpdateProductionTotalPlantPondWeight(PlantPondWeightDTO uDto)
        {
            var pr = new ProductionTotalRepository();
            var prod = new ProductionTotal();
            var prodexists = pr.GetByDateAndPond(DateTime.Parse(uDto.PPWDateTime), int.Parse(uDto.PondID));

            if (prodexists == null)
            {
                prod.PondId = int.Parse(uDto.PondID);
                prod.ProductionDate = DateTime.Parse(uDto.PPWDateTime);
                if (uDto.PlantWeight != null)
                {
                    prod.PlantWeight = decimal.Parse(uDto.PlantWeight);
                }

                if (uDto.PondWeight != null)
                {
                    prod.PondWeight = decimal.Parse(uDto.PondWeight);
                }
                pr.Save(prod);
            }
            else
            {
                prod = prodexists;
                var ppwr = new PlantPondWeightRepository();
                List<PlantPondWeight> ppwl = ppwr.GetByDateAndPond(DateTime.Parse(uDto.PPWDateTime), int.Parse(uDto.PondID));
                decimal plw = ppwl.Where(x => x.PlantWeight != null).Sum(x => x.PlantWeight).Value;
                decimal pw = ppwl.Where(x => x.PondWeight != null).Sum(x => x.PondWeight).Value;
                if (pw != 0) { prod.PondWeight = pw; }
                if (plw != 0) { prod.PlantWeight = plw; }

                //if (prod.PlantWeight == null && uDto.PlantWeight != null)
                //{
                //    prod.PlantWeight = decimal.Parse(uDto.PlantWeight);
                //}
                //else if (prod.PlantWeight != null && uDto.PlantWeight != null)
                //{
                //    prod.PlantWeight = prod.PlantWeight + decimal.Parse(uDto.PlantWeight);
                //}
                //if (prod.PondWeight == null && uDto.PondWeight != null)
                //{
                //    prod.PondWeight = decimal.Parse(uDto.PondWeight);
                //}
                //else if (prod.PondWeight != null && uDto.PondWeight != null)
                //{
                //    prod.PondWeight = prod.PondWeight + decimal.Parse(uDto.PondWeight);
                //}
                pr.Save(prod);
            }
        }
 private void UpdateProductionTotalWeighBack(WeighBackDTO uDto)
 {
     var pr = new ProductionTotalRepository();
     var prod = new ProductionTotal();
     var prodexists = pr.GetByDateAndPond(DateTime.Parse(uDto.WBDateTime), int.Parse(uDto.PondID));
     decimal wb = 0;
     wb = uDto.Backs == null ? 0 : decimal.Parse(uDto.Backs);
     wb += uDto.BigFish == null ? 0 : decimal.Parse(uDto.BigFish);
     wb += uDto.Bream == null ? 0 : decimal.Parse(uDto.Bream);
     wb += uDto.Carp == null ? 0 : decimal.Parse(uDto.Carp);
     wb += uDto.DOAs == null ? 0 : decimal.Parse(uDto.DOAs);
     wb += uDto.DressedDisease == null ? 0 : decimal.Parse(uDto.DressedDisease) / decimal.Parse("0.6");
     wb += uDto.LiveDisease == null ? 0 : decimal.Parse(uDto.LiveDisease);
     wb += uDto.RedFillet == null ? 0 : decimal.Parse(uDto.RedFillet) / decimal.Parse("0.36");
     wb += uDto.Shad == null ? 0 : decimal.Parse(uDto.Shad);
     wb += uDto.Trash == null ? 0 : decimal.Parse(uDto.Trash);
     wb += uDto.Turtle == null ? 0 : decimal.Parse(uDto.Turtle);
     if (prodexists == null)
     {
         prod.PondId = int.Parse(uDto.PondID);
         prod.ProductionDate = DateTime.Parse(uDto.WBDateTime);
         prod.WeighBacks = wb;
         pr.Save(prod);
     }
     else
     {
         prod = prodexists;
         var wbr = new WeighBackRepository();
         List<WeighBack> wbl = wbr.GetByDateAndPond(DateTime.Parse(uDto.WBDateTime), int.Parse(uDto.PondID));
         wb = wbl.Where(x => x.Backs != null).Sum(x => x.Backs).Value;
         wb += wbl.Where(x => x.BigFish != null).Sum(x => x.BigFish).Value;
         wb += wbl.Where(x => x.Bream != null).Sum(x => x.Bream).Value;
         wb += wbl.Where(x => x.Carp != null).Sum(x => x.Carp).Value;
         wb += wbl.Where(x => x.DOAs != null).Sum(x => x.DOAs).Value;
         wb += wbl.Where(x => x.DressedDisease != null).Sum(x => x.DressedDisease).Value / decimal.Parse("0.6");
         wb += wbl.Where(x => x.LiveDisease != null).Sum(x => x.LiveDisease).Value;
         wb += wbl.Where(x => x.RedFillet != null).Sum(x => x.RedFillet).Value / decimal.Parse("0.36");
         wb += wbl.Where(x => x.Shad != null).Sum(x => x.Shad).Value;
         wb += wbl.Where(x => x.Trash != null).Sum(x => x.Trash).Value;
         wb += wbl.Where(x => x.Turtle != null).Sum(x => x.Turtle).Value;
         //wb += uDto.Bream == null ? 0 : decimal.Parse(uDto.Bream);
         //wb += uDto.Carp == null ? 0 : decimal.Parse(uDto.Carp);
         //wb += uDto.DOAs == null ? 0 : decimal.Parse(uDto.DOAs);
         //wb += uDto.DressedDisease == null ? 0 : decimal.Parse(uDto.DressedDisease);
         //wb += uDto.LiveDisease == null ? 0 : decimal.Parse(uDto.LiveDisease);
         //wb += uDto.RedFillet == null ? 0 : decimal.Parse(uDto.RedFillet);
         //wb += uDto.Shad == null ? 0 : decimal.Parse(uDto.Shad);
         //wb += uDto.Trash == null ? 0 : decimal.Parse(uDto.Trash);
         //wb += uDto.Turtle == null ? 0 : decimal.Parse(uDto.Turtle);
         //if (prod.WeighBacks == null)
         //{
         //    prod.WeighBacks = wb;
         //}
         //else
         //{
         //    prod.WeighBacks = prod.WeighBacks + wb;
         //}
         if (wb != 0) { prod.WeighBacks = wb; }
         pr.Save(prod);
     }
 }