You are here

function commerce_purchase_order_rules_issue_po_action in Commerce Purchase Order 7

1 string reference to 'commerce_purchase_order_rules_issue_po_action'
commerce_purchase_order_rules_action_info in ./commerce_purchase_order.rules.inc
Implements hook_rules_action_info().

File

./commerce_purchase_order.rules.inc, line 51

Code

function commerce_purchase_order_rules_issue_po_action($order, $charge = NULL, $po_number = NULL, $forced_instance_id = NULL) {
  $response = array(
    'status' => FALSE,
    'code' => 'insufficient',
    'message' => '',
    'message_variables' => array(),
  );

  // Exit if the order does not have order_id.
  if (empty($order->order_id)) {
    $response['message'] = 'Order ID is not provided.';
    return $response;
  }
  $response['message_variables'] += array(
    '@order_id' => $order->order_id,
  );

  // Exit if no user associated with the order.
  if (empty($order->uid)) {
    $response['message'] = 'Order owner not provided for order @order_id.';
    return $response;
  }
  $response['message_variables'] += array(
    '@uid' => $order->uid,
  );

  // Set charge to order balance if none provided.
  if (empty($charge)) {
    $charge = commerce_payment_order_balance($order);
  }

  // Exit if no charge.
  if (empty($charge) || empty($charge['amount']) || empty($charge['currency_code'])) {
    $response['message'] = 'Charge amount not provided for order @order_id.';
    return $response;
  }
  $response['message_variables'] += array(
    '@charge' => commerce_currency_format($charge['amount'], $charge['currency_code']),
  );

  // Default PO number to timestamp-order_id.
  if (empty($po_number)) {
    $po_number = time() . '-' . $order->order_id;
  }

  // Load payment method.
  $payment_method = commerce_payment_method_load('commerce_purchase_order');

  // Set forced instance ID
  if (!empty($forced_instance_id)) {
    $order->data['payment_method'] = $forced_instance_id;
    $response['message_variables'] += array(
      '@instance_id' => $forced_instance_id,
    );
    $payment_method['instance_id'] = $forced_instance_id;
  }

  // Void function! Assume success.
  commerce_purchase_order_transaction($payment_method, $order, $charge, $po_number);
  $response['status'] = TRUE;
  $response['code'] = 'success';
  $response['message'] = 'The payment method  @method instance (@instance_id) was successful for order @order_id, user @uid, charge amount @charge.';
  return array(
    'charge_po_response' => $response,
  );
}