You are here

public function ReportQueryBuilder::alterQuery in Commerce Reporting 8

Alters the report query.

Parameters

\Drupal\Core\Database\Query\AlterableInterface $query: The alterable query.

File

src/ReportQueryBuilder.php, line 71

Class

ReportQueryBuilder
Provides an aggregate query builder for order reports.

Namespace

Drupal\commerce_reports

Code

public function alterQuery(AlterableInterface $query) {
  if ($query instanceof SelectInterface) {
    $report_date_format = $query
      ->getMetaData('report_date_format');
    if (empty($report_date_format)) {
      $report_date_format = 'F Y';
    }
    if (Database::getConnection()
      ->databaseType() == 'mysql') {
      $field = 'FROM_UNIXTIME(base_table.created)';
    }
    else {
      $field = 'base_table.created';
    }
    $expression = $this
      ->getDateFormat($field, $report_date_format);
    $query
      ->addExpression($expression, 'formatted_date');
    $query
      ->groupBy('formatted_date');
    $query
      ->orderBy('formatted_date', 'DESC');
  }
}