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 HttpResponseMessage ProcessNewFarmYieldRecord(HttpRequestMessage request, FarmYieldDTO uDto, string key, int FarmYieldId, int userId) { var ur = new FarmYieldRepository(); var user = new FarmYield(); var validationErrors = GetValidationErrors(ur, user, uDto, FarmYieldId, userId); if (validationErrors.Any()) { return ProcessValidationErrors(request, validationErrors, key); } user = ur.Save(user); uDto.Key = key; uDto.YieldID = user.YieldID.ToString(); UpdateProductionTotalYield(uDto); var response = request.CreateResponse(HttpStatusCode.Created, uDto); response.Headers.Location = new Uri(Url.Link("Default", new { id = user.YieldID })); return response; }
private List<DbValidationError> GetValidationErrors(FarmYieldRepository pr, FarmYield contact, FarmYieldDTO cqDto, int YieldID, int userId) { contact.ProcessRecord(cqDto); return pr.Validate(contact); }
private HttpResponseMessage ProcessExistingFarmYieldRecord(HttpRequestMessage request, FarmYieldDTO cqDto, int contactId, string key, int FarmYieldId, int userId) { var ur = new FarmYieldRepository(); var user = new FarmYield(); user = ur.GetById(contactId); var validationErrors = GetValidationErrors(ur, user, cqDto, FarmYieldId, 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); } UpdateProductionTotalYield(cqDto); cqDto.Key = key; return request.CreateResponse(HttpStatusCode.Accepted, cqDto); }
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); }