/** * Retrieve Amount object based on given float amount, product and exclude option. * It is possible to pass "true" or adjustment code to exclude all or specific adjustment from an amount. * * @param float|string $amount * @param SaleableInterface $saleableItem * @param null|bool|string $exclude * @return \Magento\Framework\Pricing\Amount\AmountInterface */ public function getAmount($amount, SaleableInterface $saleableItem, $exclude = null) { $baseAmount = $fullAmount = $amount; $adjustments = []; foreach ($saleableItem->getPriceInfo()->getAdjustments() as $adjustment) { $code = $adjustment->getAdjustmentCode(); $toExclude = false; if ($exclude === true || $exclude !== null && $code === $exclude) { $toExclude = true; } if ($adjustment->isIncludedInBasePrice()) { $adjust = $adjustment->extractAdjustment($baseAmount, $saleableItem); $baseAmount -= $adjust; $fullAmount = $adjustment->applyAdjustment($fullAmount, $saleableItem); $adjust = $fullAmount - $baseAmount; if (!$toExclude) { $adjustments[$code] = $adjust; } } elseif ($adjustment->isIncludedInDisplayPrice($saleableItem)) { if ($toExclude) { continue; } $newAmount = $adjustment->applyAdjustment($fullAmount, $saleableItem); $adjust = $newAmount - $fullAmount; $adjustments[$code] = $adjust; $fullAmount = $newAmount; } } return $this->amountFactory->create($fullAmount, $adjustments); }
/** * Calculate amount for dynamic bundle product * * @param float $basePriceValue * @param Product $bundleProduct * @param \Magento\Bundle\Pricing\Price\BundleSelectionPrice[] $selectionPriceList * @param null|string $exclude * @return \Magento\Framework\Pricing\Amount\AmountInterface */ protected function calculateDynamicBundleAmount($basePriceValue, $bundleProduct, $selectionPriceList, $exclude) { $fullAmount = 0.0; $adjustments = []; $amountList = [$this->calculator->getAmount($basePriceValue, $bundleProduct, $exclude)]; /** @var $option \Magento\Bundle\Model\Option */ foreach ($selectionPriceList as $selectionPrice) { $amountList[] = $selectionPrice->getAmount(); } /** @var Store $store */ $store = $bundleProduct->getStore(); $roundingMethod = $this->taxHelper->getCalculationAgorithm($store); /** @var \Magento\Framework\Pricing\Amount\AmountInterface $itemAmount */ foreach ($amountList as $itemAmount) { if ($roundingMethod != TaxCalculationServiceInterface::CALC_TOTAL_BASE) { //We need to round the individual selection first $fullAmount += $store->roundPrice($itemAmount->getValue()); foreach ($itemAmount->getAdjustmentAmounts() as $code => $adjustment) { $adjustment = $store->roundPrice($adjustment); $adjustments[$code] = isset($adjustments[$code]) ? $adjustments[$code] + $adjustment : $adjustment; } } else { $fullAmount += $itemAmount->getValue(); foreach ($itemAmount->getAdjustmentAmounts() as $code => $adjustment) { $adjustments[$code] = isset($adjustments[$code]) ? $adjustments[$code] + $adjustment : $adjustment; } } } if ($exclude && isset($adjustments[$exclude])) { $fullAmount -= $adjustments[$exclude]; unset($adjustments[$exclude]); } return $this->amountFactory->create($fullAmount, $adjustments); }
public function testGetAmountExclude() { $amount = 10; $fullamount = 10; $taxAdjustmentCode = 'tax'; $weeeAdjustmentCode = 'weee'; $adjustment = 5; $expectedAdjustments = []; $productMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->setMethods(['getPriceInfo', '__wakeup'])->getMock(); $taxAdjustmentMock = $this->getMockBuilder('Magento\\Tax\\Pricing\\Adjustment')->disableOriginalConstructor()->getMock(); $taxAdjustmentMock->expects($this->once())->method('getAdjustmentCode')->will($this->returnValue($taxAdjustmentCode)); $taxAdjustmentMock->expects($this->once())->method('isIncludedInBasePrice')->will($this->returnValue(true)); $taxAdjustmentMock->expects($this->once())->method('extractAdjustment')->with($this->equalTo($amount), $this->equalTo($productMock))->will($this->returnValue($adjustment)); $taxAdjustmentMock->expects($this->once())->method('applyAdjustment')->with($this->equalTo($fullamount), $this->equalTo($productMock))->will($this->returnValue($amount)); $weeeAdjustmentMock = $this->getMockBuilder('Magento\\Weee\\Pricing\\Adjustment')->disableOriginalConstructor()->getMock(); $weeeAdjustmentMock->expects($this->once())->method('getAdjustmentCode')->will($this->returnValue($weeeAdjustmentCode)); $weeeAdjustmentMock->expects($this->once())->method('isIncludedInBasePrice')->will($this->returnValue(false)); $weeeAdjustmentMock->expects($this->once())->method('isIncludedInDisplayPrice')->with($this->equalTo($productMock))->will($this->returnValue(true)); $weeeAdjustmentMock->expects($this->never())->method('applyAdjustment'); $adjustments = [$taxAdjustmentMock, $weeeAdjustmentMock]; $priceInfoMock = $this->getMockBuilder('\\Magento\\Framework\\Pricing\\PriceInfo\\Base')->disableOriginalConstructor()->getMock(); $priceInfoMock->expects($this->any())->method('getAdjustments')->will($this->returnValue($adjustments)); $productMock->expects($this->any())->method('getPriceInfo')->will($this->returnValue($priceInfoMock)); $amountBaseMock = $this->getMockBuilder('Magento\\Framework\\Pricing\\Amount\\Base')->disableOriginalConstructor()->getMock(); $this->amountFactoryMock->expects($this->once())->method('create')->with($this->equalTo($amount), $this->equalTo($expectedAdjustments))->will($this->returnValue($amountBaseMock)); $result = $this->model->getAmount($amount, $productMock, true); $this->assertInstanceOf('Magento\\Framework\\Pricing\\Amount\\AmountInterface', $result); }
/** * Calculate amount for dynamic bundle product * * @param float $basePriceValue * @param Product $bundleProduct * @param \Magento\Bundle\Pricing\Price\BundleSelectionPrice[] $selectionPriceList * @param null|bool|string|array $exclude * @return \Magento\Framework\Pricing\Amount\AmountInterface * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function calculateDynamicBundleAmount($basePriceValue, $bundleProduct, $selectionPriceList, $exclude) { $fullAmount = 0.0; $adjustments = []; $i = 0; $amountList[$i]['amount'] = $this->calculator->getAmount($basePriceValue, $bundleProduct, $exclude); $amountList[$i]['quantity'] = 1; foreach ($selectionPriceList as $selectionPrice) { ++$i; $amountList[$i]['amount'] = $selectionPrice->getAmount(); // always honor the quantity given $amountList[$i]['quantity'] = $selectionPrice->getQuantity(); } /** @var Store $store */ $store = $bundleProduct->getStore(); $roundingMethod = $this->taxHelper->getCalculationAlgorithm($store); foreach ($amountList as $amountInfo) { /** @var \Magento\Framework\Pricing\Amount\AmountInterface $itemAmount */ $itemAmount = $amountInfo['amount']; $qty = $amountInfo['quantity']; if ($roundingMethod != TaxCalculationInterface::CALC_TOTAL_BASE) { //We need to round the individual selection first $fullAmount += $this->priceCurrency->round($itemAmount->getValue()) * $qty; foreach ($itemAmount->getAdjustmentAmounts() as $code => $adjustment) { $adjustment = $this->priceCurrency->round($adjustment) * $qty; $adjustments[$code] = isset($adjustments[$code]) ? $adjustments[$code] + $adjustment : $adjustment; } } else { $fullAmount += $itemAmount->getValue() * $qty; foreach ($itemAmount->getAdjustmentAmounts() as $code => $adjustment) { $adjustment = $adjustment * $qty; $adjustments[$code] = isset($adjustments[$code]) ? $adjustments[$code] + $adjustment : $adjustment; } } } if (is_array($exclude) == false) { if ($exclude && isset($adjustments[$exclude])) { $fullAmount -= $adjustments[$exclude]; unset($adjustments[$exclude]); } } else { foreach ($exclude as $oneExclusion) { if ($oneExclusion && isset($adjustments[$oneExclusion])) { $fullAmount -= $adjustments[$oneExclusion]; unset($adjustments[$oneExclusion]); } } } return $this->amountFactory->create($fullAmount, $adjustments); }
/** * Test method create * * @expectedException \InvalidArgumentException */ public function testCreateException() { $this->objectManagerMock->expects($this->once())->method('create')->with($this->equalTo('Magento\\Framework\\Pricing\\Amount\\AmountInterface'), $this->equalTo(['amount' => 'this-is-float-amount', 'adjustmentAmounts' => ['this-is-array-of-adjustments']]))->will($this->returnValue(new \stdClass())); $this->assertEquals($this->amountMock, $this->factory->create('this-is-float-amount', ['this-is-array-of-adjustments'])); }