You are here

function commerce_recurring_process_recurring in Commerce Recurring Framework 7

Rules action: create a new order duplicting the given line item on the new order.

Parameters

$master_order object: The master order object (first in the sequence)

$line_items array: array of line items on the order

File

./commerce_recurring.rules.inc, line 185

Code

function commerce_recurring_process_recurring($master_order) {

  // Create a new order
  $recurring_order = commerce_order_new($master_order->uid, 'pending', 'recurring_order');
  $recurring_order->commerce_customer_billing = $master_order->commerce_customer_billing;

  // Save it so it gets an order ID and return the full object.
  commerce_order_save($recurring_order);

  // Wrap the order for easy access to field data.
  $recurring_order_wrapper = entity_metadata_wrapper('commerce_order', $recurring_order);

  // Wrap the master order for easy access to field data.
  $master_order_wrapper = entity_metadata_wrapper('commerce_order', $master_order);
  foreach ($master_order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {

    // Load and validate the specified product ID.
    $product = $line_item_wrapper->commerce_product
      ->value();
    if ($product->type != 'recurring') {

      // We don't need this item
      continue;
    }
    $interval = field_get_items('commerce_product', $product, 'commerce_recurring_interval');

    // Get first value
    $interval = $interval[0];
    $interval_label = interval_format_interval($interval);

    // Create the new product line item.
    $new_line_item = commerce_product_line_item_new($product, $line_item_wrapper->quantity
      ->value(), $recurring_order->order_id, array(), $line_item_wrapper->type
      ->value());
    $new_line_item->line_item_label = t('Recurring !old_label (charged every !interval)', array(
      '!old_label' => $new_line_item->line_item_label,
      '!interval' => $interval_label,
    ));

    // Save the line item now so we get its ID.
    commerce_line_item_save($new_line_item);

    // Add it to the order's line item reference value.
    $recurring_order_wrapper->commerce_line_items[] = $new_line_item;
  }

  // Set the parent order
  $recurring_order->commerce_recurring_parent_order[LANGUAGE_NONE][0]['order_id'] = $master_order->order_id;

  // Set the payment due date
  $payment_interval = variable_get('commerce_recurring_payment_interval', 3);
  $payment_dt = new DateObject('now');
  if ($payment_interval) {
    $payment_dt
      ->modify("+{$payment_interval} days");
  }
  $recurring_order->commerce_recurring_payment_due[LANGUAGE_NONE][0]['value'] = $payment_dt
    ->format('U');

  // Mark as unprocessed
  $recurring_order->commerce_recurring_payment[LANGUAGE_NONE][0]['value'] = 0;

  // Save the updated order.
  commerce_order_save($recurring_order);

  // Invoke rules event to notify others that the new order has been created
  rules_invoke_all('commerce_recurring_rules_event_new_order', $recurring_order);

  // Get the due date
  $due_date = $master_order_wrapper->commerce_recurring_next_due
    ->value();

  // Create an object
  $due_date_obj = new DateObject($due_date, date_default_timezone(), 'U');

  // Apply the inteval
  if ($interval && interval_apply_interval($due_date_obj, $interval)) {

    // Update the parent next due
    $master_order->commerce_recurring_next_due[LANGUAGE_NONE][0]['value'] = $due_date_obj
      ->format('U');

    // Save the parent
    commerce_order_save($master_order);
  }
  watchdog('Commerce Recurring', 'Generated new recurring order @new_id as child of recurring master @master_id.', array(
    '@master_id' => $master_order->order_id,
    '@new_id' => $recurring_order->order_id,
  ), WATCHDOG_NOTICE);
  return array(
    'recurring_order' => $recurring_order,
  );
}