You are here

function uc_reports_sales_custom in Ubercart 7.3

Same name and namespace in other branches
  1. 5 uc_reports/uc_reports.module \uc_reports_sales_custom()
  2. 6.2 uc_reports/uc_reports.admin.inc \uc_reports_sales_custom()

Displays the custom sales report form and table.

1 string reference to 'uc_reports_sales_custom'
uc_reports_menu in uc_reports/uc_reports.module
Implements hook_menu().

File

uc_reports/uc_reports.admin.inc, line 1020
Reports administration menu items.

Code

function uc_reports_sales_custom() {

  // Use default report parameters if we don't detect values in the URL.
  if (arg(5) == '') {
    $args = array(
      'start_date' => mktime(0, 0, 0, date('n'), 1, date('Y') - 1),
      'end_date' => REQUEST_TIME,
      'length' => 'month',
      'status' => uc_reports_order_statuses(),
      'detail' => FALSE,
    );
  }
  else {
    $args = array(
      'start_date' => arg(5),
      'end_date' => arg(6),
      'length' => arg(7),
      'status' => explode(',', arg(8)),
      'detail' => arg(9),
    );
  }

  // Build the header for the report table.
  $header = array(
    t('Date'),
    t('Number of orders'),
    t('Products sold'),
    t('Total revenue'),
  );

  // Build the header to the CSV export.
  $csv_rows = array(
    array(
      t('Date'),
      t('Number of orders'),
      t('Products sold'),
      t('Total revenue'),
    ),
  );

  // Grab the subreports based on the date range and the report breakdown.
  $subreports = uc_reports_subreport_intervals($args['start_date'], $args['end_date'], $args['length']);

  // Loop through the subreports and build the report table.
  foreach ($subreports as $subreport) {
    $product_data = '';
    $product_csv = '';
    $order_data = '';
    $order_csv = '';

    // Create the date title for the subreport.
    if ($args['length'] == 'day') {
      $date = format_date($subreport['start'], 'custom', variable_get('date_format_uc_store', 'm/d/Y') . ' - D');
    }
    else {
      $date = format_date($subreport['start'], 'uc_store') . ' - ' . format_date($subreport['end'], 'uc_store');
    }

    // Build the order data for the subreport.
    $result = db_query("SELECT COUNT(*) as count, title FROM {uc_orders} LEFT JOIN {uc_order_statuses} ON order_status_id = order_status WHERE :start <= created AND created <= :end AND order_status IN (:statuses) GROUP BY order_status, {uc_order_statuses}.title, {uc_order_statuses}.weight ORDER BY weight ASC", array(
      ':statuses' => $args['status'],
      ':start' => $subreport['start'],
      ':end' => $subreport['end'],
    ));
    $statuses = array();

    // Put the order counts into an array by status.
    foreach ($result as $status) {
      $statuses[] = t('!count - @title', array(
        '!count' => $status->count,
        '@title' => $status->title,
      ));
    }
    $order_data = implode('<br />', $statuses);
    $order_csv = implode("\n", $statuses);

    // Build the product data for the subreport.
    if ($args['detail']) {

      // Grab the detailed product breakdown if selected.
      $result = db_query("SELECT SUM(op.qty) as count, n.title, n.nid FROM {uc_order_products} op LEFT JOIN {uc_orders} o ON o.order_id = op.order_id LEFT JOIN {node} n ON n.nid = op.nid WHERE :start <= o.created AND o.created <= :end AND o.order_status IN (:statuses) GROUP BY n.nid ORDER BY count DESC, n.title ASC", array(
        ':statuses' => $args['status'],
        ':start' => $subreport['start'],
        ':end' => $subreport['end'],
      ));
      foreach ($result as $product_breakdown) {
        $product_data .= $product_breakdown->count . ' x ' . l($product_breakdown->title, 'node/' . $product_breakdown->nid) . "<br />\n";
        $product_csv .= $product_breakdown->count . ' x ' . $product_breakdown->title . "\n";
      }
    }
    else {

      // Otherwise just display the total number of products sold.
      $product_data = db_query("SELECT SUM(qty) FROM {uc_orders} o LEFT JOIN {uc_order_products} op ON o.order_id = op.order_id WHERE :start <= created AND created <= :end AND order_status IN (:statuses)", array(
        ':statuses' => $args['status'],
        ':start' => $subreport['start'],
        ':end' => $subreport['end'],
      ))
        ->fetchField();
      $product_csv = $product_data;
    }

    // Tally up the revenue from the orders.
    $revenue_count = db_query("SELECT SUM(order_total) FROM {uc_orders} WHERE :start <= created AND created <= :end AND order_status IN (:statuses)", array(
      ':statuses' => $args['status'],
      ':start' => $subreport['start'],
      ':end' => $subreport['end'],
    ))
      ->fetchField();

    // Add the subreport's row to the report table.
    $rows[] = array(
      $date,
      empty($order_data) ? '0' : $order_data,
      empty($product_data) ? '0' : $product_data,
      uc_currency_format($revenue_count),
    );

    // Add the data to the CSV export.
    $csv_rows[] = array(
      $date,
      empty($order_csv) ? '0' : $order_csv,
      empty($product_csv) ? '0' : $product_csv,
      $revenue_count,
    );
  }

  // Calculate the totals for the report.
  $order_total = db_query("SELECT COUNT(*) FROM {uc_orders} WHERE :start <= created AND created <= :end AND order_status IN (:statuses)", array(
    ':statuses' => $args['status'],
    ':start' => $args['start_date'],
    ':end' => $args['end_date'],
  ))
    ->fetchField();
  $product_total = db_query("SELECT SUM(qty) FROM {uc_orders} o LEFT JOIN {uc_order_products} op ON o.order_id = op.order_id WHERE :start <= created AND created <= :end AND order_status IN (:statuses)", array(
    ':statuses' => $args['status'],
    ':start' => $args['start_date'],
    ':end' => $args['end_date'],
  ))
    ->fetchField();
  $revenue_total = db_query("SELECT SUM(order_total) FROM {uc_orders} WHERE :start <= created AND created <= :end AND order_status IN (:statuses)", array(
    ':statuses' => $args['status'],
    ':start' => $args['start_date'],
    ':end' => $args['end_date'],
  ))
    ->fetchField();

  // Add the total row to the report table.
  $rows[] = array(
    t('Total'),
    $order_total,
    $product_total,
    uc_currency_format($revenue_total),
  );

  // Add the total data to the CSV export.
  $csv_rows[] = array(
    t('Total'),
    $order_total,
    $product_total,
    $revenue_total,
  );

  // Cache the CSV export.
  $csv_data = uc_reports_store_csv('uc_sales_custom', $csv_rows);

  // Build the page output holding the form, table, and CSV export link.
  $build['form'] = drupal_get_form('uc_reports_sales_custom_form', $args, $args['status']);
  $build['report'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#attributes' => array(
      'width' => '100%',
      'class' => array(
        'uc-sales-table',
      ),
    ),
  );
  $build['links'] = array(
    '#prefix' => '<div class="uc-reports-links">',
    '#suffix' => '</div>',
  );
  $build['links']['export_csv'] = array(
    '#markup' => l(t('Export to CSV file.'), 'admin/store/reports/getcsv/' . $csv_data['report'] . '/' . $csv_data['user']),
  );
  return $build;
}