-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
IOURequestStepTaxAmountPage.tsx
192 lines (168 loc) · 8.66 KB
/
IOURequestStepTaxAmountPage.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import {useFocusEffect} from '@react-navigation/native';
import React, {useCallback, useEffect, useRef} from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import type {BaseTextInputRef} from '@components/TextInput/BaseTextInput/types';
import useLocalize from '@hooks/useLocalize';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import Navigation from '@libs/Navigation/Navigation';
import * as ReportUtils from '@libs/ReportUtils';
import * as TransactionUtils from '@libs/TransactionUtils';
import type {CurrentMoney} from '@pages/iou/steps/MoneyRequestAmountForm';
import MoneyRequestAmountForm from '@pages/iou/steps/MoneyRequestAmountForm';
import * as IOU from '@userActions/IOU';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type {Policy, PolicyCategories, PolicyTagList, TaxRatesWithDefault, Transaction} from '@src/types/onyx';
import StepScreenWrapper from './StepScreenWrapper';
import withFullTransactionOrNotFound from './withFullTransactionOrNotFound';
import type {WithWritableReportOrNotFoundProps} from './withWritableReportOrNotFound';
import withWritableReportOrNotFound from './withWritableReportOrNotFound';
type IOURequestStepTaxAmountPageOnyxProps = {
policy: OnyxEntry<Policy>;
policyCategories: OnyxEntry<PolicyCategories>;
/** Collection of tag list on a policy */
policyTags: OnyxEntry<PolicyTagList>;
};
type IOURequestStepTaxAmountPageProps = IOURequestStepTaxAmountPageOnyxProps &
WithWritableReportOrNotFoundProps<typeof SCREENS.MONEY_REQUEST.STEP_TAX_AMOUNT> & {
transaction: OnyxEntry<Transaction>;
};
function getTaxAmount(transaction: OnyxEntry<Transaction>, taxRates: TaxRatesWithDefault | undefined, isEditing: boolean): number | undefined {
if (!transaction?.amount) {
return;
}
const transactionTaxAmount = TransactionUtils.getAmount(transaction);
const transactionTaxCode = transaction?.taxCode ?? '';
const defaultTaxValue = taxRates?.defaultValue;
const editingTaxPercentage = (transactionTaxCode ? taxRates?.taxes[transactionTaxCode]?.value : taxRates?.defaultValue) ?? '';
const moneyRequestTaxPercentage = (transaction?.taxRate ? transaction?.taxRate?.data?.value : defaultTaxValue) ?? '';
const taxPercentage = isEditing ? editingTaxPercentage : moneyRequestTaxPercentage;
return CurrencyUtils.convertToBackendAmount(TransactionUtils.calculateTaxAmount(taxPercentage, transactionTaxAmount));
}
function IOURequestStepTaxAmountPage({
route: {
params: {action, iouType, reportID, transactionID, backTo},
},
transaction,
report,
policy,
policyTags,
policyCategories,
}: IOURequestStepTaxAmountPageProps) {
const {translate} = useLocalize();
const textInput = useRef<BaseTextInputRef | null>();
const isEditing = action === CONST.IOU.ACTION.EDIT;
const focusTimeoutRef = useRef<NodeJS.Timeout>();
const isSaveButtonPressed = useRef(false);
const originalCurrency = useRef<string>();
const transactionDetails = ReportUtils.getTransactionDetails(transaction);
const taxRates = policy?.taxRates;
useEffect(() => {
if (transaction?.originalCurrency) {
originalCurrency.current = transaction.originalCurrency;
} else if (transaction?.currency) {
originalCurrency.current = transaction.currency;
IOU.setMoneyRequestOriginalCurrency_temporaryForRefactor(transactionID, transaction?.currency);
}
return () => {
if (isSaveButtonPressed.current || !originalCurrency.current) {
return;
}
IOU.setMoneyRequestCurrency_temporaryForRefactor(transactionID, originalCurrency.current, true);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useFocusEffect(
useCallback(() => {
focusTimeoutRef.current = setTimeout(() => textInput.current?.focus(), CONST.ANIMATED_TRANSITION);
return () => {
if (!focusTimeoutRef.current) {
return;
}
clearTimeout(focusTimeoutRef.current);
};
}, []),
);
const navigateBack = () => {
Navigation.goBack(backTo);
};
const navigateToCurrencySelectionPage = () => {
// If the money request being created is a distance request, don't allow the user to choose the currency.
// Only USD is allowed for distance requests.
// Remove query from the route and encode it.
Navigation.navigate(
ROUTES.MONEY_REQUEST_STEP_CURRENCY.getRoute(CONST.IOU.ACTION.CREATE, iouType, transactionID, reportID, backTo ? 'confirm' : '', Navigation.getActiveRouteWithoutParams()),
);
};
const updateTaxAmount = (currentAmount: CurrentMoney) => {
isSaveButtonPressed.current = true;
const amountInSmallestCurrencyUnits = CurrencyUtils.convertToBackendAmount(Number.parseFloat(currentAmount.amount));
if (isEditing) {
if (amountInSmallestCurrencyUnits === TransactionUtils.getTaxAmount(transaction, false)) {
navigateBack();
return;
}
IOU.updateMoneyRequestTaxAmount(transactionID, report?.reportID ?? '', amountInSmallestCurrencyUnits, policy, policyTags, policyCategories);
navigateBack();
return;
}
IOU.setMoneyRequestTaxAmount(transactionID, amountInSmallestCurrencyUnits, true);
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
IOU.setMoneyRequestCurrency_temporaryForRefactor(transactionID, transaction?.currency || CONST.CURRENCY.USD, true);
if (backTo) {
Navigation.goBack(backTo);
return;
}
// If a reportID exists in the report object, it's because the user started this flow from using the + button in the composer
// inside a report. In this case, the participants can be automatically assigned from the report and the user can skip the participants step and go straight
// to the confirm step.
if (report?.reportID) {
// TODO: Is this really needed at all?
IOU.setMoneyRequestParticipantsFromReport(transactionID, report);
Navigation.navigate(ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(CONST.IOU.ACTION.CREATE, iouType, transactionID, reportID));
return;
}
// If there was no reportID, then that means the user started this flow from the global + menu
// and an optimistic reportID was generated. In that case, the next step is to select the participants for this request.
Navigation.navigate(ROUTES.MONEY_REQUEST_STEP_PARTICIPANTS.getRoute(iouType, transactionID, reportID));
};
return (
<StepScreenWrapper
headerTitle={translate('iou.taxAmount')}
onBackButtonPress={navigateBack}
testID={IOURequestStepTaxAmountPage.displayName}
shouldShowWrapper={Boolean(backTo || isEditing)}
includeSafeAreaPaddingBottom
>
<MoneyRequestAmountForm
isEditing={Boolean(backTo || isEditing)}
currency={transaction?.currency}
amount={transactionDetails?.taxAmount}
taxAmount={getTaxAmount(transaction, taxRates, Boolean(backTo || isEditing))}
ref={(e) => (textInput.current = e)}
onCurrencyButtonPress={navigateToCurrencySelectionPage}
onSubmitButtonPress={updateTaxAmount}
/>
</StepScreenWrapper>
);
}
IOURequestStepTaxAmountPage.displayName = 'IOURequestStepTaxAmountPage';
const IOURequestStepTaxAmountPageWithOnyx = withOnyx<IOURequestStepTaxAmountPageProps, IOURequestStepTaxAmountPageOnyxProps>({
policy: {
key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY}${report ? report.policyID : '0'}`,
},
policyCategories: {
key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${report ? report.policyID : '0'}`,
},
policyTags: {
key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY_TAGS}${report ? report.policyID : '0'}`,
},
})(IOURequestStepTaxAmountPage);
// eslint-disable-next-line rulesdir/no-negated-variables
const IOURequestStepTaxAmountPageWithWritableReportOrNotFound = withWritableReportOrNotFound(IOURequestStepTaxAmountPageWithOnyx);
// eslint-disable-next-line rulesdir/no-negated-variables
const IOURequestStepTaxAmountPageWithFullTransactionOrNotFound = withFullTransactionOrNotFound(IOURequestStepTaxAmountPageWithWritableReportOrNotFound);
export default IOURequestStepTaxAmountPageWithFullTransactionOrNotFound;