Skip to content

Commit

Permalink
Merge pull request #34909 from Expensify/cmartins-getMoneyRequestReim…
Browse files Browse the repository at this point in the history
…bursableTotal

Fix Settlement button total
  • Loading branch information
techievivek committed Jan 29, 2024
2 parents c5ca9a8 + fbda64a commit 1aea625
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 35 deletions.
10 changes: 5 additions & 5 deletions src/components/MoneyReportHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function MoneyReportHeader({session, personalDetails, policy, chatReport, nextSt
const styles = useThemeStyles();
const {translate} = useLocalize();
const {windowWidth, isSmallScreenWidth} = useWindowDimensions();
const reimbursableTotal = ReportUtils.getMoneyRequestReimbursableTotal(moneyRequestReport);
const {reimbursableSpend} = ReportUtils.getMoneyRequestSpendBreakdown(moneyRequestReport);
const isApproved = ReportUtils.isReportApproved(moneyRequestReport);
const isSettled = ReportUtils.isSettled(moneyRequestReport.reportID);
const policyType = policy?.type;
Expand All @@ -66,8 +66,8 @@ function MoneyReportHeader({session, personalDetails, policy, chatReport, nextSt
: isPolicyAdmin || (ReportUtils.isMoneyRequestReport(moneyRequestReport) && isManager);
const isDraft = ReportUtils.isDraftExpenseReport(moneyRequestReport);
const shouldShowPayButton = useMemo(
() => isPayer && !isDraft && !isSettled && !moneyRequestReport.isWaitingOnBankAccount && reimbursableTotal !== 0 && !ReportUtils.isArchivedRoom(chatReport) && !isAutoReimbursable,
[isPayer, isDraft, isSettled, moneyRequestReport, reimbursableTotal, chatReport, isAutoReimbursable],
() => isPayer && !isDraft && !isSettled && !moneyRequestReport.isWaitingOnBankAccount && reimbursableSpend !== 0 && !ReportUtils.isArchivedRoom(chatReport) && !isAutoReimbursable,
[isPayer, isDraft, isSettled, moneyRequestReport, reimbursableSpend, chatReport, isAutoReimbursable],
);
const shouldShowApproveButton = useMemo(() => {
if (!isPaidGroupPolicy) {
Expand All @@ -76,12 +76,12 @@ function MoneyReportHeader({session, personalDetails, policy, chatReport, nextSt
return isManager && !isDraft && !isApproved && !isSettled;
}, [isPaidGroupPolicy, isManager, isDraft, isApproved, isSettled]);
const shouldShowSettlementButton = shouldShowPayButton || shouldShowApproveButton;
const shouldShowSubmitButton = isDraft && reimbursableTotal !== 0;
const shouldShowSubmitButton = isDraft && reimbursableSpend !== 0;
const isFromPaidPolicy = policyType === CONST.POLICY.TYPE.TEAM || policyType === CONST.POLICY.TYPE.CORPORATE;
const shouldShowNextStep = isFromPaidPolicy && !!nextStep?.message?.length;
const shouldShowAnyButton = shouldShowSettlementButton || shouldShowApproveButton || shouldShowSubmitButton || shouldShowNextStep;
const bankAccountRoute = ReportUtils.getBankAccountRoute(chatReport);
const formattedAmount = CurrencyUtils.convertToDisplayString(reimbursableTotal, moneyRequestReport.currency);
const formattedAmount = CurrencyUtils.convertToDisplayString(reimbursableSpend, moneyRequestReport.currency);
const isMoreContentShown = shouldShowNextStep || (shouldShowAnyButton && isSmallScreenWidth);

// The submit button should be success green colour only if the user is submitter and the policy does not have Scheduled Submit turned on
Expand Down
2 changes: 1 addition & 1 deletion src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ function createOption(
}

result.isIOUReportOwner = ReportUtils.isIOUOwnedByCurrentUser(result);
result.iouReportAmount = ReportUtils.getMoneyRequestReimbursableTotal(result);
result.iouReportAmount = ReportUtils.getMoneyRequestSpendBreakdown(result).totalDisplaySpend;

if (!hasMultipleParticipants) {
result.login = personalDetail?.login;
Expand Down
31 changes: 4 additions & 27 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1794,28 +1794,6 @@ function hasNonReimbursableTransactions(iouReportID: string | undefined): boolea
return transactions.filter((transaction) => transaction.reimbursable === false).length > 0;
}

function getMoneyRequestReimbursableTotal(report: OnyxEntry<Report>, allReportsDict: OnyxCollection<Report> = null): number {
const allAvailableReports = allReportsDict ?? allReports;
let moneyRequestReport: OnyxEntry<Report> | undefined;
if (isMoneyRequestReport(report)) {
moneyRequestReport = report;
}
if (allAvailableReports && report?.iouReportID) {
moneyRequestReport = allAvailableReports[`${ONYXKEYS.COLLECTION.REPORT}${report.iouReportID}`];
}
if (moneyRequestReport) {
const total = moneyRequestReport?.total ?? 0;

if (total !== 0) {
// There is a possibility that if the Expense report has a negative total.
// This is because there are instances where you can get a credit back on your card,
// or you enter a negative expense to “offset” future expenses
return isExpenseReport(moneyRequestReport) ? total * -1 : Math.abs(total);
}
}
return 0;
}

function getMoneyRequestSpendBreakdown(report: OnyxEntry<Report>, allReportsDict: OnyxCollection<Report> = null): SpendBreakdown {
const allAvailableReports = allReportsDict ?? allReports;
let moneyRequestReport;
Expand Down Expand Up @@ -1896,7 +1874,7 @@ function getPolicyExpenseChatName(report: OnyxEntry<Report>, policy: OnyxEntry<P
* Get the title for an IOU or expense chat which will be showing the payer and the amount
*/
function getMoneyRequestReportName(report: OnyxEntry<Report>, policy: OnyxEntry<Policy> | undefined = undefined): string {
const moneyRequestTotal = getMoneyRequestReimbursableTotal(report);
const moneyRequestTotal = getMoneyRequestSpendBreakdown(report).totalDisplaySpend;
const formattedAmount = CurrencyUtils.convertToDisplayString(moneyRequestTotal, report?.currency, hasOnlyDistanceRequestTransactions(report?.reportID));
const payerOrApproverName = isExpenseReport(report) ? getPolicyName(report, false, policy) : getDisplayNameForParticipant(report?.managerID) ?? '';
const payerPaidAmountMessage = Localize.translateLocal('iou.payerPaidAmount', {
Expand Down Expand Up @@ -2196,7 +2174,7 @@ function getReportPreviewMessage(
}
}

const totalAmount = getMoneyRequestReimbursableTotal(report);
const totalAmount = getMoneyRequestSpendBreakdown(report).totalDisplaySpend;
const policyName = getPolicyName(report, false, policy);
const payerName = isExpenseReport(report) ? policyName : getDisplayNameForParticipant(report.managerID, !isPreviewMessageForParentChatReport);

Expand Down Expand Up @@ -2791,7 +2769,7 @@ function getIOUReportActionMessage(iouReportID: string, type: string, total: num
const report = getReport(iouReportID);
const amount =
type === CONST.IOU.REPORT_ACTION_TYPE.PAY
? CurrencyUtils.convertToDisplayString(getMoneyRequestReimbursableTotal(!isEmptyObject(report) ? report : null), currency)
? CurrencyUtils.convertToDisplayString(getMoneyRequestSpendBreakdown(!isEmptyObject(report) ? report : null).totalDisplaySpend, currency)
: CurrencyUtils.convertToDisplayString(total, currency);

let paymentMethodMessage;
Expand Down Expand Up @@ -4645,7 +4623,7 @@ function canBeAutoReimbursed(report: OnyxEntry<Report>, policy: OnyxEntry<Policy
return false;
}
type CurrencyType = (typeof CONST.DIRECT_REIMBURSEMENT_CURRENCIES)[number];
const reimbursableTotal = getMoneyRequestReimbursableTotal(report);
const reimbursableTotal = getMoneyRequestSpendBreakdown(report).totalDisplaySpend;
const autoReimbursementLimit = policy.autoReimbursementLimit ?? 0;
const isAutoReimbursable =
isGroupPolicy(report) &&
Expand Down Expand Up @@ -4692,7 +4670,6 @@ export {
hasExpensifyGuidesEmails,
requiresAttentionFromCurrentUser,
isIOUOwnedByCurrentUser,
getMoneyRequestReimbursableTotal,
getMoneyRequestSpendBreakdown,
canShowReportRecipientLocalTime,
formatReportLastMessageText,
Expand Down
4 changes: 2 additions & 2 deletions src/libs/SidebarUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function getOrderedReportIDs(
report.displayName = ReportUtils.getReportName(report);

// eslint-disable-next-line no-param-reassign
report.iouReportAmount = ReportUtils.getMoneyRequestReimbursableTotal(report, allReports);
report.iouReportAmount = ReportUtils.getMoneyRequestSpendBreakdown(report, allReports).totalDisplaySpend;

const isPinned = report.isPinned ?? false;
const reportAction = ReportActionsUtils.getReportAction(report.parentReportID ?? '', report.parentReportActionID ?? '');
Expand Down Expand Up @@ -459,7 +459,7 @@ function getOptionData({
}

result.isIOUReportOwner = ReportUtils.isIOUOwnedByCurrentUser(result as Report);
result.iouReportAmount = ReportUtils.getMoneyRequestReimbursableTotal(result as Report);
result.iouReportAmount = ReportUtils.getMoneyRequestSpendBreakdown(result as Report).totalDisplaySpend;

if (!hasMultipleParticipants) {
result.accountID = personalDetail?.accountID;
Expand Down

0 comments on commit 1aea625

Please sign in to comment.