public TaskProxy SetDueDate(int taskID, string data)
        {
            LoginUser     loginUser   = TSAuthentication.GetLoginUser();
            Task          task        = Tasks.GetTask(loginUser, taskID);
            StringBuilder description = new StringBuilder();
            TaskJsonInfo  info        = Newtonsoft.Json.JsonConvert.DeserializeObject <TaskJsonInfo>(data);
            DateTime      dueDate     = (DateTime)info.DueDate;

            if (task.DueDate == null)
            {
                description.Append(String.Format("Changed Due Date from \"{0}\" to \"{1}\".", "Unassigned", (dueDate).ToString()));
            }
            else
            {
                description.Append(String.Format("Changed Due Date from \"{0}\" to \"{1}\".", ((DateTime)task.DueDate).ToString(), (dueDate).ToString()));
            }
            task.DueDate = DataUtils.DateToUtc(loginUser, dueDate);
            task.Collection.Save();
            TaskLogs.AddTaskLog(loginUser, taskID, description.ToString());

            if (task.UserID != null && loginUser.UserID != task.UserID)
            {
                SendModifiedNotification(loginUser.UserID, task.TaskID);
            }

            return(task.GetProxy());
        }
        public ReminderProxy SetReminderDueDate(int taskID, string data)
        {
            LoginUser     loginUser       = TSAuthentication.GetLoginUser();
            Reminder      reminder        = Reminders.GetReminderByTaskID(loginUser, taskID);
            StringBuilder description     = new StringBuilder();
            Task          task            = Tasks.GetTask(loginUser, taskID);
            TaskJsonInfo  info            = Newtonsoft.Json.JsonConvert.DeserializeObject <TaskJsonInfo>(data);
            DateTime      reminderDueDate = (DateTime)info.Reminder;

            if (reminder != null)
            {
                if (reminder.DueDate == null)
                {
                    description.Append(String.Format("Changed Reminder from \"{0}\" to \"{1}\".", "Unassigned", (reminderDueDate).ToString()));
                }
                else
                {
                    description.Append(String.Format("Changed Reminder from \"{0}\" to \"{1}\".", ((DateTime)reminder.DueDate).ToString(), (reminderDueDate).ToString()));
                }
                reminder.DueDate      = DataUtils.DateToUtc(loginUser, reminderDueDate);
                reminder.IsDismissed  = false;
                reminder.HasEmailSent = false;
                reminder.Collection.Save();
                TaskLogs.AddTaskLog(loginUser, taskID, description.ToString());
            }
            else
            {
                if (reminder == null)
                {
                    if (task.UserID == null)
                    {
                        reminder = CreateReminder(loginUser, taskID, task.Name, DataUtils.DateToUtc(loginUser, reminderDueDate), false);
                    }
                    else
                    {
                        reminder = CreateReminder(loginUser, taskID, task.Name, DataUtils.DateToUtc(loginUser, reminderDueDate), false, (int)task.UserID);
                    }
                    task.ReminderID = reminder.ReminderID;
                    task.Collection.Save();
                }
            }

            if (task.UserID != null && loginUser.UserID != task.UserID)
            {
                SendModifiedNotification(loginUser.UserID, task.TaskID);
            }

            return(reminder.GetProxy());
        }
        public TaskProxy NewTask(string data)
        {
            TaskJsonInfo info      = Newtonsoft.Json.JsonConvert.DeserializeObject <TaskJsonInfo>(data);
            LoginUser    loginUser = TSAuthentication.GetLoginUser();

            Task newTask = (new Tasks(loginUser)).AddNewTask();

            newTask.ParentID       = info.ParentID;
            newTask.OrganizationID = TSAuthentication.OrganizationID;
            newTask.Name           = info.Name;
            newTask.Description    = info.Description;
            newTask.UserID         = info.UserID;
            newTask.IsComplete     = info.IsComplete;

            if (newTask.IsComplete)
            {
                newTask.DateCompleted = DateTime.UtcNow;
            }

            newTask.DueDate = DataUtils.DateToUtc(loginUser, info.DueDate);

            newTask.Collection.Save();

            if (info.Reminder != null)
            {
                Reminder reminder = null;
                if (newTask.UserID == null)
                {
                    reminder = CreateReminder(loginUser, newTask.TaskID, info.Name, DataUtils.DateToUtc(loginUser, (DateTime)info.Reminder), false);
                }
                else
                {
                    reminder = CreateReminder(loginUser, newTask.TaskID, info.Name, DataUtils.DateToUtc(loginUser, (DateTime)info.Reminder), false, (int)newTask.UserID);
                }

                if (reminder != null)
                {
                    Tasks taskHelper = new Tasks(loginUser);
                    taskHelper.LoadByTaskID(newTask.TaskID);

                    if (taskHelper.Any())
                    {
                        taskHelper[0].ReminderID = reminder.ReminderID;
                        taskHelper.Save();
                    }
                }
            }

            foreach (int ticketID in info.Tickets)
            {
                AddAssociation(newTask.TaskID, ticketID, (int)ReferenceType.Tickets);
            }

            foreach (int productID in info.Products)
            {
                AddAssociation(newTask.TaskID, productID, (int)ReferenceType.Products);
            }

            foreach (int CompanyID in info.Company)
            {
                AddAssociation(newTask.TaskID, CompanyID, (int)ReferenceType.Organizations);
            }

            foreach (int UserID in info.Contacts)
            {
                AddAssociation(newTask.TaskID, UserID, (int)ReferenceType.Contacts);
            }

            foreach (int groupID in info.Groups)
            {
                AddAssociation(newTask.TaskID, groupID, (int)ReferenceType.Groups);
            }

            foreach (int UserID in info.User)
            {
                AddAssociation(newTask.TaskID, UserID, (int)ReferenceType.Users);
            }

            foreach (int ActivityID in info.Activities)
            {
                Notes notes = new Notes(TSAuthentication.GetLoginUser());
                notes.LoadByNoteID(ActivityID);

                if (notes.Count > 0)
                {
                    var note = notes[0];
                    if (note.RefType == ReferenceType.Organizations)
                    {
                        AddAssociation(newTask.TaskID, ActivityID, (int)ReferenceType.CompanyActivity);
                    }
                    else if (note.RefType == ReferenceType.Users)
                    {
                        AddAssociation(newTask.TaskID, ActivityID, (int)ReferenceType.ContactActivity);
                    }
                }
            }

            string description = String.Format("{0} created task.", TSAuthentication.GetUser(loginUser).FirstLastName);

            TaskLogs.AddTaskLog(loginUser, newTask.TaskID, description);

            if (newTask.UserID != null && loginUser.UserID != newTask.UserID)
            {
                SendAssignedNotification(loginUser.UserID, newTask.TaskID);
            }

            return(newTask.GetProxy());
        }