Skip to content

Commit

Permalink
Merge pull request #5 from adhikariavisek/main
Browse files Browse the repository at this point in the history
V2.4.5-p1 compatiblity
  • Loading branch information
sagoontech committed Dec 26, 2022
2 parents ef25c1e + b875bdb commit 42a29e8
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 83 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
Plugin/.DS_Store
Plugin/Pricing/.DS_Store
20 changes: 20 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,24 @@ public function isModuleEnabled($store = null)
{
return $this->scopeConfig->getValue(self::MODULE_ENABLED, ScopeInterface::SCOPE_STORE, $store);
}

/**
* Check if there are any decimal value in the price.
* @param $price
*
* @return bool
*/
public function isNoPrecision($price):bool
{
if ($this->isModuleEnabled() && $price > 0) {
$priceNumber = floor($price);
$fraction = $price - $priceNumber;
if ($fraction > 0 && $fraction < 1) {
//do nothing, we use default
} else {
return true;
}
}
return false;
}
}
10 changes: 2 additions & 8 deletions Plugin/Block/Tax/Item/Price/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,8 @@ public function aroundFormatPrice(
$price
){
$precision = PriceCurrencyInterface::DEFAULT_PRECISION;
if($this->moduleHelper->isModuleEnabled()){
$priceNumber = floor($price);
$fraction = $price - $priceNumber;
if($fraction > 0 && $fraction < 1){
//do nothing, we use default
} else{
$precision = 0;
}
if($this->moduleHelper->isNoPrecision($price)){
$precision = 0;
}

$item = $subject->getItem();
Expand Down
12 changes: 3 additions & 9 deletions Plugin/Model/Directory/PriceCurrency.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,9 @@ public function aroundFormat(
$scope = null,
$currency = null
) {
if($this->moduleHelper->isModuleEnabled()) {
$priceNumber = floor($amount);
$fraction = $amount - $priceNumber;
if ($fraction > 0 && $fraction < 1) {
//do nothing, we use default
} else {
$precision = 0;
}
}
if ($this->moduleHelper->isNoPrecision($amount)){
$precision = 0;
}

return $subject->getCurrency($scope, $currency)
->formatPrecision($amount, $precision, [], $includeContainer);
Expand Down
11 changes: 2 additions & 9 deletions Plugin/Pricing/Render/Amount.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,8 @@ public function aroundFormatCurrency(
$includeContainer = true,
$precision = PriceCurrencyInterface::DEFAULT_PRECISION
) {
if($this->moduleHelper->isModuleEnabled()){
$priceNumber = floor($price);
$fraction = $price - $priceNumber;
if($fraction > 0 && $fraction < 1){
//do nothing, we use default
//$precision = 0;
} else{
$precision = 0;
}
if ($this->moduleHelper->isNoPrecision($price)){
$precision = 0;
}

$renderPrice = $this->priceCurrency->format($price, $includeContainer, $precision);
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"magento/module-backend": "~101.0|~102.0"
},
"type": "magento2-module",
"version": "1.0.0",
"version": "1.0.1",
"autoload": {
"files": [
"registration.php"
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="SagoonTech_PriceDecimal" setup_version="1.0.0">
<module name="SagoonTech_PriceDecimal" setup_version="1.0.1">
<sequence>
<module name="SagoonTech_Base"/>
<module name="Magento_Catalog"/>
Expand Down
11 changes: 6 additions & 5 deletions view/frontend/requirejs-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
* author Adarsh Khatri
* url sagoontech.com
*/
var config = {
var config = {
map: {
'*': {
'Magento_Catalog/js/price-utils' : 'SagoonTech_PriceDecimal/js/price-utils'
}
}
"*": {
"Magento_Catalog/js/price-utils":
"SagoonTech_PriceDecimal/js/price-utils",
},
},
};
193 changes: 143 additions & 50 deletions view/frontend/web/js/price-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@
/**
* @api
*/
define([
'jquery',
'underscore'
], function ($, _) {
'use strict';
define(["jquery", "underscore"], function ($, _) {
"use strict";

var globalPriceFormat = {
requiredPrecision: 2,
integerRequired: 1,
decimalSymbol: ',',
groupSymbol: ',',
groupLength: ','
decimalSymbol: ",",
groupSymbol: ",",
groupLength: ",",
};

/**
Expand All @@ -27,7 +24,64 @@ define([
* @return {String}
*/
function stringPad(string, times) {
return (new Array(times + 1)).join(string);
return new Array(times + 1).join(string);
}

/**
* Format the price with the compliance to the specified locale
*
* @param {Number} amount
* @param {Object} format
* @param {Boolean} isShowSign
*/
function formatPriceLocale(amount, format, isShowSign) {
var s = "",
precision,
pattern,
locale,
r;

format = _.extend(globalPriceFormat, format);
precision = isNaN(
(format.requiredPrecision = Math.abs(format.requiredPrecision))
)
? 2
: format.requiredPrecision;
pattern = format.pattern || "%s";
locale = window.LOCALE || "en-US";
if (isShowSign === undefined || isShowSign === true) {
s = amount < 0 ? "-" : isShowSign ? "+" : "";
} else if (isShowSign === false) {
s = "";
}
pattern =
pattern.indexOf("{sign}") < 0
? s + pattern
: pattern.replace("{sign}", s);
amount = Number(
Math.round(Math.abs(+amount || 0) + "e+" + precision) +
("e-" + precision)
);

/*
* Added by Sagoon Tech <sagoontech@gmail.com>
*/
var am = amount;
if (am % 1) {
//use default
} else {
precision = 0;
}
/*
* Ended addition
*/

r = amount.toLocaleString(locale, { minimumFractionDigits: precision });

return pattern
.replace("%s", r)
.replace(/^\s\s*/, "")
.replace(/\s\s*$/, "");
}

/**
Expand All @@ -36,65 +90,103 @@ define([
* @param {Object} format
* @param {Boolean} isShowSign
* @return {String} Formatted value
* @deprecated
*/
function formatPrice(amount, format, isShowSign) {
var s = '',
precision, integerRequired, decimalSymbol, groupSymbol, groupLength, pattern, i, pad, j, re, r, am;
var s = "",
precision,
integerRequired,
decimalSymbol,
groupSymbol,
groupLength,
pattern,
i,
pad,
j,
re,
r,
am;

format = _.extend(globalPriceFormat, format);

// copied from price-option.js | Could be refactored with varien/js.js

precision = isNaN(format.requiredPrecision = Math.abs(format.requiredPrecision)) ? 2 : format.requiredPrecision;
integerRequired = isNaN(format.integerRequired = Math.abs(format.integerRequired)) ? 1 : format.integerRequired;
decimalSymbol = format.decimalSymbol === undefined ? ',' : format.decimalSymbol;
groupSymbol = format.groupSymbol === undefined ? '.' : format.groupSymbol;
precision = isNaN(
(format.requiredPrecision = Math.abs(format.requiredPrecision))
)
? 2
: format.requiredPrecision;
integerRequired = isNaN(
(format.integerRequired = Math.abs(format.integerRequired))
)
? 1
: format.integerRequired;
decimalSymbol =
format.decimalSymbol === undefined ? "," : format.decimalSymbol;
groupSymbol =
format.groupSymbol === undefined ? "." : format.groupSymbol;
groupLength = format.groupLength === undefined ? 3 : format.groupLength;
pattern = format.pattern || '%s';
pattern = format.pattern || "%s";

if (isShowSign === undefined || isShowSign === true) {
s = amount < 0 ? '-' : isShowSign ? '+' : '';
s = amount < 0 ? "-" : isShowSign ? "+" : "";
} else if (isShowSign === false) {
s = '';
s = "";
}
pattern = pattern.indexOf('{sign}') < 0 ? s + pattern : pattern.replace('{sign}', s);
pattern =
pattern.indexOf("{sign}") < 0
? s + pattern
: pattern.replace("{sign}", s);

// 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
) + '';
// numbers like 1.005 = 1.01. Using ToFixed to only provide trailing 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;

i = stringPad("0", pad) + i;

j = i.length > groupLength ? i.length % groupLength : 0;
re = new RegExp('(\\d{' + groupLength + '})(?=\\d)', 'g');
re = new RegExp("(\\d{" + groupLength + "})(?=\\d)", "g");

// 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));

/*
* Added by Adarsh Khatri @me.adarshkhatri@gmail.com
*/
if(am > 0 && am < 1){
//use default
} else{
precision = 0;
}
/*
* Ended addition
*/

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

return pattern.replace('%s', r).replace(/^\s\s*/, '').replace(/\s\s*$/, '');
am = Number(
Math.round(Math.abs(amount - i) + "e+" + precision) +
("e-" + precision)
);

/*
* Added by Sagoon Tech <sagoontech@gmail.com>
*/
if (am % 1) {
//use default
} else {
precision = 0;
}
/*
* Ended addition
*/

r =
(j ? i.substr(0, j) + groupSymbol : "") +
i.substr(j).replace(re, "$1" + groupSymbol) +
(precision
? decimalSymbol + am.toFixed(precision).replace(/-/, 0).slice(2)
: "");

return pattern
.replace("%s", r)
.replace(/^\s\s*/, "")
.replace(/\s\s*$/, "");
}

/**
Expand All @@ -117,9 +209,9 @@ define([
if (!element) {
return id;
}
name = $(element).attr('name');
name = $(element).attr("name");

if (name.indexOf('[') !== -1) {
if (name.indexOf("[") !== -1) {
re = /\[([^\]]+)?\]/;
} else {
re = /_([^\]]+)?_/; // just to support file-type-option
Expand All @@ -132,9 +224,10 @@ define([
}

return {
formatPriceLocale: formatPriceLocale,
formatPrice: formatPrice,
deepClone: objectDeepClone,
strPad: stringPad,
findOptionId: findOptionId
findOptionId: findOptionId,
};
});

0 comments on commit 42a29e8

Please sign in to comment.