You are here

protected function OrderItemDiscountAdjustment::split in Commerce Migrate 8.2

Same name and namespace in other branches
  1. 3.1.x modules/commerce/src/Plugin/migrate/process/commerce1/OrderItemDiscountAdjustment.php \Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1\OrderItemDiscountAdjustment::split()
  2. 3.0.x modules/commerce/src/Plugin/migrate/process/commerce1/OrderItemDiscountAdjustment.php \Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1\OrderItemDiscountAdjustment::split()

Calculates the adjustment amount for this line.

Parameters

int $num_product_line: The product line number.

bool $last_line: TRUE if this is the last line of the order to process FALSE otherwise.

\Drupal\commerce_price\Price $price: The price for this line.

Return value

\Drupal\commerce_price\Price|null The new price for this line item. NULL if no line number was provided.

2 calls to OrderItemDiscountAdjustment::split()
OrderItemDiscountAdjustment::getAdjustment in modules/commerce/src/Plugin/migrate/process/commerce1/OrderItemDiscountAdjustment.php
Get the adjustments on this order item.
TestOrderItemDiscountAdjustment::split in modules/commerce/tests/src/Unit/Plugin/migrate/process/commerce1/OrderItemDiscountAdjustmentTest.php
Calculates the adjustment amount for this line.
1 method overrides OrderItemDiscountAdjustment::split()
TestOrderItemDiscountAdjustment::split in modules/commerce/tests/src/Unit/Plugin/migrate/process/commerce1/OrderItemDiscountAdjustmentTest.php
Calculates the adjustment amount for this line.

File

modules/commerce/src/Plugin/migrate/process/commerce1/OrderItemDiscountAdjustment.php, line 233

Class

OrderItemDiscountAdjustment
Builds an array of adjustment data.

Namespace

Drupal\commerce_migrate_commerce\Plugin\migrate\process\commerce1

Code

protected function split($num_product_line, $last_line, Price $price) {
  $individual_amount = NULL;
  if ($num_product_line > 0) {

    // Get the amount to add to each product line item.
    $percentage = 1 / $num_product_line;
    $percentage = new Price((string) $percentage, $price
      ->getCurrencyCode());

    // Calculate the initial per-order-item amounts using the percentage.
    // Round down to ensure that their sum isn't larger than the full amount.
    $individual_amount = $price
      ->multiply($percentage
      ->getNumber());
    $individual_amount = $this->rounder
      ->round($individual_amount, PHP_ROUND_HALF_DOWN);

    // Make any adjustments needed in the last line item for this order.
    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;
}