Exemplo n.º 1
0
        private HttpResponseMessage ProcessNewFeedRecord(HttpRequestMessage request, FeedingDTO uDto, string key, int companyId, int UserId)
        {
            var ur = new FeedingRepository();
            var o2 = new Feeding();
            var validationErrors = GetFeedValidationErrors(ur, o2, uDto, companyId, UserId);

            if (validationErrors.Any())
            {
                return(ProcessValidationErrors(request, validationErrors, key));
            }
            //  no validation errors...
            //Pond.CompanyId = companyId;
            o2 = ur.Save(o2);
            var farmid = uDto.FarmID;

            var br       = new BinRepository();
            var binCount = br.GetFarmBinList(farmid).Count();

            if (binCount > 0)
            {
                var binDisb = br.GetNewBinDisbursementRecord();
                binDisb.DateCreated = DateTime.Now;
                var disbType  = br.GetDisbursementType("Routine Feeding");
                var ticketNbr = br.GetLastBinLoadTicketNumber(uDto.BinID.Value);
                if (ticketNbr == 0)
                {
                    return(request.CreateErrorResponse(HttpStatusCode.InternalServerError,
                                                       string.Format("{0}{1}", "There are no Tickets in BinLoads for BinID ", uDto.BinID)));
                }

                var dto = new BinDisbursementDto()
                {
                    BinID            = uDto.BinID.Value,
                    TicketNumber     = ticketNbr,
                    Pounds           = int.Parse(uDto.PoundsFed),
                    Note             = "Record created from daily feed disbursement input screen",
                    DisbursementType = disbType,
                    DisbursementDate = DateTime.Now,
                    CreatedDate      = DateTime.Now,
                    UserID           = UserId,
                    FeedID           = o2.FeedingId
                };
                validationErrors = GetBinDisbursementErrors(br, binDisb, dto, companyId, UserId);
                if (validationErrors.Any())
                {
                    return(ProcessValidationErrors(request, validationErrors, key));
                }
                br.SaveChanges();
                br.UpdateBinCurrentPounds(null, binDisb);
            }

            uDto.Key = key;
            var response = request.CreateResponse(HttpStatusCode.Created, uDto);

            response.Headers.Location = new Uri(Url.Link("Default", new {
                id = o2.FeedingId
            }));
            return(response);
        }
Exemplo n.º 2
0
        internal HttpResponseMessage Ponds(HttpRequestMessage request, PondDTO cqDTO)
        {
            string key;
            var    aur                 = new AppUserRepository();
            var    companyId           = 0;
            var    UserId              = aur.ValidateUser(cqDTO.Key, out key, ref companyId);
            AppUserRoleRepository aur1 = new AppUserRoleRepository();


            if (UserId > 0 && aur1.IsInRole(UserId, "User"))
            {
                var ur        = new PondRepository();
                var u         = new Pond();
                var predicate = ur.GetPredicate(cqDTO, u, companyId);
                var data      = ur.GetByPredicate(predicate);
                var col       = new Collection <Dictionary <string, string> >();
                var bins      = new Collection <Dictionary <string, string> >();
                if (cqDTO.FarmId != null)
                {
                    var br      = new BinRepository();
                    var binList = br.GetFarmBinList(int.Parse(cqDTO.FarmId));
                    foreach (var bin in binList)
                    {
                        bins.Add(
                            new Dictionary <string, string>()
                        {
                            { "BinID", bin.BinID.ToString() },
                            { "BinName", bin.BinName },
                            { "FarmID", bin.FarmID.HasValue ? bin.FarmID.Value.ToString() : "" },
                            { "CurrentTicket", bin.CurrentTicket.HasValue ? bin.CurrentTicket.Value.ToString() : "" },
                            { "CurrentPounds", bin.CurrentPounds.HasValue ? bin.CurrentPounds.Value.ToString() : "" },
                            { "LastDispersement", bin.LastDisbursement.HasValue ? bin.LastDisbursement.Value.ToShortDateString() : "" },
                            { "LastLoaded", bin.LastLoaded.HasValue ? bin.LastLoaded.Value.ToShortDateString() : "" }
                        }
                            );
                    }
                }
                var farmCol = new Collection <Tuple <int, int> >();
                foreach (var item in data)
                {
                    var dic = new Dictionary <string, string>();
                    dic.Add("PondId", item.PondId.ToString());
                    dic.Add("PondName", item.PondName);
                    dic.Add("StatusId", item.StatusId.ToString());
                    dic.Add("InnovaName", item.InnovaName != null ? item.InnovaName : "");
                    dic.Add("InnovaCode", item.InnovaCode != null ? item.InnovaCode : "");
                    dic.Add("Size", item.Size.ToString());
                    dic.Add("NoFeed", item.NoFeed.ToString());
                    int poundsfedsinceharvest = 0;
                    if (item.Harvests.OrderByDescending(x => x.HarvestDate).FirstOrDefault() != null)
                    {
                        dic.Add("LastHarvest", item.Harvests.OrderByDescending(x => x.HarvestDate).FirstOrDefault().HarvestDate.ToString());
                        poundsfedsinceharvest = item.Feedings.Where(x => x.FeedDate > item.Harvests.OrderByDescending(y => y.HarvestDate).FirstOrDefault().HarvestDate).Sum(x => x.PoundsFed);
                    }
                    else
                    {
                        dic.Add("LastHarvest", "");
                        poundsfedsinceharvest = item.Feedings.Sum(x => x.PoundsFed);
                    }
                    int salepounds = poundsfedsinceharvest / 2;
                    dic.Add("PoundsFedSinceHarvest", poundsfedsinceharvest.ToString());
                    dic.Add("SalesPoundsSinceHarvest", salepounds.ToString());
                    dic.Add("HealthStatus", item.HealthStatus.ToString());
                    col.Add(dic);
                }

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

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