Example #1
0
        static string ProcessIncidents()
        {
            PSsqmEntities entities          = new PSsqmEntities();
            SETTINGS      setting           = null;
            int           workdays          = 7;
            int           rollupMonthsAhead = 0;
            string        nextStep          = "";
            DateTime      fromDate          = DateTime.UtcNow.AddMonths(-11);  // set the incident 'select from' date.  TODO: get this from SETTINGS table
            // set end date to end of current month to clear spurrious entries ?
            DateTime rollupToDate = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.DaysInMonth(DateTime.UtcNow.Year, DateTime.UtcNow.Month));

            WriteLine("INCIDENT Rollup Started: " + DateTime.UtcNow.ToString("hh:mm MM/dd/yyyy"));

            List <SETTINGS> sets = SQMSettings.SelectSettingsGroup("AUTOMATE", "");            // ABW 20140805

            try
            {
                setting = sets.Where(x => x.SETTING_CD == "ROLLUP_WORKDAYS").FirstOrDefault();
                if (setting != null && !string.IsNullOrEmpty(setting.VALUE))
                {
                    if (!int.TryParse(setting.VALUE, out workdays))
                    {
                        workdays = 7;
                    }
                }

                setting = sets.Where(x => x.SETTING_CD == "ROLLUP_NEXTPAGE").FirstOrDefault();
                if (setting != null && !string.IsNullOrEmpty(setting.VALUE) && setting.VALUE.Length > 1)
                {
                    nextStep = setting.VALUE;
                }

                // fetch all incidents occurring after the minimum reporting date
                List <INCIDENT> incidentList = (from i in entities.INCIDENT.Include("INCFORM_INJURYILLNESS")
                                                where
                                                i.ISSUE_TYPE_ID != (decimal)EHSIncidentTypeId.PreventativeAction &&
                                                i.INCIDENT_DT >= fromDate && i.DETECT_PLANT_ID > 0
                                                select i).OrderBy(l => l.DETECT_PLANT_ID).ThenBy(l => l.INCIDENT_DT).ToList();

                List <PLANT> plantList = SQMModelMgr.SelectPlantList(entities, 1, 0);
                PLANT        plant     = null;

                // fetch all the plant accounting records for the target timespan
                DateTime minDate = incidentList.Select(l => l.INCIDENT_DT).Min();
                minDate = minDate.AddMonths(-1);
                PLANT_ACCOUNTING        pa     = null;
                List <PLANT_ACCOUNTING> paList = (from a in entities.PLANT_ACCOUNTING
                                                  where
                                                  EntityFunctions.CreateDateTime(a.PERIOD_YEAR, a.PERIOD_MONTH, 1, 0, 0, 0) >= minDate && EntityFunctions.CreateDateTime(a.PERIOD_YEAR, a.PERIOD_MONTH, 1, 0, 0, 0) <= rollupToDate
                                                  select a).OrderBy(l => l.PLANT_ID).ThenBy(l => l.PERIOD_YEAR).ThenBy(l => l.PERIOD_MONTH).ToList();

                List <EHSIncidentTimeAccounting> summaryList = new List <EHSIncidentTimeAccounting>();

                foreach (INCIDENT incident in incidentList)
                {
                    WriteLine("Incident ID: " + incident.INCIDENT_ID.ToString() + "  Occur Date: " + Convert.ToDateTime(incident.INCIDENT_DT).ToShortDateString());
                    incident.INCFORM_CAUSATION.Load();
                    if (incident.ISSUE_TYPE_ID == (decimal)EHSIncidentTypeId.InjuryIllness)
                    {
                        incident.INCFORM_LOSTTIME_HIST.Load();
                    }
                    plant       = plantList.Where(l => l.PLANT_ID == (decimal)incident.DETECT_PLANT_ID).FirstOrDefault();
                    summaryList = EHSIncidentMgr.SummarizeIncidentAccounting(summaryList, EHSIncidentMgr.CalculateIncidentAccounting(incident, plant.LOCAL_TIMEZONE, workdays));
                }

                plant = null;
                PLANT_ACTIVE pact = null;
                DateTime     periodDate;

                foreach (PLANT_ACCOUNTING pah in paList.OrderBy(l => l.PLANT_ID).ToList())
                {
                    if (pact == null || pact.PLANT_ID != pah.PLANT_ID)
                    {
                        pact = (from a in entities.PLANT_ACTIVE where a.PLANT_ID == pah.PLANT_ID && a.RECORD_TYPE == (int)TaskRecordType.HealthSafetyIncident select a).SingleOrDefault();
                    }
                    //if (pact != null && pact.EFF_END_DATE.HasValue && new DateTime(pah.PERIOD_YEAR, pah.PERIOD_MONTH, 1).Date >= ((DateTime)pact.EFF_START_DATE).Date)
                    if (pact != null && pact.EFF_START_DATE.HasValue && new DateTime(pah.PERIOD_YEAR, pah.PERIOD_MONTH, 1).Date >= ((DateTime)pact.EFF_START_DATE).Date)
                    {
                        pah.TIME_LOST       = pah.TOTAL_DAYS_RESTRICTED = 0;
                        pah.TIME_LOST_CASES = pah.RECORDED_CASES = pah.FIRST_AID_CASES = 0;
                    }
                }

                plant = null;
                pact  = null;
                foreach (EHSIncidentTimeAccounting period in summaryList.OrderBy(l => l.PlantID).ThenBy(l => l.PeriodYear).ThenBy(l => l.PeriodMonth).ToList())
                {
                    if (plant == null || plant.PLANT_ID != period.PlantID)
                    {
                        plant = plantList.Where(l => l.PLANT_ID == period.PlantID).FirstOrDefault();
                        pact  = (from a in entities.PLANT_ACTIVE where a.PLANT_ID == plant.PLANT_ID && a.RECORD_TYPE == (int)TaskRecordType.HealthSafetyIncident select a).SingleOrDefault();
                    }
                    periodDate = new DateTime(period.PeriodYear, period.PeriodMonth, 1);

                    if (pact != null && pact.EFF_START_DATE.HasValue && periodDate >= pact.EFF_START_DATE)
                    {
                        // write PLANT_ACCOUNTING metrics
                        if ((pa = paList.Where(l => l.PLANT_ID == period.PlantID && l.PERIOD_YEAR == period.PeriodYear && l.PERIOD_MONTH == period.PeriodMonth).FirstOrDefault()) == null)
                        {
                            paList.Add((pa = new PLANT_ACCOUNTING()));
                            pa.PLANT_ID     = period.PlantID;
                            pa.PERIOD_YEAR  = period.PeriodYear;
                            pa.PERIOD_MONTH = period.PeriodMonth;
                        }
                        pa.TIME_LOST             = period.LostTime;
                        pa.TOTAL_DAYS_RESTRICTED = period.RestrictedTime;
                        pa.TIME_LOST_CASES       = period.LostTimeCase;
                        pa.RECORDED_CASES        = period.RecordableCase;
                        pa.FIRST_AID_CASES       = period.FirstAidCase;
                        pa.LAST_UPD_DT           = DateTime.UtcNow;
                        pa.LAST_UPD_BY           = "automated";

                        EHSModel.UpdatePlantAccounting(entities, pa);
                    }
                }

                WriteLine("INCIDENT Rollup Completed: " + DateTime.UtcNow.ToString("hh:mm MM/dd/yyyy"));
            }
            catch (Exception ex)
            {
                WriteLine("INCIDENT RollUp Error: " + ex.ToString());
            }

            return(nextStep);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string pageMode = "";

            if (!string.IsNullOrEmpty(Request.QueryString["m"]))               // .../...aspx?p=xxxxx
            {
                pageMode = Request.QueryString["m"].ToLower();                 // page mode (web == running manually from the menu)
            }

            if (IsPostBack)
            {
                if (pageMode != "web")
                {
                    System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(), "closePage", "window.onunload = CloseWindow();", true);
                }
                return;
            }

            output = new StringBuilder();
            SETTINGS         setting  = null;
            bool             validIP  = true;
            int              workdays = 7;
            string           pageURI  = HttpContext.Current.Request.Url.AbsoluteUri;
            string           nextPage = "";
            PLANT_ACCOUNTING pa       = null;

            fromDate = DateTime.UtcNow.AddMonths(-11);                // set the incident 'select from' date.  TODO: get this from SETTINGS table
            // set end date to end of current month to clear spurrious entries ?
            DateTime rollupToDate = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.DaysInMonth(DateTime.UtcNow.Year, DateTime.UtcNow.Month));

            WriteLine("Incident Rollup Started: " + DateTime.UtcNow.ToString("hh:mm MM/dd/yyyy"));

            WriteLine(pageURI);

            try
            {
                string          currentIP = GetIPAddress();
                List <SETTINGS> sets      = SQMSettings.SelectSettingsGroup("AUTOMATE", "");           // ABW 20140805

                string strValidIP = sets.Find(x => x.SETTING_CD == "ValidIP").VALUE.ToString();
                setting = sets.Where(x => x.SETTING_CD == "ROLLUP_WORKDAYS").FirstOrDefault();
                if (setting != null && !string.IsNullOrEmpty(setting.VALUE))
                {
                    if (!int.TryParse(setting.VALUE, out workdays))
                    {
                        workdays = 7;
                    }
                }
                setting = sets.Where(x => x.SETTING_CD == "ROLLUP_NEXTPAGE").FirstOrDefault();
                if (setting != null && !string.IsNullOrEmpty(setting.VALUE) && setting.VALUE.Length > 1)
                {
                    nextPage = setting.VALUE;
                }

                /*
                 * int rollupMonthsAhead = 0;
                 * setting = sets.Where(x => x.SETTING_CD == "ROLLUP_MONTHS_AHEAD").FirstOrDefault();
                 * if (setting != null  &&  !string.IsNullOrEmpty(setting.VALUE))
                 * {
                 *      int.TryParse(setting.VALUE, out rollupMonthsAhead);
                 *      rollupToDate = rollupToDate.AddMonths(rollupMonthsAhead);
                 * }
                 */
                /*
                 * if (strValidIP.Equals(currentIP))
                 * {
                 *      WriteLine("Main Incident RollUp being accessed from a valid IP address " + currentIP);
                 *      validIP = true;
                 *
                 *      if (Request.QueryString["validation"] != null)
                 *      {
                 *              if (Request.QueryString["validation"].ToString().Equals("Vb12M11a4"))
                 *                      validIP = true;
                 *      }
                 *      else
                 *      {
                 *              WriteLine("Main Incident RollUp requested from incorrect source.");
                 *              validIP = false;
                 *      }
                 * }
                 * else
                 * {
                 *      WriteLine("Main Incident RollUp being accessed from invalid IP address " + currentIP);
                 *      validIP = false;
                 * }
                 */
            }
            catch (Exception ex)
            {
                validIP = false;
                WriteLine("Main Incident RollUp Error validating IP Address: " + ex.ToString());
            }

            // make sure this code is NOT moved to production
            //validIP = true;

            if (!validIP)
            {
                WriteLine("Main Incident RollUp Invalid IP Address");
                ltrStatus.Text = output.ToString().Replace("\n", "<br/>");
                WriteLogFile();

                if (pageMode != "web")
                {
                    System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(), "closePage", "window.onunload = CloseWindow();", true);
                }
                return;
            }

            try
            {
                PSsqmEntities entities = new PSsqmEntities();

                // fetch all incidents occurring after the minimum reporting date
                List <INCIDENT> incidentList = (from i in entities.INCIDENT.Include("INCFORM_INJURYILLNESS")
                                                where
                                                i.ISSUE_TYPE_ID != (decimal)EHSIncidentTypeId.PreventativeAction &&
                                                i.INCIDENT_DT >= fromDate && i.DETECT_PLANT_ID > 0
                                                select i).OrderBy(l => l.DETECT_PLANT_ID).ThenBy(l => l.INCIDENT_DT).ToList();

                List <PLANT> plantList = SQMModelMgr.SelectPlantList(entities, 1, 0);
                PLANT        plant     = null;

                // fetch all the plant accounting records for the target timespan
                DateTime minDate = incidentList.Select(l => l.INCIDENT_DT).Min();
                minDate = minDate.AddMonths(-1);
                List <PLANT_ACCOUNTING> paList = (from a in entities.PLANT_ACCOUNTING
                                                  where
                                                  EntityFunctions.CreateDateTime(a.PERIOD_YEAR, a.PERIOD_MONTH, 1, 0, 0, 0) >= minDate && EntityFunctions.CreateDateTime(a.PERIOD_YEAR, a.PERIOD_MONTH, 1, 0, 0, 0) <= rollupToDate
                                                  select a).OrderBy(l => l.PLANT_ID).ThenBy(l => l.PERIOD_YEAR).ThenBy(l => l.PERIOD_MONTH).ToList();

                List <EHSIncidentTimeAccounting> summaryList = new List <EHSIncidentTimeAccounting>();

                foreach (INCIDENT incident in incidentList)
                {
                    WriteLine("Incident ID: " + incident.INCIDENT_ID.ToString() + "  Occur Date: " + Convert.ToDateTime(incident.INCIDENT_DT).ToShortDateString());
                    incident.INCFORM_CAUSATION.Load();
                    if (incident.ISSUE_TYPE_ID == (decimal)EHSIncidentTypeId.InjuryIllness)
                    {
                        incident.INCFORM_LOSTTIME_HIST.Load();
                    }
                    plant       = plantList.Where(l => l.PLANT_ID == (decimal)incident.DETECT_PLANT_ID).FirstOrDefault();
                    summaryList = EHSIncidentMgr.SummarizeIncidentAccounting(summaryList, EHSIncidentMgr.CalculateIncidentAccounting(incident, plant.LOCAL_TIMEZONE, workdays));
                }

                plant = null;
                PLANT_ACTIVE pact = null;
                DateTime     periodDate;

                foreach (PLANT_ACCOUNTING pah in paList.OrderBy(l => l.PLANT_ID).ToList())
                {
                    if (pact == null || pact.PLANT_ID != pah.PLANT_ID)
                    {
                        pact = (from a in entities.PLANT_ACTIVE where a.PLANT_ID == pah.PLANT_ID && a.RECORD_TYPE == (int)TaskRecordType.HealthSafetyIncident select a).SingleOrDefault();
                    }
                    //if (pact != null && pact.EFF_END_DATE.HasValue && new DateTime(pah.PERIOD_YEAR, pah.PERIOD_MONTH, 1).Date >= ((DateTime)pact.EFF_START_DATE).Date)
                    if (pact != null && pact.EFF_START_DATE.HasValue && new DateTime(pah.PERIOD_YEAR, pah.PERIOD_MONTH, 1).Date >= ((DateTime)pact.EFF_START_DATE).Date)
                    {
                        pah.TIME_LOST       = pah.TOTAL_DAYS_RESTRICTED = 0;
                        pah.TIME_LOST_CASES = pah.RECORDED_CASES = pah.FIRST_AID_CASES = 0;
                    }
                }

                plant = null;
                pact  = null;
                foreach (EHSIncidentTimeAccounting period in summaryList.OrderBy(l => l.PlantID).ThenBy(l => l.PeriodYear).ThenBy(l => l.PeriodMonth).ToList())
                {
                    if (plant == null || plant.PLANT_ID != period.PlantID)
                    {
                        plant = plantList.Where(l => l.PLANT_ID == period.PlantID).FirstOrDefault();
                        pact  = (from a in entities.PLANT_ACTIVE where a.PLANT_ID == plant.PLANT_ID && a.RECORD_TYPE == (int)TaskRecordType.HealthSafetyIncident select a).SingleOrDefault();
                    }
                    periodDate = new DateTime(period.PeriodYear, period.PeriodMonth, 1);

                    if (pact != null && pact.EFF_START_DATE.HasValue && periodDate >= pact.EFF_START_DATE)
                    {
                        // write PLANT_ACCOUNTING metrics
                        if ((pa = paList.Where(l => l.PLANT_ID == period.PlantID && l.PERIOD_YEAR == period.PeriodYear && l.PERIOD_MONTH == period.PeriodMonth).FirstOrDefault()) == null)
                        {
                            paList.Add((pa = new PLANT_ACCOUNTING()));
                            pa.PLANT_ID     = period.PlantID;
                            pa.PERIOD_YEAR  = period.PeriodYear;
                            pa.PERIOD_MONTH = period.PeriodMonth;
                        }
                        pa.TIME_LOST             = period.LostTime;
                        pa.TOTAL_DAYS_RESTRICTED = period.RestrictedTime;
                        pa.TIME_LOST_CASES       = period.LostTimeCase;
                        pa.RECORDED_CASES        = period.RecordableCase;
                        pa.FIRST_AID_CASES       = period.FirstAidCase;
                        pa.LAST_UPD_DT           = DateTime.UtcNow;
                        pa.LAST_UPD_BY           = "automated";

                        EHSModel.UpdatePlantAccounting(entities, pa);
                    }
                }
            }
            catch (Exception ex)
            {
                WriteLine("Main Incident RollUp Error - " + ex.ToString());
            }

            WriteLine("");
            WriteLine("Completed: " + DateTime.UtcNow.ToString("hh:mm MM/dd/yyyy"));
            ltrStatus.Text = output.ToString().Replace("\n", "<br/>");
            WriteLogFile();

            try
            {
                if (!string.IsNullOrEmpty(nextPage))
                {
                    int    s1          = pageURI.LastIndexOf('/');
                    int    s2          = pageURI.LastIndexOf('.') > -1 ? pageURI.LastIndexOf('.') : pageURI.Length;
                    string nextPageURI = pageURI.Substring(0, s1 + 1) + nextPage + pageURI.Substring(s2, pageURI.Length - s2);
                    Response.Redirect(nextPageURI);
                }
            }
            catch (Exception ex)
            {
                output = new StringBuilder();
                WriteLine("RollUp Redirect Error - " + ex.ToString());
                WriteLogFile();
            }

            if (pageMode != "web")
            {
                System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(), "closePage", "window.onunload = CloseWindow();", true);
            }
        }