You are here

public function CommercePayment::import in Commerce Migrate 3.0.x

Same name and namespace in other branches
  1. 8.2 modules/ubercart/src/Plugin/migrate/destination/CommercePayment.php \Drupal\commerce_migrate_ubercart\Plugin\migrate\destination\CommercePayment::import()
  2. 3.1.x modules/ubercart/src/Plugin/migrate/destination/CommercePayment.php \Drupal\commerce_migrate_ubercart\Plugin\migrate\destination\CommercePayment::import()

Throws

\Drupal\migrate\MigrateException When an entity cannot be looked up.

\Drupal\migrate\Exception\EntityValidationException When an entity validation hasn't been passed.

Overrides EntityContentBase::import

File

modules/ubercart/src/Plugin/migrate/destination/CommercePayment.php, line 78

Class

CommercePayment
Commerce payment destination for Ubercart.

Namespace

Drupal\commerce_migrate_ubercart\Plugin\migrate\destination

Code

public function import(Row $row, array $old_destination_id_values = []) {
  $amount = $row
    ->getDestinationProperty('amount/number');
  if ($amount >= 0) {

    // Not a refund, nothing to do here.
    return parent::import($row, $old_destination_id_values);
  }
  else {
    $saved_ids = 0;

    // This is a refund and it needs to be attached to a commerce payment.
    // Search all existing payments for this order to find a suitable payment
    // or payments to add the refund to. The refund may be spread across more
    // than one payment.
    // Get all the existing payments for this order from the destination.
    $order_id = $row
      ->getSourceProperty('order_id');
    $query = $this->entityTypeManager
      ->getStorage('commerce_payment')
      ->getQuery();
    $ids = $query
      ->condition('order_id', $order_id)
      ->sort('payment_id')
      ->execute();
    $payments = $this->entityTypeManager
      ->getStorage('commerce_payment')
      ->loadMultiple($ids);
    $current_refund = strval(abs($amount));

    /** @var \Drupal\commerce_payment\Entity\Payment $payment */
    foreach ($payments as $payment) {

      // Loop through all payments adding the current refund amount, or a
      // portion thereof, to the current payment. The refund amount is not to
      // be more than the payment amount.
      $paid_amount = $payment
        ->getAmount()
        ->getNumber();
      if ($paid_amount > 0) {

        // Only add refunds to payments with a positive payment amount.
        $refund_number = $payment
          ->getRefundedAmount()
          ->getNumber();
        $total_refund_amount = Calculator::add($refund_number, $current_refund);
        $diff = Calculator::subtract($paid_amount, $total_refund_amount);
        if ($diff < 0) {

          // The paid amount does not cover any existing refund plus the
          // current refund. Set the refund amount to the paid amount of the
          // current payment.
          $new_refund_amount = $paid_amount;
          $state = 'refunded';
        }
        else {

          // The total current refund amount can be attached to this payment.

          /** @var \Drupal\commerce_price\Price $new_refund_amount */
          $new_refund_amount = strval(abs($total_refund_amount));
          $state = Calculator::subtract($paid_amount, $total_refund_amount) == 0 ? 'refunded' : 'partially_refunded';
        }

        // Set the calculated values in the destination row.
        $row
          ->setDestinationProperty('payment_id', $payment
          ->id());
        $row
          ->setDestinationProperty('amount/number', $paid_amount);
        $row
          ->setDestinationProperty('amount/currency_code', $payment
          ->getAmount()
          ->getCurrencyCode());
        $row
          ->setDestinationProperty('refunded_amount/number', $new_refund_amount);
        $row
          ->setDestinationProperty('refunded_amount/currency_code', $payment
          ->getRefundedAmount()
          ->getCurrencyCode());
        $row
          ->setDestinationProperty('state', $state);

        // Update the entity and save.
        parent::updateEntity($payment, $row);
        $payment
          ->setState($state);
        $saved_ids = $payment
          ->save();

        // Update the current refund amount.
        $current_refund = Calculator::subtract($current_refund, Calculator::subtract($new_refund_amount, $refund_number));
        if ($current_refund == 0) {
          break;
        }
      }
    }
    if ($current_refund != 0) {
      $payment_id = $row
        ->getDestinationProperty('payment_id');
      $message = 'Refund exceeds payments for payment ' . $payment_id;
      $source_ids = [
        'receipt_id' => $payment_id,
      ];
      $this->migration
        ->getIdMap()
        ->saveMessage($source_ids, $message, MigrationInterface::MESSAGE_INFORMATIONAL);
    }
    return [
      $saved_ids,
    ];
  }
}