You are here

function PaymentEntityController::saveAttachedData in Payment 7

Save the line items and payment status items if needed.

Parameters

Payment $payment: The payment object.

File

./payment.classes.inc, line 403
The API and related functions for executing and managing payments.

Class

PaymentEntityController
Entity API controller for payment entities.

Code

function saveAttachedData($payment) {

  // Save line items.
  foreach ($payment->line_items as $line_item) {
    $schema = drupal_get_schema('payment_line_item');
    $fields = $schema['fields'];
    $data = array();
    foreach ($fields as $column => $spec) {
      if (property_exists($line_item, $column)) {
        $data[$column] = $line_item->{$column};
        if (!empty($spec['serialize'])) {
          $data[$column] = serialize($data[$column]);
        }
      }
    }
    unset($data['pid']);
    unset($data['name']);
    $data['amount_total'] = $line_item->amount * $line_item->quantity * ($line_item->tax_rate + 1);
    db_merge('payment_line_item')
      ->key(array(
      'name' => $line_item->name,
      'pid' => $payment->pid,
    ))
      ->fields($data)
      ->execute();
  }

  // Save the payment's status items.
  $update = empty(reset($payment->statuses)->psiid) || empty(end($payment->statuses)->psiid);
  foreach ($payment->statuses as $status_item) {

    // Statuses cannot be edited, so only save the ones without a PSIID set.
    if (!$status_item->psiid) {
      $status_item->pid = $payment->pid;
      drupal_write_record('payment_status_item', $status_item);
    }
  }
  if ($update) {
    $payment->psiid_first = reset($payment->statuses)->psiid;
    $payment->psiid_last = end($payment->statuses)->psiid;
    $query = db_update('payment')
      ->condition('pid', $payment->pid)
      ->fields(array(
      'psiid_first' => reset($payment->statuses)->psiid,
      'psiid_last' => end($payment->statuses)->psiid,
    ));
    $query
      ->execute();
  }
}