public static function OrderReportGenerateForm::processBatch in Commerce Reporting 8
Processes the batch and generates the order reports.
Parameters
array $context: The batch context information.
File
- src/
Form/ OrderReportGenerateForm.php, line 113
Class
- OrderReportGenerateForm
- Profides a form for bulk generating order reports.
Namespace
Drupal\commerce_reports\FormCode
public static function processBatch($plugin_id, array &$context) {
$order_storage = \Drupal::entityTypeManager()
->getStorage('commerce_order');
// Initialization.
if (empty($context['sandbox'])) {
$context['results']['total_generated'] = 0;
// Determine maximum id for a non-draft order.
$order_ids = $order_storage
->getQuery()
->condition('state', 'draft', '<>')
->sort('order_id', 'DESC')
->range(0, 1)
->execute();
// No orders to process.
if (empty($order_ids)) {
$context['finished'] = 1;
return;
}
$context['sandbox']['maximum_id'] = reset($order_ids);
$context['sandbox']['current_offset'] = 0;
}
$maximum_id = $context['sandbox']['maximum_id'];
$current_offset =& $context['sandbox']['current_offset'];
$max_remaining = $maximum_id - $current_offset;
if ($max_remaining < 1) {
$context['finished'] = 1;
return;
}
$limit = $max_remaining < self::BATCH_SIZE ? $max_remaining : self::BATCH_SIZE;
// Get a batch of orders to be processed.
$batch_orders_ids = $order_storage
->getQuery()
->sort('order_id')
->range($current_offset, $limit)
->execute();
// Generate order reports for the batch (after deleting any existing reports).
$order_report_generator = \Drupal::service('commerce_reports.order_report_generator');
$generated = $order_report_generator
->refreshReports($batch_orders_ids, $plugin_id);
$current_offset += $limit;
$context['results']['total_generated'] += $generated;
// Finished when order with maximum id has been processed in the batch.
$batch_maximum_id = end($batch_orders_ids);
if ($batch_maximum_id >= $maximum_id) {
$context['finished'] = 1;
return;
}
$context['message'] = t('Generated reports for @created orders of @total_quantity.', [
'@created' => $batch_maximum_id,
'@total_quantity' => $maximum_id,
]);
$context['finished'] = $batch_maximum_id / $maximum_id;
}