Esempio n. 1
0
        static string ProcessEnvironmental()
        {
            PSsqmEntities entities     = new PSsqmEntities();
            SETTINGS      setting      = null;
            string        nextStep     = "";
            DateTime      toDate       = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1);
            DateTime      fromDate     = toDate.AddMonths(-3);
            int           status       = 0;
            DateTime      currencyDate = DateTime.MinValue;

            WriteLine("ENVIRONMENTAL 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_ENV_PERIODSPAN").FirstOrDefault();
                if (setting != null)
                {
                    fromDate = toDate.AddMonths(Convert.ToInt32(setting.VALUE) * -1);
                }

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

                CURRENCY_XREF latestCurrency = CurrencyMgr.GetLatestRecord(entities);
                if (latestCurrency != null)
                {
                    currencyDate = new DateTime(latestCurrency.EFF_YEAR, latestCurrency.EFF_MONTH, DateTime.DaysInMonth(latestCurrency.EFF_YEAR, latestCurrency.EFF_MONTH));
                }
                WriteLine("Max Currency Date = " + currencyDate.ToShortDateString());

                List <EHSProfile> profileList = new List <EHSProfile>();
                foreach (decimal plantID in (from p in entities.EHS_PROFILE select p.PLANT_ID).ToList())
                {
                    profileList.Add(new EHSProfile().Load(Convert.ToDecimal(plantID), false, true));
                }

                foreach (EHSProfile profile in profileList)                             // do each plant having a metric profile
                {
                    WriteLine(profile.Plant.PLANT_NAME);
                    DateTime periodDate = fromDate;

                    while (periodDate <= toDate)                                                // do each month within the rollup span
                    {
                        WriteLine(" " + periodDate.Year.ToString() + "/" + periodDate.Month.ToString());
                        if (profile.InputPeriod == null || profile.InputPeriod.PeriodDate != periodDate)
                        {
                            profile.LoadPeriod(periodDate);
                        }

                        if (profile.ValidPeriod())
                        {
                            if (!profile.InputPeriod.PlantAccounting.APPROVAL_DT.HasValue)
                            {
                                profile.InputPeriod.PlantAccounting.APPROVAL_DT = toDate;
                                profile.InputPeriod.PlantAccounting.APPROVER_ID = 1m;                      // default to the sysadmin user
                            }
                            status = profile.UpdateMetricHistory(periodDate);                              // new roll-up logic
                            WriteLine(" ... " + status.ToString());
                        }
                        periodDate = periodDate.AddMonths(1);
                    }
                }

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

            return(nextStep);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(), "closePage", "window.onunload = CloseWindow();", true);
                return;
            }

            output = new StringBuilder();
            SETTINGS setting  = null;
            bool     validIP  = true;
            string   pageURI  = HttpContext.Current.Request.Url.AbsoluteUri;
            string   nextPage = "";
            DateTime toDate   = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1);
            DateTime fromDate = toDate.AddMonths(-3);

            WriteLine("ENV Data 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_ENV_PERIODSPAN").FirstOrDefault();
                if (setting != null)
                {
                    fromDate = toDate.AddMonths(Convert.ToInt32(setting.VALUE) * -1);
                }

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

                /*
                 * 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 ENV Data RollUp Error validating IP Address: " + ex.ToString());
            }

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

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

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

            try
            {
                PSsqmEntities entities = new PSsqmEntities();

                int      status       = 0;
                DateTime currencyDate = DateTime.MinValue;

                CURRENCY_XREF latestCurrency = CurrencyMgr.GetLatestRecord(entities);
                if (latestCurrency != null)
                {
                    currencyDate = new DateTime(latestCurrency.EFF_YEAR, latestCurrency.EFF_MONTH, DateTime.DaysInMonth(latestCurrency.EFF_YEAR, latestCurrency.EFF_MONTH));
                }
                WriteLine("Max Currency Date = " + currencyDate.ToShortDateString());


                List <EHSProfile> profileList = new List <EHSProfile>();
                foreach (decimal plantID in (from p in entities.EHS_PROFILE select p.PLANT_ID).ToList())
                {
                    profileList.Add(new EHSProfile().Load(Convert.ToDecimal(plantID), false, true));
                }

                foreach (EHSProfile profile in profileList)                             // do each plant having a metric profile
                {
                    WriteLine(profile.Plant.PLANT_NAME);
                    DateTime periodDate = fromDate;

                    while (periodDate <= toDate)                                                // do each month within the rollup span
                    {
                        WriteLine(" " + periodDate.Year.ToString() + "/" + periodDate.Month.ToString());
                        if (profile.InputPeriod == null || profile.InputPeriod.PeriodDate != periodDate)
                        {
                            profile.LoadPeriod(periodDate);
                        }

                        if (profile.ValidPeriod())
                        {
                            if (!profile.InputPeriod.PlantAccounting.APPROVAL_DT.HasValue)
                            {
                                profile.InputPeriod.PlantAccounting.APPROVAL_DT = toDate;
                                profile.InputPeriod.PlantAccounting.APPROVER_ID = 1m;                      // default to the sysadmin user
                            }
                            status = profile.UpdateMetricHistory(periodDate);                              // new roll-up logic
                            WriteLine(" ... " + status.ToString());
                            periodDate = periodDate.AddMonths(1);
                        }
                    }
                }
            }

            catch (Exception ex)
            {
                WriteLine("Main ENV Data 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();
            }

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