You are here

protected function OrderItemAdjustment::split in Commerce Migrate 3.1.x

Same name and namespace in other branches
  1. 8.2 modules/ubercart/src/Plugin/migrate/process/OrderItemAdjustment.php \Drupal\commerce_migrate_ubercart\Plugin\migrate\process\OrderItemAdjustment::split()
  2. 3.0.x modules/ubercart/src/Plugin/migrate/process/OrderItemAdjustment.php \Drupal\commerce_migrate_ubercart\Plugin\migrate\process\OrderItemAdjustment::split()

Computes the percentage of a price to apply to this line.

Parameters

string $num_product_line: The current product line number.

bool $last_line: Indicates if this is the last line item for the order.

\Drupal\commerce_price\Price $price: The rounded total price for this sline item.

Return value

\Drupal\commerce_price\Price|null The price to apply to this line of NULL if there was an error.

2 calls to OrderItemAdjustment::split()
OrderItemAdjustment::transform in modules/ubercart/src/Plugin/migrate/process/OrderItemAdjustment.php
Performs the associated process.
TestOrderItemAdjustment::split in modules/ubercart/tests/src/Unit/Plugin/migrate/process/OrderItemAdjustmentTest.php
Computes the percentage of a price to apply to this line.
1 method overrides OrderItemAdjustment::split()
TestOrderItemAdjustment::split in modules/ubercart/tests/src/Unit/Plugin/migrate/process/OrderItemAdjustmentTest.php
Computes the percentage of a price to apply to this line.

File

modules/ubercart/src/Plugin/migrate/process/OrderItemAdjustment.php, line 191

Class

OrderItemAdjustment
Builds an array of adjustment data.

Namespace

Drupal\commerce_migrate_ubercart\Plugin\migrate\process

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;
}