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);
        }