Ejemplo n.º 1
0
        public ActionResult Create()
        {
            var model = new WorkorderCreateViewModel();

            model.Reporter             = User.Identity.Name;
            model.ResultOfPersonServed = false;

            //fill dropdowns data
            model.Departments = GetDepartmentsSelectList(null);
            model.Consumers   = GetConsumersSelectList(null);
            model.Priorities  = GetPrioritiesSelectList(null);
            model.Areas       = GetAreasSelectList();

            return(View(model));
        }
Ejemplo n.º 2
0
        public ActionResult Create(WorkorderCreateViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                string reporter = string.IsNullOrEmpty(User.Identity.Name) == false ? User.Identity.Name : "Guest";

                string supervisorName = null, supervisorEmail = null; string alertRecipientEmails = null;

                //get department
                var department = _db.Departments.FirstOrDefault(x => x.Id == viewModel.DepartmentId);

                //get department supervisor detail by department id
                var departmentSupervisorEmail = _db.DepartmentSupervisorEmails.FirstOrDefault(x => x.DepartmentId == viewModel.DepartmentId);

                //get all alert receipients
                var alertrecipients = _db.AlertRecipients.Where(x => x.AreaId == viewModel.DepartmentAreaId).ToList();
                alertRecipientEmails = string.Join(";", alertrecipients.Select(x => x.Emails).Distinct());

                if (departmentSupervisorEmail != null)
                {
                    //supervisorName = departmentSupervisorEmail.SupervisorName;
                    //supervisorEmail = departmentSupervisorEmail.EmailAddress;
                    supervisorName  = "David Eaton";
                    supervisorEmail = "*****@*****.**";
                }

                Workorder workorder = new Workorder()
                {
                    DepartmentId = viewModel.DepartmentId,
                    Priority     = viewModel.Priority,
                    Reporter     = reporter,
                    Reported     = viewModel.Reported,
                    Details      = viewModel.Details,
                    //ConsumerId = viewModel.ConsumerId,
                    //Approver = supervisorName,
                    //Approved = DateTime.Now,

                    PersonServed = viewModel.ResultOfPersonServed ? viewModel.PersonServed : null
                };

                _db.Workorders.Add(workorder);
                _db.SaveChanges();

                TempData["SuccessMessage"] = "Workorder has been created successfully and is awaiting supervisor approval.";

                //if we don't have supervisor email
                if (string.IsNullOrEmpty(supervisorEmail) && string.IsNullOrEmpty(alertRecipientEmails))
                {
                    //use default email from web.config
                    supervisorEmail = ConfigurationManager.AppSettings["DepartmentSupervisorEmailDefault"];
                }

                //if still  supervisor email is null, then don't send email
                if (string.IsNullOrEmpty(supervisorEmail) == false && string.IsNullOrEmpty(alertRecipientEmails) == false)
                {
                    var message = "New Workorder # " + workorder.Id + " created for " + department.DepartmentFullName;
                    message += "<br />Priority : " + workorder.Priority.ToString() + "<br />";
                    message += "Detail : " + workorder.Details + "<br/>";

                    Email.SendEmail(supervisorEmail, "New Workorder for " + department.DepartmentFullName, message, message, null, null, alertRecipientEmails);
                }

                return(RedirectToAction("Index"));
            }

            //fill dropdowns data
            viewModel.Departments = GetDepartmentsSelectList(viewModel.DepartmentId);
            viewModel.Consumers   = GetConsumersSelectList(viewModel.ConsumerId);
            viewModel.Priorities  = GetPrioritiesSelectList(viewModel.Priority);


            return(View(viewModel));
        }