You are here

public function OrderReportGenerator::generateReports in Commerce Reporting 8

Generates order reports for the given order IDs.

New order reports are created for all orders that have been placed, regardless of whether order reports already exist for the orders. Specify a report type plugin id to generate reports for a single report type; otherwise, reports for all types will be generated.

Parameters

array $order_ids: An array of order IDs.

string $plugin_id: (optional) The report type plugin id to be used to generate reports.

Return value

int The number of orders for which reports were generated.

Overrides OrderReportGeneratorInterface::generateReports

1 call to OrderReportGenerator::generateReports()
OrderReportGenerator::refreshReports in src/OrderReportGenerator.php
Refreshes order reports for the given order IDs.

File

src/OrderReportGenerator.php, line 52

Class

OrderReportGenerator
Generates order reports for orders.

Namespace

Drupal\commerce_reports

Code

public function generateReports(array $order_ids, $plugin_id = NULL) {
  $orders = $this->orderStorage
    ->loadMultiple($order_ids);
  $plugin_types = $this->reportTypeManager
    ->getDefinitions();
  $generated = 0;

  // Generate reports for a single report type.
  if ($plugin_id) {
    if (!isset($plugin_types[$plugin_id])) {
      return $generated;
    }
    $plugin_types = [
      $plugin_types[$plugin_id],
    ];
  }

  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  foreach ($orders as $order) {

    // Do not generate order reports for orders that have not been placed.
    if (empty($order
      ->getPlacedTime())) {
      continue;
    }
    foreach ($plugin_types as $plugin_type) {

      /** @var \Drupal\commerce_reports\Plugin\Commerce\ReportType\ReportTypeInterface $instance */
      $instance = $this->reportTypeManager
        ->createInstance($plugin_type['id'], []);
      $instance
        ->generateReports($order);
    }
    $generated++;
  }
  return $generated;
}