Skip to content
Merged
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
41 changes: 29 additions & 12 deletions src/libs/actions/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,8 @@ function changeTransactionsReport(
const successData: OnyxUpdate[] = [];

const existingSelfDMReportID = findSelfDMReportID();
let selfDMReport: Report;
let selfDMCreatedReportAction: ReportAction;
let selfDMReport: Report | undefined;
let selfDMCreatedReportAction: ReportAction | undefined;

if (!existingSelfDMReportID && reportID === CONST.REPORT.UNREPORTED_REPORT_ID) {
const currentTime = DateUtils.getDBTime();
Expand Down Expand Up @@ -899,14 +899,20 @@ function changeTransactionsReport(
(updatedReportUnheldNonReimbursableTotals[oldReportID] ? updatedReportUnheldNonReimbursableTotals[oldReportID] : (oldReport?.unheldNonReimbursableTotal ?? 0)) +
(transaction?.reimbursable && !isOnHold(transaction) ? 0 : transactionAmount);
}
if (reportID && newReport) {
updatedReportTotals[targetReportID] = (updatedReportTotals[targetReportID] ? updatedReportTotals[targetReportID] : (newReport.total ?? 0)) - transactionAmount;
updatedReportNonReimbursableTotals[targetReportID] =
(updatedReportNonReimbursableTotals[targetReportID] ? updatedReportNonReimbursableTotals[targetReportID] : (newReport.nonReimbursableTotal ?? 0)) -
(transactionReimbursable ? 0 : transactionAmount);
updatedReportUnheldNonReimbursableTotals[targetReportID] =
(updatedReportUnheldNonReimbursableTotals[targetReportID] ? updatedReportUnheldNonReimbursableTotals[targetReportID] : (newReport.unheldNonReimbursableTotal ?? 0)) -
(transactionReimbursable && !isOnHold(transaction) ? 0 : transactionAmount);

if (targetReportID) {
const targetReportKey = `${ONYXKEYS.COLLECTION.REPORT}${targetReportID}`;
const targetReport =
allReports?.[targetReportKey] ?? (targetReportID === newReport?.reportID ? newReport : undefined) ?? (targetReportID === selfDMReport?.reportID ? selfDMReport : undefined);

const currentTotal = updatedReportTotals[targetReportID] ?? targetReport?.total ?? 0;
updatedReportTotals[targetReportID] = currentTotal - transactionAmount;

const currentNonReimbursableTotal = updatedReportNonReimbursableTotals[targetReportID] ?? targetReport?.nonReimbursableTotal ?? 0;
updatedReportNonReimbursableTotals[targetReportID] = currentNonReimbursableTotal - (transactionReimbursable ? 0 : transactionAmount);

const currentUnheldNonReimbursableTotal = updatedReportUnheldNonReimbursableTotals[targetReportID] ?? targetReport?.unheldNonReimbursableTotal ?? 0;
updatedReportUnheldNonReimbursableTotals[targetReportID] = currentUnheldNonReimbursableTotal - (transactionReimbursable && !isOnHold(transaction) ? 0 : transactionAmount);
}

// 4. Optimistically update the IOU action reportID
Expand Down Expand Up @@ -1108,7 +1114,7 @@ function changeTransactionsReport(
: {}),
};

if (!existingSelfDMReportID && reportID === CONST.REPORT.UNREPORTED_REPORT_ID) {
if (!existingSelfDMReportID && reportID === CONST.REPORT.UNREPORTED_REPORT_ID && selfDMReport && selfDMCreatedReportAction) {
// Add self DM data to transaction data
transactionIDToReportActionAndThreadData[transaction.transactionID] = {
...baseTransactionData,
Expand Down Expand Up @@ -1224,7 +1230,18 @@ function changeTransactionsReport(
});

// 9. Update next step for report
const nextStepReport = {...newReport, total: updatedReportTotals[reportID] ?? newReport?.total, reportID: newReport?.reportID ?? reportID};
const destinationReportID = reportID === CONST.REPORT.UNREPORTED_REPORT_ID ? (existingSelfDMReportID ?? selfDMReport?.reportID) : reportID;
const destinationReport = destinationReportID
? (allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${destinationReportID}`] ??
(destinationReportID === newReport?.reportID ? newReport : undefined) ??
(destinationReportID === selfDMReport?.reportID ? selfDMReport : undefined))
: undefined;
const destinationTotal = (destinationReportID ? updatedReportTotals[destinationReportID] : undefined) ?? destinationReport?.total ?? newReport?.total;
const nextStepReport = {
...destinationReport,
reportID: destinationReport?.reportID ?? destinationReportID ?? reportID,
total: destinationTotal,
};
const hasViolations = hasViolationsReportUtils(nextStepReport?.reportID, allTransactionViolation);
const optimisticNextStep = buildNextStepNew({
report: nextStepReport,
Expand Down