You are here

private function WebformCivicrmPostProcess::processContribution in Webform CiviCRM Integration 8.5

Post-processing of contribution This happens during form post-processing

1 call to WebformCivicrmPostProcess::processContribution()
WebformCivicrmPostProcess::postSave in src/WebformCivicrmPostProcess.php
Process webform submission after it is has been saved. Called by the following hooks:

File

src/WebformCivicrmPostProcess.php, line 2262
Front-end form validation and post-processing.

Class

WebformCivicrmPostProcess

Namespace

Drupal\webform_civicrm

Code

private function processContribution() {
  $utils = \Drupal::service('webform_civicrm.utils');
  $contribution = $this->data['contribution'][1]['contribution'][1];
  $id = $this->ent['contribution'][1]['id'];

  // Save soft credits
  if (!empty($contribution['soft'])) {

    // Get total amount from total amount after line item calculation
    $amount = $this->totalContribution;

    // Get Default softcredit type
    $default_soft_credit_type = $utils
      ->wf_civicrm_api('OptionValue', 'getsingle', [
      'return' => "value",
      'option_group_id' => "soft_credit_type",
      'is_default' => 1,
    ]);
    foreach (array_filter($contribution['soft']) as $cid) {
      $utils
        ->wf_civicrm_api('contribution_soft', 'create', [
        'contact_id' => $cid,
        'contribution_id' => $id,
        'amount' => $amount,
        'currency' => wf_crm_aval($this->data, "contribution:1:currency"),
        'soft_credit_type_id' => $default_soft_credit_type['value'],
      ]);
    }
  }

  // Save honoree
  // FIXME: these api params were deprecated in 4.5, should be switched to use soft-credits when we drop support for 4.4
  if (!empty($contribution['honor_contact_id']) && !empty($contribution['honor_type_id'])) {
    $utils
      ->wf_civicrm_api('contribution', 'create', [
      'id' => $id,
      'total_amount' => $contribution['total_amount'],
      'honor_contact_id' => $contribution['honor_contact_id'],
      'honor_type_id' => $contribution['honor_type_id'],
    ]);
  }
  $contributionResult = \CRM_Contribute_BAO_Contribution::getValues([
    'id' => $id,
  ], \CRM_Core_DAO::$_nullArray, \CRM_Core_DAO::$_nullArray);

  // Save line-items
  foreach ($this->line_items as &$item) {
    if (empty($item['line_total'])) {
      continue;
    }
    if (empty($item['entity_id'])) {
      $item['entity_id'] = $id;
    }

    // tax integration
    if (empty($item['contribution_id'])) {
      $item['contribution_id'] = $id;
    }

    // Membership
    if (!empty($item['membership_id'])) {
      $item['entity_id'] = $item['membership_id'];
      $lineItemArray = $utils
        ->wf_civicrm_api('LineItem', 'get', [
        'entity_table' => "civicrm_membership",
        'entity_id' => $item['entity_id'],
      ]);
      if ($lineItemArray['count'] != 0) {

        // We only require first membership (signup) entry to make this work.
        $firstLineItem = array_shift($lineItemArray['values']);

        // Membership signup line item entry.
        // Line Item record is already present for membership by this stage.
        // Just need to upgrade contribution_id column in the record.
        if (!isset($firstLineItem['contribution_id'])) {
          $item['id'] = $firstLineItem['id'];
        }
      }
    }

    // Save the line_item
    $line_result = $utils
      ->wf_civicrm_api('line_item', 'create', $item);
    $item['id'] = $line_result['id'];
    $lineItemRecord = json_decode(json_encode($item), FALSE);

    // Add financial_item and entity_financial_trxn
    // hence that we call \CRM_Financial_BAO_FinancialItem::add() twice,
    // once to create the line item 'total amount' financial_item record and the 2nd
    // one to create the line item 'tax amount' financial_item  record.
    \CRM_Financial_BAO_FinancialItem::add($lineItemRecord, $contributionResult);
    if (!empty($lineItemRecord->tax_amount)) {
      \CRM_Financial_BAO_FinancialItem::add($lineItemRecord, $contributionResult, TRUE);
    }

    // Create participant/membership payment records
    if (isset($item['membership_id']) || isset($item['participant_id'])) {
      $type = isset($item['participant_id']) ? 'participant' : 'membership';
      $utils
        ->wf_civicrm_api("{$type}_payment", 'create', [
        "{$type}_id" => $item["{$type}_id"],
        'contribution_id' => $id,
      ]);
    }
  }
}