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 HttpResponseMessage ProcessNewPlantPondWeightRecord(HttpRequestMessage request, PlantPondWeightDTO uDto, string key, int PlantPondWeightId, int userId)
        {
            var ur = new PlantPondWeightRepository();
            var user = new PlantPondWeight();

            var validationErrors = GetValidationErrors(ur, user, uDto, PlantPondWeightId, userId);

            if (validationErrors.Any())
            {
                return ProcessValidationErrors(request, validationErrors, key);
            }

            user = ur.Save(user);

            UpdateProductionTotalPlantPondWeight(uDto);
            uDto.Key = key;
            uDto.PlantPondWeightID = user.PlantPondWeightID.ToString();
            var response = request.CreateResponse(HttpStatusCode.Created, uDto);
            response.Headers.Location = new Uri(Url.Link("Default", new
            {
                id = user.PlantPondWeightID
            }));
            return response;
        }
        private HttpResponseMessage ProcessExistingPlantPondWeightRecord(HttpRequestMessage request, PlantPondWeightDTO cqDto, int contactId, string key, int PlantPondWeightId, int userId)
        {
            var ur = new PlantPondWeightRepository();
            var user = new PlantPondWeight();
            user = ur.GetById(contactId);

            var validationErrors = GetValidationErrors(ur, user, cqDto, PlantPondWeightId, userId);
            if (validationErrors.Any())
            {
                return ProcessValidationErrors(request, validationErrors, key);
            }

            //if (cqDto.Remove != null && int.Parse(cqDto.Remove) == 1)
            //{
            //    ur.Delete(user);
            //}
            else
            {
                ur.Save(user);
            }
            UpdateProductionTotalPlantPondWeight(cqDto);
            cqDto.Key = key;
            return request.CreateResponse(HttpStatusCode.Accepted, cqDto);
        }
 private List<DbValidationError> GetValidationErrors(PlantPondWeightRepository pr, PlantPondWeight contact, PlantPondWeightDTO cqDto, int YieldID, int userId)
 {
     contact.ProcessRecord(cqDto);
     return pr.Validate(contact);
 }
        internal HttpResponseMessage PlantPondWeights(HttpRequestMessage request, PlantPondWeightDTO 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 PlantPondWeightRepository();
                var u = new PlantPondWeight();
                if (cqDTO.PPWDateTime != null)
                {
                    cqDTO.Start_PPWDateTime = DateTime.Parse(cqDTO.PPWDateTime).ToString();
                    cqDTO.End_PPWDateTime = DateTime.Parse(cqDTO.PPWDateTime).AddDays(1).ToString();
                }
                else
                {
                    int sm = int.Parse(cqDTO.StartDateMonth);
                    if (sm == 1)
                    {
                        cqDTO.Start_PPWDateTime = DateTime.Parse("12/23/" + (int.Parse(cqDTO.StartDateYear) - 1).ToString()).ToString();
                        cqDTO.End_PPWDateTime = DateTime.Parse("2/14/" + cqDTO.StartDateYear).ToString();
                    }
                    else if (sm == 12)
                    {
                        cqDTO.Start_PPWDateTime = DateTime.Parse("11/23/" + cqDTO.StartDateYear).ToString();
                        cqDTO.End_PPWDateTime = DateTime.Parse("1/14/" + (int.Parse(cqDTO.StartDateYear) + 1).ToString()).ToString();
                    }
                    else
                    {
                        cqDTO.Start_PPWDateTime = DateTime.Parse((int.Parse(cqDTO.StartDateMonth) - 1).ToString() + "/23/" + cqDTO.StartDateYear).ToString();
                        cqDTO.End_PPWDateTime = 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.PPWDateTime).ToList();
                foreach (var item in data)
                {

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

                    dic.Add("PlantPondWeightID", item.PlantPondWeightID.ToString());
                    dic.Add("PondID", item.PondID.ToString());
                    dic.Add("PondName", item.Pond.PondName);
                    dic.Add("FarmID", item.Pond.FarmId.ToString());
                    dic.Add("PPWDateTime", item.PPWDateTime.ToShortDateString());
                    dic.Add("PondWeight", item.PondWeight != null ? item.PondWeight.ToString() : "---");
                    dic.Add("PlantWeight", item.PlantWeight != null ? item.PlantWeight.ToString() : "---");
                    //dic.Add("Turtle", item.Turtle.ToString());
                    //dic.Add("Trash", item.Trash.ToString());
                    //dic.Add("Shad", item.Shad.ToString());
                    //dic.Add("Carp", item.Carp.ToString());
                    //dic.Add("Bream", item.Bream.ToString());
                    //dic.Add("LiveDisease", item.LiveDisease.ToString());
                    //dic.Add("DressedDisease", item.DressedDisease.ToString());
                    //dic.Add("Backs", item.Backs.ToString());
                    //dic.Add("RedFillet", item.RedFillet.ToString());
                    //dic.Add("BigFish", item.BigFish.ToString());
                    //dic.Add("DOAs", item.DOAs.ToString());
                    //dic.Add("DressedDiseasePct", item.DressedDiseasePct.ToString());
                    //dic.Add("RedFilletPct", item.RedFilletPct.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);
        }
        internal HttpResponseMessage PlantPondWeightDates(HttpRequestMessage request, PlantPondWeightDTO 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 PlantPondWeightRepository();
                var u = new PlantPondWeight();
                if (cqDTO.PPWDateTime != null)
                {
                    cqDTO.Start_PPWDateTime = DateTime.Parse(cqDTO.PPWDateTime).ToString();
                    cqDTO.End_PPWDateTime = DateTime.Parse(cqDTO.PPWDateTime).AddDays(1).ToString();
                }
                else
                {
                    int sm = int.Parse(cqDTO.StartDateMonth);
                    if (sm == 1)
                    {
                        cqDTO.Start_PPWDateTime = DateTime.Parse("12/23/" + (int.Parse(cqDTO.StartDateYear) - 1).ToString()).ToString();
                        cqDTO.End_PPWDateTime = DateTime.Parse("2/14/" + cqDTO.StartDateYear).ToString();
                    }
                    else if (sm == 12)
                    {
                        cqDTO.Start_PPWDateTime = DateTime.Parse("11/23/" + cqDTO.StartDateYear).ToString();
                        cqDTO.End_PPWDateTime = DateTime.Parse("1/14/" + (int.Parse(cqDTO.StartDateYear) + 1).ToString()).ToString();
                    }
                    else
                    {
                        cqDTO.Start_PPWDateTime = DateTime.Parse((int.Parse(cqDTO.StartDateMonth) - 1).ToString() + "/23/" + cqDTO.StartDateYear).ToString();
                        cqDTO.End_PPWDateTime = 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.PPWDateTime).Select(x => x.First()).OrderBy(x => x.PPWDateTime).ToList();
                foreach (var item in data)
                {

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

                    dic.Add("PPWDateTime", item.PPWDateTime.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);
        }