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

Feat/36985 create new rate field #38543

Merged
Merged
Show file tree
Hide file tree
Changes from 97 commits
Commits
Show all changes
98 commits
Select commit Hold shift + click to select a range
1db16d9
feat: create new rate field
koko57 Feb 26, 2024
2c19aa3
feat: create rate selection page
koko57 Feb 26, 2024
f3b17ad
fix: resolve conflicts
koko57 Feb 29, 2024
b3e1f48
feat: add translation for the rate page
koko57 Feb 29, 2024
6e94444
feat: wip - display rates for policy
koko57 Feb 29, 2024
e759979
fix: resolve conflicts
koko57 Mar 1, 2024
cde1d19
feat: wip - get rate for p2p
koko57 Mar 1, 2024
c3cb80b
feat: wip - new rate field logic
koko57 Mar 1, 2024
5bf399a
fix: revert filtering changes
koko57 Mar 1, 2024
857c320
fix: resolve conflicts
koko57 Mar 12, 2024
bf5e278
fix: resolve conflicts
koko57 Mar 13, 2024
ef7b30e
refactor: use canUseP2PDistanceRequests from usePermissions
koko57 Mar 13, 2024
6f3afdd
fix: remove mock rates
koko57 Mar 13, 2024
50174f3
feat: create function for getting personal policy
koko57 Mar 13, 2024
eb3aff8
feat: add rate field to MoneyRequestView
koko57 Mar 14, 2024
18e2c7c
fix: resolve conflicts
koko57 Mar 15, 2024
1ddc839
Merge branch 'main' into feat/36985-create-new-rate-field
koko57 Mar 18, 2024
7dc206c
feat: change customRateID when participant changes, other improvements
koko57 Mar 18, 2024
7698b5a
fix: resolve conflicts
koko57 Mar 19, 2024
a0227e0
fix: run prettier
koko57 Mar 19, 2024
eb350ee
fix: lint fix
koko57 Mar 19, 2024
af94a75
fix: resolve conflicts
koko57 Mar 28, 2024
272c300
fix: resolve conflicts
koko57 Mar 28, 2024
0682afb
fix: add missing screen
koko57 Mar 28, 2024
6105b5d
fix: show check mark on lastSelectedDistanceRate
koko57 Mar 28, 2024
063a8b6
fix: fix distance edit route
koko57 Mar 28, 2024
7fe595a
fix: path fix
koko57 Mar 28, 2024
9e68814
fix: minor fix
koko57 Mar 28, 2024
68eecf0
fix: enable fields for split, fix route
koko57 Mar 28, 2024
35baf30
fix: minor fix
koko57 Mar 28, 2024
e9a15d7
fix: change copy on rate selection page
koko57 Mar 28, 2024
e9f448b
fix: minor fix
koko57 Mar 29, 2024
4217a98
fix: remove a comment
koko57 Mar 29, 2024
ae3ded4
Merge branch 'main' into feat/36985-create-new-rate-field
koko57 Mar 29, 2024
f432cb5
fix: resolve conflicts
koko57 Apr 2, 2024
b100cf1
fix: resolve conflicts
koko57 Apr 2, 2024
0826359
Merge branch 'main' into feat/36985-create-new-rate-field
koko57 Apr 4, 2024
c7b8854
fix: display distance and rate properly
koko57 Apr 4, 2024
b16107a
fix: minor fixes
koko57 Apr 4, 2024
3bd9aad
fix: apply requested changes
koko57 Apr 5, 2024
bd83588
fix: revert unnecessary change
koko57 Apr 5, 2024
27f1fc1
Merge branch 'main' into feat/36985-create-new-rate-field
koko57 Apr 8, 2024
7712cec
fix: get rates from withOnyx hoc, rename DefaultMileageRate
koko57 Apr 8, 2024
72d3142
fix: apply requested changes
koko57 Apr 8, 2024
34908eb
Merge branch 'main' into feat/36985-create-new-rate-field
koko57 Apr 9, 2024
fcbdddd
fix: remove unnecessary line
koko57 Apr 9, 2024
7bf1e97
fix: change hardcoded transactionID value for the one from the route …
koko57 Apr 9, 2024
6b04d40
Merge branch 'main' into feat/36985-create-new-rate-field
koko57 Apr 10, 2024
fc2c260
fix: type IOURequestStepRate properly
koko57 Apr 10, 2024
7fb35ab
fix: minor fix
koko57 Apr 10, 2024
c37ef58
fix: resolve conflicts
koko57 Apr 10, 2024
ddf117d
fix: minor fixes
koko57 Apr 10, 2024
118aa7b
fix: add comment
koko57 Apr 10, 2024
a9d6ef7
fix: resolve conflicts
koko57 Apr 10, 2024
ec0bfec
Merge branch 'main' into feat/36985-create-new-rate-field
koko57 Apr 11, 2024
1511b2e
fix: display only rate when no rate name
koko57 Apr 11, 2024
5c03b38
fix: remove use of lastSelectedDistanceRate
koko57 Apr 11, 2024
96c281d
fix: minor fix
koko57 Apr 11, 2024
4658af3
fix: amend comment
koko57 Apr 11, 2024
2d99306
fix: resolve conflicts
koko57 Apr 11, 2024
6234c99
fix: lint
koko57 Apr 11, 2024
96a7032
fix: add missing arg
koko57 Apr 11, 2024
5de2028
fix: minor fix
koko57 Apr 11, 2024
80f9b5e
fix: recalculate amount, fallback to default rate
koko57 Apr 11, 2024
42206ee
fix: extract logic to a new function
koko57 Apr 12, 2024
8f26c89
Merge branch 'main' into feat/36985-create-new-rate-field
koko57 Apr 12, 2024
592bf7a
fix: fix problem with workspace customRateID
koko57 Apr 12, 2024
dfa02a3
fix: minor fix
koko57 Apr 15, 2024
5dc83c5
Merge branch 'main' into feat/36985-create-new-rate-field
koko57 Apr 15, 2024
410fdb1
fix: apply requested changes
koko57 Apr 15, 2024
3c47a2c
fix: restore setting customUnitRateID when data available
koko57 Apr 15, 2024
997ef8d
fix: resolve conflicts
koko57 Apr 15, 2024
949bf4e
fix: fallback to first rate when all custom named
koko57 Apr 16, 2024
64951eb
Merge branch 'main' into feat/36985-create-new-rate-field
koko57 Apr 16, 2024
28948ab
fix: apply requested changes
koko57 Apr 16, 2024
6bcd257
fix: check for quantity instead of route
koko57 Apr 16, 2024
520cf1f
fix: minor fix
koko57 Apr 16, 2024
5b9b4d1
fix: resolve conflicts
koko57 Apr 16, 2024
706ba6f
fix: resolve conflicts
koko57 Apr 16, 2024
fb59ef1
fix: navigation fix
koko57 Apr 16, 2024
2955ee9
fix: translation
koko57 Apr 16, 2024
05fe83a
fix: resolve conflicts
koko57 Apr 17, 2024
f6ef05f
fix: minor fix
koko57 Apr 17, 2024
a73490b
fix: currency and rate for p2p
koko57 Apr 17, 2024
779cbea
fix: resolve conflicts
koko57 Apr 17, 2024
d6d6f96
fix: remove hasRoute condition from getRateForDisplay
koko57 Apr 17, 2024
fc8f59c
fix: resolve conflicts
koko57 Apr 17, 2024
bdfbbdb
fix: prettier
koko57 Apr 18, 2024
911d684
Merge branch 'main' into feat/36985-create-new-rate-field
koko57 Apr 18, 2024
1a142b8
fix: resolve conflicts
koko57 Apr 18, 2024
5229462
fix: add missing export
koko57 Apr 18, 2024
9c8fbdf
fix: minor fix
koko57 Apr 18, 2024
63c5edd
fix: copy temporary confirmation list content to the original file
koko57 Apr 18, 2024
1bb47d0
fix: resolve conflicts
koko57 Apr 18, 2024
8fe0f19
fix: minor fix
koko57 Apr 18, 2024
29da5dc
fix: resolve conflicts
koko57 Apr 19, 2024
6bb3199
fix: resolve conflicts
koko57 Apr 19, 2024
a93e729
fix: sending customUnitRateID
koko57 Apr 19, 2024
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
5 changes: 3 additions & 2 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import * as KeyCommand from 'react-native-key-command';
import type {ValueOf} from 'type-fest';
import * as Url from './libs/Url';
import SCREENS from './SCREENS';
import type {Unit} from './types/onyx/Policy';

type RateAndUnit = {
unit: string;
unit: Unit;
rate: number;
};
type CurrencyDefaultMileageRate = Record<string, RateAndUnit>;
Expand Down Expand Up @@ -4351,6 +4352,6 @@ type Country = keyof typeof CONST.ALL_COUNTRIES;
type IOUType = ValueOf<typeof CONST.IOU.TYPE>;
type IOUAction = ValueOf<typeof CONST.IOU.ACTION>;

export type {Country, IOUAction, IOUType};
export type {Country, IOUAction, IOUType, RateAndUnit};

export default CONST;
5 changes: 5 additions & 0 deletions src/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,11 @@ const ROUTES = {
getRoute: (action: IOUAction, iouType: IOUType, transactionID: string, reportID: string, backTo = '') =>
getUrlWithBackToParam(`${action as string}/${iouType as string}/distance/${transactionID}/${reportID}`, backTo),
},
MONEY_REQUEST_STEP_DISTANCE_RATE: {
route: ':action/:iouType/distanceRate/:transactionID/:reportID',
getRoute: (action: ValueOf<typeof CONST.IOU.ACTION>, iouType: ValueOf<typeof CONST.IOU.TYPE>, transactionID: string, reportID: string, backTo = '') =>
getUrlWithBackToParam(`${action}/${iouType}/distanceRate/${transactionID}/${reportID}`, backTo),
},
MONEY_REQUEST_STEP_MERCHANT: {
route: ':action/:iouType/merchant/:transactionID/:reportID',
getRoute: (action: IOUAction, iouType: IOUType, transactionID: string, reportID: string, backTo = '') =>
Expand Down
1 change: 1 addition & 0 deletions src/SCREENS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ const SCREENS = {
STEP_DATE: 'Money_Request_Step_Date',
STEP_DESCRIPTION: 'Money_Request_Step_Description',
STEP_DISTANCE: 'Money_Request_Step_Distance',
STEP_DISTANCE_RATE: 'Money_Request_Step_Rate',
STEP_MERCHANT: 'Money_Request_Step_Merchant',
STEP_PARTICIPANTS: 'Money_Request_Step_Participants',
STEP_SCAN: 'Money_Request_Step_Scan',
Expand Down
109 changes: 91 additions & 18 deletions src/components/MoneyRequestConfirmationList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import usePermissions from '@hooks/usePermissions';
import usePrevious from '@hooks/usePrevious';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import DistanceRequestUtils from '@libs/DistanceRequestUtils';
import type {DefaultMileageRate} from '@libs/DistanceRequestUtils';
import type {MileageRate} from '@libs/DistanceRequestUtils';
import * as IOUUtils from '@libs/IOUUtils';
import Log from '@libs/Log';
import * as MoneyRequestUtils from '@libs/MoneyRequestUtils';
Expand Down Expand Up @@ -65,7 +66,13 @@ type MoneyRequestConfirmationListOnyxProps = {
session: OnyxEntry<OnyxTypes.Session>;

/** Unit and rate used for if the expense is a distance expense */
mileageRate: OnyxEntry<DefaultMileageRate>;
mileageRates: OnyxEntry<Record<string, MileageRate>>;

/** Mileage rate default for the policy */
defaultMileageRate: OnyxEntry<MileageRate>;

/** Last selected distance rates */
lastSelectedDistanceRates: OnyxEntry<Record<string, string>>;
};

type MoneyRequestConfirmationListProps = MoneyRequestConfirmationListOnyxProps & {
Expand Down Expand Up @@ -180,7 +187,7 @@ function MoneyRequestConfirmationList({
isScanRequest = false,
iouAmount,
policyCategories,
mileageRate,
mileageRates,
isDistanceRequest = false,
policy,
isPolicyExpenseChat = false,
Expand Down Expand Up @@ -208,28 +215,50 @@ function MoneyRequestConfirmationList({
onToggleBillable,
hasSmartScanFailed,
reportActionID,
defaultMileageRate,
lastSelectedDistanceRates,
action = CONST.IOU.ACTION.CREATE,
}: MoneyRequestConfirmationListProps) {
const theme = useTheme();
const styles = useThemeStyles();
const {translate, toLocaleDigit} = useLocalize();
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
const {canUseViolations} = usePermissions();
const {canUseP2PDistanceRequests, canUseViolations} = usePermissions(iouType);
const {isOffline} = useNetwork();

const isTypeRequest = iouType === CONST.IOU.TYPE.REQUEST;
const isTypeSplit = iouType === CONST.IOU.TYPE.SPLIT;
const isTypeSend = iouType === CONST.IOU.TYPE.SEND;
const isTypeTrackExpense = iouType === CONST.IOU.TYPE.TRACK_EXPENSE;

const {unit, rate, currency} = mileageRate ?? {
unit: CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES,
rate: 0,
currency: CONST.CURRENCY.USD,
};
const distance = transaction?.routes?.route0.distance ?? 0;
const shouldCalculateDistanceAmount = isDistanceRequest && iouAmount === 0;
const taxRates = policy?.taxRates;
const transactionID = transaction?.transactionID ?? '';
const customUnitRateID = TransactionUtils.getRateID(transaction) ?? '';

useEffect(() => {
if (customUnitRateID || !canUseP2PDistanceRequests) {
neil-marcellini marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't have returned early for P2P distance requests, instead we should had checked for canUseP2PDistanceRequests while assigning rateID, this caused #45856

return;
}
if (!customUnitRateID) {
const rateID = lastSelectedDistanceRates?.[policy?.id ?? ''] ?? defaultMileageRate?.customUnitRateID ?? '';
IOU.setCustomUnitRateID(transactionID, rateID);
}
}, [defaultMileageRate, customUnitRateID, lastSelectedDistanceRates, policy?.id, canUseP2PDistanceRequests, transactionID]);

const policyCurrency = policy?.outputCurrency ?? PolicyUtils.getPersonalPolicy()?.outputCurrency ?? CONST.CURRENCY.USD;

const mileageRate = TransactionUtils.isCustomUnitRateIDForP2P(transaction)
? DistanceRequestUtils.getRateForP2P(policyCurrency)
: mileageRates?.[customUnitRateID] ?? DistanceRequestUtils.getDefaultMileageRate(policy);

const {unit, rate} = mileageRate ?? {};

const prevRate = usePrevious(rate);
const shouldCalculateDistanceAmount = isDistanceRequest && (iouAmount === 0 || prevRate !== rate);

const currency = (mileageRate as MileageRate)?.currency ?? policyCurrency;

const distance = transaction?.routes?.route0?.distance ?? 0;
const taxRates = policy?.taxRates ?? null;

// A flag for showing the categories field
const shouldShowCategories = isPolicyExpenseChat && (!!iouCategory || OptionsListUtils.hasEnabledOptions(Object.values(policyCategories ?? {})));
Expand All @@ -255,12 +284,12 @@ function MoneyRequestConfirmationList({
// A flag for showing the billable field
const shouldShowBillable = policy?.disabledFields?.defaultBillable === false;
const isMovingTransactionFromTrackExpense = IOUUtils.isMovingTransactionFromTrackExpense(action);
const hasRoute = TransactionUtils.hasRoute(transaction);
const hasRoute = TransactionUtils.hasRoute(transaction, isDistanceRequest);
const isDistanceRequestWithPendingRoute = isDistanceRequest && (!hasRoute || !rate) && !isMovingTransactionFromTrackExpense;
const formattedAmount = isDistanceRequestWithPendingRoute
? ''
: CurrencyUtils.convertToDisplayString(
shouldCalculateDistanceAmount ? DistanceRequestUtils.getDistanceRequestAmount(distance, unit, rate ?? 0) : iouAmount,
shouldCalculateDistanceAmount ? DistanceRequestUtils.getDistanceRequestAmount(distance, unit ?? CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES, rate ?? 0) : iouAmount,
isDistanceRequest ? currency : iouCurrencyCode,
);
const formattedTaxAmount = CurrencyUtils.convertToDisplayString(transaction?.taxAmount, iouCurrencyCode);
Expand Down Expand Up @@ -329,7 +358,7 @@ function MoneyRequestConfirmationList({
return;
}

const amount = DistanceRequestUtils.getDistanceRequestAmount(distance, unit, rate ?? 0);
const amount = DistanceRequestUtils.getDistanceRequestAmount(distance, unit ?? CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES, rate ?? 0);
IOU.setMoneyRequestAmount(transactionID, amount, currency ?? '');
}, [shouldCalculateDistanceAmount, distance, rate, unit, transactionID, currency]);

Expand Down Expand Up @@ -720,13 +749,50 @@ function MoneyRequestConfirmationList({
style={[styles.moneyRequestMenuItem]}
titleStyle={styles.flex1}
onPress={() => Navigation.navigate(ROUTES.MONEY_REQUEST_STEP_DISTANCE.getRoute(action, iouType, transactionID, reportID, Navigation.getActiveRouteWithoutParams()))}
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
neil-marcellini marked this conversation as resolved.
Show resolved Hide resolved
disabled={didConfirm}
// todo: handle edit for transaction while moving from track expense
interactive={!isReadOnly && !isMovingTransactionFromTrackExpense}
/>
),
shouldShow: isDistanceRequest,
isSupplementary: true,
shouldShow: isDistanceRequest && !canUseP2PDistanceRequests,
isSupplementary: false,
},
{
item: (
<MenuItemWithTopDescription
key={translate('common.distance')}
shouldShowRightIcon={!isReadOnly}
title={DistanceRequestUtils.getDistanceForDisplay(hasRoute, distance, unit, rate, translate)}
description={translate('common.distance')}
style={[styles.moneyRequestMenuItem]}
titleStyle={styles.flex1}
onPress={() =>
Navigation.navigate(ROUTES.MONEY_REQUEST_STEP_DISTANCE.getRoute(CONST.IOU.ACTION.CREATE, iouType, transactionID, reportID, Navigation.getActiveRouteWithoutParams()))
neil-marcellini marked this conversation as resolved.
Show resolved Hide resolved
}
disabled={didConfirm}
interactive={!isReadOnly}
/>
),
shouldShow: isDistanceRequest && canUseP2PDistanceRequests,
isSupplementary: false,
},
{
item: (
<MenuItemWithTopDescription
key={translate('common.rate')}
shouldShowRightIcon={Boolean(rate) && !isReadOnly && isPolicyExpenseChat}
title={DistanceRequestUtils.getRateForDisplay(unit, rate, currency, translate, toLocaleDigit, isOffline)}
description={translate('common.rate')}
style={[styles.moneyRequestMenuItem]}
titleStyle={styles.flex1}
onPress={() => Navigation.navigate(ROUTES.MONEY_REQUEST_STEP_DISTANCE_RATE.getRoute(action, iouType, transactionID, reportID, Navigation.getActiveRouteWithoutParams()))}
disabled={didConfirm}
interactive={Boolean(rate) && !isReadOnly && isPolicyExpenseChat}
/>
),
shouldShow: isDistanceRequest && canUseP2PDistanceRequests,
isSupplementary: false,
},
{
item: (
Expand Down Expand Up @@ -986,11 +1052,18 @@ export default withOnyx<MoneyRequestConfirmationListProps, MoneyRequestConfirmat
policyTags: {
key: ({policyID}) => `${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`,
},
mileageRate: {
defaultMileageRate: {
key: ({policyID}) => `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
selector: DistanceRequestUtils.getDefaultMileageRate,
},
mileageRates: {
key: ({policyID}) => `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
selector: DistanceRequestUtils.getMileageRates,
},
policy: {
key: ({policyID}) => `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
},
lastSelectedDistanceRates: {
key: ONYXKEYS.NVP_LAST_SELECTED_DISTANCE_RATES,
},
})(MoneyRequestConfirmationList);
Loading
Loading