You are here

function uc_discounts_report in Ubercart Discounts (Alternative) 6.2

Generate report for all discounts.

2 string references to 'uc_discounts_report'
uc_discounts_menu in uc_discounts/uc_discounts.module
Implementation of hook_menu().
uc_discounts_report_for_discount in uc_discounts/uc_discounts.admin.inc
Generate report for discount.

File

uc_discounts/uc_discounts.admin.inc, line 858

Code

function uc_discounts_report() {
  $op = arg(4) == 'download' ? 'download' : 'show';
  $header = array(
    array(
      'data' => t('Active'),
      'field' => 'd.is_active',
    ),
    array(
      'data' => t('Name'),
      'field' => 'd.name',
    ),
    array(
      'data' => t('Short Description'),
      'field' => 'd.short_description',
    ),
    array(
      'data' => t('Qualifying Type'),
      'field' => 'd.qualifying_type',
    ),
    array(
      'data' => t('Type'),
      'field' => 'd.discount_type',
    ),
    array(
      'data' => t('Uses'),
      'field' => 'use_count',
    ),
    array(
      'data' => t('Times Applied'),
      'field' => 'total_times_applied',
    ),
    array(
      'data' => t('Discounted Amount'),
      'field' => 'total_amount',
    ),
    array(
      'data' => t('Revenue Amount'),
      'field' => 'total_revenue',
    ),
    array(
      'data' => t('Weight'),
      'field' => 'd.weight',
    ),
    array(
      'data' => t('Created At'),
      'field' => 'insert_timestamp',
      'sort' => 'desc',
    ),
    array(
      'data' => t('Operations'),
    ),
  );
  $query = "SELECT d.*,\n                       COUNT(du.discount_use_id) total_use_count,\n                       SUM(du.times_applied) total_times_applied,\n                       SUM(du.amount) total_amount,\n                       SUM(o.order_total) total_revenue\n                FROM {uc_discounts} d\n                LEFT JOIN {uc_discounts_uses} du ON d.discount_id=du.discount_id\n                LEFT JOIN {uc_orders} o ON du.order_id=o.order_id AND o.order_status = 'completed' AND o.order_total > 0\n                GROUP BY d.discount_id";
  $query .= tablesort_sql($header);
  $count_sql = 'SELECT COUNT(*) FROM {uc_discounts}';
  $result = $op == 'download' ? db_query($query) : pager_query($query, 50, 0, $count_sql);
  $rows = array();
  while ($discount = db_fetch_object($result)) {
    $total_use_count = is_numeric($discount->total_use_count) ? $discount->total_use_count : 0;
    $total_times_applied = is_numeric($discount->total_times_applied) ? $discount->total_times_applied : 0;
    $operations = array(
      l(t('usage'), 'admin/reports/uc_discounts/discount/' . $discount->discount_id),
      l(t('edit'), 'admin/store/uc_discounts/edit/' . $discount->discount_id),
      l(t('delete'), 'admin/store/uc_discounts/delete/' . $discount->discount_id),
    );
    $rows[] = array(
      array(
        'data' => $discount->is_active ? '✓' : 'X',
        'class' => $discount->is_active ? 'is_active' : 'is_inactive',
      ),
      $discount->name,
      $discount->short_description,
      qualifying_type_name($discount->qualifying_type),
      discount_type_name($discount->discount_type),
      array(
        'data' => $total_use_count,
        'class' => 'numeric',
      ),
      array(
        'data' => $total_times_applied,
        'class' => 'numeric',
      ),
      uc_currency_format($discount->total_amount),
      uc_currency_format($discount->total_revenue),
      array(
        'data' => $discount->weight,
        'class' => 'weight',
      ),
      array(
        'data' => format_date($discount->insert_timestamp, 'small'),
        'class' => 'date',
      ),
      array(
        'data' => implode(' ', $operations),
        'class' => 'operations',
      ),
    );
  }
  if ($op == 'download') {
    $http_headers = array(
      'Pragma: no-cache',
      'Expires: 0',
      'Cache-Control: no-cache, must-revalidate',
      'Cache-Control: private',
      'Content-Transfer-Encoding: binary',
      'Content-Disposition: attachment; filename="discount_usage_report.csv"',
      'Content-Type: text/csv',
    );
    foreach ($http_headers as $http_header) {
      $http_header = preg_replace('/\\r?\\n(?!\\t| )/', '', $http_header);
      drupal_set_header($http_header);
    }
    $fp = fopen('php://output', 'w');
    array_unshift($rows, $header);
    foreach ($rows as $key => $row) {
      $csv_row = array();

      // remove Operations cell
      array_pop($row);
      foreach ($row as $cell) {
        $csv_row[] = is_array($cell) ? $cell['data'] : $cell;
      }
      if ($key > 0) {

        // make is_active CSV friendly
        $csv_row[0] = $csv_row[0] == 'X' ? 0 : 1;
      }
      fputcsv($fp, $csv_row);
    }
    fclose($fp);
    exit;
  }
  else {
    if (empty($rows)) {
      $rows[] = array(
        array(
          'data' => t('No discounts.'),
          'colspan' => count($header),
        ),
      );
    }
    $output .= theme('table', $header, $rows, array(
      'class' => 'uc-discounts',
    ));
    $output .= theme('pager', NULL, 50, 0);
    $output .= l(t('Export to CSV'), 'admin/reports/uc_discounts/all/download');
    return $output;
  }
}