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|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); }
/** * @param float $amount * @param null|bool|string $exclude * @param null|array $context * @return AmountInterface|bool|float */ public function getCustomAmount($amount = null, $exclude = null, $context = []) { if (null !== $amount) { $amount = $this->priceCurrency->convertAndRound($amount); } else { $amount = $this->getValue(); } return $this->calculator->getAmount($amount, $this->getProduct(), $exclude, $context); }
/** * 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); }
/** * @param float $amount * @param null|bool|string $exclude * @param null|array $context * @return AmountInterface|bool|float */ public function getCustomAmount($amount = null, $exclude = null, $context = []) { $amount = null === $amount ? $this->getValue() : $amount; return $this->calculator->getAmount($amount, $this->product, $exclude, $context); }