You are here

function uc_reports_sales_year in Ubercart 5

Same name and namespace in other branches
  1. 6.2 uc_reports/uc_reports.admin.inc \uc_reports_sales_year()
  2. 7.3 uc_reports/uc_reports.admin.inc \uc_reports_sales_year()
1 string reference to 'uc_reports_sales_year'
uc_reports_menu in uc_reports/uc_reports.module
Implementation of hook_menu().

File

uc_reports/uc_reports.module, line 851
Displays reports on sales, customers, and products to store admin

Code

function uc_reports_sales_year() {
  $timezone_offset = time() + _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($timezone_offset, 'custom', 'Y', 0);
  }
  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'),
    ),
  );

  // 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);
    $month_end = gmmktime(23, 59, 59, $i + 1, 0, $year);

    // 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(gmdate('M Y', $month_start), 'admin/store/orders/search/results/0/0/0/0/0/0/' . $month_start . '/' . $month_end),
      $month_sales['total'],
      uc_currency_format($month_sales['income']),
      uc_currency_format($month_average),
    );

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

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

  // 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_currency_format($year_sales['income']),
    uc_currency_format($year_average),
  );

  // Add the total data to the CSV export.
  $csv_rows[] = array(
    t('Total @year', array(
      '@year' => $year,
    )),
    $year_sales['total'],
    uc_currency_format($year_sales['income']),
    uc_currency_format($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) . theme('table', $header, $rows, array(
    'width' => '100%',
    'class' => 'uc-sales-table',
  )) . '<div class="uc-reports-links">' . l(t('Export to CSV file.'), 'admin/store/reports/getcsv/' . $csv_data['report'] . '/' . $csv_data['user']) . '</div>';
  return $output;
}