You are here

function uc_reports_sales_year in Ubercart 6.2

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

Display the yearly sales report form and table.

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

File

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

Code

function uc_reports_sales_year() {

  // "Now" timestamp
  $time = time();

  // Site time minus GMT time, in seconds
  $timezone_offset = _uc_reports_timezone_offset();
  $order_statuses = _uc_reports_order_statuses();

  // Get the year for the report from the URL.
  if (intval(arg(5)) == 0) {
    $year = format_date($time, 'custom', 'Y', $timezone_offset);
  }
  else {
    $year = arg(5);
  }

  // Build the header for the report table.
  $header = array(
    t('Month'),
    t('Number of orders'),
    t('Total revenue'),
    t('Average order'),
  );

  // Build the header to the CSV export.
  $csv_rows = array(
    array(
      t('Month'),
      t('Number of orders'),
      t('Total revenue'),
      t('Average order'),
    ),
  );
  $context = array(
    'revision' => 'formatted-original',
    'type' => 'amount',
  );

  // For each month of the year...
  for ($i = 1; $i <= 12; $i++) {

    // Calculate the start and end timestamps for the month in local time.
    $month_start = gmmktime(0, 0, 0, $i, 1, $year) - $timezone_offset;
    $month_end = gmmktime(23, 59, 59, $i + 1, 0, $year) - $timezone_offset;

    // Get the sales report for the month.
    $month_sales = _uc_reports_get_sales($month_start, 'month');

    // Calculate the average order total for the month.
    if ($month_sales['total'] != 0) {
      $month_average = round($month_sales['income'] / $month_sales['total'], 2);
    }
    else {
      $month_average = 0;
    }

    // Add the month's row to the report table.
    $rows[] = array(
      l(format_date($month_start, 'custom', 'M Y', $timezone_offset), 'admin/store/orders/search/results/0/0/0/0/0/0/' . $month_start . '/' . $month_end),
      $month_sales['total'],
      uc_price($month_sales['income'], $context),
      uc_price($month_average, $context),
    );

    // Add the data to the CSV export.
    $csv_rows[] = array(
      format_date($month_start, 'custom', 'M Y', $timezone_offset),
      $month_sales['total'],
      $month_sales['income'],
      $month_average,
    );
  }

  // Calculate the start and end timestamps for the year in local time.
  $year_start = gmmktime(0, 0, 0, 1, 1, $year) - $timezone_offset;
  $year_end = gmmktime(23, 59, 59, 1, 0, $year + 1) - $timezone_offset;

  // Get the sales report for the year.
  $year_sales = _uc_reports_get_sales($year_start, 'year');

  // Calculate the average order total for the year.
  if ($year_sales['total'] != 0) {
    $year_average = round($year_sales['income'] / $year_sales['total'], 2);
  }
  else {
    $year_average = 0;
  }

  // Add the total row to the report table.
  $rows[] = array(
    l(t('Total @year', array(
      '@year' => $year,
    )), 'admin/store/orders/search/results/0/0/0/0/0/0/' . $year_start . '/' . $year_end),
    $year_sales['total'],
    uc_price($year_sales['income'], $context),
    uc_price($year_average, $context),
  );

  // Add the total data to the CSV export.
  $csv_rows[] = array(
    t('Total @year', array(
      '@year' => $year,
    )),
    $year_sales['total'],
    $year_sales['income'],
    $year_average,
  );

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

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