public static void CreateOrUpdateTask(decimal auditId, decimal responsiblePersonId, int recordTypeId, DateTime dueDate, string status) { var entities = new PSsqmEntities(); AUDIT audit = SelectAuditById(entities, auditId); AUDIT_TYPE type = SelectAuditTypeById(entities, audit.AUDIT_TYPE_ID); var taskMgr = new TaskStatusMgr(); taskMgr.Initialize(recordTypeId, auditId); taskMgr.LoadTaskList(recordTypeId, auditId); TASK_STATUS task = taskMgr.FindTask("0", "T", responsiblePersonId); if (task == null) { task = taskMgr.CreateTask("0", "T", 0, type.TITLE.ToString(), dueDate, responsiblePersonId); task.STATUS = ((int)TaskMgr.CalculateTaskStatus(task)).ToString(); } else { switch (status) { case "C": task.STATUS = ((int)TaskStatus.Complete).ToString(); taskMgr.SetTaskComplete(task, responsiblePersonId); break; } //task = taskMgr.UpdateTask(task, dueDate, responsiblePersonId, audit.AUDIT_TYPE_ID.ToString()); } taskMgr.UpdateTaskList(auditId); }
static void UpdatePastDueAuditStatus() { string status = ""; // get a list of all audits that do not have a close date List <AUDIT> openAudits = EHSAuditMgr.SelectOpenAudits(0, null); foreach (AUDIT audit in openAudits) { AUDIT_TYPE type = EHSAuditMgr.SelectAuditTypeById(entities, audit.AUDIT_TYPE_ID); PLANT plant = SQMModelMgr.LookupPlant((decimal)audit.DETECT_PLANT_ID); DateTime closeDT = Convert.ToDateTime(audit.AUDIT_DT.AddDays(type.DAYS_TO_COMPLETE + 1)); // add one to the date and it will default to the next day at 00:00:00, which means midnight DateTime localTime = WebSiteCommon.LocalTime(DateTime.UtcNow, plant.LOCAL_TIMEZONE); if (closeDT.CompareTo(localTime) < 0) { // close the audit // valid status codes... A = active, C = complete, I = incomplete/in-process, E = Expired. We are closing audits that are past due, so Expired. try { status = audit.CURRENT_STATUS; if (status != "C") { status = "E"; } } catch { status = "E"; } EHSAuditMgr.CloseAudit(audit.AUDIT_ID, status, closeDT.AddDays(-1)); // now take the one day back off so that the close date sets correctly // now mark the Task as expired too! EHSAuditMgr.CreateOrUpdateTask(audit.AUDIT_ID, (decimal)audit.AUDIT_PERSON, 50, closeDT.AddDays(-1), status, 0); } } }
static void ScheduleAllAudits() { List <SETTINGS> sets = SQMSettings.SelectSettingsGroup("AUTOMATE", ""); // ABW 20140805 int startRangeHours = 04; int startRangeMinutes = 45; int endRangeHours = 05; int endRangeMinutes = 15; try { startRange = sets.Find(x => x.SETTING_CD == "AuditScheduleStart").VALUE.ToString(); startRangeHours = Convert.ToInt16(startRange.Substring(0, 2)); startRangeMinutes = Convert.ToInt16(startRange.Substring(3, 2)); } catch { } try { endRange = sets.Find(x => x.SETTING_CD == "AuditScheduleEnd").VALUE.ToString(); endRangeHours = Convert.ToInt16(endRange.Substring(0, 2)); endRangeMinutes = Convert.ToInt16(endRange.Substring(3, 2)); } catch { } List <AUDIT_SCHEDULER> scheduler = EHSAuditMgr.SelectActiveAuditSchedulers(0, null); // currently, we will select all schedules for all plants AUDIT audit = null; List <EHSAuditQuestion> questions = null; AUDIT_ANSWER answer = null; decimal auditId = 0; TimeSpan start = new TimeSpan(startRangeHours, startRangeMinutes, 0); TimeSpan end = new TimeSpan(endRangeHours, endRangeMinutes, 0); WriteLine("Audits will be created for locations with a local time of " + startRangeHours + ":" + startRangeMinutes + " through " + endRangeHours + ":" + endRangeMinutes); foreach (AUDIT_SCHEDULER schedule in scheduler) { AUDIT_TYPE audittype = EHSAuditMgr.SelectAuditTypeById(entities, (decimal)schedule.AUDIT_TYPE_ID); // check that the audit is still active if (audittype != null) { if (!audittype.INACTIVE) { // ABW 1/5/16 - changing the scheduler from scheduling one week of audits to creating audits that are to be scheduled that day. // All audits will be scheduled at 5am local plant time for the day. //WriteLine(""); //WriteLine("The following " + type.TITLE + " assessments were created for Assessment Scheduler " + schedule.AUDIT_SCHEDULER_ID + ": "); //// determine the date to schedule, by finding the next occurance of the selected day of the week after the current day //DateTime auditDate = DateTime.Today; //while ((int)auditDate.DayOfWeek != schedule.DAY_OF_WEEK) //{ // auditDate = auditDate.AddDays(1); //} // get the plant PLANT auditPlant = SQMModelMgr.LookupPlant((decimal)schedule.PLANT_ID); // check the local plant time to see if it is almost 5am. If so, schedule the audit. If not, do nothing. DateTime localTime = WebSiteCommon.LocalTime(DateTime.UtcNow, auditPlant.LOCAL_TIMEZONE); if ((int)localTime.DayOfWeek == schedule.DAY_OF_WEEK && ((localTime.TimeOfDay > start) && (localTime.TimeOfDay < end))) { WriteLine(""); WriteLine("The following " + audittype.TITLE + " assessments were created for Assessment Scheduler " + schedule.AUDIT_SCHEDULER_ID + ": "); // for the location, select all people that should get the audit List <PERSON> auditors = SQMModelMgr.SelectPlantPrivgroupPersonList(auditPlant.PLANT_ID, new string[1] { schedule.JOBCODE_CD }, true); foreach (PERSON person in auditors) { // check to see if there is already an audit for this plant/type/date/person audit = EHSAuditMgr.SelectAuditForSchedule(auditPlant.PLANT_ID, audittype.AUDIT_TYPE_ID, person.PERSON_ID, localTime); if (audit == null) { // create audit header auditId = 0; audit = new AUDIT() { DETECT_COMPANY_ID = Convert.ToDecimal(auditPlant.COMPANY_ID), DETECT_BUS_ORG_ID = auditPlant.BUS_ORG_ID, DETECT_PLANT_ID = auditPlant.PLANT_ID, AUDIT_TYPE = "EHS", CREATE_DT = localTime, CREATE_BY = "Automated Scheduler", DESCRIPTION = audittype.TITLE, // CREATE_PERSON = SessionManager.UserContext.Person.PERSON_ID, // do we want to set this to admin? AUDIT_DT = localTime, AUDIT_TYPE_ID = audittype.AUDIT_TYPE_ID, AUDIT_PERSON = person.PERSON_ID, CURRENT_STATUS = "A", PERCENT_COMPLETE = 0, TOTAL_SCORE = 0 }; entities.AddToAUDIT(audit); entities.SaveChanges(); auditId = audit.AUDIT_ID; // create audit answer records questions = EHSAuditMgr.SelectAuditQuestionListByType(audittype.AUDIT_TYPE_ID); foreach (var q in questions) { answer = new AUDIT_ANSWER() { AUDIT_ID = auditId, AUDIT_QUESTION_ID = q.QuestionId, ANSWER_VALUE = "", ORIGINAL_QUESTION_TEXT = q.QuestionText, //COMMENT = q.AnswerComment }; entities.AddToAUDIT_ANSWER(answer); } entities.SaveChanges(); // create task record for their calendar EHSAuditMgr.CreateOrUpdateTask(auditId, person.PERSON_ID, 50, localTime.AddDays(audittype.DAYS_TO_COMPLETE), "A", 0); // send an email EHSNotificationMgr.NotifyOnAuditCreate(auditId, person.PERSON_ID); System.Threading.Thread.Sleep(timer); //will wait for 2 seconds to allow Google Mail to process email requests WriteLine(person.LAST_NAME + ", " + person.FIRST_NAME + " - assessment added"); } else { // ABW 1/5/16 - Since this will be running once every hour now, we don't want to see this message //WriteLine(person.LAST_NAME + ", " + person.FIRST_NAME + " - assessment already exists for this date"); } } } else { // ABW 1/5/16 - Do we need to write any message out to explaing why the audit wasn't created? I don't think so //WriteLine("Assessment Type " + schedule.AUDIT_TYPE_ID + " - assessment already exists for this date OR is not scheduled to be created"); } } else { WriteLine("Assessment Type " + schedule.AUDIT_TYPE_ID + " inactive. Assessments not created for Scheduler Record " + schedule.AUDIT_SCHEDULER_ID.ToString()); } } else { WriteLine("Assessment Type " + schedule.AUDIT_TYPE_ID + " not found. Assessments not created for Scheduler Record " + schedule.AUDIT_SCHEDULER_ID.ToString()); } } }
static void ScheduleAllAudits() { List <AUDIT_SCHEDULER> scheduler = EHSAuditMgr.SelectActiveAuditSchedulers(0, null); // currently, we will select all schedules for all plants AUDIT audit = null; List <EHSAuditQuestion> questions = null; AUDIT_ANSWER answer = null; decimal auditId = 0; foreach (AUDIT_SCHEDULER schedule in scheduler) { AUDIT_TYPE type = EHSAuditMgr.SelectAuditTypeById(entities, (decimal)schedule.AUDIT_TYPE_ID); // check that the audit is still active if (type != null) { if (!type.INACTIVE) { WriteLine(""); WriteLine("The following " + type.TITLE + " audits were created for Audit Scheduler " + schedule.AUDIT_SCHEDULER_ID + ": "); // determine the date to schedule, by finding the next occurance of the selected day of the week after the current day DateTime auditDate = DateTime.Today; while ((int)auditDate.DayOfWeek != schedule.DAY_OF_WEEK) { auditDate = auditDate.AddDays(1); } // get the plant PLANT auditPlant = SQMModelMgr.LookupPlant((decimal)schedule.PLANT_ID); // for the location, select all people that should get the audit List <PERSON> auditors = SQMModelMgr.SelectPlantPrivgroupPersonList(auditPlant.PLANT_ID, new string[1] { schedule.JOBCODE_CD }); foreach (PERSON person in auditors) { // check to see if there is already an audit for this plant/type/date/person audit = EHSAuditMgr.SelectAuditForSchedule(auditPlant.PLANT_ID, type.AUDIT_TYPE_ID, person.PERSON_ID, auditDate); if (audit == null) { // create audit header auditId = 0; audit = new AUDIT() { DETECT_COMPANY_ID = Convert.ToDecimal(auditPlant.COMPANY_ID), DETECT_BUS_ORG_ID = auditPlant.BUS_ORG_ID, DETECT_PLANT_ID = auditPlant.PLANT_ID, AUDIT_TYPE = "EHS", CREATE_DT = DateTime.Now, CREATE_BY = "Automated Scheduler", DESCRIPTION = type.TITLE, // CREATE_PERSON = SessionManager.UserContext.Person.PERSON_ID, // do we want to set this to admin? AUDIT_DT = auditDate, AUDIT_TYPE_ID = type.AUDIT_TYPE_ID, AUDIT_PERSON = person.PERSON_ID, CURRENT_STATUS = "A", PERCENT_COMPLETE = 0, TOTAL_SCORE = 0 }; entities.AddToAUDIT(audit); entities.SaveChanges(); auditId = audit.AUDIT_ID; // create audit answer records questions = EHSAuditMgr.SelectAuditQuestionList(type.AUDIT_TYPE_ID, 0, 0); // do not specify the audit ID foreach (var q in questions) { answer = new AUDIT_ANSWER() { AUDIT_ID = auditId, AUDIT_QUESTION_ID = q.QuestionId, ANSWER_VALUE = q.AnswerText, ORIGINAL_QUESTION_TEXT = q.QuestionText, COMMENT = q.AnswerComment }; entities.AddToAUDIT_ANSWER(answer); } entities.SaveChanges(); // create task record for their calendar EHSAuditMgr.CreateOrUpdateTask(auditId, person.PERSON_ID, 50, auditDate.AddDays(type.DAYS_TO_COMPLETE), "A"); // send an email EHSNotificationMgr.NotifyOnAuditCreate(auditId, person.PERSON_ID); WriteLine(person.LAST_NAME + ", " + person.FIRST_NAME + " - audit added"); } else { WriteLine(person.LAST_NAME + ", " + person.FIRST_NAME + " - audit already exists for this date"); } } } else { WriteLine("Adit Type " + schedule.AUDIT_TYPE_ID + " inactive. Audits not created for Scheduler Record " + schedule.AUDIT_SCHEDULER_ID.ToString()); } } else { WriteLine("Adit Type " + schedule.AUDIT_TYPE_ID + " not found. Audits not created for Scheduler Record " + schedule.AUDIT_SCHEDULER_ID.ToString()); } } }
AuditData PopulateByAuditId(decimal aid) { AuditData d = new AuditData(); var entities = new PSsqmEntities(); d.audit = EHSAuditMgr.SelectAuditById(entities, aid); if (d.audit != null) { try { string plantName = EHSAuditMgr.SelectPlantNameById((decimal)d.audit.DETECT_PLANT_ID); d.auditLocation = plantName; d.auditNumber = aid.ToString(); AUDIT_TYPE auditType = EHSAuditMgr.SelectAuditTypeById(entities, d.audit.AUDIT_TYPE_ID); d.auditType = auditType.TITLE; // do I need this? decimal auditTypeId = d.audit.AUDIT_TYPE_ID; // if I have all this, why am I redefining it? decimal companyId = d.audit.DETECT_COMPANY_ID; if (d.audit.DEPT_ID != null) { if (d.audit.DEPT_ID == 0) { d.auditDepartment = "Plant Wide"; } else { DEPARTMENT dept = SQMModelMgr.LookupDepartment(entities, (decimal)d.audit.DEPT_ID); d.auditDepartment = dept.DEPT_NAME; } } var questions = EHSAuditMgr.SelectAuditQuestionList(auditTypeId, 0, aid); d.questionList = questions; List <AUDIT_TOPIC> topics = new List <AUDIT_TOPIC>(); AUDIT_TOPIC topic = new AUDIT_TOPIC(); decimal previousTopicId = 0; foreach (EHSAuditQuestion question in questions) { if (question.TopicId != previousTopicId) { topic = new AUDIT_TOPIC(); topic.AUDIT_TOPIC_ID = question.TopicId; topic.TITLE = question.TopicTitle; topics.Add(topic); previousTopicId = question.TopicId; } } d.topicList = topics; // Date/Time d.auditDate = d.audit.AUDIT_DT.ToShortDateString(); DateTime closeDate = d.audit.AUDIT_DT.AddDays(auditType.DAYS_TO_COMPLETE); d.auditCloseDate = closeDate.ToShortDateString(); // Description d.auditDescription = d.audit.DESCRIPTION; d.auditPerson = SQMModelMgr.LookupPerson(entities, (decimal)d.audit.AUDIT_PERSON, "", false); // Audit Exception Actions foreach (TASK_STATUS ac in EHSAuditMgr.GetAuditActionList(aid, 0)) { //if (ac.RESPONSIBLE_ID.HasValue) //{ // ac.COMMENTS = SQMModelMgr.FormatPersonListItem(SQMModelMgr.LookupPerson((decimal)ac.RESPONSIBLE_ID, "")); //} d.actionList.Add(ac); } // not showing the attachments at this point, but not deleting code... just in case //var files = (from a in entities.ATTACHMENT // where // (a.RECORD_ID == aid && a.RECORD_TYPE == 40 && a.DISPLAY_TYPE > 0) && // (a.FILE_NAME.ToLower().Contains(".jpg") || a.FILE_NAME.ToLower().Contains(".jpeg") || // a.FILE_NAME.ToLower().Contains(".gif") || a.FILE_NAME.ToLower().Contains(".png") || // a.FILE_NAME.ToLower().Contains(".bmp")) // orderby a.RECORD_TYPE, a.FILE_NAME // select new // { // Data = (from f in entities.ATTACHMENT_FILE where f.ATTACHMENT_ID == a.ATTACHMENT_ID select f.ATTACHMENT_DATA).FirstOrDefault(), // Description = (string.IsNullOrEmpty(a.FILE_DESC)) ? "" : a.FILE_DESC, // }).ToList(); //if (files.Count > 0) //{ // d.photoData = new List<byte[]>(); // d.photoCaptions = new List<string>(); // foreach (var f in files) // { // d.photoData.Add(f.Data); // d.photoCaptions.Add(f.Description); // } //} } catch { } } return(d); }