You are here

function uc_coupon_reports in Ubercart Discount Coupons 7.3

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

Output coupon report.

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

File

./uc_coupon.reports.inc, line 77
Discount Coupons reports pages.

Code

function uc_coupon_reports($start = NULL, $end = NULL, $statuses = NULL) {
  drupal_add_css(drupal_get_path('module', 'uc_coupon') . '/reports.css', array(
    'type' => 'uc_coupon',
  ));
  if (is_null($statuses)) {
    $statuses = variable_get('uc_reports_reported_statuses', array(
      'completed',
    ));
  }
  else {
    $statuses = explode(',', $statuses);
  }
  $output = drupal_render(drupal_get_form('uc_coupon_reports_form', $start, $end, $statuses));
  if (isset($start) && isset($end)) {
    $result = db_query("SELECT co.cid, co.oid, co.value, co.code, o.order_total, o.created FROM {uc_coupons_orders} AS co \n    \tLEFT JOIN {uc_orders} AS o ON (co.oid = o.order_id) \n    \tWHERE o.created > :start AND o.created < :end AND o.order_status IN (:statuses) \n    \tORDER BY co.cid, o.created ASC", array(
      ':start' => $start,
      ':end' => $end,
      ':statuses' => $statuses,
    ));
    $total = 0;
    $row_header = array(
      t('Order #'),
      t('Purchase date'),
      t('Total'),
      t('Coupon value'),
    );
    $last_cid = $orders_total = $coupons_total = 0;
    $data = '';
    $num_uses = 0;
    $coupon_sale_amount = 0;
    $coupon_amount = 0;
    foreach ($result as $row) {

      // Display the table of coupons if this is the next set of coupons
      if ($row->cid != $last_cid) {
        if ($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', array(
            'header' => $row_header,
            'rows' => $td,
            'attributes' => 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/coupons/' . $row->cid . '/edit'),
        )) . '</div>';
      }
      $td[] = array(
        l('#' . $row->oid, 'admin/store/orders/' . $row->oid),
        format_date($row->created, 'custom', variable_get('date_format_uc_store', '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;
      $total++;
    }
    $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', array(
      'header' => $row_header,
      'rows' => $td,
      'attributes' => 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' => $total,
    )) . '</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;
}