public static void RollupPlantAccounting(decimal initialPlantId, decimal finalPlantId) { var entities = new PSsqmEntities(); var plantIds = new HashSet <decimal> { initialPlantId, finalPlantId }; plantIds.Remove(0); // Default to January 1 of current year if web.config value not found DateTime startDate = new DateTime(DateTime.Now.Year, 1, 1); string plantAccountingCalcStartDate = System.Configuration.ConfigurationManager.AppSettings["PlantAccountingCalcStartDate"]; if (!string.IsNullOrEmpty(plantAccountingCalcStartDate)) { DateTime result; if (DateTime.TryParse(plantAccountingCalcStartDate, out result)) { startDate = result; } } // Step 1 - zero out incident plant accounting values or create new records foreach (decimal pid in plantIds) { DateTime incDate = startDate; while (incDate < DateTime.Now || (incDate.Month == DateTime.Now.Month && incDate.Year == DateTime.Now.Year)) { var pa = EHSModel.LookupPlantAccounting(entities, pid, incDate.Year, incDate.Month, true); pa.RECORDED_CASES = 0; pa.TIME_LOST_CASES = 0; pa.TIME_LOST = 0; EHSModel.UpdatePlantAccounting(entities, pa); incDate = incDate.AddMonths(1); } } // Step 2 - update records incident by incident foreach (decimal pid in plantIds) { DateTime incDate = startDate; while (incDate < DateTime.Now || (incDate.Month == DateTime.Now.Month && incDate.Year == DateTime.Now.Year)) { var pa = EHSModel.LookupPlantAccounting(entities, pid, incDate.Year, incDate.Month, true); var incidentList = EHSIncidentMgr.SelectInjuryIllnessIncidents(pid, incDate); // this might be wrong ?? foreach (INCIDENT incident in incidentList) { string recordableAnswerValue = EHSIncidentMgr.SelectIncidentAnswer(incident, (decimal)EHSQuestionId.Recordable); if (!string.IsNullOrEmpty(recordableAnswerValue) && recordableAnswerValue == "Yes") { pa.RECORDED_CASES++; } string ltcAnswerValue = EHSIncidentMgr.SelectIncidentAnswer(incident, (decimal)EHSQuestionId.LostTimeCase); if (!string.IsNullOrEmpty(ltcAnswerValue) && ltcAnswerValue == "Yes") { pa.TIME_LOST_CASES++; } string someReturnDate = ""; // expected or actual return date string erdAnswerValue = EHSIncidentMgr.SelectIncidentAnswer(incident, (decimal)EHSQuestionId.ExpectedReturnDate); if (!string.IsNullOrEmpty(erdAnswerValue)) { someReturnDate = erdAnswerValue; } string ardAnswerValue = EHSIncidentMgr.SelectIncidentAnswer(incident, (decimal)EHSQuestionId.ActualReturnDate); if (!string.IsNullOrEmpty(ardAnswerValue)) { someReturnDate = ardAnswerValue; } if (!string.IsNullOrEmpty(someReturnDate)) { UpdateIncidentLostTimeDays(incident, someReturnDate); } } EHSModel.UpdatePlantAccounting(entities, pa); incDate = incDate.AddMonths(1); } } }