View source
<?php
namespace Drupal\commerce_migrate_ubercart\Plugin\migrate\destination;
use Drupal\commerce_price\Calculator;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CommercePayment extends EntityContentBase {
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles, $entity_field_manager, $field_type_manager);
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
$entity_type = static::getEntityTypeId($plugin_id);
return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
->get('entity_type.manager')
->getStorage($entity_type), array_keys($container
->get('entity_type.bundle.info')
->getBundleInfo($entity_type)), $container
->get('entity_field.manager'), $container
->get('plugin.manager.field.field_type'), $container
->get('entity_type.manager'));
}
public function import(Row $row, array $old_destination_id_values = []) {
$amount = $row
->getDestinationProperty('amount/number');
if ($amount >= 0) {
return parent::import($row, $old_destination_id_values);
}
else {
$saved_ids = 0;
$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));
foreach ($payments as $payment) {
$paid_amount = $payment
->getAmount()
->getNumber();
if ($paid_amount > 0) {
$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) {
$new_refund_amount = $paid_amount;
$state = 'refunded';
}
else {
$new_refund_amount = strval(abs($total_refund_amount));
$state = Calculator::subtract($paid_amount, $total_refund_amount) == 0 ? 'refunded' : 'partially_refunded';
}
$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);
parent::updateEntity($payment, $row);
$payment
->setState($state);
$saved_ids = $payment
->save();
$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,
];
}
}
}