Skip to content

Commit

Permalink
Merge pull request #426 from magento-fearless-kiwis/develop
Browse files Browse the repository at this point in the history
[FearlessKiwis] Sprint 63: Bug fixes
  • Loading branch information
Momotenko,Natalia(nmomotenko) committed Mar 14, 2016
2 parents 8fd3e8a + 112b9f6 commit 0fbca05
Show file tree
Hide file tree
Showing 23 changed files with 458 additions and 185 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ protected function getBundleSelections()
'arguments' => [
'data' => [
'config' => [
'component' => 'Magento_Bundle/js/components/bundle-option-qty',
'formElement' => Form\Element\Input::NAME,
'componentType' => Form\Field::NAME,
'dataType' => Form\Element\DataType\Number::NAME,
Expand All @@ -577,8 +578,12 @@ protected function getBundleSelections()
'value' => '1',
'sortOrder' => 100,
'validation' => [
'required-entry' => true,
'validate-number' => true,
],
'imports' => [
'isInteger' => '${ $.provider }:${ $.parentScope }.selection_qty_is_integer'
],
],
],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public function modifyMeta(array $meta)

/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function modifyData(array $data)
{
Expand All @@ -103,6 +105,12 @@ public function modifyData(array $data)
/** @var \Magento\Bundle\Api\Data\LinkInterface $productLink */
foreach ($option->getProductLinks() as $productLink) {
$linkedProduct = $this->productRepository->get($productLink->getSku());
$integerQty = 1;
if ($linkedProduct->getExtensionAttributes()->getStockItem()) {
if ($linkedProduct->getExtensionAttributes()->getStockItem()->getIsQtyDecimal()) {
$integerQty = 0;
}
}
$selections[] = [
'selection_id' => $productLink->getId(),
'option_id' => $productLink->getOptionId(),
Expand All @@ -112,8 +120,9 @@ public function modifyData(array $data)
'is_default' => ($productLink->getIsDefault()) ? '1' : '0',
'selection_price_value' => $productLink->getPrice(),
'selection_price_type' => $productLink->getPriceType(),
'selection_qty' => $productLink->getQty(),
'selection_qty' => (bool)$integerQty ? (int)$productLink->getQty() : $productLink->getQty(),
'selection_can_change_qty' => $productLink->getCanChangeQuantity(),
'selection_qty_is_integer' => (bool)$integerQty,
'position' => $productLink->getPosition(),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ Bundle.Selection.prototype = {
if (data.can_read_price != undefined && !data.can_read_price) {
data.selection_price_value = '';
} else {
data.selection_price_value = Number(data.selection_price_value).toFixed(2);
data.selection_price_value = Number(Math.round(data.selection_price_value + "e+2") + "e-2").toFixed(2);
}

data.index = this.itemsCount++;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

define([
'Magento_Ui/js/form/element/abstract'
], function (Abstract) {
'use strict';

return Abstract.extend({
defaults: {
valueUpdate: 'input',
isInteger: true
},

/**
* update event
*/
onUpdate: function () {
this.validation['validate-number'] = true;
this.validation['validate-digits'] = this.isInteger;
this.validate();
}
});
});
11 changes: 8 additions & 3 deletions app/code/Magento/Catalog/view/base/web/js/price-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ define([
pattern = format.pattern || '%s',
s = '',
i, pad,
j, re, r;
j, re, r, am;

if (isShowSign === undefined || isShowSign === true) {
s = amount < 0 ? '-' : (isShowSign ? '+' : '');
Expand All @@ -52,7 +52,9 @@ define([
}
pattern = pattern.indexOf('{sign}') < 0 ? s + pattern : pattern.replace('{sign}', s);

i = parseInt(amount = Math.abs(+amount || 0).toFixed(precision), 10) + '';
// we're avoiding the usage of to fixed, and using round instead with the e representation to address
// numbers like 1.005 = 1.01. Using ToFixed to only provide trailig zeroes in case we have a whole number
i = parseInt(amount = Number(Math.round(Math.abs(+amount || 0) + 'e+' + precision) + ('e-' + precision)) , 10) + '';
pad = (i.length < integerRequired) ? (integerRequired - i.length) : 0;

i = stringPad('0', pad) + i;
Expand All @@ -63,9 +65,12 @@ define([
// replace(/-/, 0) is only for fixing Safari bug which appears
// when Math.abs(0).toFixed() executed on '0' number.
// Result is '0.-0' :(


am = Number(Math.round(Math.abs(amount - i) + 'e+' + precision) + ('e-' + precision));
r = (j ? i.substr(0, j) + groupSymbol : '') +
i.substr(j).replace(re, '$1' + groupSymbol) +
(precision ? decimalSymbol + Math.abs(amount - i).toFixed(precision).replace(/-/, 0).slice(2) : '');
(precision ? decimalSymbol + am.toFixed(2).replace(/-/, 0).slice(2) : '');

return pattern.replace('%s', r).replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ Product.Config.prototype = {
}
}

var roundedPrice = (Math.round(price*100)/100).toString();
var roundedPrice = Number(Math.round(price + "e+2") + "e-2").toString();

if (this.prices && this.prices[roundedPrice]) {
str+= this.prices[roundedPrice];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ define([
},
name: product.name || product.sku,
options: options,
price: parseFloat(product.price.replace(/[^\d.]+/g, '')).toFixed(4),
price: parseFloat(Math.round(product.price.replace(/[^\d.]+/g, '') + "e+4") + "e-4").toFixed(4),
productId: productId,
productUrl: this.buildProductUrl(productId),
quantity: product.quantity || null,
Expand Down
3 changes: 3 additions & 0 deletions app/code/Magento/Quote/etc/fieldset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@
<field name="row_total">
<aspect name="to_order_item" />
</field>
<field name="base_price">
<aspect name="to_order_item" />
</field>
<field name="base_original_price">
<aspect name="to_order_item" />
</field>
Expand Down
99 changes: 68 additions & 31 deletions app/code/Magento/Sales/Model/Order/Creditmemo/Total/Shipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class Shipping extends AbstractTotal
*/
protected $priceCurrency;

/**
* Tax config
*
* @var \Magento\Tax\Model\Config
*/
private $taxConfig;

/**
* @param PriceCurrencyInterface $priceCurrency
* @param array $data
Expand All @@ -37,62 +44,64 @@ public function __construct(
public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
{
$order = $creditmemo->getOrder();
$allowedAmount = $order->getShippingAmount() - $order->getShippingRefunded();
$baseAllowedAmount = $order->getBaseShippingAmount() - $order->getBaseShippingRefunded();

// amounts without tax
$orderShippingAmount = $order->getShippingAmount();
$orderBaseShippingAmount = $order->getBaseShippingAmount();
$allowedAmount = $orderShippingAmount - $order->getShippingRefunded();
$baseAllowedAmount = $orderBaseShippingAmount - $order->getBaseShippingRefunded();

// amounts including tax
$orderShippingInclTax = $order->getShippingInclTax();
$orderBaseShippingInclTax = $order->getBaseShippingInclTax();
$allowedTaxAmount = $order->getShippingTaxAmount() - $order->getShippingTaxRefunded();
$baseAllowedTaxAmount = $order->getBaseShippingTaxAmount() - $order->getBaseShippingTaxRefunded();
$allowedAmountInclTax = $allowedAmount + $allowedTaxAmount;
$baseAllowedAmountInclTax = $baseAllowedAmount + $baseAllowedTaxAmount;

// for the credit memo
$shippingAmount = $baseShippingAmount = $shippingInclTax = $baseShippingInclTax = 0;

/**
* Check if shipping amount was specified (from invoice or another source).
* Using has magic method to allow setting 0 as shipping amount.
*/
// Check if the desired shipping amount to refund was specified (from invoice or another source).
if ($creditmemo->hasBaseShippingAmount()) {
$baseShippingAmount = $this->priceCurrency->round($creditmemo->getBaseShippingAmount());
/*
* Rounded allowed shipping refund amount is the highest acceptable shipping refund amount.
* Shipping refund amount shouldn't cause errors, if it doesn't exceed that limit.
* Note: ($x < $y + 0.0001) means ($x <= $y) for floats
*/
if ($baseShippingAmount < $this->priceCurrency->round($baseAllowedAmount) + 0.0001) {
// For the conditional logic, we will either use amounts that always include tax -OR- never include tax.
// The logic uses the 'base' currency to be consistent with what the user (admin) provided as input.
$useAmountsWithTax = $this->isSuppliedShippingAmountInclTax($order);

// Since the user (admin) supplied 'desiredAmount' it already has tax -OR- does not include tax
$desiredAmount = $this->priceCurrency->round($creditmemo->getBaseShippingAmount());
$maxAllowedAmount = ($useAmountsWithTax ? $baseAllowedAmountInclTax : $baseAllowedAmount);
$originalTotalAmount = ($useAmountsWithTax ? $orderBaseShippingInclTax : $orderBaseShippingAmount);

// Note: ($x < $y + 0.0001) means ($x <= $y) for floats
if ($desiredAmount < $this->priceCurrency->round($maxAllowedAmount) + 0.0001) {
// since the admin is returning less than the allowed amount, compute the ratio being returned
$ratio = 0;
if ($orderBaseShippingAmount > 0) {
$ratio = $baseShippingAmount / $orderBaseShippingAmount;
if ($originalTotalAmount > 0) {
$ratio = $desiredAmount / $originalTotalAmount;
}
/*
* Shipping refund amount should be equated to allowed refund amount,
* if it exceeds that limit.
* Note: ($x > $y - 0.0001) means ($x >= $y) for floats
*/
if ($baseShippingAmount > $baseAllowedAmount - 0.0001) {
// capture amounts without tax
// Note: ($x > $y - 0.0001) means ($x >= $y) for floats
if ($desiredAmount > $maxAllowedAmount - 0.0001) {
$shippingAmount = $allowedAmount;
$baseShippingAmount = $baseAllowedAmount;
} else {
$shippingAmount = $this->priceCurrency->round($orderShippingAmount * $ratio);
$baseShippingAmount = $this->priceCurrency->round($orderBaseShippingAmount * $ratio);
}
$shippingInclTax = $this->priceCurrency->round($orderShippingInclTax * $ratio);
$baseShippingInclTax = $this->priceCurrency->round($orderBaseShippingInclTax * $ratio);
} else {
$baseAllowedAmount = $order->getBaseCurrency()->format($baseAllowedAmount, null, false);
$maxAllowedAmount = $order->getBaseCurrency()->format($maxAllowedAmount, null, false);
throw new \Magento\Framework\Exception\LocalizedException(
__('Maximum shipping amount allowed to refund is: %1', $baseAllowedAmount)
__('Maximum shipping amount allowed to refund is: %1', $maxAllowedAmount)
);
}
} else {
$shippingAmount = $allowedAmount;
$baseShippingAmount = $baseAllowedAmount;

$allowedTaxAmount = $order->getShippingTaxAmount() - $order->getShippingTaxRefunded();
$baseAllowedTaxAmount = $order->getBaseShippingTaxAmount() - $order->getBaseShippingTaxRefunded();

$shippingInclTax = $this->priceCurrency->round($allowedAmount + $allowedTaxAmount);
$baseShippingInclTax = $this->priceCurrency->round(
$baseAllowedAmount + $baseAllowedTaxAmount
);
$shippingInclTax = $this->priceCurrency->round($allowedAmountInclTax);
$baseShippingInclTax = $this->priceCurrency->round($baseAllowedAmountInclTax);
}

$creditmemo->setShippingAmount($shippingAmount);
Expand All @@ -104,4 +113,32 @@ public function collect(\Magento\Sales\Model\Order\Creditmemo $creditmemo)
$creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseShippingAmount);
return $this;
}

/**
* Returns whether the user specified a shipping amount that already includes tax
*
* @param \Magento\Sales\Model\Order $order
* @return bool
*/
private function isSuppliedShippingAmountInclTax($order)
{
// returns true if we are only displaying shipping including tax, otherwise returns false
return $this->getTaxConfig()->displaySalesShippingInclTax($order->getStoreId());
}

/**
* Get the Tax Config.
* In a future release, will become a constructor parameter.
*
* @return \Magento\Tax\Model\Config
*
* @deprecated
*/
private function getTaxConfig()
{
if ($this->taxConfig === null) {
$this->taxConfig = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Tax\Model\Config');
}
return $this->taxConfig;
}
}
Loading

0 comments on commit 0fbca05

Please sign in to comment.