Exemplo n.º 1
0
        public ContentResult Update(FormCollection form)
        {
            JObject json = new JObject();

            json["error"]   = false;
            json["message"] = "";

            if (form.GetValue("id") != null && form.GetValue("v") != null)
            {
                if (((Employee)this.GetAccount().Profile).Department.Type == DepartmentType.HumanResources)
                {
                    try
                    {
                        Leave Leave = new Leave(Int32.Parse(form.GetValue("id").AttemptedValue));
                        Leave.Status = (LeaveStatus)Int32.Parse(form.GetValue("v").AttemptedValue);

                        if (Leave.Status == LeaveStatus.Approved)
                        {
                            try
                            {
                                Attendance at = new Attendance().Find(
                                    Leave.Employee.Profile.ProfileID,
                                    Leave.StartDate,
                                    recursive: true,
                                    byPrimary: false);

                                at.Leave += 1;
                                at.Update(false);
                            }
                            catch (Exception e)
                            { }
                        }

                        Leave.Update(recursive: false);

                        Notification notif = new Notification();
                        notif.Account = new Account().FindByProfile(Leave.Employee.Profile.ProfileID);
                        notif.Message = "<b>Info:</b> Your leave request #" + Leave.LeaveID + " has been updated to: <i>"
                                        + (Leave.Status == LeaveStatus.Approved ?
                                           " Approved " : Leave.Status == LeaveStatus.Denied ? " Denied " : " Pending ") + "</i>";
                        notif.TimeStamp = DateTime.Now;
                        notif.Status    = NotificationStatus.Unread;
                        notif.Create();

                        if (Leave.Status == LeaveStatus.Denied)
                        {
                            Leave.Delete();
                        }
                    }
                    catch (Exception e)
                    {
                        json["error"]   = true;
                        json["message"] = e.Message;
                    }
                }
                else
                {
                    json["error"]   = true;
                    json["message"] = "You are not authorized to continue";
                }
            }
            else
            {
                json["error"]   = true;
                json["message"] = "Form is incomplete";
            }
            return(Content(json.ToString(), "application/json"));
        }