//============================================================================== // Copyright 2022-2023 Moraware, Inc. // // Purpose: JavaScript to support New Inventory. // //============================================================================== /* eslint no-redeclare: "off", no-unused-vars: "off", no-useless-assignment: "off"*/ //#region Lint - Extern Declarations /*global JSON // SharedClientFunctions.js consoleLog consoleLogBlockOpen consoleLogBlockClose consoleLogJ trim */ //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ var CurrencyFormatting = function () { //-------------------------------------------------------------------------- // // Converts a raw value representing a currency amount into a string that // can be passed as a text field's "fieldValue". // // Includes an option to treat a zero as an empty string // //-------------------------------------------------------------------------- function pbGetEditableFieldValueString(params_) { var tmpRawCurrencyAmount = params_.rawCurrencyAmount, tmpFormatZeroAsEmptyString = params_.formatZeroAsEmptyString, rc; if (0 === tmpRawCurrencyAmount) { if (tmpFormatZeroAsEmptyString) { rc = ''; } else { rc = '0'; } } else { rc = '' + (tmpRawCurrencyAmount || ''); } return trim(rc); } //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- function pbSplitTriosWithCommas(params_) { params_ = params_ || {}; var strRawString = params_.string, strString = '' + strRawString, rc = ''; while (strString.length) { if (rc) { rc = ',' + rc; } if (strString.length > 3) { rc = strString.substring(strString.length - 3) + rc; strString = strString.substring(0, strString.length - 3); } else { rc = strString + rc; strString = ''; } } return rc; } //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- function pbPadDollars(params_) { var tmpRawDollarValue = params_.dollarValue, tmpForceDecimalsWhenNone = params_.forceDecimalsWhenNone, tmpIncludeCommas = params_.includeCommas, strDollarValue = trim( typeof tmpRawDollarValue === 'string' ? tmpRawDollarValue : '' + tmpRawDollarValue), tmpStringLength = strDollarValue.length, tmpDecimalPos = strDollarValue.indexOf('.'); if (tmpDecimalPos < 0) { if (!tmpForceDecimalsWhenNone) { // Not forcing decimals when there aren't any... if (tmpIncludeCommas) { return pbSplitTriosWithCommas({ string: strDollarValue }); } return strDollarValue; } // No decimal point. Add one at the end. tmpDecimalPos = tmpStringLength; strDollarValue += '.'; ++tmpStringLength; } // By this point there is definitely a decimal point. // Make sure it is followed by at least 2 digits. while (tmpDecimalPos + 3 > tmpStringLength) { strDollarValue += '0'; ++tmpStringLength; } if (tmpIncludeCommas) { strDollarValue = pbSplitTriosWithCommas({ string: strDollarValue.substring( 0, // number tmpDecimalPos) // end }) + strDollarValue.substring(tmpDecimalPos); } return strDollarValue; } //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- function pbPadAndPrefixSignedDollars(params_) { var tmpDollarValue = params_.dollarValue, tmpForceDecimalsWhenNone = params_.forceDecimalsWhenNone, tmpEmptyIfZero = params_.emptyIfZero, tmpTreatNullAsZero = params_.treatNullAsZero, strRawCurrencySymbol = params_.currencySymbol, strCurrencySymbolToUse = strRawCurrencySymbol || '$', strPrefix = '', strSuffix = '', rc; if (null === tmpDollarValue && !tmpTreatNullAsZero) { // If we aren't treating a null as $0 then it's a blank. rc = ''; } else { if (null === tmpDollarValue) { // At this point, we know that we're treating nulls as zero... tmpDollarValue = 0; } if (tmpEmptyIfZero && !tmpDollarValue) { // Empty string... rc = ''; } else { if (tmpDollarValue < 0) { tmpDollarValue = -tmpDollarValue; strPrefix = '(' + strCurrencySymbolToUse; strSuffix = ')'; } else { strPrefix = strCurrencySymbolToUse; } rc = strPrefix + pbPadDollars({ dollarValue: tmpDollarValue, forceDecimalsWhenNone: tmpForceDecimalsWhenNone, includeCommas: 1 }) + strSuffix; } } return rc; } return { getEditableFieldValueString: pbGetEditableFieldValueString, splitTriosWithCommas: pbSplitTriosWithCommas, padDollars: pbPadDollars, padAndPrefixSignedDollars: pbPadAndPrefixSignedDollars }; }();