You are here

function commerce_payment_handler_area_totals::render in Commerce Core 7

Render the area.

Overrides views_handler_area::render

File

modules/payment/includes/views/handlers/commerce_payment_handler_area_totals.inc, line 64

Class

commerce_payment_handler_area_totals
Defines a handler area that provides payment totals, the order balance, and a form to add new payments.

Code

function render($empty = FALSE) {

  // Load an order object for the View if a single order argument is present.
  if (array_key_exists('order_id', $this->view->argument) && !array_key_exists('order_id_1', $this->view->argument) && !empty($this->view->args[$this->view->argument['order_id']->position])) {

    // Load the specified order.
    $order = commerce_order_load($this->view->args[$this->view->argument['order_id']->position]);
  }
  else {

    // Otherwise indicate a valid order is not present.
    $order = FALSE;
  }

  // Calculate a total of successful payments for each currency.
  $transaction_statuses = commerce_payment_transaction_statuses();
  $totals = array();
  foreach ($this->view->result as $result) {
    $status = $this
      ->get_value($result, 'status');
    $currency_code = $this
      ->get_value($result, 'currency_code');
    $amount = $this
      ->get_value($result, 'amount');

    // If the payment transaction status indicates it should include the
    // current transaction in the total...
    if (!empty($transaction_statuses[$status]) && $transaction_statuses[$status]['total']) {

      // Add the transaction to its currency's running total if it exists...
      if (isset($totals[$currency_code])) {
        $totals[$currency_code] += $amount;
      }
      else {

        // Or begin a new running total for the currency.
        $totals[$currency_code] = $amount;
      }
    }
  }

  // Build and render the form to add a payment if the View contains a valid
  // order argument.
  if ($this->options['add_payment_form'] && $order) {
    module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.forms');
    $content = drupal_get_form('commerce_payment_order_transaction_add_form', $order);
    $form = drupal_render($content);
  }
  else {
    $form = NULL;
  }

  // Prepare variables for use in the theme function.
  $variables = array(
    'rows' => commerce_payment_totals_rows($totals, $order),
    'form' => $form,
    'view' => $this->view,
    'totals' => $totals,
    'order' => $order,
  );
  return theme('commerce_payment_totals', $variables);
}