You are here

function uc_discounts_report_for_discount in Ubercart Discounts (Alternative) 6.2

Generate report for discount.

1 string reference to 'uc_discounts_report_for_discount'
uc_discounts_menu in uc_discounts/uc_discounts.module
Implementation of hook_menu().

File

uc_discounts/uc_discounts.admin.inc, line 962

Code

function uc_discounts_report_for_discount($discount_id) {

  //Add link to all discount data
  $output = l(t("Report for all discounts."), "admin/reports/uc_discounts/all");
  $output .= "<br/><br/>";

  //Add table for discount's data
  $header = array(
    t("Name"),
    t("Uses"),
    t("Times Applied"),
    t("Discounted Amount"),
    t("Revenue Amount"),
    array(
      "data" => t("Operations"),
      "colspan" => 3,
    ),
  );
  $query = "SELECT d.*, COUNT(du.discount_use_id) total_use_count,\n                            SUM(du.times_applied) total_times_applied, SUM(du.amount) total_amount, 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                          WHERE d.discount_id=%d\n                          GROUP BY d.discount_id";
  $discount = db_fetch_object(db_query($query, $discount_id));
  $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;
  $discounts[] = array(
    $discount->name,
    $total_use_count,
    $total_times_applied,
    uc_currency_format($discount->total_amount),
    uc_currency_format($discount->total_revenue),
    l(t("edit"), "admin/store/uc_discounts/edit/" . $discount->discount_id),
    l(t("copy"), "admin/store/uc_discounts/copy/" . $discount->discount_id),
    l(t("delete"), "admin/store/uc_discounts/delete/" . $discount->discount_id),
  );
  $output .= theme("table", $header, $discounts);
  $output .= "<br/><br/>";

  //Add table of discount's usage data
  $header = array(
    array(
      "data" => t("User"),
      "field" => "user_id",
    ),
    array(
      "data" => t("Order"),
      "field" => "order_id",
    ),
    array(
      "data" => t("Code"),
      "field" => "code",
    ),
    array(
      "data" => t("Times Applied"),
      "field" => "times_applied",
    ),
    array(
      "data" => t("Amount"),
      "field" => "amount",
    ),
    array(
      "data" => t("Date"),
      "field" => "insert_timestamp",
      "sort" => "asc",
    ),
  );
  $query = sprintf("SELECT du.*, u.uid user_id, u.name username, u.mail email FROM {uc_discounts_uses} du" . " LEFT JOIN {users} u ON du.user_id=u.uid" . " WHERE discount_id=%d", $discount_id);
  $tablesort = tablesort_sql($header);
  $result = pager_query($query . $tablesort, 50);
  $rows = array();
  while ($use = db_fetch_object($result)) {
    $user_description = $use->user_id != 0 ? $use->username . " (" . $use->email . ")" : t("Anonymous");
    $rows[] = array(
      "data" => array(
        $user_description,
        $use->order_id,
        !empty($use->code) ? $use->code : check_plain("<" . t("no code") . ">"),
        $use->times_applied,
        uc_currency_format($use->amount),
        date("Y-m-d H:i:s", $use->insert_timestamp),
      ),
    );
  }
  if (empty($rows)) {
    $rows[] = array(
      array(
        "data" => t("No discount data available."),
        "colspan" => 11,
      ),
    );
  }
  $output .= theme("table", $header, $rows, array(
    "id" => "uc_discounts_report",
  ));
  $output .= theme("pager", NULL, 50, 0);
  return $output;
}