Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't show pay button if report has negative total #57983

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5281,7 +5281,7 @@ function buildOptimisticExpenseReport(
return expenseReport;
}

function getFormattedAmount(reportAction: ReportAction) {
function getFormattedAmount(reportAction: ReportAction, report?: Report | null) {
if (
!isSubmittedAction(reportAction) &&
!isForwardedAction(reportAction) &&
Expand All @@ -5292,30 +5292,38 @@ function getFormattedAmount(reportAction: ReportAction) {
return '';
}
const originalMessage = getOriginalMessage(reportAction);
const formattedAmount = convertToDisplayString(Math.abs(originalMessage?.amount ?? 0), originalMessage?.currency);

// Expense reports can have a negative amount and we need to display it as negative in the UI
// the amount found in originalMessage does not accurately track this so we need to use the total from the report instead
const amount = report && isExpenseReport(report) ? (report?.total ?? 0) * -1 : Math.abs(originalMessage?.amount ?? 0);
const formattedAmount = convertToDisplayString(amount, originalMessage?.currency);
return formattedAmount;
}

function getReportAutomaticallySubmittedMessage(
reportAction: ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.SUBMITTED> | ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.SUBMITTED_AND_CLOSED>,
report?: Report,
) {
return translateLocal('iou.automaticallySubmittedAmount', {formattedAmount: getFormattedAmount(reportAction)});
return translateLocal('iou.automaticallySubmittedAmount', {formattedAmount: getFormattedAmount(reportAction, report)});
}

function getIOUSubmittedMessage(reportAction: ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.SUBMITTED> | ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.SUBMITTED_AND_CLOSED>) {
return translateLocal('iou.submittedAmount', {formattedAmount: getFormattedAmount(reportAction)});
function getIOUSubmittedMessage(
reportAction: ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.SUBMITTED> | ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.SUBMITTED_AND_CLOSED>,
report?: Report,
) {
return translateLocal('iou.submittedAmount', {formattedAmount: getFormattedAmount(reportAction, report)});
}

function getReportAutomaticallyApprovedMessage(reportAction: ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.APPROVED>) {
return translateLocal('iou.automaticallyApprovedAmount', {amount: getFormattedAmount(reportAction)});
function getReportAutomaticallyApprovedMessage(reportAction: ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.APPROVED>, report?: Report) {
return translateLocal('iou.automaticallyApprovedAmount', {amount: getFormattedAmount(reportAction, report)});
}

function getIOUUnapprovedMessage(reportAction: ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.UNAPPROVED>) {
return translateLocal('iou.unapprovedAmount', {amount: getFormattedAmount(reportAction)});
function getIOUUnapprovedMessage(reportAction: ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.UNAPPROVED>, report?: Report) {
return translateLocal('iou.unapprovedAmount', {amount: getFormattedAmount(reportAction, report)});
}

function getIOUApprovedMessage(reportAction: ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.APPROVED>) {
return translateLocal('iou.approvedAmount', {amount: getFormattedAmount(reportAction)});
function getIOUApprovedMessage(reportAction: ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.APPROVED>, report?: Report) {
return translateLocal('iou.approvedAmount', {amount: getFormattedAmount(reportAction, report)});
}

/**
Expand All @@ -5329,7 +5337,7 @@ function getReportAutomaticallyForwardedMessage(reportAction: ReportAction<typeo

// Older FORWARDED action might not have the amount stored in the original message, we'll fallback to getting the amount from the report instead.
if (originalMessage?.amount) {
formattedAmount = getFormattedAmount(reportAction);
formattedAmount = getFormattedAmount(reportAction, expenseReport);
} else {
formattedAmount = convertToDisplayString(getMoneyRequestSpendBreakdown(expenseReport).totalDisplaySpend, expenseReport?.currency);
}
Expand All @@ -5352,7 +5360,7 @@ function getIOUForwardedMessage(

// Older FORWARDED action might not have the amount stored in the original message, we'll fallback to getting the amount from the report instead.
if (originalMessage?.amount) {
formattedAmount = getFormattedAmount(reportAction);
formattedAmount = getFormattedAmount(reportAction, expenseReport);
} else {
formattedAmount = convertToDisplayString(getMoneyRequestSpendBreakdown(expenseReport, reports).totalDisplaySpend, expenseReport?.currency);
}
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8277,7 +8277,7 @@ function canIOUBePaid(
!isOpenExpenseReport &&
!iouSettled &&
!iouReport?.isWaitingOnBankAccount &&
reimbursableSpend !== 0 &&
reimbursableSpend > 0 &&
!isChatReportArchived &&
!isAutoReimbursable &&
(!shouldBeApproved || !shouldCheckApprovedState) &&
Expand Down
10 changes: 5 additions & 5 deletions src/pages/home/report/PureReportActionItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -835,25 +835,25 @@ function PureReportActionItem({
if (wasSubmittedViaHarvesting) {
children = (
<ReportActionItemBasicMessage message="">
<RenderHTML html={`<comment><muted-text>${getReportAutomaticallySubmittedMessage(action)}</muted-text></comment>`} />
<RenderHTML html={`<comment><muted-text>${getReportAutomaticallySubmittedMessage(action, report)}</muted-text></comment>`} />
</ReportActionItemBasicMessage>
);
} else {
children = <ReportActionItemBasicMessage message={getIOUSubmittedMessage(action)} />;
children = <ReportActionItemBasicMessage message={getIOUSubmittedMessage(action, report)} />;
}
} else if (isActionOfType(action, CONST.REPORT.ACTIONS.TYPE.APPROVED)) {
const wasAutoApproved = getOriginalMessage(action)?.automaticAction ?? false;
if (wasAutoApproved) {
children = (
<ReportActionItemBasicMessage message="">
<RenderHTML html={`<comment><muted-text>${getReportAutomaticallyApprovedMessage(action)}</muted-text></comment>`} />
<RenderHTML html={`<comment><muted-text>${getReportAutomaticallyApprovedMessage(action, report)}</muted-text></comment>`} />
</ReportActionItemBasicMessage>
);
} else {
children = <ReportActionItemBasicMessage message={getIOUApprovedMessage(action)} />;
children = <ReportActionItemBasicMessage message={getIOUApprovedMessage(action, report)} />;
}
} else if (isUnapprovedAction(action)) {
children = <ReportActionItemBasicMessage message={getIOUUnapprovedMessage(action)} />;
children = <ReportActionItemBasicMessage message={getIOUUnapprovedMessage(action, report)} />;
} else if (isActionOfType(action, CONST.REPORT.ACTIONS.TYPE.FORWARDED)) {
const wasAutoForwarded = getOriginalMessage(action)?.automaticAction ?? false;
if (wasAutoForwarded) {
Expand Down
23 changes: 23 additions & 0 deletions tests/actions/IOUTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
canApproveIOU,
canCancelPayment,
cancelPayment,
canIOUBePaid,
canUnapproveIOU,
deleteMoneyRequest,
payMoneyRequest,
Expand Down Expand Up @@ -4768,4 +4769,26 @@ describe('actions/IOU', () => {
expect(canCancelPayment(fakeReport, {accountID: RORY_ACCOUNT_ID})).toBeTruthy();
});
});

describe('canIOUBePaid', () => {
it('should return false if the report has negative total', () => {
const policyChat = createRandomReport(1);
const fakePolicy: Policy = {
...createRandomPolicy(Number('AA')),
type: CONST.POLICY.TYPE.TEAM,
approvalMode: CONST.POLICY.APPROVAL_MODE.BASIC,
};

const fakeReport: Report = {
...createRandomReport(1),
type: CONST.REPORT.TYPE.EXPENSE,
policyID: 'AA',
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
statusNum: CONST.REPORT.STATUS_NUM.APPROVED,
managerID: RORY_ACCOUNT_ID,
total: 100, // positive amount in the DB means negative amount in the UI
};
expect(canIOUBePaid(fakeReport, policyChat, fakePolicy)).toBeFalsy();
});
});
});