Skip to content
Merged
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
8 changes: 6 additions & 2 deletions src/libs/actions/IOU/Duplicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ import type {PerDiemExpenseInformation} from './PerDiem';
import type {CreateDistanceRequestInformation} from './Split';
import type {CreateTrackExpenseParams} from './TrackExpense';

import {getAllReports, getAllTransactions} from '.';
import {getAllReports, getAllTransactions, getPolicyTags} from '.';
import {getCleanUpTransactionThreadReportOnyxData} from './DeleteMoneyRequest';
import {getMoneyRequestParticipantsFromReport} from './MoneyRequest';
import {submitPerDiemExpense} from './PerDiem';
import {getPerDiemExpensePolicyID, submitPerDiemExpense} from './PerDiem';
import {createDistanceRequest} from './Split';
import {requestMoney, trackExpense} from './TrackExpense';

Expand Down Expand Up @@ -729,6 +729,9 @@ function createExpenseByType({
return createDistanceRequest(distanceParams);
}
case CONST.SEARCH.TRANSACTION_TYPE.PER_DIEM: {
const earlyPolicyID = getPerDiemExpensePolicyID(params);
const policyTags = getPolicyTags()?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${earlyPolicyID}`] ?? {};

const perDiemParams: PerDiemExpenseInformation = {
...params,
transactionParams: {
Expand All @@ -740,6 +743,7 @@ function createExpenseByType({
customUnitPolicyID,
isTrackIntentUser,
formatPhoneNumber,
policyTags,
};
return submitPerDiemExpense(perDiemParams);
}
Expand Down
58 changes: 54 additions & 4 deletions src/libs/actions/IOU/PerDiem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import type BasePolicyParams from './types/BasePolicyParams';
import type BaseTransactionParams from './types/BaseTransactionParams';
import type RequestMoneyParticipantParams from './types/RequestMoneyParticipantParams';

import {getAllPersonalDetails, getAllReports, getPolicyTags} from '.';
import {getAllPersonalDetails, getAllReports} from '.';
import {
buildMinimalTransactionForFormula,
buildOnyxDataForMoneyRequest,
Expand Down Expand Up @@ -229,6 +229,8 @@ type PerDiemExpenseInformation = {
recentlyUsedParams?: RecentlyUsedParams;
transactionParams: PerDiemExpenseTransactionParams;
existingIOUReport?: OnyxEntry<OnyxTypes.Report>;
/** The policy's tags, keyed the same way `getPolicyTags()` would return for this expense's policyID. */
policyTags: OnyxTypes.PolicyTagLists;
isASAPSubmitBetaEnabled: boolean;
currentUserAccountIDParam: number;
currentUserEmailParam: string;
Expand Down Expand Up @@ -257,6 +259,8 @@ type PerDiemExpenseInformationParams = {
recentlyUsedParams?: RecentlyUsedParams;
existingIOUReport?: OnyxEntry<OnyxTypes.Report>;
moneyRequestReportID?: string;
/** The policy's tags, keyed the same way `getPolicyTags()` would return for this expense's policyID. */
policyTags: OnyxTypes.PolicyTagLists;
isASAPSubmitBetaEnabled: boolean;
currentUserAccountIDParam: number;
currentUserEmailParam: string;
Expand Down Expand Up @@ -303,6 +307,50 @@ type PerDiemExpenseInformationForSelfDMResult = {
>;
};

type GetPerDiemExpensePolicyIDParams = {
report: OnyxEntry<OnyxTypes.Report>;
participantParams: RequestMoneyParticipantParams;
existingIOUReport?: OnyxEntry<OnyxTypes.Report>;
betas: OnyxEntry<OnyxTypes.Beta[]>;
currentUserAccountIDParam: number;
};

/**
* Resolves the policyID that `getPerDiemExpenseInformation`'s iouReport will end up with, without waiting for its
* STEP 1/STEP 2 (chatReport/iouReport resolution) to run. It is read-only and does not build any optimistic report or
* transaction. Keep in sync with STEP 1/STEP 2 in `getPerDiemExpenseInformation` (and the chat report/moneyRequestReportID
* resolution in `submitPerDiemExpense`) if their resolution order changes.
*/
function getPerDiemExpensePolicyID({report, participantParams, existingIOUReport, betas, currentUserAccountIDParam}: GetPerDiemExpensePolicyIDParams): string | undefined {
const {payeeAccountID = currentUserAccountIDParam, participant} = participantParams;
const payerAccountID = Number(participant.accountID);
const isPolicyExpenseChat = participant.isPolicyExpenseChat;
const allReports = getAllReports();

const isMoneyRequestReport = isMoneyRequestReportReportUtils(report);
const parentChatReport = isMoneyRequestReport ? getReportOrDraftReport(report?.chatReportID) : report;
const moneyRequestReportID = isMoneyRequestReport ? report?.reportID : '';

const parentChatReportCandidate = parentChatReport?.reportID ? parentChatReport : null;
const policyExpenseChatCandidate = isPolicyExpenseChat ? (allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${participant.reportID}`] ?? null) : null;
const chatReport = parentChatReportCandidate ?? policyExpenseChatCandidate ?? getChatByParticipants([payerAccountID, payeeAccountID]) ?? null;

let iouReport: OnyxInputValue<OnyxTypes.Report> = null;
if (existingIOUReport) {
iouReport = existingIOUReport;
} else if (moneyRequestReportID) {
iouReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${moneyRequestReportID}`] ?? null;
} else if (chatReport) {
iouReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${chatReport.iouReportID}`] ?? null;
}
const shouldCreateNew = shouldCreateNewMoneyRequestReportReportUtils(iouReport, chatReport, false, betas);

if (iouReport && !shouldCreateNew) {
return iouReport.policyID;
}
return isPolicyExpenseChat ? (chatReport?.policyID ?? CONST.POLICY.OWNER_EMAIL_FAKE) : undefined;
}

/**
* Gathers all the data needed to submit a per diem expense. It attempts to find existing reports, iouReports, and receipts. If it doesn't find them, then
* it creates optimistic versions of them and uses those instead
Expand All @@ -316,6 +364,7 @@ function getPerDiemExpenseInformation(perDiemExpenseInformation: PerDiemExpenseI
recentlyUsedParams = {},
existingIOUReport: existingIOUReportParam,
moneyRequestReportID = '',
policyTags,
isASAPSubmitBetaEnabled,
currentUserAccountIDParam,
currentUserEmailParam,
Expand Down Expand Up @@ -456,9 +505,7 @@ function getPerDiemExpenseInformation(perDiemExpenseInformation: PerDiemExpenseI
optimisticTransaction.hasEReceipt = true;
const optimisticPolicyRecentlyUsedCategories = mergePolicyRecentlyUsedCategories(category, policyRecentlyUsedCategories);
const optimisticPolicyRecentlyUsedTags = buildOptimisticPolicyRecentlyUsedTags({
// TODO: Replace getPolicyTags (https://github.com/Expensify/App/issues/72721) and getPolicyRecentlyUsedTagsData (https://github.com/Expensify/App/issues/71491) with useOnyx hook

policyTags: getPolicyTags()?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${iouReport.policyID}`] ?? {},
policyTags,
policyRecentlyUsedTags,
transactionTags: tag,
});
Expand Down Expand Up @@ -929,6 +976,7 @@ function submitPerDiemExpense(submitPerDiemExpenseInformation: PerDiemExpenseInf
recentlyUsedParams = {},
transactionParams,
existingIOUReport,
policyTags,
isASAPSubmitBetaEnabled,
currentUserAccountIDParam,
currentUserEmailParam,
Expand Down Expand Up @@ -987,6 +1035,7 @@ function submitPerDiemExpense(submitPerDiemExpenseInformation: PerDiemExpenseInf
transactionParams,
existingIOUReport,
moneyRequestReportID,
policyTags,
isASAPSubmitBetaEnabled,
currentUserAccountIDParam,
currentUserEmailParam,
Expand Down Expand Up @@ -1164,6 +1213,7 @@ export {
addSubrate,
computePerDiemExpenseAmount,
isValidPerDiemExpenseAmount,
getPerDiemExpensePolicyID,
getPerDiemExpenseInformation,
submitPerDiemExpense,
submitPerDiemExpenseForSelfDM,
Expand Down
24 changes: 18 additions & 6 deletions src/pages/iou/request/step/confirmation/useExpenseSubmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ import {

import {resolveChatTargetForSubmitCleanup} from '@pages/iou/request/step/resolveChatTarget';

import {getPolicyTags} from '@userActions/IOU';
import {isOneToTwoTransactionTransition} from '@userActions/IOU/PendingNewTransactions';
import {submitPerDiemExpenseForSelfDM, submitPerDiemExpense as submitPerDiemExpenseIOUActions} from '@userActions/IOU/PerDiem';
import {getPerDiemExpensePolicyID, submitPerDiemExpenseForSelfDM, submitPerDiemExpense as submitPerDiemExpenseIOUActions} from '@userActions/IOU/PerDiem';
import {getReceiverType, sendInvoice} from '@userActions/IOU/SendInvoice';
import {sendMoneyElsewhere, sendMoneyWithWallet} from '@userActions/IOU/SendMoney';
import {createDistanceRequest as createDistanceRequestIOUActions, splitBill, splitBillAndOpenReport, startSplitBill} from '@userActions/IOU/Split';
Expand Down Expand Up @@ -613,14 +614,24 @@ function useExpenseSubmission(params: UseExpenseSubmissionParams) {
);
const activeReportID = isExpenseReport ? report?.reportID : chatReportID;

const perDiemParticipantParams = {
payeeEmail: currentUserPersonalDetails.login,
payeeAccountID: currentUserPersonalDetails.accountID,
participant,
};
const earlyPolicyID = getPerDiemExpensePolicyID({
report,
participantParams: perDiemParticipantParams,
existingIOUReport: undefined,
betas,
currentUserAccountIDParam: currentUserPersonalDetails.accountID,
});
const perDiemExpensePolicyTags = getPolicyTags()?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${earlyPolicyID}`] ?? {};

const result = submitPerDiemExpenseIOUActions({
getCurrencyDecimals,
report,
participantParams: {
payeeEmail: currentUserPersonalDetails.login,
payeeAccountID: currentUserPersonalDetails.accountID,
participant,
},
participantParams: perDiemParticipantParams,
policyParams: {
policy,
policyTagList: policyTags,
Expand All @@ -643,6 +654,7 @@ function useExpenseSubmission(params: UseExpenseSubmissionParams) {
attendees: transaction.comment?.attendees,
isFromGlobalCreate: getIsFromGlobalCreate(transaction),
},
policyTags: perDiemExpensePolicyTags,
isASAPSubmitBetaEnabled,
currentUserAccountIDParam: currentUserPersonalDetails.accountID,
currentUserEmailParam: currentUserPersonalDetails.login ?? '',
Expand Down
Loading
Loading