public function Reports::yearSales in Ubercart 8.4
Displays the yearly sales report form and table.
1 string reference to 'Reports::yearSales'
- uc_report.routing.yml in uc_report/
uc_report.routing.yml - uc_report/uc_report.routing.yml
File
- uc_report/
src/ Controller/ Reports.php, line 686
Class
- Reports
- Provides reports for Ubercart.
Namespace
Drupal\uc_report\ControllerCode
public function yearSales() {
// 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 = [
$this
->t('Month'),
$this
->t('Number of orders'),
$this
->t('Total revenue'),
$this
->t('Average order'),
];
// Build the header to the CSV export.
$csv_rows = [
[
$this
->t('Month'),
$this
->t('Number of orders'),
$this
->t('Total revenue'),
$this
->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 = self::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[] = [
Link::fromTextAndUrl(date('M Y', $month_start), Url::fromUri('base:admin/store/orders/search/results/0/0/0/0/0/0/' . $month_start . '/' . $month_end))
->toString(),
$month_sales['total'],
uc_currency_format($month_sales['income']),
uc_currency_format($month_average),
];
// Add the data to the CSV export.
$csv_rows[] = [
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 = self::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[] = [
Link::fromTextAndUrl($this
->t('Total @year', [
'@year' => $year,
]), Url::fromUri('base:admin/store/orders/search/results/0/0/0/0/0/0/' . $year_start . '/' . $year_end))
->toString(),
$year_sales['total'],
uc_currency_format($year_sales['income']),
uc_currency_format($year_average),
];
// Add the total data to the CSV export.
$csv_rows[] = [
$this
->t('Total @year', [
'@year' => $year,
]),
$year_sales['total'],
$year_sales['income'],
$year_average,
];
// Cache the CSV export.
$csv_data = $this
->store_csv('uc_sales_yearly', $csv_rows);
// Build the page output holding the form, table, and CSV export link.
$build['form'] = $this
->formBuilder()
->getForm('uc_report_sales_year_form', $year);
$build['report'] = [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => [
'width' => '100%',
'class' => [
'uc-sales-table',
],
],
];
$build['links'] = [
'#prefix' => '<div class="uc-reports-links">',
'#suffix' => '</div>',
];
$build['links']['export_csv'] = [
'#markup' => Link::createFromRoute($this
->t('Export to CSV file.'), 'uc_report.getcsv', [
'report_id' => $csv_data['report'],
'user_id' => $csv_data['user'],
])
->toString(),
];
return $build;
}