View source  
  <?php
namespace Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\commerce_price\Price;
use Drupal\commerce_price\RounderInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
class OrderItemDiscountAdjustment extends CommercePrice implements ContainerFactoryPluginInterface {
  
  protected $migrationPluginManager;
  
  protected $migration;
  
  protected $entityTypeManager;
  
  protected $rounder;
  
  protected $number;
  
  protected $currencyCode;
  
  public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, MigrationPluginManagerInterface $migration_plugin_manager, EntityTypeManagerInterface $entity_type_manager, RounderInterface $rounder) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->migrationPluginManager = $migration_plugin_manager;
    $this->migration = $migration;
    $this->entityTypeManager = $entity_type_manager;
    $this->rounder = $rounder;
  }
  
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
    return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
      ->get('plugin.manager.migration'), $container
      ->get('entity_type.manager'), $container
      ->get('commerce_price.rounder'));
  }
  
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    $adjustment = [];
    
    $order_components = $row
      ->getSourceProperty('order_components/0/data/components');
    $order_names = [];
    foreach ($order_components as $order_component) {
      $order_names[] = $order_component['name'];
    }
    $shipping = $row
      ->getSourceProperty('shipping');
    $not_shipping = [];
    if (!empty($order_names)) {
      $shipping_names = [];
      foreach ($shipping as $shipping_item) {
        $data = unserialize($shipping_item['data']);
        $shipping_names[] = $data['shipping_service']['price_component'];
      }
      $not_shipping = array_diff($order_names, $shipping_names);
    }
    if (!empty($not_shipping)) {
      foreach ($not_shipping as $item) {
        if ($value['name'] === $item) {
          $adjustment = $this
            ->getAdjustment($value, $migrate_executable, $row);
        }
      }
    }
    return $adjustment;
  }
  
  protected function getAdjustment($value, MigrateExecutableInterface $migrate_executable, Row $row) {
    $adjustment = [];
    if (is_array($value)) {
      if ($value['name'] !== 'base_price') {
        $parts = explode('|', $value['name'], -1);
        if (!empty($parts)) {
          $percentage = NULL;
          $type = '';
          $label = '';
          $amount = (string) $value['price']['amount'];
          $currency_code = $value['price']['currency_code'];
          if ($parts[0] === 'tax') {
            $type = 'tax';
            $tax_rate = $value['price']['data']['tax_rate'];
            $label = $tax_rate['display_title'];
            $percentage = $tax_rate['rate'];
          }
          if ($parts[0] === 'discount') {
            $type = 'promotion';
            $label = $value['price']['data']['discount_component_title'];
          }
          if (empty($type)) {
            throw new MigrateSkipRowException(sprintf("Unknown adjustment type for line item '%s'.", $row
              ->getSourceProperty('line_item_id')));
          }
          
          $fraction_digits = isset($value['price']['fraction_digits']) ? $value['price']['fraction_digits']['fraction_digits'] : '2';
          $input = [
            'amount' => $amount,
            'fraction_digits' => $fraction_digits,
            'currency_code' => $currency_code,
          ];
          $price_scaled = parent::transform($input, $migrate_executable, $row, NULL);
          $price = new Price((string) $price_scaled['number'], $price_scaled['currency_code']);
          $price = $this->rounder
            ->round($price);
          $num_product_line = $row
            ->getSourceProperty('num_product_line');
          $last_line = FALSE;
          if ($row
            ->getSourceProperty('line_item_id') == $row
            ->getSourceProperty('max_line_item_id')) {
            $last_line = TRUE;
          }
          $amount = $this
            ->split($num_product_line, $last_line, $price);
          if ($amount) {
            $adjustment = [
              'type' => $type,
              'label' => $label,
              'amount' => $amount
                ->getNumber(),
              'currency_code' => $amount
                ->getCurrencyCode(),
              'percentage' => $percentage,
              'source_id' => 'custom',
              'included' => FALSE,
              'locked' => TRUE,
            ];
          }
        }
      }
    }
    return $adjustment;
  }
  
  protected function split($num_product_line, $last_line, Price $price) {
    $individual_amount = NULL;
    if ($num_product_line > 0) {
      
      $percentage = 1 / $num_product_line;
      $percentage = new Price((string) $percentage, $price
        ->getCurrencyCode());
      
      $individual_amount = $price
        ->multiply($percentage
        ->getNumber());
      $individual_amount = $this->rounder
        ->round($individual_amount, PHP_ROUND_HALF_DOWN);
      
      if ($last_line) {
        $price_calculated = $individual_amount
          ->multiply($num_product_line);
        $difference = $price
          ->subtract($price_calculated);
        if (!$difference
          ->isZero()) {
          $individual_amount = $individual_amount
            ->add($difference);
        }
      }
    }
    return $individual_amount;
  }
}