public HttpResponseMessage ShiftEndAddOrEdit([FromBody] ShiftEndDTO uDto)
        {
            string key;
            var ur = new AppUserRepository();
            var ShiftEndId = 0;
            var userId = ur.ValidateUser(uDto.Key, out key, ref ShiftEndId);

            AppUserRoleRepository aur = new AppUserRoleRepository();

            if (userId > 0 && aur.IsInRole(userId, "Data Entry"))
            {
                var ShiftEnd = new ShiftEnd();
                uDto.DayFinishedFreezing = uDto.DayFinishedFreezing == "" ? null : uDto.DayFinishedFreezing;
                uDto.RegEmpLate = uDto.RegEmpLate == "" ? null : uDto.RegEmpLate;
                uDto.RegEmpOut = uDto.RegEmpOut == "" ? null : uDto.RegEmpOut;
                uDto.RegEmplLeftEarly = uDto.RegEmplLeftEarly == "" ? null : uDto.RegEmplLeftEarly;
                uDto.TempEmpOut = uDto.TempEmpOut == "" ? null : uDto.TempEmpOut;
                uDto.InmateLeftEarly = uDto.InmateLeftEarly == "" ? null : uDto.InmateLeftEarly;
                uDto.FinishedKill = uDto.FinishedKill == "" ? null : uDto.FinishedKill;
                uDto.FinishedFillet = uDto.FinishedFillet == "" ? null : uDto.FinishedFillet;
                uDto.FinishedSkinning = uDto.FinishedSkinning == "" ? null : uDto.FinishedSkinning;
                uDto.DayFinishedFreezing = uDto.DayFinishedFreezing == "" ? null : uDto.DayFinishedFreezing;
                uDto.NightFinishedFreezing = uDto.NightFinishedFreezing == "" ? null : uDto.NightFinishedFreezing;
                uDto.DayShiftFroze = uDto.DayShiftFroze == "" ? null : uDto.DayShiftFroze;
                uDto.NightShiftFroze = uDto.NightShiftFroze == "" ? null : uDto.NightShiftFroze;
                uDto.FilletScaleReading = uDto.FilletScaleReading == "" ? null : uDto.FilletScaleReading;
                uDto.DowntimeMinutes = uDto.DowntimeMinutes == "" ? null : uDto.DowntimeMinutes;
                uDto.InLateOut = uDto.InLateOut == "" ? null : uDto.InLateOut;
                uDto.EmployeesOnVacation = uDto.EmployeesOnVacation == "" ? null : uDto.EmployeesOnVacation;
                var errors = ValidateDtoData(uDto, ShiftEnd);
                if (errors.Any())
                {
                    return ProcessValidationErrors(Request, errors, key);
                }
                var NEUserId = 0;
                if (int.TryParse(uDto.ShiftEndID, out NEUserId))
                {
                    if (NEUserId == -1)
                    {
                        //  creating new User record
                        return ProcessNewShiftEndRecord(Request, uDto, key, ShiftEndId, userId);
                    }
                    else
                    {
                        //  editing existing User record
                        return ProcessExistingShiftEndRecord(Request, uDto, NEUserId, key, ShiftEndId, userId);
                    }
                }
                //  no idea what this is
                var msg = "invalid data structure submitted";
                return Request.CreateResponse(HttpStatusCode.BadRequest, msg);
            }
            var message = "validation failed";
            return Request.CreateResponse(HttpStatusCode.NotFound, message);
        }
        internal HttpResponseMessage ShiftEnds(HttpRequestMessage request, ShiftEndDTO 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 ShiftEndRepository();
                var u = new ShiftEnd();
                if (cqDTO.ShiftDate != null)
                {
                    cqDTO.Start_ShiftDate = DateTime.Parse(cqDTO.ShiftDate).ToString();
                    cqDTO.End_ShiftDate = DateTime.Parse(cqDTO.ShiftDate).AddDays(1).ToString();
                }
                var predicate = ur.GetPredicate(cqDTO, u, companyId);
                var data = ur.GetByPredicate(predicate);
                data = data.OrderBy(x => x.ShiftDate).ToList();
                var col = new Collection<Dictionary<string, string>>();

                foreach (var item in data)
                {

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

                    dic.Add("ShiftEndID", item.ShiftEndID.ToString());
                    dic.Add("DayFinishedFreezing", item.DayFinishedFreezing == null ? "" : DateTime.Parse(item.DayFinishedFreezing.ToString()).ToString("HH:mm"));
                    dic.Add("DayShiftFroze", item.DayShiftFroze == null ? "" : item.DayShiftFroze.ToString());
                    dic.Add("DowntimeMinutes", item.DowntimeMinutes == null ? "" : item.DowntimeMinutes.ToString());
                    dic.Add("EmployeesOnVacation", item.EmployeesOnVacation == null ? "" : item.EmployeesOnVacation.ToString());
                    dic.Add("FilletScaleReading", item.FilletScaleReading == null ? "" : item.FilletScaleReading.ToString());
                    dic.Add("FinishedFillet", item.FinishedFillet == null ? "" : DateTime.Parse(item.FinishedFillet.ToString()).ToString("HH:mm"));
                    dic.Add("FinishedKill", item.FinishedKill == null ? "" : DateTime.Parse(item.FinishedKill.ToString()).ToString("HH:mm"));
                    dic.Add("FinishedSkinning", item.FinishedSkinning == null ? "" : DateTime.Parse(item.FinishedSkinning.ToString()).ToString("HH:mm"));
                    dic.Add("InmateLeftEarly", item.InmateLeftEarly == null ? "" : item.InmateLeftEarly.ToString());
                    dic.Add("InLateOut", item.InLateOut == null ? "" : item.InLateOut.ToString());
                    dic.Add("NightFinishedFreezing", item.NightFinishedFreezing == null ? "" : DateTime.Parse(item.NightFinishedFreezing.ToString()).ToString("HH:mm"));
                    dic.Add("NightShiftFroze", item.NightShiftFroze == null ? "" : item.NightShiftFroze.ToString());
                    dic.Add("RegEmpLate", item.RegEmpLate == null ? "" : item.RegEmpLate.ToString());
                    dic.Add("RegEmpOut", item.RegEmpOut == null ? "" : item.RegEmpOut.ToString());
                    dic.Add("RegEmplLeftEarly", item.RegEmplLeftEarly == null ? "" : item.RegEmplLeftEarly.ToString());
                    dic.Add("ShiftDate", item.ShiftDate == null ? "" : item.ShiftDate.ToString());
                    dic.Add("TempEmpOut", item.TempEmpOut == null ? "" : item.TempEmpOut.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);
        }
        private HttpResponseMessage ProcessNewShiftEndRecord(HttpRequestMessage request, ShiftEndDTO uDto, string key, int ShiftEndId, int userId)
        {
            var ur = new ShiftEndRepository();
            var user = new ShiftEnd();

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

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

            user = ur.Save(user);
            uDto.Key = key;
            uDto.ShiftEndID = user.ShiftEndID.ToString();
            var response = request.CreateResponse(HttpStatusCode.Created, uDto);
            response.Headers.Location = new Uri(Url.Link("Default", new
            {
                id = user.ShiftEndID
            }));
            return response;
        }
        private HttpResponseMessage ProcessExistingShiftEndRecord(HttpRequestMessage request, ShiftEndDTO cqDto, int contactId, string key, int ShiftEndId, int userId)
        {
            var ur = new ShiftEndRepository();
            var user = new ShiftEnd();
            user = ur.GetById(contactId);

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

            ur.Save(user);
            cqDto.Key = key;
            return request.CreateResponse(HttpStatusCode.Accepted, cqDto);
        }
 private List<DbValidationError> GetValidationErrors(ShiftEndRepository pr, ShiftEnd contact, ShiftEndDTO cqDto, int ShiftEndID, int userId)
 {
     contact.ProcessRecord(cqDto);
     return pr.Validate(contact);
 }
        internal HttpResponseMessage DailyReport(HttpRequestMessage request, DailyReportDTO cqDTO)
        {
            string key;
            var aur = new AppUserRepository();
            var companyId = 0;
            var userId = aur.ValidateUser(cqDTO.Key, out key, ref companyId);
            if (userId > 0)
            {
                var fyr = new FarmYieldRepository();
                var ser = new ShiftEndRepository();
                var fyhr = new FarmYieldHeaderRepository();
                var fyh = new FarmYieldHeader();
                //var lfsr = new LiveFishSamplingRepository();
                //var lfs = new LiveFishSampling();

                var reportDate = new DateTime();
                var se = new ShiftEnd();
                decimal? pondWeight = 0;
                if (cqDTO.ReportDate != null)
                {
                    reportDate = DateTime.Parse(cqDTO.ReportDate).AddDays(-1);
                }
                fyh = fyhr.GetByDate(reportDate);
                pondWeight = fyr.GetPondWeightByDate(reportDate);
                se = ser.GetByDate(reportDate);
                string downtime = "";
                //lfs = lfsr.GetByDate(reportDate);
                if (se != null && se.DowntimeMinutes != null)
                {
                    downtime = String.Format((se.DowntimeMinutes / 60).ToString(), "{0, D2}") + ":" + String.Format((se.DowntimeMinutes % 60).ToString(), "{0, D2}");
                }
                else
                {
                    downtime = "";
                }
                var col = new Collection<Dictionary<string, string>>();
                var dic = new Dictionary<string, string>();
                if (fyh != null)
                {
                    dic.Add("PlantWeight", string.IsNullOrEmpty(fyh.PlantWeight.ToString()) ? "" : (fyh.PlantWeight.ToString()));
                    dic.Add("WeighBacks", string.IsNullOrEmpty(fyh.WeighBacks.ToString()) ? "" : (fyh.WeighBacks.ToString()));
                    dic.Add("TotalPounds", string.IsNullOrEmpty(fyh.WeighBacks.ToString()) || string.IsNullOrEmpty(fyh.PlantWeight.ToString()) ? "" : (fyh.PlantWeight - fyh.WeighBacks).ToString());
                    dic.Add("PondWeight", string.IsNullOrEmpty(pondWeight.ToString()) ? "" : pondWeight.ToString());
                    dic.Add("Variance", string.IsNullOrEmpty(pondWeight.ToString()) || string.IsNullOrEmpty(fyh.PlantWeight.ToString()) ? "" : (pondWeight - fyh.PlantWeight).ToString());
                    dic.Add("DownTime", downtime);
                }
                col.Add(dic);
                var data = fyr.GetByDate(reportDate);
                var col1 = new Collection<Dictionary<string, string>>();
                data = data.OrderBy(x => x.YieldDate).ToList();

                foreach (var item in data)
                {

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

                    dic1.Add("YieldId", string.IsNullOrEmpty(item.YieldID.ToString()) ? "" : (item.YieldID.ToString()));

                    dic1.Add("PondID", string.IsNullOrEmpty(item.PondID.ToString()) ? "" : (item.PondID.ToString()));
                    dic1.Add("PondName", string.IsNullOrEmpty(item.Pond.PondName) ? "" : (item.Pond.PondName));
                    dic1.Add("FarmID", string.IsNullOrEmpty(item.Pond.Farm.FarmName) ? "" : (item.Pond.Farm.FarmName));
                    dic1.Add("YieldDate", string.IsNullOrEmpty(item.YieldDate.ToString()) ? "" : (item.YieldDate.ToString()));
                    dic1.Add("PoundsYielded", string.IsNullOrEmpty(item.PoundsYielded.ToString()) ? "" : (item.PoundsYielded.ToString()));
                    dic1.Add("PoundsPlant", string.IsNullOrEmpty(item.PoundsPlant.ToString()) ? "" : (item.PoundsPlant.ToString()));
                    dic1.Add("PoundsHeaded", string.IsNullOrEmpty(item.PoundsHeaded.ToString()) ? "" : (item.PoundsHeaded.ToString()));
                    dic1.Add("PercentYield", string.IsNullOrEmpty(item.PercentYield.ToString()) ? "" : (item.PercentYield.ToString())); ;
                    dic1.Add("PercentYield2", string.IsNullOrEmpty(item.PercentYield2.ToString()) ? "" : (item.PercentYield2.ToString()));
                    col1.Add(dic1);

                }

                var col2 = new Collection<Dictionary<string, string>>();

                var dic2 = new Dictionary<string, string>();
                var col3 = new Collection<Dictionary<string, string>>();

                var dic3 = new Dictionary<string, string>();
                var col4 = new Collection<Dictionary<string, string>>();

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

                if (se != null)
                {
                    dic4.Add("DayFinishedFreezing", string.IsNullOrEmpty(se.DayFinishedFreezing.ToString()) ? "" : DateTime.Parse(se.DayFinishedFreezing.ToString()).ToString("HH:mm"));
                    dic2.Add("DayShiftFroze", string.IsNullOrEmpty(se.DayShiftFroze.ToString()) ? "" : (se.DayShiftFroze.ToString()));
                    dic3.Add("EmployeesOnVacation", string.IsNullOrEmpty(se.EmployeesOnVacation.ToString()) ? "" : (se.EmployeesOnVacation.ToString()));
                    dic2.Add("FilletScaleReading", string.IsNullOrEmpty(se.FilletScaleReading.ToString()) ? "" : (se.FilletScaleReading.ToString()));
                    dic4.Add("FinishedFillet", string.IsNullOrEmpty(se.FinishedFillet.ToString()) ? "" : DateTime.Parse(se.FinishedFillet.ToString()).ToString("HH:mm"));
                    dic4.Add("FinishedKill", string.IsNullOrEmpty(se.FinishedKill.ToString()) ? "" : DateTime.Parse(se.FinishedKill.ToString()).ToString("HH:mm"));
                    dic4.Add("FinishedSkinning", string.IsNullOrEmpty(se.FinishedSkinning.ToString()) ? "" : DateTime.Parse(se.FinishedSkinning.ToString()).ToString("HH:mm"));
                    dic3.Add("InmateLeftEarly", string.IsNullOrEmpty(se.InmateLeftEarly.ToString()) ? "" : (se.InmateLeftEarly.ToString()));
                    dic3.Add("InLateOut", string.IsNullOrEmpty(se.InLateOut.ToString()) ? "" : (se.InLateOut.ToString()));
                    dic4.Add("NightFinishedFreezing", string.IsNullOrEmpty(se.NightFinishedFreezing.ToString()) ? "" : DateTime.Parse(se.NightFinishedFreezing.ToString()).ToString("HH:mm"));
                    dic2.Add("NightShiftFroze", string.IsNullOrEmpty(se.NightShiftFroze.ToString()) ? "" : (se.NightShiftFroze.ToString()));
                    dic3.Add("RegEmpLate", string.IsNullOrEmpty(se.RegEmpLate.ToString()) ? "" : (se.RegEmpLate.ToString()));
                    dic3.Add("RegEmpOut", string.IsNullOrEmpty(se.RegEmpOut.ToString()) ? "" : (se.RegEmpOut.ToString()));
                    dic3.Add("RegEmplLeftEarly", string.IsNullOrEmpty(se.RegEmplLeftEarly.ToString()) ? "" : (se.RegEmplLeftEarly.ToString()));
                    dic3.Add("TempEmpOut", string.IsNullOrEmpty(se.TempEmpOut.ToString()) ? "" : (se.TempEmpOut.ToString()));
                }
                col2.Add(dic2);
                col3.Add(dic3);
                col4.Add(dic4);

                //var col5 = new Collection<Dictionary<string, string>>();

                //var dic5 = new Dictionary<string, string>();
                //if (lfs != null)
                //{
                //    dic5.Add("Pct0_125", string.IsNullOrEmpty(lfs.Pct0_125.ToString()) ? "" : (lfs.Pct0_125.ToString()));
                //    dic5.Add("Avg0_125", string.IsNullOrEmpty(lfs.Avg0_125.ToString()) ? "" : (lfs.Avg0_125.ToString()));
                //    dic5.Add("Pct125_225", string.IsNullOrEmpty(lfs.Pct125_225.ToString()) ? "" : (lfs.Pct125_225.ToString()));
                //    dic5.Add("Avg125_225", string.IsNullOrEmpty(lfs.Avg125_225.ToString()) ? "" : (lfs.Avg125_225.ToString()));
                //    dic5.Add("Pct225_3", string.IsNullOrEmpty(lfs.Pct225_3.ToString()) ? "" : (lfs.Pct225_3.ToString()));
                //    dic5.Add("Avg225_3", string.IsNullOrEmpty(lfs.Avg225_3.ToString()) ? "" : (lfs.Avg225_3.ToString()));
                //    dic5.Add("Pct3_5", string.IsNullOrEmpty(lfs.Pct3_5.ToString()) ? "" : (lfs.Pct3_5.ToString()));
                //    dic5.Add("Avg3_5", string.IsNullOrEmpty(lfs.Avg3_5.ToString()) ? "" : (lfs.Avg3_5.ToString()));
                //    dic5.Add("Pct5_Up", string.IsNullOrEmpty(lfs.Pct5_Up.ToString()) ? "" : (lfs.Pct5_Up.ToString()));
                //    dic5.Add("Avg5_Up", string.IsNullOrEmpty(lfs.Avg5_Up.ToString()) ? "" : (lfs.Avg5_Up.ToString()));
                //}
                //col5.Add(dic5);

                var retVal = new DailyReportDTO
                {
                    Key = key,
                    Header = col,
                    Ponds = col1,
                    Employees = col3,
                    Finish = col4,
                    Freezing = col2,
                    //Samplings = col5
                };
                return Request.CreateResponse(HttpStatusCode.OK, retVal);
            }
            var message = "validation failed";
            return request.CreateResponse(HttpStatusCode.NotFound, message);
        }