OrderReportGenerator.php in Commerce Reporting 8
File
src/OrderReportGenerator.php
View source
<?php
namespace Drupal\commerce_reports;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class OrderReportGenerator implements OrderReportGeneratorInterface {
protected $orderStorage;
protected $orderReportStorage;
protected $reportTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, ReportTypeManager $report_type_manager) {
$this->orderStorage = $entity_type_manager
->getStorage('commerce_order');
$this->orderReportStorage = $entity_type_manager
->getStorage('commerce_order_report');
$this->reportTypeManager = $report_type_manager;
}
public function generateReports(array $order_ids, $plugin_id = NULL) {
$orders = $this->orderStorage
->loadMultiple($order_ids);
$plugin_types = $this->reportTypeManager
->getDefinitions();
$generated = 0;
if ($plugin_id) {
if (!isset($plugin_types[$plugin_id])) {
return $generated;
}
$plugin_types = [
$plugin_types[$plugin_id],
];
}
foreach ($orders as $order) {
if (empty($order
->getPlacedTime())) {
continue;
}
foreach ($plugin_types as $plugin_type) {
$instance = $this->reportTypeManager
->createInstance($plugin_type['id'], []);
$instance
->generateReports($order);
}
$generated++;
}
return $generated;
}
public function refreshReports(array $order_ids, $plugin_id = NULL) {
$query = $this->orderReportStorage
->getQuery()
->condition('order_id', $order_ids, 'IN');
if ($plugin_id) {
$query
->condition('type', $plugin_id);
}
$order_report_ids = $query
->execute();
$reports = $this->orderReportStorage
->loadMultiple($order_report_ids);
$this->orderReportStorage
->delete($reports);
return $this
->generateReports($order_ids, $plugin_id);
}
}