Skip to content

Commit

Permalink
Merge pull request Expensify#29892 from paultsimura/fix/29553-iou-dis…
Browse files Browse the repository at this point in the history
…tance-tbd

fix: IOU - "TBD" is missing in the IOU details of the "Amount" field in offline mode
  • Loading branch information
Julesssss authored Oct 20, 2023
2 parents 262c8d0 + e012a67 commit 73733d5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/components/ReportActionItem/MoneyReportView.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function MoneyReportView(props) {
const {totalDisplaySpend, nonReimbursableSpend, reimbursableSpend} = ReportUtils.getMoneyRequestSpendBreakdown(props.report);

const shouldShowBreakdown = nonReimbursableSpend && reimbursableSpend;
const formattedTotalAmount = CurrencyUtils.convertToDisplayString(totalDisplaySpend, props.report.currency);
const formattedTotalAmount = CurrencyUtils.convertToDisplayString(totalDisplaySpend, props.report.currency, ReportUtils.hasOnlyDistanceRequestTransactions(props.report.reportID));
const formattedOutOfPocketAmount = CurrencyUtils.convertToDisplayString(reimbursableSpend, props.report.currency);
const formattedCompanySpendAmount = CurrencyUtils.convertToDisplayString(nonReimbursableSpend, props.report.currency);

Expand Down
4 changes: 4 additions & 0 deletions src/components/ReportActionItem/ReportPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ function ReportPreview(props) {
const transactionsWithReceipts = ReportUtils.getTransactionsWithReceipts(props.iouReportID);
const numberOfScanningReceipts = _.filter(transactionsWithReceipts, (transaction) => TransactionUtils.isReceiptBeingScanned(transaction)).length;
const hasReceipts = transactionsWithReceipts.length > 0;
const hasOnlyDistanceRequests = ReportUtils.hasOnlyDistanceRequestTransactions(props.iouReportID);
const isScanning = hasReceipts && ReportUtils.areAllRequestsBeingSmartScanned(props.iouReportID, props.action);
const hasErrors = hasReceipts && ReportUtils.hasMissingSmartscanFields(props.iouReportID);
const lastThreeTransactionsWithReceipts = transactionsWithReceipts.slice(-3);
Expand All @@ -145,6 +146,9 @@ function ReportPreview(props) {
if (isScanning) {
return props.translate('iou.receiptScanning');
}
if (hasOnlyDistanceRequests) {
return props.translate('common.tbd');
}

// If iouReport is not available, get amount from the action message (Ex: "Domain20821's Workspace owes $33.00" or "paid ₫60" or "paid -₫60 elsewhere")
let displayAmount = '';
Expand Down
8 changes: 7 additions & 1 deletion src/libs/CurrencyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Onyx from 'react-native-onyx';
import ONYXKEYS, {OnyxValues} from '../ONYXKEYS';
import CONST from '../CONST';
import BaseLocaleListener from './Localize/LocaleListener/BaseLocaleListener';
import * as Localize from './Localize';
import * as NumberFormatUtils from './NumberFormatUtils';

let currencyList: OnyxValues[typeof ONYXKEYS.CURRENCY_LIST] = {};
Expand Down Expand Up @@ -96,8 +97,13 @@ function convertToFrontendAmount(amountAsInt: number): number {
*
* @param amountInCents – should be an integer. Anything after a decimal place will be dropped.
* @param currency - IOU currency
* @param shouldFallbackToTbd - whether to return 'TBD' instead of a falsy value (e.g. 0.00)
*/
function convertToDisplayString(amountInCents: number, currency: string = CONST.CURRENCY.USD): string {
function convertToDisplayString(amountInCents: number, currency: string = CONST.CURRENCY.USD, shouldFallbackToTbd = false): string {
if (shouldFallbackToTbd && !amountInCents) {
return Localize.translateLocal('common.tbd');
}

const convertedAmount = convertToFrontendAmount(amountInCents);
return NumberFormatUtils.format(BaseLocaleListener.getPreferredLocale(), convertedAmount, {
style: 'currency',
Expand Down
26 changes: 19 additions & 7 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,17 @@ function hasSingleParticipant(report) {
return report && report.participantAccountIDs && report.participantAccountIDs.length === 1;
}

/**
* Checks whether all the transactions linked to the IOU report are of the Distance Request type
*
* @param {string|null} iouReportID
* @returns {boolean}
*/
function hasOnlyDistanceRequestTransactions(iouReportID) {
const allTransactions = TransactionUtils.getAllReportTransactions(iouReportID);
return _.all(allTransactions, (transaction) => TransactionUtils.isDistanceRequest(transaction));
}

/**
* If the report is a thread and has a chat type set, it is a workspace chat.
*
Expand Down Expand Up @@ -1415,23 +1426,23 @@ function getPolicyExpenseChatName(report, policy = undefined) {
}

/**
* Get the title for a IOU or expense chat which will be showing the payer and the amount
* Get the title for an IOU or expense chat which will be showing the payer and the amount
*
* @param {Object} report
* @param {Object} [policy]
* @returns {String}
*/
function getMoneyRequestReportName(report, policy = undefined) {
const moneyRequestTotal = getMoneyRequestReimbursableTotal(report);
const formattedAmount = CurrencyUtils.convertToDisplayString(moneyRequestTotal, report.currency);
const formattedAmount = CurrencyUtils.convertToDisplayString(moneyRequestTotal, report.currency, hasOnlyDistanceRequestTransactions(report.reportID));
const payerName = isExpenseReport(report) ? getPolicyName(report, false, policy) : getDisplayNameForParticipant(report.managerID);
const payerPaidAmountMesssage = Localize.translateLocal('iou.payerPaidAmount', {
const payerPaidAmountMessage = Localize.translateLocal('iou.payerPaidAmount', {
payer: payerName,
amount: formattedAmount,
});

if (report.isWaitingOnBankAccount) {
return `${payerPaidAmountMesssage}${Localize.translateLocal('iou.pending')}`;
return `${payerPaidAmountMessage}${Localize.translateLocal('iou.pending')}`;
}

if (hasNonReimbursableTransactions(report.reportID)) {
Expand All @@ -1442,7 +1453,7 @@ function getMoneyRequestReportName(report, policy = undefined) {
return Localize.translateLocal('iou.payerOwesAmount', {payer: payerName, amount: formattedAmount});
}

return payerPaidAmountMesssage;
return payerPaidAmountMessage;
}

/**
Expand Down Expand Up @@ -1527,7 +1538,7 @@ function canEditReportAction(reportAction) {
/**
* Gets all transactions on an IOU report with a receipt
*
* @param {Object|null} iouReportID
* @param {string|null} iouReportID
* @returns {[Object]}
*/
function getTransactionsWithReceipts(iouReportID) {
Expand Down Expand Up @@ -1593,7 +1604,7 @@ function getTransactionReportName(reportAction) {
const {amount, currency, comment} = getTransactionDetails(transaction);

return Localize.translateLocal(ReportActionsUtils.isSentMoneyReportAction(reportAction) ? 'iou.threadSentMoneyReportName' : 'iou.threadRequestReportName', {
formattedAmount: CurrencyUtils.convertToDisplayString(amount, currency),
formattedAmount: CurrencyUtils.convertToDisplayString(amount, currency, TransactionUtils.isDistanceRequest(transaction)),
comment,
});
}
Expand Down Expand Up @@ -4079,6 +4090,7 @@ export {
buildTransactionThread,
areAllRequestsBeingSmartScanned,
getTransactionsWithReceipts,
hasOnlyDistanceRequestTransactions,
hasNonReimbursableTransactions,
hasMissingSmartscanFields,
getIOUReportActionDisplayMessage,
Expand Down

0 comments on commit 73733d5

Please sign in to comment.