Skip to content

Commit

Permalink
Added improvements for credit memo (new page) total fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaha committed Mar 30, 2020
1 parent 55f3961 commit 4046760
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
namespace Magento\Sales\Block\Adminhtml\Order\Creditmemo\Create;

use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Sales\Model\Order;
use Zend_Currency;

/**
* Credit memo adjustments block
Expand Down Expand Up @@ -68,6 +70,27 @@ public function initTotals()
return $this;
}

/**
* Format value based on order currency
*
* @param null|float $value
*
* @return string
*/
public function formatValue($value)
{
/** @var Order $order */
$order = $this->getSource()->getOrder();

return $order->getOrderCurrency()->formatPrecision(
$value,
2,
['display' => Zend_Currency::NO_SYMBOL],
false,
false
);
}

/**
* Get source object
*
Expand Down
63 changes: 53 additions & 10 deletions app/code/Magento/Sales/Model/Order/CreditmemoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
namespace Magento\Sales\Model\Order;

use Magento\Bundle\Ui\DataProvider\Product\Listing\Collector\BundlePrice;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Locale\FormatInterface;
use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
use Magento\Sales\Api\Data\OrderItemInterface;

/**
* Factory class for @see \Magento\Sales\Model\Order\Creditmemo
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class CreditmemoFactory
{
Expand All @@ -32,7 +37,12 @@ class CreditmemoFactory
protected $unserialize;

/**
* @var \Magento\Framework\Serialize\Serializer\Json
* @var FormatInterface
*/
private $localeFormat;

/**
* @var JsonSerializer
*/
private $serializer;

Expand All @@ -41,18 +51,19 @@ class CreditmemoFactory
*
* @param \Magento\Sales\Model\Convert\OrderFactory $convertOrderFactory
* @param \Magento\Tax\Model\Config $taxConfig
* @param \Magento\Framework\Serialize\Serializer\Json $serializer
* @param JsonSerializer $serializer
* @param FormatInterface $localeFormat
*/
public function __construct(
\Magento\Sales\Model\Convert\OrderFactory $convertOrderFactory,
\Magento\Tax\Model\Config $taxConfig,
\Magento\Framework\Serialize\Serializer\Json $serializer = null
JsonSerializer $serializer = null,
FormatInterface $localeFormat = null
) {
$this->convertor = $convertOrderFactory->create();
$this->taxConfig = $taxConfig;
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
\Magento\Framework\Serialize\Serializer\Json::class
);
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(JsonSerializer::class);
$this->localeFormat = $localeFormat ?: ObjectManager::getInstance()->get(FormatInterface::class);
}

/**
Expand Down Expand Up @@ -166,6 +177,7 @@ protected function canRefundItem($item, $qtys = [], $invoiceQtysRefundLimits = [
return isset($qtys[$parent->getId()]) && $qtys[$parent->getId()] > 0;
}
}
return false;
} else {
return $this->canRefundNoDummyItem($item, $invoiceQtysRefundLimits);
}
Expand Down Expand Up @@ -199,14 +211,17 @@ protected function canRefundNoDummyItem($item, $invoiceQtysRefundLimits = [])
protected function initData($creditmemo, $data)
{
if (isset($data['shipping_amount'])) {
$creditmemo->setBaseShippingAmount((double)$data['shipping_amount']);
$creditmemo->setBaseShippingInclTax((double)$data['shipping_amount']);
$shippingAmount = $this->parseNumber($data['shipping_amount']);
$creditmemo->setBaseShippingAmount($shippingAmount);
$creditmemo->setBaseShippingInclTax($shippingAmount);
}
if (isset($data['adjustment_positive'])) {
$creditmemo->setAdjustmentPositive($data['adjustment_positive']);
$adjustmentPositiveAmount = $this->parseAdjustmentAmount($data['adjustment_positive']);
$creditmemo->setAdjustmentPositive($adjustmentPositiveAmount);
}
if (isset($data['adjustment_negative'])) {
$creditmemo->setAdjustmentNegative($data['adjustment_negative']);
$adjustmentNegativeAmount = $this->parseAdjustmentAmount($data['adjustment_negative']);
$creditmemo->setAdjustmentNegative($adjustmentNegativeAmount);
}
}

Expand Down Expand Up @@ -340,4 +355,32 @@ private function getShippingAmount(Invoice $invoice): float

return (float)$amount;
}

/**
* Parse adjustment amount value to number
*
* @param string|null $amount
*
* @return float|null
*/
private function parseAdjustmentAmount($amount)
{
$amount = trim($amount);
$percentAmount = substr($amount, -1) == '%';
$amount = $this->parseNumber($amount);

return $percentAmount ? $amount . '%' : $amount;
}

/**
* Parse value to number
*
* @param string|null $value
*
* @return float|null
*/
private function parseNumber($value)
{
return $this->localeFormat->getNumber($value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="AssertAdminCreditMemoNewPageTotalsActionGroup">
<annotations>
<description>Checks totals values on the Credit Memo new page.</description>
</annotations>
<arguments>
<argument name="refundShipping" type="string" defaultValue="0"/>
<argument name="adjustmentRefund" type="string" defaultValue="0"/>
<argument name="adjustmentFee" type="string" defaultValue="0"/>
<argument name="grandTotal" type="string" defaultValue="0"/>
</arguments>

<seeInField selector="{{AdminCreditMemoTotalSection.refundShipping}}" userInput="{{refundShipping}}" stepKey="seeRefundShipping"/>
<seeInField selector="{{AdminCreditMemoTotalSection.adjustmentRefund}}" userInput="{{adjustmentRefund}}" stepKey="seeAdjustmentRefund"/>
<seeInField selector="{{AdminCreditMemoTotalSection.adjustmentFee}}" userInput="{{adjustmentFee}}" stepKey="seeAdjustmentFee"/>
<see selector="{{AdminCreditMemoTotalSection.grandTotal}}" userInput="{{grandTotal}}" stepKey="seeGrandTotal"/>
</actionGroup>
</actionGroups>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="AssertAdminCreditMemoViewPageTotalsActionGroup">
<annotations>
<description>Checks totals values on the Credit Memo view page.</description>
</annotations>
<arguments>
<argument name="subtotal" type="string" defaultValue="0"/>
<argument name="adjustmentRefund" type="string" defaultValue="0"/>
<argument name="adjustmentFee" type="string" defaultValue="0"/>
<argument name="grandTotal" type="string" defaultValue="0"/>
</arguments>

<see selector="{{AdminCreditMemoViewTotalSection.subtotal}}" userInput="{{subtotal}}" stepKey="seeSubtotal"/>
<see selector="{{AdminCreditMemoViewTotalSection.adjustmentRefund}}" userInput="{{adjustmentRefund}}" stepKey="seeAdjustmentRefund"/>
<see selector="{{AdminCreditMemoViewTotalSection.adjustmentFee}}" userInput="{{adjustmentFee}}" stepKey="seeAdjustmentFee"/>
<see selector="{{AdminCreditMemoViewTotalSection.grandTotal}}" userInput="{{grandTotal}}" stepKey="seeGrandTotal"/>
</actionGroup>
</actionGroups>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
<test name="AdminCheckNewCreditMemoTotalsForFranceTest" extends="AdminCreateCreditMemoWithCashOnDeliveryTest">
<annotations>
<stories value="Credit memo entity for France locale"/>
<title value="Credit memo entity for France locale"/>
<description value="Create Credit Memo with cash on delivery payment and assert 0 shipping refund for France locale"/>
<group value="sales"/>
</annotations>
<before>
<magentoCLI command="setup:static-content:deploy fr_FR" stepKey="deployStaticContentWithFrenchLocale" before="LoginAsAdmin"/>
<actionGroup ref="SetAdminAccountActionGroup" stepKey="setAdminInterfaceLocaleToFrance" after="LoginAsAdmin">
<argument name="InterfaceLocaleByValue" value="fr_FR"/>
</actionGroup>
</before>
<after>
<actionGroup ref="SetAdminAccountActionGroup" stepKey="setAdminInterfaceLocaleToDefaultValue" before="logout">
<argument name="InterfaceLocaleByValue" value="en_US"/>
</actionGroup>
</after>

<actionGroup ref="AdminOpenAndFillCreditMemoRefundActionGroup" stepKey="fillCreditMemoRefund">
<argument name="itemQtyToRefund" value="1"/>
<argument name="shippingRefund" value="0"/>
<argument name="adjustmentRefund" value="5,31"/>
<argument name="adjustmentFee" value="10,31"/>
</actionGroup>

<actionGroup ref="AssertAdminCreditMemoNewPageTotalsActionGroup" stepKey="assertCreditMemoRefundTotals" after="fillCreditMemoRefund">
<argument name="refundShipping" value="0,00"/>
<argument name="adjustmentRefund" value="5,31"/>
<argument name="adjustmentFee" value="10,31"/>
<argument name="subtotalRow" value="560,00"/>
<argument name="grandTotal" value="555,00"/>
</actionGroup>

<actionGroup ref="AssertAdminCreditMemoViewPageTotalsActionGroup" stepKey="assertCreditMemoViewPageTotals">
<argument name="subtotal" value="560,00"/>
<argument name="adjustmentRefund" value="5,31"/>
<argument name="adjustmentFee" value="10,31"/>
<argument name="grandTotal" value="555,00"/>
</actionGroup>
</test>
</tests>
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@
<click selector="{{AdminCreditMemosGridSection.memoId}}" stepKey="clickView"/>
<waitForPageLoad stepKey="waitForCreditMemo"/>
<scrollTo selector="{{AdminCreditMemoViewTotalSection.subtotal}}" stepKey="scrollToTotal"/>
<see selector="{{AdminCreditMemoViewTotalSection.subtotal}}" userInput="$560.00" stepKey="seeSubtotal"/>
<see selector="{{AdminCreditMemoViewTotalSection.adjustmentRefund}}" userInput="$5.00" stepKey="seeAdjustmentRefund"/>
<see selector="{{AdminCreditMemoViewTotalSection.adjustmentFee}}" userInput="$10.00" stepKey="seeAdjustmentFee"/>
<see selector="{{AdminCreditMemoViewTotalSection.grandTotal}}" userInput="$555.00" stepKey="assertRefundOnCreditMemoTab"/>
<actionGroup ref="AssertAdminCreditMemoViewPageTotalsActionGroup" stepKey="assertCreditMemoViewPageTotals">
<argument name="subtotal" value="$560.00"/>
<argument name="adjustmentRefund" value="$5.00"/>
<argument name="adjustmentFee" value="$10.00"/>
<argument name="grandTotal" value="$555.00"/>
</actionGroup>

<!--Login to storefront as previously created customer-->
<actionGroup ref="LoginToStorefrontActionGroup" stepKey="loginAsCustomer">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,59 @@
*/
?>
<?php $_source = $block->getSource() ?>
<?php if ($_source) : ?>
<?php if ($_source): ?>
<tr>
<td class="label"><?= $block->escapeHtml($block->getShippingLabel()) ?><div id="shipping_amount_adv"></div></td>
<td class="label">
<?= $block->escapeHtml($block->getShippingLabel()) ?>
<div id="shipping_amount_adv"></div>
</td>
<td>
<input type="text"
name="creditmemo[shipping_amount]"
value="<?= $block->escapeHtmlAttr($block->getShippingAmount()) ?>"
value="<?= /* @noEscape */ $block->formatValue($block->getShippingAmount()) ?>"
class="input-text admin__control-text not-negative-amount"
id="shipping_amount" />
</td>
</tr>
<tr>
<td class="label"><?= $block->escapeHtml(__('Adjustment Refund')) ?><div id="adjustment_positive_adv"></div></td>
<td class="label">
<?= $block->escapeHtml(__('Adjustment Refund')) ?>
<div id="adjustment_positive_adv"></div>
</td>
<td>
<input type="text"
name="creditmemo[adjustment_positive]"
value="<?= $block->escapeHtmlAttr($_source->getBaseAdjustmentPositive()) ?>"
value="<?= /* @noEscape */ $block->formatValue($_source->getBaseAdjustmentPositive()) ?>"
class="input-text admin__control-text not-negative-amount"
id="adjustment_positive" />
</td>
</tr>
<tr>
<td class="label"><?= $block->escapeHtml(__('Adjustment Fee')) ?><div id="adjustment_negative_adv"></div></td>
<td class="label">
<?= $block->escapeHtml(__('Adjustment Fee')) ?>
<div id="adjustment_negative_adv"></div>
</td>
<td>
<input type="text"
name="creditmemo[adjustment_negative]"
value="<?= $block->escapeHtmlAttr($_source->getBaseAdjustmentNegative()) ?>"
value="<?= /* @noEscape */ $block->formatValue($_source->getBaseAdjustmentNegative()) ?>"
class="input-text admin__control-text not-negative-amount"
id="adjustment_negative"/>
<script>
require(['prototype'], function(){

//<![CDATA[
Validation.addAllThese([
['not-negative-amount', '<?= $block->escapeJs(__('Please enter a positive number in this field.')) ?>', function(v) {
if(v.length)
return /^\s*\d+([,.]\d+)*\s*%?\s*$/.test(v);
else
return true;
}]
[
'not-negative-amount',
'<?= $block->escapeJs(__('Please enter a positive number in this field.')) ?>',
function (v) {
if (v.length)
return /^\s*\d+([,.]\d+)*\s*%?\s*$/.test(v);
else
return true;
}
]
]);

if ($('shipping_amount')) {
Expand Down

0 comments on commit 4046760

Please sign in to comment.