/// <summary> /// Add Grid Irrigation Unit with all columns data /// Using DailyRecords for obtain Irrigation, Rain information. /// </summary> /// <param name="pDayOfReference"></param> /// <param name="pDayOfData"></param> /// <param name="pIrrigationList"></param> /// <param name="pRainList"></param> /// <param name="pDailyRecordList"></param> /// <returns></returns> private GridPivotDetailHome AddGridIrrigationUnit(DateTime pDayOfReference, DateTime pDayOfData, List <Models.Water.Irrigation> pIrrigationList, List <Rain> pRainList, List <DailyRecord> pDailyRecordList) { #region local variables String lSomeData = ""; GridPivotDetailHome lReturn = null; Double lIrrigationQuantity = 0; Double lRainQuantity = 0; Double lForcastIrrigationQuantity = 0; Double lWaterQuantity = 0; DateTime lDateOfData = Utils.MIN_DATETIME; DateTime lDayOfReference = Utils.MIN_DATETIME; bool lIsToday = false; Utils.IrrigationStatus lIrrigationStatus = Utils.IrrigationStatus.Default; String lPhenology = ""; Rain lRain = null; DailyRecord lDailyRecord = null; #endregion try { lDateOfData = pDayOfData.Date; lDayOfReference = pDayOfReference.Date; lSomeData = lSomeData + "DayOfReference: " + lDayOfReference.Date + "-"; lSomeData = lSomeData + "DateOfData: " + lDateOfData.Date + "-"; //Find Daily Record of the Date of Data lDailyRecord = pDailyRecordList.Where(dr => dr.DailyRecordDateTime.Date == lDateOfData.Date).FirstOrDefault(); #region Irrigation in the past if (lDailyRecord != null && lDateOfData < lDayOfReference) { if (lDailyRecord.Irrigation != null && lDailyRecord.Irrigation.Input > 0) { lIrrigationQuantity += lDailyRecord.Irrigation.Input; } else if (lDailyRecord.Irrigation != null && lDailyRecord.Irrigation.ExtraInput > 0) { lIrrigationQuantity += lDailyRecord.Irrigation.ExtraInput; } } #endregion lWaterQuantity = lIrrigationQuantity; lSomeData = lSomeData + "IrrigationQuantity: " + lIrrigationQuantity + "-"; #region Rain //Find Rain of the Date of Data lRain = pRainList.Where(r => r.Date.Date == lDateOfData || r.ExtraDate.Date == lDateOfData).FirstOrDefault(); if (lRain != null && lRain.GetTotalInput() > 0) { lRainQuantity = lRain.GetTotalInput(); } #endregion lWaterQuantity += lRainQuantity; lSomeData = lSomeData + "RainQuantity: " + lRainQuantity + "-"; #region Irrigation for today or in the future //Find Daily Record of the Date of Data lDailyRecord = pDailyRecordList.Where(dr => dr.DailyRecordDateTime.Date == lDateOfData).FirstOrDefault(); if (lDailyRecord != null && lDateOfData >= lDayOfReference) { if (lDailyRecord.Irrigation != null && lDailyRecord.Irrigation.Input > 0) { lForcastIrrigationQuantity += lDailyRecord.Irrigation.Input; } else if (lDailyRecord.Irrigation != null && lDailyRecord.Irrigation.ExtraInput > 0) { lForcastIrrigationQuantity += lDailyRecord.Irrigation.ExtraInput; } } #endregion lWaterQuantity += lForcastIrrigationQuantity; lSomeData = lSomeData + "ForcastIrrigationQuantity: " + lForcastIrrigationQuantity + "-"; lIsToday = lDateOfData == lDayOfReference; if (lIsToday && lDailyRecord != null) { lPhenology = lDailyRecord.PhenologicalStage.Stage.ShortName; } #region IrrigationStatus if (lRainQuantity > 0 && lRainQuantity >= (lWaterQuantity - lRainQuantity)) { lIrrigationStatus = Utils.IrrigationStatus.Rain; } else if (lIrrigationQuantity > 0 && lIrrigationQuantity > lRainQuantity) { lIrrigationStatus = Utils.IrrigationStatus.Irrigated; } else if (lForcastIrrigationQuantity > 0 && lForcastIrrigationQuantity > lRainQuantity) { lIrrigationStatus = Utils.IrrigationStatus.NextIrrigation; } else if (lDailyRecord != null && (lDailyRecord.Irrigation != null && lDailyRecord.Irrigation.Type == Utils.WaterInputType.CantIrrigate)) { lIrrigationStatus = Utils.IrrigationStatus.CantIrrigate; } else if (lDailyRecord != null && (lDailyRecord.Irrigation != null && lDailyRecord.Irrigation.Type == Utils.WaterInputType.IrrigationWasNotDecided)) { lIrrigationStatus = Utils.IrrigationStatus.IrrigationWasNotDecided; } else { lIrrigationStatus = Utils.IrrigationStatus.Default; } #endregion lSomeData = lSomeData + "IrrigationStatus: " + lIrrigationStatus.ToString() + "-"; lReturn = new GridPivotDetailHome(lIrrigationQuantity, lRainQuantity, lForcastIrrigationQuantity, lDateOfData, lIsToday, lIrrigationStatus, lPhenology, lDailyRecord); } catch (Exception ex) { Utils.LogError(ex, "Exception in HomeController.AddGridIrrigationUnit \n {0}", lSomeData); throw ex; } return(lReturn); }