You are here

function uc_coupon_reports in Ubercart Discount Coupons 5

Same name and namespace in other branches
  1. 6 uc_coupon.reports.inc \uc_coupon_reports()
  2. 7.3 uc_coupon.reports.inc \uc_coupon_reports()
  3. 7.2 uc_coupon.reports.inc \uc_coupon_reports()

Output Coupon Reports

TODO: Integrate with UberCart reports functionality.

1 string reference to 'uc_coupon_reports'
uc_coupon_menu in ./uc_coupon.module
Implementation of hook_menu().

File

./uc_coupon.module, line 1180
Provides discount coupons for Ubercart.

Code

function uc_coupon_reports($start = NULL, $end = NULL, $statuses = NULL) {
  drupal_add_css(drupal_get_path('module', 'uc_coupon') . '/reports.css', 'uc_coupon');
  if (is_null($statuses)) {
    $statuses = variable_get('uc_reports_reported_statuses', array(
      'completed',
    ));
  }
  else {
    $statuses = explode(',', $statuses);
  }
  $output = drupal_get_form('uc_coupon_reports_form', $start, $end, $statuses);
  if (isset($start) && isset($end)) {
    $query = db_query("SELECT co.cid, co.oid, co.value, co.code, o.order_total, o.created FROM {uc_coupons_orders} AS co LEFT JOIN {uc_orders} AS o ON (co.oid = o.order_id) WHERE o.created > %d AND o.created < %d AND o.order_status IN ('" . implode("', '", $statuses) . "') ORDER BY co.cid, o.created ASC", $start, $end);
    $row_header = array(
      t('Order #'),
      t('Purchase date'),
      t('Total'),
      t('Coupon value'),
    );
    $last_cid = 0;
    while ($row = db_fetch_object($query)) {

      // Display the table of coupons if this is the next set of coupons
      if ($row->cid != $last_cid and $last_cid != 0) {
        $td[] = array(
          '',
          '<b>' . t('Uses: @total', array(
            '@total' => $num_uses,
          )) . '</b>',
          '<b>' . uc_currency_format($coupon_sale_amount) . '</b>',
          '<b>' . uc_currency_format($coupon_amount) . '</b>',
        );
        $data .= theme('table', $row_header, $td, array(
          'width' => '100%',
        ));
        $td = array();
        $num_uses = 0;
        $coupon_amount = 0;
        $coupon_sale_amount = 0;
      }

      // if this is the first coupon of the set display the header first
      if ($row->cid != $last_cid || ($last_cid = 0)) {
        $data .= '<div class="totals">' . t('Coupon code: !link', array(
          '!link' => l($row->code, 'admin/store/customers/coupon/' . $row->cid . '/edit'),
        )) . '</div>';
      }
      $td[] = array(
        l('#' . $row->oid, 'admin/store/orders/' . $row->oid),
        format_date($row->created, 'custom', variable_get('uc_date_format_default', 'm/d/Y')),
        uc_currency_format($row->order_total),
        uc_currency_format($row->value),
      );
      $num_uses++;
      $coupon_amount += $row->value;
      $coupon_sale_amount += $row->order_total;
      $last_cid = $row->cid;
      $orders_total += $row->order_total;
      $coupons_total += $row->value;
    }
    $td[] = array(
      '',
      '<b>' . t('Uses: @total', array(
        '@total' => $num_uses,
      )) . '</b>',
      '<b>' . uc_currency_format($coupon_sale_amount) . '</b>',
      '<b>' . uc_currency_format($coupon_amount) . '</b>',
    );
    $data .= theme('table', $row_header, $td, array(
      'width' => '100%',
    ));
    $output .= '<h2>' . t('Coupon usage report') . '</h2>';
    $output .= $data;
    $output .= '<br /><table width="100%"><tr>';
    $output .= '<td>' . t('Coupons used: @total', array(
      '@total' => db_num_rows($query),
    )) . '</td>';
    $output .= '<td>' . t('Orders total: @total', array(
      '@total' => uc_currency_format($orders_total),
    )) . '</td>';
    $output .= '<td>' . t('Coupons total: @total', array(
      '@total' => uc_currency_format($coupons_total),
    )) . '</td>';
    $output .= '</tr></table>';
  }
  return $output;
}