|
| 1 | +import {useMemo} from 'react'; |
1 | 2 | import type {OnyxEntry} from 'react-native-onyx'; |
| 3 | +import {getPolicyByCustomUnitID} from '@libs/PolicyUtils'; |
2 | 4 | import {isExpenseUnreported} from '@libs/TransactionUtils'; |
3 | 5 | import CONST from '@src/CONST'; |
4 | 6 | import ONYXKEYS from '@src/ONYXKEYS'; |
5 | | -import type {Policy, Report, Transaction} from '@src/types/onyx'; |
| 7 | +import type {Policy, Transaction} from '@src/types/onyx'; |
6 | 8 | import useOnyx from './useOnyx'; |
7 | 9 | import usePolicyForMovingExpenses from './usePolicyForMovingExpenses'; |
8 | 10 |
|
9 | 11 | type UsePolicyForTransactionParams = { |
10 | 12 | /** The transaction to determine the policy for */ |
11 | 13 | transaction: OnyxEntry<Transaction>; |
12 | 14 |
|
13 | | - /** The report associated with the transaction */ |
14 | | - report: OnyxEntry<Report>; |
| 15 | + /** The report policy ID associated with the transaction */ |
| 16 | + reportPolicyID: string | undefined; |
15 | 17 |
|
16 | 18 | /** The current action being performed */ |
17 | 19 | action: string; |
18 | 20 |
|
19 | 21 | /** The type of IOU (split, track, submit, etc.) */ |
20 | 22 | iouType: string; |
| 23 | + |
| 24 | + /** Indicates if the request is a per diem request */ |
| 25 | + isPerDiemRequest?: boolean; |
21 | 26 | }; |
22 | 27 |
|
23 | 28 | type UsePolicyForTransactionResult = { |
24 | 29 | /** The policy to use for the transaction */ |
25 | 30 | policy: OnyxEntry<Policy>; |
26 | 31 | }; |
27 | 32 |
|
28 | | -function usePolicyForTransaction({transaction, report, action, iouType}: UsePolicyForTransactionParams): UsePolicyForTransactionResult { |
| 33 | +function usePolicyForTransaction({transaction, reportPolicyID, action, iouType, isPerDiemRequest}: UsePolicyForTransactionParams): UsePolicyForTransactionResult { |
29 | 34 | const {policyForMovingExpenses} = usePolicyForMovingExpenses(); |
| 35 | + |
| 36 | + const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: true}); |
| 37 | + |
| 38 | + const customUnitPolicy = useMemo(() => { |
| 39 | + return getPolicyByCustomUnitID(transaction, allPolicies); |
| 40 | + }, [transaction, allPolicies]); |
| 41 | + |
| 42 | + const [reportPolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${reportPolicyID}`, {canBeMissing: true}); |
| 43 | + |
30 | 44 | const isUnreportedExpense = isExpenseUnreported(transaction); |
31 | 45 | const isCreatingTrackExpense = action === CONST.IOU.ACTION.CREATE && iouType === CONST.IOU.TYPE.TRACK; |
32 | 46 |
|
33 | | - const [reportPolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`, {canBeMissing: true}); |
34 | | - const policy = isUnreportedExpense || isCreatingTrackExpense ? policyForMovingExpenses : reportPolicy; |
| 47 | + const policyForSelfDMExpense = isPerDiemRequest ? customUnitPolicy : policyForMovingExpenses; |
| 48 | + const policy = isUnreportedExpense || isCreatingTrackExpense ? policyForSelfDMExpense : reportPolicy; |
35 | 49 |
|
36 | 50 | return {policy}; |
37 | 51 | } |
|
0 commit comments