Exemplo n.º 1
0
        public FarmView(DefaultUser DU)
        {
            InitializeComponent();
            var fv = new FarmViewModel(this, DU);

            DataContext = fv;
        }
Exemplo n.º 2
0
        public IActionResult Farm(FarmViewModel fvm)
        {
            fvm.regOptions = _sd.GetRegionsDll().ToList();

            if (ModelState.IsValid)
            {
                var farmData = _ud.FarmDetails();

                fvm.regOptions = _sd.GetRegionsDll().ToList();

                farmData.year       = fvm.year;
                farmData.farmName   = fvm.farmName;
                farmData.farmRegion = fvm.selRegOption;

                _ud.UpdateFarmDetails(farmData);
                HttpContext.Session.SetObject("Farm", _ud.FarmDetails().farmName + " " + _ud.FarmDetails().year);

                fvm.currYear = fvm.year;
                ModelState.Remove("userData");

                return(RedirectToAction("Fields", "Fields"));
            }
            else
            {
                return(View(fvm));
            }
        }
Exemplo n.º 3
0
        public ActionResult Create(FarmViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var newFarm = new Farm
                    {
                        ApplicationUserId = model.ApplicationUserId,
                        FarmName          = model.FarmName,
                        EmailAddress      = model.EmailAddress,
                        PhotoPath         = "fm.jpg",
                        Size        = model.Size,
                        IsActive    = true,
                        DateCreated = DateTime.Now,
                        IsVerified  = false
                    };

                    newFarm = FarmService.Create(newFarm);

                    if (newFarm != null)
                    {
                        SaveCropFarm(newFarm);
                    }
                    return(RedirectToAction("AddFarmAddress", "FarmAddress", new { farmId = newFarm.Id }));
                }
                catch
                {
                    return(View());
                }
            }
            return(View());
        }
Exemplo n.º 4
0
        public HomeViewModel(User pUser, List <FarmViewModel> pFarmViewModelList,
                             DateTime pDateOfReference,
                             FarmViewModel pDefaultFarmViewModel,
                             String pDefaultFarmLatitude, String pDefaultFarmLongitude,
                             List <CropIrrigationWeather> pCropIrrigationWeatherViewModelList,
                             List <DailyRecordViewModel> pDailyRecordViewModelList,
                             List <RainViewModel> pRainViewModelList,
                             List <IrrigationViewModel> pIrrigationViewModelList,
                             DateTime pMinDateOfReference,
                             DateTime pMaxDateOfReference)
        {
            FarmViewModelList    = pFarmViewModelList;
            DateOfReference      = pDateOfReference;
            DefaultFarmViewModel = pDefaultFarmViewModel;
            DefaultFarmLatitude  = pDefaultFarmLatitude;
            DefaultFarmLongitude = pDefaultFarmLongitude;

            IrrigationUnitViewModelList = DefaultFarmViewModel.IrrigationUnitViewModelList;

            TestIrrigationUnitViewModel        = IrrigationUnitViewModelList.FirstOrDefault();
            CropIrrigationWeatherViewModelList = GetCropIrrigationWeatherViewModelListBy(pCropIrrigationWeatherViewModelList);

            RainViewModelList       = pRainViewModelList;
            IrrigationViewModelList = pIrrigationViewModelList;

            DailyRecordViewModelList = pDailyRecordViewModelList;

            MinDateOfReference = pMinDateOfReference;
            MaxDateOfReference = pMaxDateOfReference;
        }
Exemplo n.º 5
0
        public OperationResult <AuthViewModel> Get(string userName, string password)
        {
            OperationResult <AuthViewModel> result = new OperationResult <AuthViewModel>();

            try
            {
                IrrigationAdvisorWebApiEntities1 context = new IrrigationAdvisorWebApiEntities1();

                AuthViewModel vw = new AuthViewModel()
                {
                    Token = "ffdvskdidmf5656483"
                };

                Authentication auth = new Authentication(userName, password);

                if (auth.IsAuthenticated())
                {
                    string cleanUserName = userName.ToLower().Trim();
                    var    user          = context.Users.Where(n => n.UserName == cleanUserName).Single();

                    var farms = from uf in context.UserFarms
                                join iu in context.IrrigationUnits
                                on uf.FarmId equals iu.FarmId
                                join f in context.Farms
                                on iu.FarmId equals f.FarmId
                                join ciw in context.CropIrrigationWeathers
                                on iu.IrrigationUnitId equals ciw.IrrigationUnitId
                                where uf.UserId == user.UserId &&
                                iu.Show && ciw.SowingDate <= DateTime.Now && f.IsActive &&
                                ciw.HarvestDate >= DateTime.Now
                                select uf;

                    foreach (var f in farms.Distinct())
                    {
                        FarmViewModel newFarm = new FarmViewModel()
                        {
                            Description = f.Name,
                            FarmId      = f.FarmId
                        };

                        vw.Farms.Add(newFarm);
                    }
                }
                else
                {
                    throw new UnauthorizedAccessException("Usuario o contraseña invalido");
                }


                result.IsOk = true;
                result.Data = vw;
            }
            catch (Exception ex)
            {
                result.IsOk         = false;
                result.ErrorMessage = ex.Message;
            }

            return(result);
        }
Exemplo n.º 6
0
        public async Task <IActionResult> Index()
        {
            await this.heroService.ValidateCurrentHeroLocation(WorkStatus.Farm);

            FarmViewModel viewModel = await this.heroService.GetCurrentHeroViewModel <FarmViewModel>();

            return(this.View(viewModel));
        }
Exemplo n.º 7
0
        public IActionResult Manure()
        {
            var farmData = HttpContext.Session.GetObjectFromJson <FarmData>("FarmData");

            FarmViewModel fvm = new FarmViewModel();

            return(View(fvm));
        }
Exemplo n.º 8
0
        public ActionResult DeleteFarm(FarmViewModel model)
        {
            var deleteFarm = FarmService.GetById(model.Id);

            deleteFarm.IsActive = false;

            FarmService.Update(deleteFarm);

            return(RedirectToAction("Index"));
        }
Exemplo n.º 9
0
        public IActionResult Farm()
        {
            var           farmData = _ud.FarmDetails();
            FarmViewModel fvm      = new FarmViewModel();

            fvm.IsLegacyNMPReleaseVersion = !_ud.FarmData().NMPReleaseVersion.HasValue || _ud.FarmData().NMPReleaseVersion.Value < 2;

            if (fvm.IsLegacyNMPReleaseVersion)
            {
                fvm.LegacyNMPMessage = _sd.GetUserPrompt("FarmDataBackwardsCompatibility");
            }
            fvm.ShowSubRegion = false;

            fvm.RegOptions   = _sd.GetRegionsDll().ToList();
            fvm.SelRegOption = null;

            fvm.Year        = farmData.Year;
            fvm.CurrentYear = farmData.Year;
            fvm.FarmName    = farmData.FarmName;

            fvm.SelRegOption    = farmData.FarmRegion;
            fvm.SelSubRegOption = farmData.FarmSubRegion;

            if (farmData.HasAnimals &&
                (farmData.HasBeefCows || farmData.HasDairyCows || farmData.HasMixedLiveStock))
            {
                fvm = SetSubRegions(fvm);
            }

            if (fvm.IsLegacyNMPReleaseVersion && fvm.SelSubRegOption.HasValue && farmData.HasAnimals)
            {
                _ud.UpdateFarmDetailsSubRegion(fvm.SelSubRegOption.Value);
            }
            else
            {
                _ud.SetLegacyDataToUnsaved();
            }

            fvm.HasAnimals        = farmData.HasAnimals;
            fvm.HasDairyCows      = farmData.HasDairyCows;
            fvm.HasBeefCows       = farmData.HasBeefCows;
            fvm.HasPoultry        = farmData.HasPoultry;
            fvm.HasMixedLiveStock = farmData.HasMixedLiveStock;

            if (fvm.HasAnimals)
            {
                fvm.ShowAnimals = true;
            }
            else
            {
                fvm.ShowAnimals = false;
            }

            return(View(fvm));
        }
Exemplo n.º 10
0
        public ActionResult Edit(int id)
        {
            FarmViewModel viewModel = service.GetEditFarm(id);

            if (viewModel == null)
            {
                return(HttpNotFound());
            }

            return(View(viewModel));
        }
Exemplo n.º 11
0
        private bool HasAnimalSelectionChanged(FarmViewModel fvm, FarmDetails farmData)
        {
            var animalSelectionsUnchanged = (
                (farmData.HasAnimals == fvm.OriginalHasAnimals) &&
                (farmData.HasBeefCows == fvm.OriginalHasBeefCows) &&
                (farmData.HasDairyCows == fvm.OriginalHasDairyCows) &&
                (farmData.HasMixedLiveStock == fvm.OriginalHasMixedLiveStock) &&
                (farmData.HasPoultry == fvm.OriginalHasPoultry)
                );

            return(!animalSelectionsUnchanged);
        }
Exemplo n.º 12
0
        public FarmViewModel GetDeleteFarm(int?id)
        {
            Farm farm = this.Data.Farms.Find(id);

            if (farm == null || farm.IsDeleted == true)
            {
                return(null);
            }

            FarmViewModel viewModel = Mapper.Instance.Map <Farm, FarmViewModel>(farm);

            return(viewModel);
        }
Exemplo n.º 13
0
        // GET: Farm/Create
        public ActionResult Create()
        {
            var             userId       = User.Identity.GetUserId();
            ApplicationUser thisUser     = db.Users.FirstOrDefault(x => x.Id == userId);
            var             thisUserFarm = FarmService.Get().Where(x => x.ApplicationUserId == userId).ToList();

            var model = new FarmViewModel
            {
                ApplicationUserId = userId,
                EmailAddress      = thisUser.Email
            };

            return(View(model));
        }
Exemplo n.º 14
0
        public OperationResult <AuthViewModel> Get(string userName, string password)
        {
            OperationResult <AuthViewModel> result = new OperationResult <AuthViewModel>();

            try
            {
                IrrigationAdvisorWebApiEntities1 context = new IrrigationAdvisorWebApiEntities1();

                AuthViewModel vw = new AuthViewModel()
                {
                    Token = "ffdvskdidmf5656483"
                };

                Authentication auth = new Authentication(userName, password);

                if (auth.IsAuthenticated())
                {
                    string cleanUserName = userName.ToLower().Trim();
                    var    user          = context.Users.Where(n => n.UserName == cleanUserName).Single();

                    var farms = context.UserFarms.Where(n => n.UserId == user.UserId).ToList();

                    foreach (var f in farms)
                    {
                        FarmViewModel newFarm = new FarmViewModel()
                        {
                            Description = f.Name,
                            FarmId      = f.FarmId
                        };

                        vw.Farms.Add(newFarm);
                    }
                }
                else
                {
                    throw new UnauthorizedAccessException("Usuario o contraseña invalido");
                }


                result.IsOk = true;
                result.Data = vw;
            }
            catch (Exception ex)
            {
                result.IsOk         = false;
                result.ErrorMessage = ex.Message;
            }

            return(result);
        }
Exemplo n.º 15
0
        // GET: Farm


        public ActionResult Index()
        {
            FarmViewModel model;

            if (User.IsInRole("Admin"))
            {
                model = new FarmViewModel
                {
                    MyFarm = FarmService.Get().ToList(),

                    FarmCrop = new FarmCropViewModel
                    {
                        CropListItem = GetCropList()
                    },
                    FarmLivestock = new FarmLivestockViewModel
                    {
                        LivestockListItem    = LivestockService.Get().ToList(),
                        AnimalGenderDropDown = GetAnimalGender(null)
                    }
                };
            }
            else
            {
                string userId = User.Identity.GetUserId();


                var thisUserFarm = FarmService.Get().Where(x => x.ApplicationUserId == userId && x.IsActive).ToList();

                model = new FarmViewModel
                {
                    ThisUser = db.Users.FirstOrDefault(x => x.Id == userId),
                    MyFarm   = thisUserFarm,
                    FarmCrop = new FarmCropViewModel
                    {
                        CropListItem        = GetCropList(),
                        CropDropDown        = GetCropEmpty(null),
                        CropVarietyDropDown = GetCropVarietyEmpty(null),
                        CropTypeDropDown    = GetCropType(null)
                    },
                    FarmLivestock = new FarmLivestockViewModel
                    {
                        AnimalGenderDropDown  = GetAnimalGender(null),
                        LivestockTypeDropDown = GetLivestockType(null),
                        LivestockDropDown     = GetLivestockEmpty(null)
                    }
                };
            }
            return(View(model));
        }
Exemplo n.º 16
0
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            FarmViewModel viewModel = service.GetDeleteFarm(id);

            if (viewModel == null)
            {
                return(HttpNotFound());
            }

            return(View(viewModel));
        }
Exemplo n.º 17
0
        public IActionResult Farm()
        {
            var farmData = _ud.FarmDetails();

            FarmViewModel fvm = new FarmViewModel();

            fvm.regOptions   = _sd.GetRegionsDll().ToList();
            fvm.selRegOption = null;

            fvm.year     = farmData.year;
            fvm.currYear = farmData.year;
            fvm.farmName = farmData.farmName;

            fvm.selRegOption = farmData.farmRegion;

            return(View(fvm));
        }
Exemplo n.º 18
0
        public IActionResult Farm()
        {
            var           farmData = _ud.FarmDetails();
            FarmViewModel fvm      = new FarmViewModel();

            fvm.IsLegacyNMPReleaseVersion = !_ud.FarmData().NMPReleaseVersion.HasValue || _ud.FarmData().NMPReleaseVersion.Value < _appSettings.Value.NMPReleaseVersion;

            if (fvm.IsLegacyNMPReleaseVersion)
            {
                fvm.LegacyNMPMessage = _sd.GetUserPrompt("FarmDataBackwardsCompatibility");
            }
            fvm.showSubRegion     = false;
            fvm.multipleSubRegion = false;

            fvm.regOptions   = _sd.GetRegionsDll().ToList();
            fvm.selRegOption = null;

            fvm.year     = farmData.year;
            fvm.currYear = farmData.year;
            fvm.farmName = farmData.farmName;

            fvm.selRegOption    = farmData.farmRegion;
            fvm.selSubRegOption = farmData.farmSubRegion;

            if (fvm.selRegOption.HasValue)
            {
                fvm = SetSubRegions(fvm);
                if (fvm.IsLegacyNMPReleaseVersion && fvm.selSubRegOption.HasValue)
                {
                    _ud.UpdateFarmDetailsSubRegion(fvm.selSubRegOption.Value);
                }
                else
                {
                    _ud.SetLegacyDataToUnsaved();
                }
            }

            fvm.HasAnimals           = farmData.HasAnimals;
            fvm.ImportsManureCompost = farmData.ImportsManureCompost;
            fvm.UsesFertilizer       = farmData.UsesFertilizer;

            return(View(fvm));
        }
Exemplo n.º 19
0
        // GET: Farms/Details/5
        public ActionResult Details(long?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Farm farm = db.Farms.Find(id);

            if (farm == null)
            {
                return(HttpNotFound());
            }
            FarmViewModel vm = new FarmViewModel(farm);

            vm.Cities          = this.LoadCity(farm.CityId, farm);
            vm.Countries       = this.LoadCountry();
            vm.WeatherStations = this.LoadWeatherStation(farm.WeatherStationId, farm);

            return(View("~/Views/Localization/Farms/Details.cshtml", vm));
        }
Exemplo n.º 20
0
        private FarmViewModel SetSubRegions(FarmViewModel fvm)
        {
            fvm.subRegionOptions = _sd.GetSubRegionsDll(fvm.selRegOption);
            if (fvm.subRegionOptions.Count == 1)
            {
                fvm.selSubRegOption   = fvm.selSubRegOption ?? fvm.subRegionOptions[0].Id;
                fvm.showSubRegion     = false;
                fvm.multipleSubRegion = false;
            }
            else if (fvm.subRegionOptions.Count > 1)
            {
                fvm.showSubRegion     = true;
                fvm.multipleSubRegion = true;
                if (fvm.selSubRegOption == null)
                {
                    ModelState.AddModelError("", "Select a sub region");
                }
            }

            return(fvm);
        }
Exemplo n.º 21
0
        public ActionResult EditFarm(FarmViewModel model)
        {
            if (ModelState.IsValid)
            {
                // TODO: Add update logic here

                var farm = FarmService.GetById(model.Id);

                farm.FarmName   = model.FarmName;
                farm.IsVerified = model.IsVerified;
                farm.PhotoPath  = model.PhotoPath;
                farm.IsActive   = farm.IsActive;

                FarmService.Update(farm);

                SaveCropFarm(farm);

                return(RedirectToAction("Index"));
            }
            return(View(model));
        }
Exemplo n.º 22
0
        // GET: Farm/Edit/5
        public ActionResult EditFarm(int?farmId)
        {
            FarmViewModel model  = null;
            var           config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <Farm, FarmViewModel>()
                .ForMember(x => x.FarmCrop, opt => opt.Ignore())
                .ForMember(x => x.FarmLivestock, opt => opt.Ignore())
                .ForMember(x => x.PersonFarm, opt => opt.Ignore())
                .ForMember(x => x.ThisUser, opt => opt.Ignore())
                .ForMember(x => x.MyFarm, opt => opt.Ignore());
            });

            IMapper iMapper = config.CreateMapper();

            Farm updateFarm = FarmService.GetById(farmId.Value);

            model = iMapper.Map <Farm, FarmViewModel>(updateFarm);
            model.FarmSizeUnitDropDown = GetFarmSizeUnit(updateFarm.FarmSideUnitId.HasValue == true ? updateFarm.FarmSideUnitId.Value : 1);

            return(View(model));
        }
Exemplo n.º 23
0
 public IActionResult SoilTests(FarmViewModel fvm)
 {
     return(View(fvm));
 }
Exemplo n.º 24
0
 public IActionResult Manure(FarmViewModel fvm)
 {
     return(View(fvm));
 }
Exemplo n.º 25
0
        public IActionResult Farm(FarmViewModel fvm)
        {
            fvm.RegOptions = _sd.GetRegionsDll().ToList();

            if (fvm.ButtonPressed == "GetHasTestResultsChange")
            {
                ModelState.Clear();
                fvm.ButtonPressed = "";
                if (fvm.HasAnimals)
                {
                    fvm.ShowAnimals = true;
                    fvm             = SetSubRegions(fvm);
                }
                else
                {
                    fvm.ShowAnimals = false;
                }

                return(View(fvm));
            }

            if (fvm.ButtonPressed == "GetsAnimalsChange")
            {
                ModelState.Clear();
                fvm.ButtonPressed = "";

                if (fvm.HasAnimals)
                {
                    fvm.ShowAnimals = true;
                    fvm             = SetSubRegions(fvm);
                }
                else
                {
                    fvm.ShowAnimals = false;
                }

                var farmData = _ud.FarmDetails();


                farmData.HasSelectedFarmType = fvm.HasSelectedFarmType;
                farmData.HasAnimals          = fvm.HasAnimals;
                farmData.HasDairyCows        = fvm.HasDairyCows;
                farmData.HasBeefCows         = fvm.HasBeefCows;
                farmData.HasPoultry          = fvm.HasPoultry;
                farmData.HasMixedLiveStock   = fvm.HasMixedLiveStock;

                _ud.UpdateFarmDetails(farmData);

                if (!HasAnimalSelectionChanged(fvm, farmData))
                {
                    fvm.TypeChangeDetected = false;
                }
                else
                {
                    var impactsData = false;

                    if (_ud.GetAnimals().Count != 0)
                    {
                        impactsData = true;
                    }

                    if (_ud.GetAllManagedManures().Count != 0)
                    {
                        impactsData = true;
                    }

                    if (_ud.GetFarmManures().Count != 0)
                    {
                        impactsData = true;
                    }

                    if (_ud.GetStorageSystems().Count != 0)
                    {
                        impactsData = true;
                    }

                    if (_ud.GetSeparatedManures().Count != 0)
                    {
                        impactsData = true;
                    }

                    if (_ud.GetFields().Count != 0)
                    {
                        impactsData = true;
                    }

                    fvm.TypeChangeDetected = impactsData;
                }

                return(View(fvm));
            }

            if (fvm.ButtonPressed == "RegionChange")
            {
                ModelState.Clear();
                fvm.ButtonPressed = "";
                if (fvm.ShowAnimals)
                {
                    fvm = SetSubRegions(fvm);
                }

                var farmData = _ud.FarmDetails();
                farmData.FarmRegion = fvm.SelRegOption;
                _ud.UpdateFarmDetails(farmData);

                if (fvm.ShowAnimals)
                {
                    return(View(fvm));
                }

                return(View(fvm));
            }

            if (fvm.ButtonPressed == "SubRegionChange")
            {
                ModelState.Clear();
                fvm.ButtonPressed = "";
                fvm = SetSubRegions(fvm);

                UpdateStorageSystemsForSubRegion(fvm.SelSubRegOption.Value);

                var farmData = _ud.FarmDetails();
                farmData.FarmSubRegion = fvm.SelSubRegOption;
                _ud.UpdateFarmDetails(farmData);

                return(View(fvm));
            }

            if (fvm.SelRegOption == 0)
            {
                ModelState.AddModelError("SelRegOption", "Select a region");
                return(View(fvm));
            }

            if (fvm.ShowAnimals &&
                (fvm.HasBeefCows || fvm.HasDairyCows || fvm.HasMixedLiveStock))
            {
                fvm = SetSubRegions(fvm);
                if ((fvm.SelSubRegOption == null || fvm.SelSubRegOption == 0))
                {
                    ModelState.AddModelError("SelSubRegOption", "Select a sub region");
                    return(View(fvm));
                }
            }

            {
                var farmData = _ud.FarmDetails();

                if (HasAnimalSelectionChanged(fvm, farmData))
                {
                    // something changed. verify user has confirmed.
                    if (!fvm.TypeChangeConfirmed)
                    {
                        ModelState.AddModelError("TypeChangeConfirmed", "Confirmation is required to proceed with this change");
                        return(View(fvm));
                    }
                }
            }



            if (ModelState.IsValid)
            {
                fvm.HasSelectedFarmType = true;
                var farmData = _ud.FarmDetails();

                fvm.RegOptions = _sd.GetRegionsDll().ToList();

                farmData.Year       = fvm.Year;
                farmData.FarmName   = fvm.FarmName;
                farmData.FarmRegion = fvm.SelRegOption;
                if (fvm.ShowAnimals)
                {
                    fvm.SubRegionOptions = _sd.GetSubRegionsDll(fvm.SelRegOption);
                    if (fvm.SubRegionOptions.Count == 1)
                    {
                        fvm.SelSubRegOption = fvm.SubRegionOptions[0].Id;
                    }
                    farmData.FarmSubRegion = fvm.SelSubRegOption;
                }

                if (HasAnimalSelectionChanged(fvm, farmData) && fvm.TypeChangeConfirmed)
                {
                    _ud.ClearYearlyData();
                }

                farmData.HasSelectedFarmType = fvm.HasSelectedFarmType;
                farmData.HasAnimals          = fvm.HasAnimals;
                farmData.HasDairyCows        = fvm.HasDairyCows;
                farmData.HasBeefCows         = fvm.HasBeefCows;
                farmData.HasPoultry          = fvm.HasPoultry;
                farmData.HasMixedLiveStock   = fvm.HasMixedLiveStock;

                _ud.UpdateFarmDetails(farmData);
                HttpContext.Session.SetObject("Farm", _ud.FarmDetails().FarmName + " " + _ud.FarmDetails().Year);

                fvm.CurrentYear = fvm.Year;
                ModelState.Remove("userData");

                //Navigate to next item past Farm
                var journey           = _ud.FarmDetails().UserJourney;
                var initialNavigation = _sd.GetJourney((int)journey)
                                        .MainMenus
                                        .Single(m => m.SortNumber == 2);

                if (fvm.HasTestResults)
                {
                    // override initial navigation if there are new soil test results
                    var soilTestsNavigation = _sd.GetJourney((int)journey).MainMenus.Single(m => m.Name == "Fields and Soil").SubMenus.Single(s => s.Name == "Soil Tests");
                    return(RedirectToAction(soilTestsNavigation.Action, soilTestsNavigation.Controller));
                }

                if (initialNavigation.UsesFeaturePages)
                {
                    return(RedirectToPage(initialNavigation.Page));
                }
                else
                {
                    return(RedirectToAction(initialNavigation.Action, initialNavigation.Controller));
                }
            }
            else
            {
                if (fvm.ShowAnimals)
                {
                    fvm.SubRegionOptions = _sd.GetSubRegionsDll(fvm.SelRegOption);
                }
                return(View(fvm));
            }
        }
Exemplo n.º 26
0
        /// <summary>
        /// Return Grid Irrigation Unit for Home
        /// </summary>
        /// <returns></returns>
        public List <GridPivotHome> GetGridPivotHome()
        {
            #region Local Variables
            String lSomeData = "";
            List <GridPivotHome>       lGridIrrigationUnitList = new List <GridPivotHome>();
            GridPivotHome              lGridIrrigationUnit;
            List <GridPivotDetailHome> lGridIrrigationUnitDetailRow;
            GridPivotDetailHome        lGridIrrigationUnitRow;
            DateTime      lDateOfReference;
            FarmViewModel lFarmViewModel;
            User          lLoggedUser;
            List <Farm>   lFarmList;
            // Farm lCurrentFarm;
            List <IrrigationUnit>        lIrrigationUnitList;
            List <CropIrrigationWeather> lCropIrrigationWeatherList;
            String      lFirstPivotName = "";
            List <Rain> lRainList;
            List <Models.Water.Irrigation> lIrrigationList;
            List <DailyRecord>             lDailyRecordList;
            String        lSowingDate;
            String        lPhenologicalStageToday;
            Double        lHydricBalancePercentage;
            DailyRecord   lDailyRecord;
            String        lCropCoefficient;
            List <Double> lETcList;
            Double        lETcItem;
            HomeViewModel lHomeViewModel;
            #endregion

            #region Configuration Variables
            UserConfiguration                  lUserConfiguration;
            FarmConfiguration                  lFarmConfiguration;
            IrrigationUnitConfiguration        lIrrigationUnitConfiguration;
            CropIrrigationWeatherConfiguration lCropIrrigationWeatherConfiguration;
            IrrigationAdvisorContext           lIrrigationAdvisorContext;
            #endregion

            try
            {
                #region Configuration - Instance
                lUserConfiguration                  = new UserConfiguration();
                lFarmConfiguration                  = new FarmConfiguration();
                lIrrigationUnitConfiguration        = new IrrigationUnitConfiguration();
                lCropIrrigationWeatherConfiguration = new CropIrrigationWeatherConfiguration();
                lIrrigationAdvisorContext           = IrrigationAdvisorContext.Instance();
                #endregion

                lDateOfReference = ManageSession.GetNavigationDate();
                lSomeData        = lSomeData + "Date: " + lDateOfReference.Date + "-";

                //Obtain logged user
                lLoggedUser = lUserConfiguration.GetUserByName(ManageSession.GetUserName());
                lSomeData   = lSomeData + "User: "******"-";

                //Get list of Farms from User
                lFarmList = lFarmConfiguration.GetFarmWithActiveCropIrrigationWeathersListBy(lLoggedUser);

                //Create IrrigationQuantity Units List
                lIrrigationUnitList = new List <IrrigationUnit>();
                foreach (Farm lCurrentFarm in lFarmList)
                {
                    lSomeData      = lSomeData + "Farm: " + lCurrentFarm.Name + "-";
                    lFarmViewModel = new FarmViewModel(lCurrentFarm);

                    lIrrigationUnitList = lIrrigationUnitConfiguration.GetIrrigationUnitListBy(lCurrentFarm);

                    lCropIrrigationWeatherList = new List <CropIrrigationWeather>();
                    lDailyRecordList           = new List <DailyRecord>();

                    foreach (var lIrrigationUnit in lIrrigationUnitList)
                    {
                        lCropIrrigationWeatherList = lIrrigationUnitConfiguration.GetCropIrrigationWeatherListIncludeCropMainWeatherStationRainListIrrigationListBy(lIrrigationUnit, lDateOfReference);

                        lFirstPivotName = "";
                        lETcList        = new List <Double>();
                        foreach (CropIrrigationWeather lCropIrrigationWeather in lCropIrrigationWeatherList)
                        {
                            lSomeData = lSomeData + "CropIrrigationWeather: " + lCropIrrigationWeather.CropIrrigationWeatherName + "-";

                            lDailyRecordList = lCropIrrigationWeatherConfiguration.GetDailyRecordListIncludeDailyRecordListBy(lIrrigationUnit, lDateOfReference, lCropIrrigationWeather.Crop);

                            lRainList       = lCropIrrigationWeather.RainList;
                            lIrrigationList = lCropIrrigationWeather.IrrigationList;
                            lSowingDate     = lCropIrrigationWeather.SowingDate.Day.ToString()
                                              + "/" + lCropIrrigationWeather.SowingDate.Month.ToString();
                            //Grid of irrigation data
                            lGridIrrigationUnitDetailRow = new List <GridPivotDetailHome>();

                            lCropCoefficient         = String.Empty;
                            lPhenologicalStageToday  = String.Empty;
                            lHydricBalancePercentage = 0;

                            for (int i = -InitialTables.MIN_DAY_SHOW_IN_GRID_BEFORE_TODAY; i <= InitialTables.MAX_DAY_SHOW_IN_GRID_AFTER_TODAY; i++)
                            {
                                //Day i
                                lGridIrrigationUnitRow = AddGridIrrigationUnit(lDateOfReference, lDateOfReference.AddDays(i), lIrrigationList, lRainList, lDailyRecordList);
                                lGridIrrigationUnitDetailRow.Add(lGridIrrigationUnitRow);
                                if (i == 0) //TODAY
                                {
                                    //Obtain All data for today from DailyRecord
                                    lPhenologicalStageToday = lGridIrrigationUnitRow.Phenology;
                                    lDailyRecord            = lGridIrrigationUnitRow.DailyRecord;
                                    if (lDailyRecord != null)
                                    {
                                        lCropCoefficient         = lDailyRecord.CropCoefficient.ToString();
                                        lPhenologicalStageToday  = lDailyRecord.PhenologicalStage.Stage.ShortName;
                                        lHydricBalancePercentage = lDailyRecord.PercentageOfHydricBalance;
                                    }
                                }
                                if (lGridIrrigationUnitRow.DailyRecord == null)
                                {
                                    if (lCropIrrigationWeather.MainWeatherStationId > 0)
                                    {
                                        lETcItem = lIrrigationAdvisorContext.WeatherDatas
                                                   .Where(wd => wd.Date == lGridIrrigationUnitRow.DateOfData.Date &&
                                                          wd.WeatherStationId == lCropIrrigationWeather.MainWeatherStationId)
                                                   .Select(wd => wd.Evapotranspiration).FirstOrDefault();
                                    }
                                    else
                                    {
                                        lETcItem = lIrrigationAdvisorContext.WeatherDatas
                                                   .Where(wd => wd.Date == lGridIrrigationUnitRow.DateOfData.Date &&
                                                          wd.WeatherStationId == lCropIrrigationWeather.AlternativeWeatherStationId)
                                                   .Select(wd => wd.Evapotranspiration).FirstOrDefault();
                                    }
                                }
                                else
                                {
                                    lETcItem = Math.Round(lGridIrrigationUnitRow.DailyRecord.EvapotranspirationCrop.Output / lGridIrrigationUnitRow.DailyRecord.CropCoefficient, 2);
                                }
                                lETcList.Add(Math.Round(lETcItem, 2));
                            }
                            if (String.IsNullOrEmpty(lFirstPivotName))
                            {
                                lFirstPivotName = lCropIrrigationWeather.IrrigationUnit.ShortName;
                            }
                            else if (lFirstPivotName == lCropIrrigationWeather.IrrigationUnit.ShortName)
                            {
                                lFirstPivotName = "";
                            }

                            lHomeViewModel = ManageSession.GetHomeViewModel();

                            //Add all the days for the IrrigationUnit
                            lGridIrrigationUnit = new GridPivotHome(lCurrentFarm.Name + " | " + lFirstPivotName,
                                                                    lCropIrrigationWeather.Crop.ShortName,
                                                                    lSowingDate,
                                                                    lPhenologicalStageToday,
                                                                    lHydricBalancePercentage.ToString() + " %",
                                                                    lCropCoefficient,
                                                                    lHomeViewModel.IsUserAdministrator,
                                                                    lETcList,
                                                                    lGridIrrigationUnitDetailRow,
                                                                    lCropIrrigationWeather.CropIrrigationWeatherId);

                            lGridIrrigationUnitList.Add(lGridIrrigationUnit);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Utils.LogError(ex, "Exception in HomeController.GetGridPivotHome \n {0} ", lSomeData);
                throw ex;
            }

            return(lGridIrrigationUnitList);
        }
Exemplo n.º 27
0
        public IActionResult Farm(FarmViewModel fvm)
        {
            fvm.RegOptions = _sd.GetRegionsDll().ToList();

            if (fvm.ButtonPressed == "GetsAnimalsChange")
            {
                ModelState.Clear();
                fvm.ButtonPressed = "";

                if (fvm.HasAnimals)
                {
                    fvm.ShowAnimals = true;
                    fvm             = SetSubRegions(fvm);
                }
                else
                {
                    fvm.ShowAnimals = false;
                }

                var farmData = _ud.FarmDetails();
                farmData.HasSelectedFarmType = fvm.HasSelectedFarmType;
                farmData.HasAnimals          = fvm.HasAnimals;
                farmData.HasDairyCows        = fvm.HasDairyCows;
                farmData.HasBeefCows         = fvm.HasBeefCows;
                farmData.HasPoultry          = fvm.HasPoultry;
                farmData.HasMixedLiveStock   = fvm.HasMixedLiveStock;
                _ud.UpdateFarmDetails(farmData);

                return(View(fvm));
            }

            if (fvm.ButtonPressed == "RegionChange")
            {
                ModelState.Clear();
                fvm.ButtonPressed = "";
                if (fvm.ShowAnimals)
                {
                    fvm = SetSubRegions(fvm);
                }

                var farmData = _ud.FarmDetails();
                farmData.FarmRegion = fvm.SelRegOption;
                _ud.UpdateFarmDetails(farmData);

                if (fvm.ShowAnimals)
                {
                    return(View(fvm));
                }

                return(View(fvm));
            }

            if (fvm.ButtonPressed == "SubRegionChange")
            {
                ModelState.Clear();
                fvm.ButtonPressed = "";
                fvm = SetSubRegions(fvm);

                UpdateStorageSystemsForSubRegion(fvm.SelSubRegOption.Value);

                var farmData = _ud.FarmDetails();
                farmData.FarmSubRegion = fvm.SelSubRegOption;
                _ud.UpdateFarmDetails(farmData);

                return(View(fvm));
            }

            if (fvm.SelRegOption == 0)
            {
                ModelState.AddModelError("SelRegOption", "Select a region");
                return(View(fvm));
            }

            if (fvm.ShowAnimals &&
                (fvm.HasBeefCows || fvm.HasDairyCows || fvm.HasMixedLiveStock) &&
                (fvm.SelSubRegOption == null || fvm.SelSubRegOption == 0))
            {
                ModelState.AddModelError("SelSubRegOption", "Select a sub region");
                fvm = SetSubRegions(fvm);
                return(View(fvm));
            }

            if (ModelState.IsValid)
            {
                fvm.HasSelectedFarmType = true;
                var farmData = _ud.FarmDetails();

                fvm.RegOptions = _sd.GetRegionsDll().ToList();

                farmData.Year       = fvm.Year;
                farmData.FarmName   = fvm.FarmName;
                farmData.FarmRegion = fvm.SelRegOption;
                if (fvm.ShowAnimals)
                {
                    fvm.SubRegionOptions = _sd.GetSubRegionsDll(fvm.SelRegOption);
                    if (fvm.SubRegionOptions.Count == 1)
                    {
                        fvm.SelSubRegOption = fvm.SubRegionOptions[0].Id;
                    }
                    farmData.FarmSubRegion = fvm.SelSubRegOption;
                }

                farmData.HasSelectedFarmType = fvm.HasSelectedFarmType;
                farmData.HasAnimals          = fvm.HasAnimals;
                farmData.HasDairyCows        = fvm.HasDairyCows;
                farmData.HasBeefCows         = fvm.HasBeefCows;
                farmData.HasPoultry          = fvm.HasPoultry;
                farmData.HasMixedLiveStock   = fvm.HasMixedLiveStock;

                _ud.UpdateFarmDetails(farmData);
                HttpContext.Session.SetObject("Farm", _ud.FarmDetails().FarmName + " " + _ud.FarmDetails().Year);

                fvm.CurrentYear = fvm.Year;
                ModelState.Remove("userData");

                //Navigate to next item past Farm
                var journey           = _ud.FarmDetails().UserJourney;
                var initialNavigation = _sd.GetJourney((int)journey)
                                        .MainMenus
                                        .Single(m => m.SortNumber == 2);

                if (initialNavigation.UsesFeaturePages)
                {
                    return(RedirectToPage(initialNavigation.Page));
                }
                else
                {
                    return(RedirectToAction(initialNavigation.Action, initialNavigation.Controller));
                }
            }
            else
            {
                if (fvm.ShowAnimals)
                {
                    fvm.SubRegionOptions = _sd.GetSubRegionsDll(fvm.SelRegOption);
                }
                return(View(fvm));
            }
        }
Exemplo n.º 28
0
        public IActionResult Farm(FarmViewModel fvm)
        {
            fvm.regOptions = _sd.GetRegionsDll().ToList();

            if (fvm.buttonPressed == "RegionChange")
            {
                ModelState.Clear();
                fvm.buttonPressed = "";
                fvm = SetSubRegions(fvm);

                var farmData = _ud.FarmDetails();
                farmData.farmRegion = fvm.selRegOption;
                _ud.UpdateFarmDetails(farmData);

                if (fvm.multipleSubRegion)
                {
                    return(View(fvm));
                }

                fvm.buttonPressed = "SubRegionChange";
            }

            if (fvm.buttonPressed == "SubRegionChange")
            {
                ModelState.Clear();
                fvm.buttonPressed = "";

                if (fvm.multipleSubRegion)
                {
                    fvm = SetSubRegions(fvm);
                }

                UpdateStorageSystemsForSubRegion(fvm.selSubRegOption.Value);

                var farmData = _ud.FarmDetails();
                farmData.farmSubRegion = fvm.selSubRegOption;
                _ud.UpdateFarmDetails(farmData);

                return(View(fvm));
            }

            if (ModelState.IsValid)
            {
                var farmData = _ud.FarmDetails();

                fvm.regOptions = _sd.GetRegionsDll().ToList();

                farmData.year        = fvm.year;
                farmData.farmName    = fvm.farmName;
                farmData.farmRegion  = fvm.selRegOption;
                fvm.subRegionOptions = _sd.GetSubRegionsDll(fvm.selRegOption);
                if (fvm.subRegionOptions.Count == 1)
                {
                    fvm.selSubRegOption = fvm.subRegionOptions[0].Id;
                }
                farmData.farmSubRegion        = fvm.selSubRegOption;
                farmData.HasAnimals           = fvm.HasAnimals;
                farmData.ImportsManureCompost = fvm.ImportsManureCompost;
                farmData.UsesFertilizer       = fvm.UsesFertilizer;

                _ud.UpdateFarmDetails(farmData);
                HttpContext.Session.SetObject("Farm", _ud.FarmDetails().farmName + " " + _ud.FarmDetails().year);

                fvm.currYear = fvm.year;
                ModelState.Remove("userData");

                if (farmData.HasAnimals)
                {
                    return(RedirectToAction("ManureGeneratedObtained", "ManureManagement"));
                }
                else if (farmData.ImportsManureCompost)
                {
                    return(RedirectToAction("ManureImported", "ManureManagement"));
                }
                else
                {
                    return(RedirectToAction("Fields", "Fields"));
                }
            }
            else
            {
                fvm.subRegionOptions = _sd.GetSubRegionsDll(fvm.selRegOption);
                return(View(fvm));
            }
        }
Exemplo n.º 29
0
 private FarmViewModel SetSubRegions(FarmViewModel fvm)
 {
     return(SetSubRegionsForRegion(fvm) as FarmViewModel);
 }
Exemplo n.º 30
0
        public IActionResult Farm(FarmViewModel fvm)
        {
            fvm.regOptions = _sd.GetRegionsDll().ToList();

            if (fvm.buttonPressed == "RegionChange")
            {
                ModelState.Clear();
                fvm.buttonPressed = "";
                fvm = SetSubRegions(fvm);

                var farmData = _ud.FarmDetails();
                farmData.farmRegion = fvm.selRegOption;
                _ud.UpdateFarmDetails(farmData);

                if (fvm.multipleSubRegion)
                {
                    return(View(fvm));
                }

                fvm.buttonPressed = "SubRegionChange";
            }

            if (fvm.buttonPressed == "SubRegionChange")
            {
                ModelState.Clear();
                fvm.buttonPressed = "";

                if (fvm.multipleSubRegion)
                {
                    fvm = SetSubRegions(fvm);
                }

                var     storageSystems      = _ud.GetStorageSystems();
                decimal conversionForLiquid = 0.024542388m;
                decimal conversionForSolid  = 0.000102408m;

                if (storageSystems.Count() > 0)
                {
                    foreach (var s in storageSystems)
                    {
                        if (s.AnnualPrecipitation != null)
                        {
                            SubRegion subregion = _sd.GetSubRegion(fvm.selSubRegOption);
                            s.AnnualPrecipitation = subregion.AnnualPrecipitation;
                            if (s.ManureMaterialType == ManureMaterialType.Liquid)
                            {
                                s.AnnualTotalPrecipitation = Convert.ToDecimal(s.RunoffAreaSquareFeet) +
                                                             Convert.ToDecimal(s.TotalAreaOfUncoveredLiquidStorage) *
                                                             Convert.ToDecimal(s.AnnualPrecipitation) *
                                                             conversionForLiquid;
                            }
                            else if (s.ManureMaterialType == ManureMaterialType.Solid)
                            {
                                s.AnnualTotalPrecipitation = Convert.ToDecimal(s.RunoffAreaSquareFeet) +
                                                             Convert.ToDecimal(s.TotalAreaOfUncoveredLiquidStorage) *
                                                             Convert.ToDecimal(s.AnnualPrecipitation) *
                                                             conversionForSolid;
                            }
                        }

                        _ud.UpdateManureStorageSystem(s);
                    }
                }

                var farmData = _ud.FarmDetails();
                farmData.farmSubRegion = fvm.selSubRegOption;
                _ud.UpdateFarmDetails(farmData);

                return(View(fvm));
            }

            if (ModelState.IsValid)
            {
                var farmData = _ud.FarmDetails();

                fvm.regOptions = _sd.GetRegionsDll().ToList();

                farmData.year        = fvm.year;
                farmData.farmName    = fvm.farmName;
                farmData.farmRegion  = fvm.selRegOption;
                fvm.subRegionOptions = _sd.GetSubRegionsDll(fvm.selRegOption);
                if (fvm.subRegionOptions.Count == 1)
                {
                    fvm.selSubRegOption = fvm.subRegionOptions[0].Id;
                }
                farmData.farmSubRegion        = fvm.selSubRegOption;
                farmData.HasAnimals           = fvm.HasAnimals;
                farmData.ImportsManureCompost = fvm.ImportsManureCompost;
                farmData.UsesFertilizer       = fvm.UsesFertilizer;

                _ud.UpdateFarmDetails(farmData);
                HttpContext.Session.SetObject("Farm", _ud.FarmDetails().farmName + " " + _ud.FarmDetails().year);

                fvm.currYear = fvm.year;
                ModelState.Remove("userData");

                if (farmData.HasAnimals)
                {
                    return(RedirectToAction("ManureGeneratedObtained", "ManureManagement"));
                }
                else if (farmData.ImportsManureCompost)
                {
                    return(RedirectToAction("ManureImported", "ManureManagement"));
                }
                else
                {
                    return(RedirectToAction("Fields", "Fields"));
                }
            }
            else
            {
                fvm.subRegionOptions = _sd.GetSubRegionsDll(fvm.selRegOption);
                return(View(fvm));
            }
        }