You are here

function commerce_recurring_rules_generate_order_from_recurring in Commerce Recurring Framework 7.2

Generate the orders from the recurring entity.

1 string reference to 'commerce_recurring_rules_generate_order_from_recurring'
commerce_recurring_rules_action_info in ./commerce_recurring.rules.inc
Implements hook_rules_action_info().

File

./commerce_recurring.rules.inc, line 478
Rules integration for recurring entities.

Code

function commerce_recurring_rules_generate_order_from_recurring() {

  // First arg is the commerce recurring entity and second is the due date.
  $args = func_get_args();
  $extra_arguments = array();
  $i = 0;

  // Get the number of arguments that the action should have.
  $actions = rules_fetch_data('action_info');
  foreach ($actions['commerce_recurring_generate_order_from_recurring']['parameter'] as $name => $parameter) {
    if ($name == 'commerce_recurring') {
      $recurring_entity = $args[$i];
    }
    elseif ($name == 'timestamp') {
      $due_date = $args[$i];
    }
    else {
      $extra_arguments[$name] = $args[$i];
    }
    $i++;
  }

  // First we get the due date, default to now if empty.
  if (empty($due_date)) {
    $due_date = new DateObject();
    $due_date = $due_date
      ->getTimestamp();
  }

  // Get the product out of the recurring entity.
  $recurring_wrapper = entity_metadata_wrapper('commerce_recurring', $recurring_entity);
  $product_wrapper = $recurring_wrapper->commerce_recurring_ref_product;

  // @TODO Take care of order fail handling? http://drupal.org/node/1969350
  // If the due date is over or equal to the end date, stop the recurring.
  if (!empty($recurring_entity->end_date) && $due_date >= $recurring_entity->end_date) {
    commerce_recurring_stop_recurring($recurring_entity);
    return;
  }

  // If the recurring entity has been disabled, don't go forward.
  if (!$recurring_entity->status) {
    return;
  }

  // Generate a new order with that product.
  $order = commerce_order_new($recurring_entity->uid, 'recurring_pending');
  $order->log = t('Created from recurring entity.');

  // Add the recurring entity id to the data array from the order to keep track.
  $order->data['recurring_entity'] = $recurring_entity->id;
  foreach ($extra_arguments as $name => $argument) {
    $order->{$name} = $argument;
  }
  commerce_order_save($order);

  // We need to add a flag to the line item to be able to know that we're in a
  // recurring context in rules.
  $data = array(
    'recurring_entity' => $recurring_entity->id,
  );
  $line_item = commerce_product_line_item_new($product_wrapper
    ->value(), $recurring_entity->quantity, 0, $data);
  $line_item->order_id = $order->order_id;

  // Update the current line item price.
  rules_invoke_event('commerce_product_calculate_sell_price', $line_item);
  commerce_line_item_save($line_item);
  $order->commerce_line_items[LANGUAGE_NONE][0]['line_item_id'] = $line_item->line_item_id;
  commerce_order_save($order);
  $recurring_wrapper->commerce_recurring_order[] = $order->order_id;
  entity_save('commerce_recurring', $recurring_wrapper
    ->value());
  return array(
    'commerce_order' => $order,
  );
}