function uc_reports_sales_year in Ubercart 7.3
Same name and namespace in other branches
- 5 uc_reports/uc_reports.module \uc_reports_sales_year()
- 6.2 uc_reports/uc_reports.admin.inc \uc_reports_sales_year()
Displays 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 875 - Reports administration menu items.
Code
function uc_reports_sales_year() {
$order_statuses = uc_reports_order_statuses();
// Get the year for the report from the URL.
if (intval(arg(5)) == 0) {
$year = date('Y');
}
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 = mktime(0, 0, 0, $i, 1, $year);
$month_end = mktime(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(date('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(
date('M Y', $month_start),
$month_sales['total'],
$month_sales['income'],
$month_average,
);
}
// Calculate the start and end timestamps for the year in local time.
$year_start = mktime(0, 0, 0, 1, 1, $year);
$year_end = mktime(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'],
$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.
$build['form'] = drupal_get_form('uc_reports_sales_year_form', $year);
$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;
}