Exemplo n.º 1
0
        public ActionResult Create([Bind(Include = @"RequestId,DepartureDate,ReturnDate,StatusId,
                                    Description,DelegationId,DepartureAddressId,ReturnAddressId")] Request request,
                                   int?StandInId, int?TransportId)
        {
            if (ModelState.IsValid)
            {
                using (var trans = db.Database.BeginTransaction())
                {
                    try
                    {
                        EmployeeDAL ed = new EmployeeDAL();

                        request.SubmitDate = DateTime.Now;

                        var loggedUser = User.Identity;

                        var id = ed.GetEmployeeIdByLoggedUser(loggedUser.Name);

                        var emp = ed.GetEmployeeById(Convert.ToInt32(id));
                        if (emp != null)
                        {
                            request.Applicant = emp;
                        }

                        var sup = emp.SuperiorEmployee;
                        if (sup != null)
                        {
                            request.Approver = sup;
                        }

                        var hr = ed.GetHREmployee("Human Resources");
                        if (hr != null)
                        {
                            request.HREmployee = hr;
                        }

                        var dep = ed.GetAddressById(request.DepartureAddressId);
                        if (dep != null)
                        {
                            request.DepartureAddress = dep;
                        }

                        var ret = ed.GetAddressById(request.ReturnAddressId);
                        if (ret != null)
                        {
                            request.ReturnAddress = ret;
                        }

                        var stdin = ed.GetStandInEmployeeById(StandInId.Value);
                        if (stdin != null)
                        {
                            request.StandIn = stdin;
                        }

                        ed.AddRequestAndSaveChanges(request);

                        string subject = "Delegation Request";
                        string body    = "Please approve my request by entering the following link http://localhost:5281/Request/IndexSup";
                        body += " \n " + request.Applicant.FullName;

                        ed.sendEmail(request.Approver, request.Applicant, subject, body);

                        trans.Commit();
                    }
                    catch (Exception e)
                    {
                        trans.Rollback();
                    }
                }
                return(RedirectToAction("IndexMyReq"));
            }

            ViewBag.DelegationId       = new SelectList(db.MyDelegation, "DelegationId", "DelegationType", request.DelegationId);
            ViewBag.StatusId           = new SelectList(db.MyStatus, "StatusId", "StatusName", request.StatusId);
            ViewBag.DepartureAddressId = new SelectList(db.MyAddress, "AddressId", "CompanyName", request.DepartureAddressId);
            ViewBag.ReturnAddressId    = new SelectList(db.MyAddress, "AddressId", "CompanyName", request.ReturnAddressId);
            ViewBag.StandInId          = new SelectList(db.MyEmployee, "EmployeeId", "FullName");


            return(View(request));
        }