function total_task_times_p()
 {
     if (array_var($_GET, 'ws') !== null) {
         $report_data = array_var($_SESSION, 'total_task_times_report_data', array());
         $report_data['project_id'] = array_var($_GET, 'ws');
         $_SESSION['total_task_times_report_data'] = $report_data;
         $this->redirectTo('reporting', 'total_task_times_p', array('type' => array_var($_GET, 'type', '')));
     }
     $comp = logged_user()->getCompany();
     $users = $comp instanceof Company ? $comp->getUsers() : owner_company()->getUsers();
     $workspaces = logged_user()->getActiveProjects();
     tpl_assign('type', array_var($_GET, 'type'));
     tpl_assign('workspaces', $workspaces);
     tpl_assign('users', $users);
     tpl_assign('has_billing', BillingCategories::count() > 0);
 }
 function total_task_times_p()
 {
     if (array_var($_GET, 'ws') !== null) {
         $report_data = array_var($_SESSION, 'total_task_times_report_data', array());
         if (array_var($_GET, 'type')) {
             $report_data['timeslot_type'] = array_var($_GET, 'type');
         }
         $_SESSION['total_task_times_report_data'] = $report_data;
         $this->redirectTo('reporting', 'total_task_times_p');
     }
     $users = Contacts::getAllUsers();
     tpl_assign('users', $users);
     tpl_assign('has_billing', BillingCategories::count() > 0);
 }
 /**
  * Edit timeslot
  *
  * @param void
  * @return null
  */
 function edit()
 {
     $this->setTemplate('add_timeslot');
     $timeslot = Timeslots::findById(get_id());
     if (!$timeslot instanceof Timeslot) {
         flash_error(lang('timeslot dnx'));
         ajx_current("empty");
         return;
     }
     $object = $timeslot->getRelObject();
     if (!$object instanceof ContentDataObject) {
         flash_error(lang('object dnx'));
         ajx_current("empty");
         return;
     }
     if (!$object->canAddTimeslot(logged_user())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     if (!$timeslot->canEdit(logged_user())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     $timeslot_data = array_var($_POST, 'timeslot');
     if (!is_array($timeslot_data)) {
         $timeslot_data = array('contact_id' => $timeslot->getContactId(), 'description' => $timeslot->getDescription(), 'start_time' => $timeslot->getStartTime(), 'end_time' => $timeslot->getEndTime(), 'is_fixed_billing' => $timeslot->getIsFixedBilling(), 'hourly_billing' => $timeslot->getHourlyBilling(), 'fixed_billing' => $timeslot->getFixedBilling());
     }
     tpl_assign('timeslot_form_object', $object);
     tpl_assign('timeslot', $timeslot);
     tpl_assign('timeslot_data', $timeslot_data);
     tpl_assign('show_billing', BillingCategories::count() > 0);
     if (is_array(array_var($_POST, 'timeslot'))) {
         try {
             $this->percent_complete_delete($timeslot);
             $timeslot->setContactId(array_var($timeslot_data, 'contact_id', logged_user()->getId()));
             $timeslot->setDescription(array_var($timeslot_data, 'description'));
             $st = getDateValue(array_var($timeslot_data, 'start_value'), DateTimeValueLib::now());
             $st->setHour(array_var($timeslot_data, 'start_hour'));
             $st->setMinute(array_var($timeslot_data, 'start_minute'));
             $et = getDateValue(array_var($timeslot_data, 'end_value'), DateTimeValueLib::now());
             $et->setHour(array_var($timeslot_data, 'end_hour'));
             $et->setMinute(array_var($timeslot_data, 'end_minute'));
             $st = new DateTimeValue($st->getTimestamp() - logged_user()->getTimezone() * 3600);
             $et = new DateTimeValue($et->getTimestamp() - logged_user()->getTimezone() * 3600);
             $timeslot->setStartTime($st);
             $timeslot->setEndTime($et);
             if ($timeslot->getStartTime() > $timeslot->getEndTime()) {
                 flash_error(lang('error start time after end time'));
                 ajx_current("empty");
                 return;
             }
             $seconds = array_var($timeslot_data, 'subtract_seconds', 0);
             $minutes = array_var($timeslot_data, 'subtract_minutes', 0);
             $hours = array_var($timeslot_data, 'subtract_hours', 0);
             $subtract = $seconds + 60 * $minutes + 3600 * $hours;
             if ($subtract < 0) {
                 flash_error(lang('pause time cannot be negative'));
                 ajx_current("empty");
                 return;
             }
             $testEndTime = new DateTimeValue($timeslot->getEndTime()->getTimestamp());
             $testEndTime->add('s', -$subtract);
             if ($timeslot->getStartTime() > $testEndTime) {
                 flash_error(lang('pause time cannot exceed timeslot time'));
                 ajx_current("empty");
                 return;
             }
             $timeslot->setSubtract($subtract);
             if ($timeslot->getUser()->getDefaultBillingId()) {
                 $timeslot->setIsFixedBilling(array_var($timeslot_data, 'is_fixed_billing', false));
                 $timeslot->setHourlyBilling(array_var($timeslot_data, 'hourly_billing', 0));
                 if ($timeslot->getIsFixedBilling()) {
                     $timeslot->setFixedBilling(array_var($timeslot_data, 'fixed_billing', 0));
                 } else {
                     $timeslot->setFixedBilling($timeslot->getHourlyBilling() * $timeslot->getMinutes() / 60);
                 }
                 if ($timeslot->getBillingId() == 0 && ($timeslot->getHourlyBilling() > 0 || $timeslot->getFixedBilling() > 0)) {
                     $timeslot->setBillingId($timeslot->getUser()->getDefaultBillingId());
                 }
             }
             DB::beginWork();
             $timeslot->save();
             $timeslot_time = ($timeslot->getEndTime()->getTimestamp() - ($timeslot->getStartTime()->getTimestamp() + $timeslot->getSubtract())) / 3600;
             $task = ProjectTasks::findById($timeslot->getRelObjectId());
             if ($task->getTimeEstimate() > 0) {
                 $timeslot_percent = round($timeslot_time * 100 / ($task->getTimeEstimate() / 60));
                 $total_percentComplete = $timeslot_percent + $task->getPercentCompleted();
                 if ($total_percentComplete < 0) {
                     $total_percentComplete = 0;
                 }
                 $task->setPercentCompleted($total_percentComplete);
                 $task->save();
             }
             $this->notifier_work_estimate($task);
             DB::commit();
             flash_success(lang('success edit timeslot'));
             ajx_current("back");
         } catch (Exception $e) {
             DB::rollback();
             Logger::log($e->getTraceAsString());
             flash_error(lang('error edit timeslot') . ": " . $e->getMessage());
             ajx_current("empty");
         }
     }
 }
 /**
  * Show dashboard index page
  *
  * @param void
  * @return null
  */
 function index()
 {
     $this->setHelp('dashboard');
     $tag = array_var($_GET, 'active_tag');
     $logged_user = logged_user();
     if (active_project() instanceof Project) {
         $wscsv = active_project()->getAllSubWorkspacesQuery(true);
     } else {
         $wscsv = null;
     }
     $activity_log = null;
     $include_private = $logged_user->isMemberOfOwnerCompany();
     $include_silent = $logged_user->isAdministrator();
     $activity_log = ApplicationLogs::getOverallLogs($include_private, $include_silent, $wscsv, config_option('dashboard_logs_count', 15));
     if (user_config_option('show charts widget') && module_enabled('reporting')) {
         $charts = ProjectCharts::getChartsAtProject(active_project(), active_tag());
         tpl_assign('charts', $charts);
         if (BillingCategories::count() > 0 && active_project() instanceof Project) {
             tpl_assign('billing_chart_data', active_project()->getBillingTotalByUsers(logged_user()));
         }
     }
     if (user_config_option('show messages widget') && module_enabled('notes')) {
         list($messages, $pagination) = ProjectMessages::getMessages(active_tag(), active_project(), 0, 10, '`updated_on`', 'DESC', false);
         tpl_assign('messages', $messages);
     }
     if (user_config_option('show comments widget')) {
         $comments = Comments::getSubscriberComments(active_project(), $tag);
         tpl_assign('comments', $comments);
     }
     if (user_config_option('show documents widget') && module_enabled('documents')) {
         list($documents, $pagination) = ProjectFiles::getProjectFiles(active_project(), null, false, ProjectFiles::ORDER_BY_MODIFYTIME, 'DESC', 1, 10, false, active_tag(), null);
         tpl_assign('documents', $documents);
     }
     if (user_config_option('show emails widget') && module_enabled('email')) {
         $activeWs = active_project();
         list($unread_emails, $pagination) = MailContents::getEmails($tag, null, 'received', 'unread', '', $activeWs, 0, 10);
         if ($activeWs && user_config_option('always show unread mail in dashboard')) {
             // add unread unclassified emails
             list($all_unread, $pagination) = MailContents::getEmails($tag, null, 'received', 'unread', 'unclassified', null, 0, 10);
             $unread_emails = array_merge($unread_emails, $all_unread);
         }
         tpl_assign('unread_emails', $unread_emails);
     }
     //Tasks widgets
     $show_pending = user_config_option('show pending tasks widget') && module_enabled('tasks');
     $show_in_progress = user_config_option('show tasks in progress widget') && module_enabled('tasks');
     $show_late = user_config_option('show late tasks and milestones widget') && module_enabled('tasks');
     if ($show_pending || $show_in_progress || $show_late) {
         $assigned_to = explode(':', user_config_option('pending tasks widget assigned to filter'));
         $to_company = array_var($assigned_to, 0, 0);
         $to_user = array_var($assigned_to, 1, 0);
         tpl_assign('assigned_to_user_filter', $to_user);
         tpl_assign('assigned_to_company_filter', $to_company);
     }
     if ($show_pending) {
         $tasks = ProjectTasks::getProjectTasks(active_project(), ProjectTasks::ORDER_BY_DUEDATE, 'ASC', null, null, $tag, $to_company, $to_user, null, true, 'all', false, false, false, 10);
         tpl_assign('dashtasks', $tasks);
     }
     if ($show_in_progress) {
         $tasks_in_progress = ProjectTasks::getOpenTimeslotTasks(logged_user(), logged_user(), active_project(), $tag, $to_company, $to_user);
         tpl_assign('tasks_in_progress', $tasks_in_progress);
     }
     if ($show_late) {
         tpl_assign('today_milestones', $logged_user->getTodayMilestones(active_project(), $tag, 10));
         tpl_assign('late_milestones', $logged_user->getLateMilestones(active_project(), $tag, 10));
         tpl_assign('today_tasks', ProjectTasks::getDayTasksByUser(DateTimeValueLib::now(), $logged_user, active_project(), $tag, $to_company, $to_user, 10));
         tpl_assign('late_tasks', ProjectTasks::getLateTasksByUser($logged_user, active_project(), $tag, $to_company, $to_user, 10));
     }
     tpl_assign('activity_log', $activity_log);
     $usu = logged_user();
     $conditions = array("conditions" => array("`state` >= 200 AND (`state`%2 = 0) AND `trashed_by_id`=0 AND `created_by_id` =" . $usu->getId()));
     $outbox_mails = MailContents::findAll($conditions);
     if ($outbox_mails != null) {
         if (count($outbox_mails) == 1) {
             flash_error(lang('outbox mail not sent', 1));
         } else {
             if (count($outbox_mails) > 1) {
                 flash_error(lang('outbox mails not sent', count($outbox_mails)));
             }
         }
     }
     ajx_set_no_toolbar(true);
 }
 /**
  * Edit timeslot
  *
  * @param void
  * @return null
  */
 function edit()
 {
     if (!can_manage_time(logged_user(), true)) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     $this->setTemplate('add_timeslot');
     $timeslot = Timeslots::findById(get_id());
     if (!$timeslot instanceof Timeslot) {
         flash_error(lang('timeslot dnx'));
         ajx_current("empty");
         return;
     }
     // if
     $object = $timeslot->getObject();
     if (!$object instanceof ProjectDataObject) {
         flash_error(lang('object dnx'));
         ajx_current("empty");
         return;
     }
     // if
     if (!$object->canAddTimeslot(logged_user())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     // if
     if (!$timeslot->canEdit(logged_user())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     // if
     $timeslot_data = array_var($_POST, 'timeslot');
     if (!is_array($timeslot_data)) {
         $timeslot_data = array('description' => $timeslot->getDescription(), 'start_time' => $timeslot->getStartTime(), 'end_time' => $timeslot->getEndTime(), 'is_fixed_billing' => $timeslot->getIsFixedBilling(), 'hourly_billing' => $timeslot->getHourlyBilling(), 'fixed_billing' => $timeslot->getFixedBilling());
         // array
     }
     // if
     tpl_assign('timeslot_form_object', $object);
     tpl_assign('timeslot', $timeslot);
     tpl_assign('timeslot_data', $timeslot_data);
     tpl_assign('show_billing', BillingCategories::count() > 0);
     if (is_array(array_var($_POST, 'timeslot'))) {
         try {
             $timeslot->setDescription(array_var($timeslot_data, 'description'));
             $st = getDateValue(array_var($timeslot_data, 'start_value'), DateTimeValueLib::now());
             $st->setHour(array_var($timeslot_data, 'start_hour'));
             $st->setMinute(array_var($timeslot_data, 'start_minute'));
             $et = getDateValue(array_var($timeslot_data, 'end_value'), DateTimeValueLib::now());
             $et->setHour(array_var($timeslot_data, 'end_hour'));
             $et->setMinute(array_var($timeslot_data, 'end_minute'));
             $st = new DateTimeValue($st->getTimestamp() - logged_user()->getTimezone() * 3600);
             $et = new DateTimeValue($et->getTimestamp() - logged_user()->getTimezone() * 3600);
             $timeslot->setStartTime($st);
             $timeslot->setEndTime($et);
             if ($timeslot->getStartTime() > $timeslot->getEndTime()) {
                 flash_error(lang('error start time after end time'));
                 ajx_current("empty");
                 return;
             }
             $seconds = array_var($timeslot_data, 'subtract_seconds', 0);
             $minutes = array_var($timeslot_data, 'subtract_minutes', 0);
             $hours = array_var($timeslot_data, 'subtract_hours', 0);
             $subtract = $seconds + 60 * $minutes + 3600 * $hours;
             if ($subtract < 0) {
                 flash_error(lang('pause time cannot be negative'));
                 ajx_current("empty");
                 return;
             }
             $testEndTime = new DateTimeValue($timeslot->getEndTime()->getTimestamp());
             $testEndTime->add('s', -$subtract);
             if ($timeslot->getStartTime() > $testEndTime) {
                 flash_error(lang('pause time cannot exceed timeslot time'));
                 ajx_current("empty");
                 return;
             }
             $timeslot->setSubtract($subtract);
             /* Billing */
             $timeslot->setIsFixedBilling(array_var($timeslot_data, 'is_fixed_billing', false));
             $timeslot->setHourlyBilling(array_var($timeslot_data, 'hourly_billing', 0));
             if ($timeslot->getIsFixedBilling()) {
                 $timeslot->setFixedBilling(array_var($timeslot_data, 'fixed_billing', 0));
             } else {
                 $timeslot->setFixedBilling($timeslot->getHourlyBilling() * $timeslot->getMinutes() / 60);
             }
             if ($timeslot->getBillingId() == 0 && ($timeslot->getHourlyBilling() > 0 || $timeslot->getFixedBilling() > 0)) {
                 $timeslot->setBillingId($timeslot->getUser()->getDefaultBillingId());
             }
             DB::beginWork();
             $timeslot->save();
             DB::commit();
             flash_success(lang('success edit timeslot'));
             ajx_current("back");
         } catch (Exception $e) {
             DB::rollback();
             flash_error(lang('error edit timeslot'));
             ajx_current("empty");
         }
         // try
     }
 }
 function total_task_times_p()
 {
     //set params from config options
     $report_data['date_type'] = user_config_option('timeReportDate');
     $now_date = DateTimeValueLib::now();
     if (strtotime(user_config_option('timeReportDateStart'))) {
         //this return null if date is 0000-00-00 00:00:00
         $report_data['start_value'] = user_config_option('timeReportDateStart');
     } else {
         $dateStart = format_date($now_date, DATE_MYSQL, 0);
         //today
         $report_data['start_value'] = $dateStart;
     }
     if (strtotime(user_config_option('timeReportDateEnd'))) {
         //this return null if date is 0000-00-00 00:00:00
         $report_data['end_value'] = user_config_option('timeReportDateEnd');
     } else {
         $dateEnd = format_date($now_date, DATE_MYSQL, 0);
         //today
         $report_data['end_value'] = $dateEnd;
     }
     $report_data['user'] = user_config_option('timeReportPerson');
     $report_data['timeslot_type'] = user_config_option('timeReportTimeslotType');
     $report_data['show_estimated_time'] = user_config_option('timeReportShowEstimatedTime');
     $group = explode(',', user_config_option('timeReportGroupBy'));
     $report_data['group_by_1'] = array_var($group, 0);
     $report_data['group_by_2'] = array_var($group, 1);
     $report_data['group_by_3'] = array_var($group, 2);
     $altGroup = explode(',', user_config_option('timeReportAltGroupBy'));
     $report_data['alt_group_by_1'] = array_var($altGroup, 0);
     $report_data['alt_group_by_2'] = array_var($altGroup, 1);
     $report_data['alt_group_by_3'] = array_var($altGroup, 2);
     $report_data['show_billing'] = user_config_option('timeReportShowBilling');
     $cp_ids = CustomProperties::getCustomPropertyIdsByObjectType(ProjectTasks::instance()->getObjectTypeId());
     tpl_assign('has_custom_properties', count($cp_ids) > 0);
     $sel_member_ids = active_context_members(false);
     if (count($sel_member_ids) == 0) {
         $users = Contacts::getAllUsers();
     } else {
         $users = allowed_users_in_context(Timeslots::instance()->getObjectTypeId(), active_context());
     }
     $_SESSION['total_task_times_report_data'] = $report_data;
     tpl_assign('report_data', $report_data);
     tpl_assign('users', $users);
     tpl_assign('has_billing', BillingCategories::count() > 0);
 }