Skip to content

Commit 516017f

Browse files
[Magento Community Engineering] Community Contributions - 2.4-develop-expedited-prs
- merged with '2.4-develop-temporary-five-prs' branch
2 parents ecc1dfc + 6f7e07f commit 516017f

File tree

7 files changed

+214
-27
lines changed

7 files changed

+214
-27
lines changed

app/code/Magento/Sales/Block/Adminhtml/Order/Creditmemo/Create/Adjustments.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
namespace Magento\Sales\Block\Adminhtml\Order\Creditmemo\Create;
77

88
use Magento\Framework\Pricing\PriceCurrencyInterface;
9+
use Magento\Sales\Model\Order;
10+
use Zend_Currency;
911

1012
/**
1113
* Credit memo adjustments block
@@ -68,6 +70,27 @@ public function initTotals()
6870
return $this;
6971
}
7072

73+
/**
74+
* Format value based on order currency
75+
*
76+
* @param null|float $value
77+
*
78+
* @return string
79+
*/
80+
public function formatValue($value)
81+
{
82+
/** @var Order $order */
83+
$order = $this->getSource()->getOrder();
84+
85+
return $order->getOrderCurrency()->formatPrecision(
86+
$value,
87+
2,
88+
['display' => Zend_Currency::NO_SYMBOL],
89+
false,
90+
false
91+
);
92+
}
93+
7194
/**
7295
* Get source object
7396
*

app/code/Magento/Sales/Model/Order/CreditmemoFactory.php

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
namespace Magento\Sales\Model\Order;
77

88
use Magento\Bundle\Ui\DataProvider\Product\Listing\Collector\BundlePrice;
9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\Locale\FormatInterface;
11+
use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
912
use Magento\Sales\Api\Data\OrderItemInterface;
1013

1114
/**
1215
* Factory class for @see \Magento\Sales\Model\Order\Creditmemo
16+
*
17+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1318
*/
1419
class CreditmemoFactory
1520
{
@@ -32,7 +37,12 @@ class CreditmemoFactory
3237
protected $unserialize;
3338

3439
/**
35-
* @var \Magento\Framework\Serialize\Serializer\Json
40+
* @var FormatInterface
41+
*/
42+
private $localeFormat;
43+
44+
/**
45+
* @var JsonSerializer
3646
*/
3747
private $serializer;
3848

@@ -41,18 +51,19 @@ class CreditmemoFactory
4151
*
4252
* @param \Magento\Sales\Model\Convert\OrderFactory $convertOrderFactory
4353
* @param \Magento\Tax\Model\Config $taxConfig
44-
* @param \Magento\Framework\Serialize\Serializer\Json $serializer
54+
* @param JsonSerializer $serializer
55+
* @param FormatInterface $localeFormat
4556
*/
4657
public function __construct(
4758
\Magento\Sales\Model\Convert\OrderFactory $convertOrderFactory,
4859
\Magento\Tax\Model\Config $taxConfig,
49-
\Magento\Framework\Serialize\Serializer\Json $serializer = null
60+
JsonSerializer $serializer = null,
61+
FormatInterface $localeFormat = null
5062
) {
5163
$this->convertor = $convertOrderFactory->create();
5264
$this->taxConfig = $taxConfig;
53-
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
54-
\Magento\Framework\Serialize\Serializer\Json::class
55-
);
65+
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(JsonSerializer::class);
66+
$this->localeFormat = $localeFormat ?: ObjectManager::getInstance()->get(FormatInterface::class);
5667
}
5768

5869
/**
@@ -166,6 +177,7 @@ protected function canRefundItem($item, $qtys = [], $invoiceQtysRefundLimits = [
166177
return isset($qtys[$parent->getId()]) && $qtys[$parent->getId()] > 0;
167178
}
168179
}
180+
return false;
169181
} else {
170182
return $this->canRefundNoDummyItem($item, $invoiceQtysRefundLimits);
171183
}
@@ -199,14 +211,17 @@ protected function canRefundNoDummyItem($item, $invoiceQtysRefundLimits = [])
199211
protected function initData($creditmemo, $data)
200212
{
201213
if (isset($data['shipping_amount'])) {
202-
$creditmemo->setBaseShippingAmount((double)$data['shipping_amount']);
203-
$creditmemo->setBaseShippingInclTax((double)$data['shipping_amount']);
214+
$shippingAmount = $this->parseNumber($data['shipping_amount']);
215+
$creditmemo->setBaseShippingAmount($shippingAmount);
216+
$creditmemo->setBaseShippingInclTax($shippingAmount);
204217
}
205218
if (isset($data['adjustment_positive'])) {
206-
$creditmemo->setAdjustmentPositive($data['adjustment_positive']);
219+
$adjustmentPositiveAmount = $this->parseAdjustmentAmount($data['adjustment_positive']);
220+
$creditmemo->setAdjustmentPositive($adjustmentPositiveAmount);
207221
}
208222
if (isset($data['adjustment_negative'])) {
209-
$creditmemo->setAdjustmentNegative($data['adjustment_negative']);
223+
$adjustmentNegativeAmount = $this->parseAdjustmentAmount($data['adjustment_negative']);
224+
$creditmemo->setAdjustmentNegative($adjustmentNegativeAmount);
210225
}
211226
}
212227

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

341356
return (float)$amount;
342357
}
358+
359+
/**
360+
* Parse adjustment amount value to number
361+
*
362+
* @param string|null $amount
363+
*
364+
* @return float|null
365+
*/
366+
private function parseAdjustmentAmount($amount)
367+
{
368+
$amount = trim($amount);
369+
$percentAmount = substr($amount, -1) == '%';
370+
$amount = $this->parseNumber($amount);
371+
372+
return $percentAmount ? $amount . '%' : $amount;
373+
}
374+
375+
/**
376+
* Parse value to number
377+
*
378+
* @param string|null $value
379+
*
380+
* @return float|null
381+
*/
382+
private function parseNumber($value)
383+
{
384+
return $this->localeFormat->getNumber($value);
385+
}
343386
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
11+
<actionGroup name="AssertAdminCreditMemoNewPageTotalsActionGroup">
12+
<annotations>
13+
<description>Checks totals values on the Credit Memo new page.</description>
14+
</annotations>
15+
<arguments>
16+
<argument name="refundShipping" type="string" defaultValue="0"/>
17+
<argument name="adjustmentRefund" type="string" defaultValue="0"/>
18+
<argument name="adjustmentFee" type="string" defaultValue="0"/>
19+
<argument name="grandTotal" type="string" defaultValue="0"/>
20+
</arguments>
21+
22+
<seeInField selector="{{AdminCreditMemoTotalSection.refundShipping}}" userInput="{{refundShipping}}" stepKey="seeRefundShipping"/>
23+
<seeInField selector="{{AdminCreditMemoTotalSection.adjustmentRefund}}" userInput="{{adjustmentRefund}}" stepKey="seeAdjustmentRefund"/>
24+
<seeInField selector="{{AdminCreditMemoTotalSection.adjustmentFee}}" userInput="{{adjustmentFee}}" stepKey="seeAdjustmentFee"/>
25+
<see selector="{{AdminCreditMemoTotalSection.grandTotal}}" userInput="{{grandTotal}}" stepKey="seeGrandTotal"/>
26+
</actionGroup>
27+
</actionGroups>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
11+
<actionGroup name="AssertAdminCreditMemoViewPageTotalsActionGroup">
12+
<annotations>
13+
<description>Checks totals values on the Credit Memo view page.</description>
14+
</annotations>
15+
<arguments>
16+
<argument name="subtotal" type="string" defaultValue="0"/>
17+
<argument name="adjustmentRefund" type="string" defaultValue="0"/>
18+
<argument name="adjustmentFee" type="string" defaultValue="0"/>
19+
<argument name="grandTotal" type="string" defaultValue="0"/>
20+
</arguments>
21+
22+
<see selector="{{AdminCreditMemoViewTotalSection.subtotal}}" userInput="{{subtotal}}" stepKey="seeSubtotal"/>
23+
<see selector="{{AdminCreditMemoViewTotalSection.adjustmentRefund}}" userInput="{{adjustmentRefund}}" stepKey="seeAdjustmentRefund"/>
24+
<see selector="{{AdminCreditMemoViewTotalSection.adjustmentFee}}" userInput="{{adjustmentFee}}" stepKey="seeAdjustmentFee"/>
25+
<see selector="{{AdminCreditMemoViewTotalSection.grandTotal}}" userInput="{{grandTotal}}" stepKey="seeGrandTotal"/>
26+
</actionGroup>
27+
</actionGroups>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
11+
<test name="AdminCheckNewCreditMemoTotalsForFranceTest" extends="AdminCreateCreditMemoWithCashOnDeliveryTest">
12+
<annotations>
13+
<stories value="Credit memo entity for France locale"/>
14+
<title value="Credit memo entity for France locale"/>
15+
<description value="Create Credit Memo with cash on delivery payment and assert 0 shipping refund for France locale"/>
16+
<group value="sales"/>
17+
</annotations>
18+
<before>
19+
<magentoCLI command="setup:static-content:deploy fr_FR" stepKey="deployStaticContentWithFrenchLocale" before="LoginAsAdmin"/>
20+
<actionGroup ref="SetAdminAccountActionGroup" stepKey="setAdminInterfaceLocaleToFrance" after="LoginAsAdmin">
21+
<argument name="InterfaceLocaleByValue" value="fr_FR"/>
22+
</actionGroup>
23+
</before>
24+
<after>
25+
<actionGroup ref="SetAdminAccountActionGroup" stepKey="setAdminInterfaceLocaleToDefaultValue" before="logout">
26+
<argument name="InterfaceLocaleByValue" value="en_US"/>
27+
</actionGroup>
28+
</after>
29+
30+
<actionGroup ref="AdminOpenAndFillCreditMemoRefundActionGroup" stepKey="fillCreditMemoRefund">
31+
<argument name="itemQtyToRefund" value="1"/>
32+
<argument name="shippingRefund" value="0"/>
33+
<argument name="adjustmentRefund" value="5,31"/>
34+
<argument name="adjustmentFee" value="10,31"/>
35+
</actionGroup>
36+
37+
<actionGroup ref="AssertAdminCreditMemoNewPageTotalsActionGroup" stepKey="assertCreditMemoRefundTotals" after="fillCreditMemoRefund">
38+
<argument name="refundShipping" value="0,00"/>
39+
<argument name="adjustmentRefund" value="5,31"/>
40+
<argument name="adjustmentFee" value="10,31"/>
41+
<argument name="subtotalRow" value="560,00"/>
42+
<argument name="grandTotal" value="555,00"/>
43+
</actionGroup>
44+
45+
<actionGroup ref="AssertAdminCreditMemoViewPageTotalsActionGroup" stepKey="assertCreditMemoViewPageTotals">
46+
<argument name="subtotal" value="560,00"/>
47+
<argument name="adjustmentRefund" value="5,31"/>
48+
<argument name="adjustmentFee" value="10,31"/>
49+
<argument name="grandTotal" value="555,00"/>
50+
</actionGroup>
51+
</test>
52+
</tests>

app/code/Magento/Sales/Test/Mftf/Test/AdminCreateCreditMemoWithCashOnDeliveryTest.xml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,12 @@
102102
<click selector="{{AdminCreditMemosGridSection.memoId}}" stepKey="clickView"/>
103103
<waitForPageLoad stepKey="waitForCreditMemo"/>
104104
<scrollTo selector="{{AdminCreditMemoViewTotalSection.subtotal}}" stepKey="scrollToTotal"/>
105-
<see selector="{{AdminCreditMemoViewTotalSection.subtotal}}" userInput="$560.00" stepKey="seeSubtotal"/>
106-
<see selector="{{AdminCreditMemoViewTotalSection.adjustmentRefund}}" userInput="$5.00" stepKey="seeAdjustmentRefund"/>
107-
<see selector="{{AdminCreditMemoViewTotalSection.adjustmentFee}}" userInput="$10.00" stepKey="seeAdjustmentFee"/>
108-
<see selector="{{AdminCreditMemoViewTotalSection.grandTotal}}" userInput="$555.00" stepKey="assertRefundOnCreditMemoTab"/>
105+
<actionGroup ref="AssertAdminCreditMemoViewPageTotalsActionGroup" stepKey="assertCreditMemoViewPageTotals">
106+
<argument name="subtotal" value="$560.00"/>
107+
<argument name="adjustmentRefund" value="$5.00"/>
108+
<argument name="adjustmentFee" value="$10.00"/>
109+
<argument name="grandTotal" value="$555.00"/>
110+
</actionGroup>
109111

110112
<!--Login to storefront as previously created customer-->
111113
<actionGroup ref="LoginToStorefrontActionGroup" stepKey="loginAsCustomer">

app/code/Magento/Sales/view/adminhtml/templates/order/creditmemo/create/totals/adjustments.phtml

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,59 @@
55
*/
66
?>
77
<?php $_source = $block->getSource() ?>
8-
<?php if ($_source) : ?>
8+
<?php if ($_source): ?>
99
<tr>
10-
<td class="label"><?= $block->escapeHtml($block->getShippingLabel()) ?><div id="shipping_amount_adv"></div></td>
10+
<td class="label">
11+
<?= $block->escapeHtml($block->getShippingLabel()) ?>
12+
<div id="shipping_amount_adv"></div>
13+
</td>
1114
<td>
1215
<input type="text"
1316
name="creditmemo[shipping_amount]"
14-
value="<?= $block->escapeHtmlAttr($block->getShippingAmount()) ?>"
17+
value="<?= /* @noEscape */ $block->formatValue($block->getShippingAmount()) ?>"
1518
class="input-text admin__control-text not-negative-amount"
1619
id="shipping_amount" />
1720
</td>
1821
</tr>
1922
<tr>
20-
<td class="label"><?= $block->escapeHtml(__('Adjustment Refund')) ?><div id="adjustment_positive_adv"></div></td>
23+
<td class="label">
24+
<?= $block->escapeHtml(__('Adjustment Refund')) ?>
25+
<div id="adjustment_positive_adv"></div>
26+
</td>
2127
<td>
2228
<input type="text"
2329
name="creditmemo[adjustment_positive]"
24-
value="<?= $block->escapeHtmlAttr($_source->getBaseAdjustmentPositive()) ?>"
30+
value="<?= /* @noEscape */ $block->formatValue($_source->getBaseAdjustmentPositive()) ?>"
2531
class="input-text admin__control-text not-negative-amount"
2632
id="adjustment_positive" />
2733
</td>
2834
</tr>
2935
<tr>
30-
<td class="label"><?= $block->escapeHtml(__('Adjustment Fee')) ?><div id="adjustment_negative_adv"></div></td>
36+
<td class="label">
37+
<?= $block->escapeHtml(__('Adjustment Fee')) ?>
38+
<div id="adjustment_negative_adv"></div>
39+
</td>
3140
<td>
3241
<input type="text"
3342
name="creditmemo[adjustment_negative]"
34-
value="<?= $block->escapeHtmlAttr($_source->getBaseAdjustmentNegative()) ?>"
43+
value="<?= /* @noEscape */ $block->formatValue($_source->getBaseAdjustmentNegative()) ?>"
3544
class="input-text admin__control-text not-negative-amount"
3645
id="adjustment_negative"/>
3746
<script>
3847
require(['prototype'], function(){
3948

4049
//<![CDATA[
4150
Validation.addAllThese([
42-
['not-negative-amount', '<?= $block->escapeJs(__('Please enter a positive number in this field.')) ?>', function(v) {
43-
if(v.length)
44-
return /^\s*\d+([,.]\d+)*\s*%?\s*$/.test(v);
45-
else
46-
return true;
47-
}]
51+
[
52+
'not-negative-amount',
53+
'<?= $block->escapeJs(__('Please enter a positive number in this field.')) ?>',
54+
function (v) {
55+
if (v.length)
56+
return /^\s*\d+([,.]\d+)*\s*%?\s*$/.test(v);
57+
else
58+
return true;
59+
}
60+
]
4861
]);
4962

5063
if ($('shipping_amount')) {

0 commit comments

Comments
 (0)