/**
 * on_time_report_footer_options event handler implementation
 *
 * @param TimeReport $report
 * @param array $options
 * @param Project $project
 * @param User $user
 * @return null
 */
function invoicing_handle_on_time_report_footer_options(&$report, &$options, &$project, &$user)
{
    if (Invoice::canAdd($user)) {
        $options[] = array('url' => instance_of($project, 'Project') ? assemble_url('invoices_add', array('time_report_id' => $report->getId(), 'project_id' => $project->getId())) : assemble_url('invoices_add', array('time_report_id' => $report->getId())), 'text' => lang('New Invoice'), 'icon' => get_image_url('create-invoice.gif', INVOICING_MODULE));
    }
    // if
}
 /**
  * Execute report
  *
  * @param User $user
  * @param TimeReport $report
  * @param Project $project
  * @return array
  */
 function executeReport($user, $report, $project = null)
 {
     $conditions = $report->prepareConditions($user, $project);
     if (empty($conditions)) {
         return null;
     }
     // if
     if ($report->getSumByUser()) {
         $rows = db_execute_all('SELECT SUM(float_field_1) AS total_time, integer_field_1 AS user_id FROM ' . TABLE_PREFIX . 'project_objects WHERE ' . $conditions . ' GROUP BY integer_field_1');
         if (is_foreachable($rows)) {
             $result = array();
             foreach ($rows as $row) {
                 $user = Users::findById($row['user_id']);
                 if (instance_of($user, 'User')) {
                     $result[] = array('user' => $user, 'total_time' => float_format($row['total_time'], 2));
                 }
                 // if
             }
             // foreach
             return $result;
         } else {
             return null;
         }
         // if
     } else {
         return TimeRecords::findBySQL('SELECT * FROM ' . TABLE_PREFIX . 'project_objects WHERE ' . $conditions . ' ORDER BY date_field_1');
     }
     // if
 }
 /**
  * Export report data as CSV
  *
  * @param void
  * @return null
  */
 function report_export()
 {
     if ($this->active_report->isNew()) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     if (!$this->active_report->canView($this->logged_user)) {
         $this->httpError(HTTP_ERR_FORBIDDEN);
     }
     // if
     download_contents(array_to_csv(TimeReports::executeReportForExport($this->logged_user, $this->active_report, $this->active_project)), 'text/csv', "time-report.csv", true);
 }
 /**
  * Delete existing report
  *
  * @param void
  * @return null
  */
 function delete()
 {
     if ($this->active_report->isNew()) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     if (!$this->active_report->canDelete($this->logged_user)) {
         $this->httpError(HTTP_ERR_FORBIDDEN);
     }
     // if
     if ($this->request->isSubmitted()) {
         $delete = $this->active_report->delete();
         if ($delete && !is_error($delete)) {
             flash_success("Report ':name' has been deleted", array('name' => $this->active_report->getName()));
         } else {
             flash_error("Failed to delete ':name' report", array('name' => $this->active_report->getName()));
         }
         // if
         $this->redirectTo('global_time');
     }
     // if
 }