diff --git a/changelog/fix-7834-zero-decimals-csv-export b/changelog/fix-7834-zero-decimals-csv-export new file mode 100644 index 00000000000..1e5b2a1cf87 --- /dev/null +++ b/changelog/fix-7834-zero-decimals-csv-export @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Fix incorrect amounts caused by zero-decimal currencies on Transactions, Deposits and Deposits CSV export diff --git a/client/deposits/list/index.tsx b/client/deposits/list/index.tsx index f9c89ae88df..6dcf942df1b 100644 --- a/client/deposits/list/index.tsx +++ b/client/deposits/list/index.tsx @@ -25,7 +25,7 @@ import { useDispatch } from '@wordpress/data'; */ import { useDeposits, useDepositsSummary } from 'wcpay/data'; import { displayType, displayStatus } from '../strings'; -import { formatExplicitCurrency } from 'utils/currency'; +import { formatExplicitCurrency, formatExportAmount } from 'utils/currency'; import DetailsLink, { getDetailsURL } from 'components/details-link'; import ClickableCell from 'components/clickable-cell'; import Page from '../../components/page'; @@ -143,7 +143,7 @@ export const DepositsList = (): JSX.Element => { display: clickable( displayType[ deposit.type ] ), }, amount: { - value: deposit.amount / 100, + value: formatExportAmount( deposit.amount, deposit.currency ), display: clickable( formatExplicitCurrency( deposit.amount, deposit.currency ) ), diff --git a/client/disputes/index.tsx b/client/disputes/index.tsx index ed2524d641a..b1f70a0d1db 100644 --- a/client/disputes/index.tsx +++ b/client/disputes/index.tsx @@ -33,7 +33,7 @@ import Page from 'components/page'; import { TestModeNotice } from 'components/test-mode-notice'; import { reasons } from './strings'; import { formatStringValue } from 'utils'; -import { formatExplicitCurrency } from 'utils/currency'; +import { formatExplicitCurrency, formatExportAmount } from 'utils/currency'; import DisputesFilters from './filters'; import DownloadButton from 'components/download-button'; import disputeStatusMapping from 'components/dispute-status-chip/mappings'; @@ -232,7 +232,7 @@ export const DisputesList = (): JSX.Element => { }; } = { amount: { - value: dispute.amount / 100, + value: formatExportAmount( dispute.amount, dispute.currency ), display: clickable( formatExplicitCurrency( dispute.amount, dispute.currency ) ), diff --git a/client/payment-details/readers/index.js b/client/payment-details/readers/index.js index a87aa84b22d..9ff428c94fe 100644 --- a/client/payment-details/readers/index.js +++ b/client/payment-details/readers/index.js @@ -19,7 +19,7 @@ import { useCardReaderStats } from 'wcpay/data'; import { TestModeNotice } from 'components/test-mode-notice'; import Page from 'components/page'; import DownloadButton from 'components/download-button'; -import { formatExplicitCurrency } from 'utils/currency'; +import { formatExplicitCurrency, formatExportAmount } from 'utils/currency'; const PaymentCardReaderChargeDetails = ( props ) => { const { readers, chargeError, isLoading } = useCardReaderStats( @@ -100,7 +100,12 @@ const RenderPaymentCardReaderChargeDetails = ( props ) => { display: reader.count, }, { - value: reader.fee ? reader.fee.amount / 100 : 0, + value: reader.fee + ? formatExportAmount( + reader.fee.amount, + reader.fee.currency + ) + : 0, display: reader.fee ? formatExplicitCurrency( reader.fee.amount, diff --git a/client/transactions/list/index.tsx b/client/transactions/list/index.tsx index df9d360b11d..822047a4afd 100644 --- a/client/transactions/list/index.tsx +++ b/client/transactions/list/index.tsx @@ -40,7 +40,11 @@ import { getDetailsURL } from 'components/details-link'; import { displayType } from 'transactions/strings'; import { displayStatus as displayDepositStatus } from 'deposits/strings'; import { formatStringValue } from 'utils'; -import { formatCurrency, formatExplicitCurrency } from 'utils/currency'; +import { + formatCurrency, + formatExplicitCurrency, + formatExportAmount, +} from 'utils/currency'; import { getChargeChannel } from 'utils/charge'; import Deposit from './deposit'; import ConvertedAmount from './converted-amount'; @@ -343,7 +347,7 @@ export const TransactionsList = ( const fromAmount = txn.customer_amount ? txn.customer_amount : 0; return { - value: amount / 100, + value: formatExportAmount( amount, currency ), display: clickable( { const isCardReader = txn.metadata && txn.metadata.charge_type === 'card_reader_fee'; - const feeAmount = - ( isCardReader ? txn.amount : txn.fees * -1 ) / 100; + const feeAmount = formatExportAmount( + isCardReader ? txn.amount : txn.fees * -1, + currency + ); return { value: feeAmount, display: clickable( @@ -461,7 +467,7 @@ export const TransactionsList = ( // fees should display as negative. The format $-9.99 is determined by WC-Admin fees: formatFees(), net: { - value: txn.net / 100, + value: formatExportAmount( txn.net, currency ), display: clickable( formatExplicitCurrency( txn.net, currency ) ), diff --git a/client/utils/currency/index.js b/client/utils/currency/index.js index e4ded8bcdd7..44428c5b372 100644 --- a/client/utils/currency/index.js +++ b/client/utils/currency/index.js @@ -81,6 +81,24 @@ export const isZeroDecimalCurrency = ( currencyCode ) => { ); }; +/** + * Formats the amount for CSV export, considering zero-decimal currencies + * + * @param {number} amount Amount + * @param {string} currencyCode Currency code + * + * @return {number} Export amount + */ +export const formatExportAmount = ( amount, currencyCode ) => { + const isZeroDecimal = isZeroDecimalCurrency( currencyCode ); + + if ( ! isZeroDecimal ) { + amount /= 100; + } + + return amount; +}; + /** * Formats amount according to the given currency. * diff --git a/client/utils/currency/test/index.js b/client/utils/currency/test/index.js index 44398147ad3..72a2f35f4e6 100644 --- a/client/utils/currency/test/index.js +++ b/client/utils/currency/test/index.js @@ -109,4 +109,11 @@ describe( 'Currency utilities', () => { expect( utils.formatCurrency( 100000, 'EUR' ) ).toEqual( '€1,000.00' ); } ); + + test( 'format export amounts', () => { + expect( utils.formatExportAmount( 1000, 'USD' ) ).toEqual( 10 ); + expect( utils.formatExportAmount( 1250, 'USD' ) ).toEqual( 12.5 ); + expect( utils.formatExportAmount( 1000, 'JPY' ) ).toEqual( 1000 ); + expect( utils.formatExportAmount( 3450, 'JPY' ) ).toEqual( 3450 ); + } ); } );