Ejemplo n.º 1
0
        /// <summary>
        /// Allow export and download info abaout CIW
        /// </summary>
        /// <returns>stream to save file</returns>
        public FileStreamResult CreateAndDownloadFileXLS()
        {
            try
            {
                CropIrrigationWeatherConfiguration ciwc = new CropIrrigationWeatherConfiguration();

                string lDate     = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString();
                string lOutPut   = ciwc.GetOutputByCropIrrigationWeatherId(ciwId);
                string lFileName = ciwc.GetNameByCropIrrigationWeatherId(ciwId);

                lFileName = lFileName + "_" + ".csv";

                var byteArray = Encoding.ASCII.GetBytes(lOutPut);
                var stream    = new MemoryStream(byteArray);

                return(File(stream, "application/vnd.ms-excel", lFileName));
            }
            catch (Exception ex)
            {
                Utils.LogError(ex, "Exception in ReportPivotState.CreateAndDownloadFileXLS \n {0} ");
                return(null);
            }
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public ActionResult Index()
        {
            try
            {
                #region userdata
                User lLoggedUser;
                UserConfiguration uc;
                LoginViewModel    lLoginViewModel;
                uc = new UserConfiguration();

                LoginViewModel localLgM = ManageSession.GetLoginViewModel();

                if (localLgM == null)
                {
                    return(RedirectToAction("Index"));
                }
                else
                {
                    lLoginViewModel = ManageSession.GetLoginViewModel();
                }
                lLoggedUser            = uc.GetUserByName(lLoginViewModel.UserName);
                vm.IsUserAdministrator = (lLoggedUser.RoleId == (int)Utils.UserRoles.Administrator);
                #endregion

                DailyRecordConfiguration           drc             = new DailyRecordConfiguration();
                CropIrrigationWeatherConfiguration ciwc            = new CropIrrigationWeatherConfiguration();
                List <DailyRecord> lDailyRecordList                = new List <DailyRecord>();
                double             lHydricBalancePercentage        = 0;
                double             lSumTotalEffectiveRain          = 0;
                double             lSumTotalEffectiveInputWater    = 0;
                double             lSumTotalEvapotranspirationCrop = 0;
                string             lCropIrrigationWeatherTitle     = "";

                ciwId = GetCropIrrigationWeatherIdFromURL();

                lDailyRecordList = drc.GetDailyRecordsListDataUntilDateBy(ciwId, Utils.GetDateOfReference().Value);

                #region get ciw
                //List<CropIrrigationWeather> lCropIrrigationWeatherList = new List<CropIrrigationWeather>();
                //List<long> lListciw = new List<long>();
                //lListciw.Add(ciwId);

                //lCropIrrigationWeatherList = ciwc.GetCropIrrigationWeatherByIds(lListciw, Utils.GetDateOfReference().Value);

                //foreach (CropIrrigationWeather lCropIrrigationWeather in lCropIrrigationWeatherList)
                //{
                //    lHydricBalancePercentage = lCropIrrigationWeather.GetPercentageOfHydricBalance();
                //}
                #endregion


                foreach (DailyRecord item in lDailyRecordList)
                {
                    if (item.Rain != null)
                    {
                        lSumTotalEffectiveRain = lSumTotalEffectiveRain + item.Rain.Input + item.Rain.ExtraInput;
                    }
                    ;

                    if (item.Irrigation != null)
                    {
                        lSumTotalEffectiveInputWater = lSumTotalEffectiveInputWater + item.Irrigation.Input + item.Irrigation.ExtraInput;
                    }

                    lSumTotalEvapotranspirationCrop = item.TotalEvapotranspirationCrop;
                    lHydricBalancePercentage        = item.PercentageOfHydricBalance;
                    lCropIrrigationWeatherTitle     = item.CropIrrigationWeather.ToString();
                }
                vm.TotalEffectiveRain          = lSumTotalEffectiveRain;
                vm.TotalEffectiveInputWater    = lSumTotalEffectiveInputWater;
                vm.TotalEvapotranspirationCrop = lSumTotalEvapotranspirationCrop;
                vm.Title = lCropIrrigationWeatherTitle;
                vm.CropIrrigationWeatherId = ciwId;
                vm.HydricBalancePercentage = lHydricBalancePercentage;
                return(View("~/Views/Reports/ReportPivotState/ReportPivotState.cshtml", vm));
            }
            catch (Exception ex)
            {
                Utils.LogError(ex, "Exception in ReportPivotState.Index \n {0} ");
                return(null);
            }
        }