From d70e9d6b611167227f2f9a30cf41d4e18d91ec0e Mon Sep 17 00:00:00 2001 From: Monil Bhavsar Date: Tue, 4 Jun 2024 23:48:59 +0530 Subject: [PATCH 1/7] Get taxname for distance request --- src/components/MoneyRequestConfirmationList.tsx | 2 +- .../ReportActionItem/MoneyRequestView.tsx | 2 +- src/libs/DistanceRequestUtils.ts | 15 +++++++++++++++ src/libs/TransactionUtils.ts | 17 +++++++++++++---- .../request/step/IOURequestStepTaxRatePage.tsx | 2 +- 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/components/MoneyRequestConfirmationList.tsx b/src/components/MoneyRequestConfirmationList.tsx index 035e434cf41c..e51361932235 100755 --- a/src/components/MoneyRequestConfirmationList.tsx +++ b/src/components/MoneyRequestConfirmationList.tsx @@ -316,7 +316,7 @@ function MoneyRequestConfirmationList({ isDistanceRequest ? currency : iouCurrencyCode, ); const formattedTaxAmount = CurrencyUtils.convertToDisplayString(transaction?.taxAmount, iouCurrencyCode); - const taxRateTitle = TransactionUtils.getTaxName(policy, transaction); + const taxRateTitle = TransactionUtils.getTaxName(policy, transaction, isDistanceRequest); const previousTransactionAmount = usePrevious(transaction?.amount); const previousTransactionCurrency = usePrevious(transaction?.currency); diff --git a/src/components/ReportActionItem/MoneyRequestView.tsx b/src/components/ReportActionItem/MoneyRequestView.tsx index 3d0964a0d57a..ee8ded5c2e7a 100644 --- a/src/components/ReportActionItem/MoneyRequestView.tsx +++ b/src/components/ReportActionItem/MoneyRequestView.tsx @@ -132,7 +132,7 @@ function MoneyRequestView({ const formattedTaxAmount = CurrencyUtils.convertToDisplayString(transactionTaxAmount, transactionCurrency); const taxRatesDescription = taxRates?.name; - const taxRateTitle = TransactionUtils.getTaxName(policy, transaction); + const taxRateTitle = TransactionUtils.getTaxName(policy, transaction, isDistanceRequest); // Flags for allowing or disallowing editing an expense const isSettled = ReportUtils.isSettled(moneyRequestReport?.reportID); diff --git a/src/libs/DistanceRequestUtils.ts b/src/libs/DistanceRequestUtils.ts index 230ad8deb130..3b502d2732f3 100644 --- a/src/libs/DistanceRequestUtils.ts +++ b/src/libs/DistanceRequestUtils.ts @@ -12,6 +12,7 @@ import * as CurrencyUtils from './CurrencyUtils'; import * as PolicyUtils from './PolicyUtils'; import * as ReportUtils from './ReportUtils'; import * as TransactionUtils from './TransactionUtils'; +import {getRateID} from "./TransactionUtils"; type MileageRate = { customUnitRateID?: string; @@ -267,6 +268,19 @@ function getCustomUnitRateID(reportID: string) { return customUnitRateID; } +/** + * Returns the taxCode associated with distance custom unit rate + */ +function getTaxCodeFromRateID(policy: OnyxEntry, customUnitRateID: string) { + const distanceUnit = PolicyUtils.getCustomUnit(policy); + const customUnitID = distanceUnit?.customUnitID; + if (!policy?.customUnits || !customUnitID || !customUnitRateID) { + return ''; + } + const policyCustomUnitRate = policy?.customUnits[customUnitID].rates[customUnitRateID]; + return policyCustomUnitRate.attributes?.taxRateExternalID ?? ''; +} + function calculateTaxAmount(policy: OnyxEntry, transaction: OnyxEntry, customUnitRateID: string) { const distanceUnit = PolicyUtils.getCustomUnit(policy); const customUnitID = distanceUnit?.customUnitID; @@ -296,6 +310,7 @@ export default { getCustomUnitRateID, convertToDistanceInMeters, calculateTaxAmount, + getTaxCodeFromRateID, }; export type {MileageRate}; diff --git a/src/libs/TransactionUtils.ts b/src/libs/TransactionUtils.ts index 7b328458c0ac..9fc84bab24d6 100644 --- a/src/libs/TransactionUtils.ts +++ b/src/libs/TransactionUtils.ts @@ -13,6 +13,8 @@ import DateUtils from './DateUtils'; import * as Localize from './Localize'; import * as NumberUtils from './NumberUtils'; import {getCleanedTagName} from './PolicyUtils'; +import * as PolicyUtils from "@libs/PolicyUtils"; +import DistanceRequestUtils from "@libs/DistanceRequestUtils"; let allTransactions: OnyxCollection = {}; Onyx.connect({ @@ -739,11 +741,18 @@ function getWorkspaceTaxesSettingsName(policy: OnyxEntry, taxCode: strin } /** - * Gets the tax name + * Gets the tax name for the given transaction. + * If it is distance request, then returns the tax name corresponding to the custom unit rate */ -function getTaxName(policy: OnyxEntry, transaction: OnyxEntry) { - const defaultTaxCode = getDefaultTaxCode(policy, transaction); - return Object.values(transformedTaxRates(policy, transaction)).find((taxRate) => taxRate.code === (transaction?.taxCode ?? defaultTaxCode))?.modifiedName; +function getTaxName(policy: OnyxEntry, transaction: OnyxEntry, isDistanceRequest: boolean) { + let taxCode: string; + if (isDistanceRequest) { + const customUnitRateID = getRateID(transaction) ?? ''; + taxCode = DistanceRequestUtils.getTaxCodeFromRateID(policy, customUnitRateID); + } else { + taxCode = getDefaultTaxCode(policy, transaction) ?? ''; + } + return Object.values(transformedTaxRates(policy, transaction)).find((taxRate) => taxRate.code === (transaction?.taxCode ?? taxCode))?.modifiedName; } export { diff --git a/src/pages/iou/request/step/IOURequestStepTaxRatePage.tsx b/src/pages/iou/request/step/IOURequestStepTaxRatePage.tsx index 9376aa65e4e6..829354fea2ec 100644 --- a/src/pages/iou/request/step/IOURequestStepTaxRatePage.tsx +++ b/src/pages/iou/request/step/IOURequestStepTaxRatePage.tsx @@ -57,7 +57,7 @@ function IOURequestStepTaxRatePage({ Navigation.goBack(backTo); }; - const taxRateTitle = TransactionUtils.getTaxName(policy, transaction); + const taxRateTitle = TransactionUtils.getTaxName(policy, transaction, false); const updateTaxRates = (taxes: TaxRatesOption) => { if (!transaction || !taxes.code || !taxRates) { From c74e185c05e498741fad0480f2e40be8c2e04b4b Mon Sep 17 00:00:00 2001 From: Monil Bhavsar Date: Tue, 4 Jun 2024 23:49:34 +0530 Subject: [PATCH 2/7] Pass taxCode associated with distance rate --- .../iou/request/step/IOURequestStepConfirmation.tsx | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx index b4eb9f1082ed..39959a6af49b 100644 --- a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx +++ b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx @@ -34,6 +34,7 @@ import type {WithFullTransactionOrNotFoundProps} from './withFullTransactionOrNo import withFullTransactionOrNotFound from './withFullTransactionOrNotFound'; import withWritableReportOrNotFound from './withWritableReportOrNotFound'; import type {WithWritableReportOrNotFoundProps} from './withWritableReportOrNotFound'; +import DistanceRequestUtils from "@libs/DistanceRequestUtils"; type IOURequestStepConfirmationOnyxProps = { /** The policy of the report */ @@ -81,11 +82,14 @@ function IOURequestStepConfirmation({ const {windowWidth} = useWindowDimensions(); const {isOffline} = useNetwork(); const [receiptFile, setReceiptFile] = useState(); + const requestType = TransactionUtils.getRequestType(transaction); + const isDistanceRequest = requestType === CONST.IOU.REQUEST_TYPE.DISTANCE; const receiptFilename = transaction?.filename; const receiptPath = transaction?.receipt?.source; const receiptType = transaction?.receipt?.type; - const defaultTaxCode = TransactionUtils.getDefaultTaxCode(policy, transaction); + const customUnitRateID = TransactionUtils.getRateID(transaction) ?? ''; + const defaultTaxCode = isDistanceRequest ? DistanceRequestUtils.getTaxCodeFromRateID(policy, customUnitRateID) : TransactionUtils.getDefaultTaxCode(policy, transaction); const transactionTaxCode = (transaction?.taxCode ? transaction?.taxCode : defaultTaxCode) ?? ''; const transactionTaxAmount = transaction?.taxAmount ?? 0; const isSharingTrackExpense = action === CONST.IOU.ACTION.SHARE; @@ -107,8 +111,6 @@ function IOURequestStepConfirmation({ }; }, [personalDetails, transaction?.participants, transaction?.splitPayerAccountIDs]); - const requestType = TransactionUtils.getRequestType(transaction); - const headerTitle = useMemo(() => { if (isCategorizingTrackExpense) { return translate('iou.categorize'); @@ -468,8 +470,7 @@ function IOURequestStepConfirmation({ return; } - if (requestType === CONST.IOU.REQUEST_TYPE.DISTANCE && !IOUUtils.isMovingTransactionFromTrackExpense(action)) { - const customUnitRateID = TransactionUtils.getRateID(transaction) ?? ''; + if (isDistanceRequest && !IOUUtils.isMovingTransactionFromTrackExpense(action)) { createDistanceRequest(selectedParticipants, trimmedComment, customUnitRateID); return; } @@ -568,7 +569,7 @@ function IOURequestStepConfirmation({ bankAccountRoute={ReportUtils.getBankAccountRoute(report)} iouMerchant={transaction?.merchant} iouCreated={transaction?.created} - isDistanceRequest={requestType === CONST.IOU.REQUEST_TYPE.DISTANCE} + isDistanceRequest={isDistanceRequest} shouldShowSmartScanFields={IOUUtils.isMovingTransactionFromTrackExpense(action) ? transaction?.amount !== 0 : requestType !== CONST.IOU.REQUEST_TYPE.SCAN} action={action} payeePersonalDetails={payeePersonalDetails} From fa44cf5598c5ae8dbd7d0dfb28d65120a72a9717 Mon Sep 17 00:00:00 2001 From: Monil Bhavsar Date: Wed, 5 Jun 2024 15:04:25 +0530 Subject: [PATCH 3/7] Fix lint --- src/components/MoneyRequestConfirmationList.tsx | 2 +- src/components/ReportActionItem/MoneyRequestView.tsx | 2 +- src/libs/DistanceRequestUtils.ts | 1 - src/libs/TransactionUtils.ts | 7 +++---- .../iou/request/step/IOURequestStepConfirmation.tsx | 11 ++++++----- .../iou/request/step/IOURequestStepTaxRatePage.tsx | 2 +- 6 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/components/MoneyRequestConfirmationList.tsx b/src/components/MoneyRequestConfirmationList.tsx index e51361932235..035e434cf41c 100755 --- a/src/components/MoneyRequestConfirmationList.tsx +++ b/src/components/MoneyRequestConfirmationList.tsx @@ -316,7 +316,7 @@ function MoneyRequestConfirmationList({ isDistanceRequest ? currency : iouCurrencyCode, ); const formattedTaxAmount = CurrencyUtils.convertToDisplayString(transaction?.taxAmount, iouCurrencyCode); - const taxRateTitle = TransactionUtils.getTaxName(policy, transaction, isDistanceRequest); + const taxRateTitle = TransactionUtils.getTaxName(policy, transaction); const previousTransactionAmount = usePrevious(transaction?.amount); const previousTransactionCurrency = usePrevious(transaction?.currency); diff --git a/src/components/ReportActionItem/MoneyRequestView.tsx b/src/components/ReportActionItem/MoneyRequestView.tsx index ee8ded5c2e7a..3d0964a0d57a 100644 --- a/src/components/ReportActionItem/MoneyRequestView.tsx +++ b/src/components/ReportActionItem/MoneyRequestView.tsx @@ -132,7 +132,7 @@ function MoneyRequestView({ const formattedTaxAmount = CurrencyUtils.convertToDisplayString(transactionTaxAmount, transactionCurrency); const taxRatesDescription = taxRates?.name; - const taxRateTitle = TransactionUtils.getTaxName(policy, transaction, isDistanceRequest); + const taxRateTitle = TransactionUtils.getTaxName(policy, transaction); // Flags for allowing or disallowing editing an expense const isSettled = ReportUtils.isSettled(moneyRequestReport?.reportID); diff --git a/src/libs/DistanceRequestUtils.ts b/src/libs/DistanceRequestUtils.ts index 3b502d2732f3..369469df2d4e 100644 --- a/src/libs/DistanceRequestUtils.ts +++ b/src/libs/DistanceRequestUtils.ts @@ -12,7 +12,6 @@ import * as CurrencyUtils from './CurrencyUtils'; import * as PolicyUtils from './PolicyUtils'; import * as ReportUtils from './ReportUtils'; import * as TransactionUtils from './TransactionUtils'; -import {getRateID} from "./TransactionUtils"; type MileageRate = { customUnitRateID?: string; diff --git a/src/libs/TransactionUtils.ts b/src/libs/TransactionUtils.ts index 9fc84bab24d6..91e014357673 100644 --- a/src/libs/TransactionUtils.ts +++ b/src/libs/TransactionUtils.ts @@ -7,14 +7,13 @@ import ONYXKEYS from '@src/ONYXKEYS'; import type {Policy, RecentWaypoint, Report, TaxRate, TaxRates, Transaction, TransactionViolation, TransactionViolations} from '@src/types/onyx'; import type {Comment, Receipt, TransactionChanges, TransactionPendingFieldsKey, Waypoint, WaypointCollection} from '@src/types/onyx/Transaction'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; +import * as DistanceRequestUtils from './DistanceRequestUtils'; import type {IOURequestType} from './actions/IOU'; import {isCorporateCard, isExpensifyCard} from './CardUtils'; import DateUtils from './DateUtils'; import * as Localize from './Localize'; import * as NumberUtils from './NumberUtils'; import {getCleanedTagName} from './PolicyUtils'; -import * as PolicyUtils from "@libs/PolicyUtils"; -import DistanceRequestUtils from "@libs/DistanceRequestUtils"; let allTransactions: OnyxCollection = {}; Onyx.connect({ @@ -744,9 +743,9 @@ function getWorkspaceTaxesSettingsName(policy: OnyxEntry, taxCode: strin * Gets the tax name for the given transaction. * If it is distance request, then returns the tax name corresponding to the custom unit rate */ -function getTaxName(policy: OnyxEntry, transaction: OnyxEntry, isDistanceRequest: boolean) { +function getTaxName(policy: OnyxEntry, transaction: OnyxEntry) { let taxCode: string; - if (isDistanceRequest) { + if (isDistanceRequest(transaction)) { const customUnitRateID = getRateID(transaction) ?? ''; taxCode = DistanceRequestUtils.getTaxCodeFromRateID(policy, customUnitRateID); } else { diff --git a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx index 39959a6af49b..cfac4e3e52aa 100644 --- a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx +++ b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx @@ -30,11 +30,11 @@ import type {Policy, PolicyCategories, PolicyTagList} from '@src/types/onyx'; import type {Participant} from '@src/types/onyx/IOU'; import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage'; import type {Receipt} from '@src/types/onyx/Transaction'; +import DistanceRequestUtils from "@libs/DistanceRequestUtils"; import type {WithFullTransactionOrNotFoundProps} from './withFullTransactionOrNotFound'; import withFullTransactionOrNotFound from './withFullTransactionOrNotFound'; import withWritableReportOrNotFound from './withWritableReportOrNotFound'; import type {WithWritableReportOrNotFoundProps} from './withWritableReportOrNotFound'; -import DistanceRequestUtils from "@libs/DistanceRequestUtils"; type IOURequestStepConfirmationOnyxProps = { /** The policy of the report */ @@ -89,6 +89,7 @@ function IOURequestStepConfirmation({ const receiptPath = transaction?.receipt?.source; const receiptType = transaction?.receipt?.type; const customUnitRateID = TransactionUtils.getRateID(transaction) ?? ''; + console.debug(`~~Monil logs customUnitRateID ${customUnitRateID}`); const defaultTaxCode = isDistanceRequest ? DistanceRequestUtils.getTaxCodeFromRateID(policy, customUnitRateID) : TransactionUtils.getDefaultTaxCode(policy, transaction); const transactionTaxCode = (transaction?.taxCode ? transaction?.taxCode : defaultTaxCode) ?? ''; const transactionTaxAmount = transaction?.taxAmount ?? 0; @@ -291,7 +292,7 @@ function IOURequestStepConfirmation({ ); const createDistanceRequest = useCallback( - (selectedParticipants: Participant[], trimmedComment: string, customUnitRateID: string) => { + (selectedParticipants: Participant[], trimmedComment: string) => { if (!transaction) { return; } @@ -315,7 +316,7 @@ function IOURequestStepConfirmation({ customUnitRateID, ); }, - [policy, policyCategories, policyTags, report, transaction, transactionTaxCode, transactionTaxAmount], + [policy, policyCategories, policyTags, report, transaction, transactionTaxCode, transactionTaxAmount, customUnitRateID], ); const createTransaction = useCallback( @@ -471,7 +472,7 @@ function IOURequestStepConfirmation({ } if (isDistanceRequest && !IOUUtils.isMovingTransactionFromTrackExpense(action)) { - createDistanceRequest(selectedParticipants, trimmedComment, customUnitRateID); + createDistanceRequest(selectedParticipants, trimmedComment); return; } @@ -482,7 +483,7 @@ function IOURequestStepConfirmation({ report, iouType, receiptFile, - requestType, + isDistanceRequest, requestMoney, currentUserPersonalDetails.login, currentUserPersonalDetails.accountID, diff --git a/src/pages/iou/request/step/IOURequestStepTaxRatePage.tsx b/src/pages/iou/request/step/IOURequestStepTaxRatePage.tsx index 829354fea2ec..9376aa65e4e6 100644 --- a/src/pages/iou/request/step/IOURequestStepTaxRatePage.tsx +++ b/src/pages/iou/request/step/IOURequestStepTaxRatePage.tsx @@ -57,7 +57,7 @@ function IOURequestStepTaxRatePage({ Navigation.goBack(backTo); }; - const taxRateTitle = TransactionUtils.getTaxName(policy, transaction, false); + const taxRateTitle = TransactionUtils.getTaxName(policy, transaction); const updateTaxRates = (taxes: TaxRatesOption) => { if (!transaction || !taxes.code || !taxRates) { From d49857fe7a1a702d89af937116e5f8a4fdd7d580 Mon Sep 17 00:00:00 2001 From: Monil Bhavsar Date: Wed, 5 Jun 2024 19:52:42 +0530 Subject: [PATCH 4/7] Refactor code and prevent cyclic dependency lint error --- .../MoneyRequestConfirmationList.tsx | 17 ++++++--- src/libs/DistanceRequestUtils.ts | 37 +++++-------------- src/libs/PolicyUtils.ts | 9 +++++ src/libs/TransactionUtils.ts | 18 ++++----- .../step/IOURequestStepConfirmation.tsx | 4 +- .../step/IOURequestStepDistanceRate.tsx | 12 +++--- 6 files changed, 46 insertions(+), 51 deletions(-) diff --git a/src/components/MoneyRequestConfirmationList.tsx b/src/components/MoneyRequestConfirmationList.tsx index 035e434cf41c..98701c5cd361 100755 --- a/src/components/MoneyRequestConfirmationList.tsx +++ b/src/components/MoneyRequestConfirmationList.tsx @@ -25,7 +25,7 @@ import * as MoneyRequestUtils from '@libs/MoneyRequestUtils'; import Navigation from '@libs/Navigation/Navigation'; import * as OptionsListUtils from '@libs/OptionsListUtils'; import * as PolicyUtils from '@libs/PolicyUtils'; -import {isTaxTrackingEnabled} from '@libs/PolicyUtils'; +import {getCustomUnitRate, isTaxTrackingEnabled} from '@libs/PolicyUtils'; import * as ReceiptUtils from '@libs/ReceiptUtils'; import * as ReportUtils from '@libs/ReportUtils'; import {getDefaultWorkspaceAvatar} from '@libs/ReportUtils'; @@ -180,12 +180,19 @@ type MoneyRequestConfirmationListProps = MoneyRequestConfirmationListOnyxProps & type MoneyRequestConfirmationListItem = Participant | ReportUtils.OptionData; const getTaxAmount = (transaction: OnyxEntry, policy: OnyxEntry, isDistanceRequest: boolean) => { + let taxableAmount: number; + let taxCode: string; if (isDistanceRequest) { - return DistanceRequestUtils.calculateTaxAmount(policy, transaction, TransactionUtils.getRateID(transaction) ?? ''); + const customUnitRateID = TransactionUtils.getRateID(transaction) ?? ''; + const customUnitRate = getCustomUnitRate(policy, customUnitRateID); + taxCode = customUnitRate?.attributes?.taxRateExternalID ?? ''; + taxableAmount = DistanceRequestUtils.getTaxableAmount(policy, customUnitRateID, TransactionUtils.getDistance(transaction)); + } else { + taxableAmount = transaction?.amount ?? 0; + taxCode = transaction?.taxCode ?? TransactionUtils.getDefaultTaxCode(policy, transaction) ?? ''; } - const defaultTaxCode = TransactionUtils.getDefaultTaxCode(policy, transaction) ?? ''; - const taxPercentage = TransactionUtils.getTaxValue(policy, transaction, transaction?.taxCode ?? defaultTaxCode) ?? ''; - return TransactionUtils.calculateTaxAmount(taxPercentage, transaction?.amount ?? 0); + const taxPercentage = TransactionUtils.getTaxValue(policy, transaction, taxCode) ?? ''; + return TransactionUtils.calculateTaxAmount(taxPercentage, taxableAmount); }; function MoneyRequestConfirmationList({ diff --git a/src/libs/DistanceRequestUtils.ts b/src/libs/DistanceRequestUtils.ts index 369469df2d4e..0ebcc4869974 100644 --- a/src/libs/DistanceRequestUtils.ts +++ b/src/libs/DistanceRequestUtils.ts @@ -4,14 +4,13 @@ import type {LocaleContextProps} from '@components/LocaleContextProvider'; import type {RateAndUnit} from '@src/CONST'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {LastSelectedDistanceRates, Report, Transaction} from '@src/types/onyx'; +import type {LastSelectedDistanceRates, Report} from '@src/types/onyx'; import type {Unit} from '@src/types/onyx/Policy'; import type Policy from '@src/types/onyx/Policy'; import type {EmptyObject} from '@src/types/utils/EmptyObject'; import * as CurrencyUtils from './CurrencyUtils'; import * as PolicyUtils from './PolicyUtils'; import * as ReportUtils from './ReportUtils'; -import * as TransactionUtils from './TransactionUtils'; type MileageRate = { customUnitRateID?: string; @@ -268,34 +267,19 @@ function getCustomUnitRateID(reportID: string) { } /** - * Returns the taxCode associated with distance custom unit rate + * Get taxable amount from a specific distance rate, taking into consideration the tax claimable amount configured for the distance rate */ -function getTaxCodeFromRateID(policy: OnyxEntry, customUnitRateID: string) { +function getTaxableAmount(policy: OnyxEntry, customUnitRateID: string, distance: number) { const distanceUnit = PolicyUtils.getCustomUnit(policy); - const customUnitID = distanceUnit?.customUnitID; - if (!policy?.customUnits || !customUnitID || !customUnitRateID) { - return ''; - } - const policyCustomUnitRate = policy?.customUnits[customUnitID].rates[customUnitRateID]; - return policyCustomUnitRate.attributes?.taxRateExternalID ?? ''; -} - -function calculateTaxAmount(policy: OnyxEntry, transaction: OnyxEntry, customUnitRateID: string) { - const distanceUnit = PolicyUtils.getCustomUnit(policy); - const customUnitID = distanceUnit?.customUnitID; - if (!policy?.customUnits || !customUnitID) { + const customUnitRate = PolicyUtils.getCustomUnitRate(policy, customUnitRateID); + if (!distanceUnit || !distanceUnit?.customUnitID || !customUnitRate) { return 0; } - const policyCustomUnitRate = policy?.customUnits[customUnitID].rates[customUnitRateID]; - const unit = policy?.customUnits[customUnitID]?.attributes?.unit ?? CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES; - const rate = policyCustomUnitRate?.rate ?? 0; - const distance = TransactionUtils.getDistance(transaction); + const unit = distanceUnit?.attributes?.unit ?? CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES; + const rate = customUnitRate?.rate ?? 0; const amount = getDistanceRequestAmount(distance, unit, rate); - const taxClaimablePercentage = policyCustomUnitRate.attributes?.taxClaimablePercentage ?? 0; - const taxRateExternalID = policyCustomUnitRate.attributes?.taxRateExternalID ?? ''; - const taxableAmount = amount * taxClaimablePercentage; - const taxPercentage = TransactionUtils.getTaxValue(policy, transaction, taxRateExternalID) ?? ''; - return TransactionUtils.calculateTaxAmount(taxPercentage, taxableAmount); + const taxClaimablePercentage = customUnitRate.attributes?.taxClaimablePercentage ?? 0; + return amount * taxClaimablePercentage; } export default { @@ -308,8 +292,7 @@ export default { getRateForP2P, getCustomUnitRateID, convertToDistanceInMeters, - calculateTaxAmount, - getTaxCodeFromRateID, + getTaxableAmount, }; export type {MileageRate}; diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index 553e775ba99f..c8da38151ffc 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -97,6 +97,14 @@ function getCustomUnit(policy: OnyxEntry | EmptyObject) { return Object.values(policy?.customUnits ?? {}).find((unit) => unit.name === CONST.CUSTOM_UNITS.NAME_DISTANCE); } +/** + * Retrieves custom unit rate object from the given customUnitRateID + */ +function getCustomUnitRate(policy: OnyxEntry | EmptyObject, customUnitRateID: string): Rate | EmptyObject { + const distanceUnit = getCustomUnit(policy); + return distanceUnit?.rates[customUnitRateID] ?? {}; +} + function getRateDisplayValue(value: number, toLocaleDigit: (arg: string) => string): string { const numValue = getNumericValue(value, toLocaleDigit); if (Number.isNaN(numValue)) { @@ -513,6 +521,7 @@ export { getCurrentXeroOrganizationName, getXeroBankAccountsWithDefaultSelect, getCustomUnit, + getCustomUnitRate, sortWorkspacesBySelected, }; diff --git a/src/libs/TransactionUtils.ts b/src/libs/TransactionUtils.ts index 91e014357673..219e66d82773 100644 --- a/src/libs/TransactionUtils.ts +++ b/src/libs/TransactionUtils.ts @@ -7,13 +7,12 @@ import ONYXKEYS from '@src/ONYXKEYS'; import type {Policy, RecentWaypoint, Report, TaxRate, TaxRates, Transaction, TransactionViolation, TransactionViolations} from '@src/types/onyx'; import type {Comment, Receipt, TransactionChanges, TransactionPendingFieldsKey, Waypoint, WaypointCollection} from '@src/types/onyx/Transaction'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; -import * as DistanceRequestUtils from './DistanceRequestUtils'; import type {IOURequestType} from './actions/IOU'; import {isCorporateCard, isExpensifyCard} from './CardUtils'; import DateUtils from './DateUtils'; import * as Localize from './Localize'; import * as NumberUtils from './NumberUtils'; -import {getCleanedTagName} from './PolicyUtils'; +import {getCleanedTagName, getCustomUnitRate} from './PolicyUtils'; let allTransactions: OnyxCollection = {}; Onyx.connect({ @@ -663,7 +662,7 @@ function hasNoticeTypeViolation(transactionID: string, transactionViolations: On } /** - * this is the formulae to calculate tax + * Calculates tax amount from the given expense amount and tax percentage */ function calculateTaxAmount(percentage: string, amount: number) { const divisor = Number(percentage.slice(0, -1)) / 100 + 1; @@ -696,6 +695,11 @@ function getRateID(transaction: OnyxEntry): string | undefined { * Returns policy default tax rate if transaction is in policy default currency, otherwise returns foreign default tax rate */ function getDefaultTaxCode(policy: OnyxEntry, transaction: OnyxEntry, currency?: string | undefined) { + if (isDistanceRequest(transaction)) { + const customUnitRateID = getRateID(transaction) ?? ''; + const customUnitRate = getCustomUnitRate(policy, customUnitRateID); + return customUnitRate?.attributes?.taxRateExternalID ?? ''; + } const defaultExternalID = policy?.taxRates?.defaultExternalID; const foreignTaxDefault = policy?.taxRates?.foreignTaxDefault; return policy?.outputCurrency === (currency ?? getCurrency(transaction)) ? defaultExternalID : foreignTaxDefault; @@ -744,13 +748,7 @@ function getWorkspaceTaxesSettingsName(policy: OnyxEntry, taxCode: strin * If it is distance request, then returns the tax name corresponding to the custom unit rate */ function getTaxName(policy: OnyxEntry, transaction: OnyxEntry) { - let taxCode: string; - if (isDistanceRequest(transaction)) { - const customUnitRateID = getRateID(transaction) ?? ''; - taxCode = DistanceRequestUtils.getTaxCodeFromRateID(policy, customUnitRateID); - } else { - taxCode = getDefaultTaxCode(policy, transaction) ?? ''; - } + const taxCode = getDefaultTaxCode(policy, transaction) ?? ''; return Object.values(transformedTaxRates(policy, transaction)).find((taxRate) => taxRate.code === (transaction?.taxCode ?? taxCode))?.modifiedName; } diff --git a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx index cfac4e3e52aa..858c509baa7c 100644 --- a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx +++ b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx @@ -30,7 +30,6 @@ import type {Policy, PolicyCategories, PolicyTagList} from '@src/types/onyx'; import type {Participant} from '@src/types/onyx/IOU'; import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage'; import type {Receipt} from '@src/types/onyx/Transaction'; -import DistanceRequestUtils from "@libs/DistanceRequestUtils"; import type {WithFullTransactionOrNotFoundProps} from './withFullTransactionOrNotFound'; import withFullTransactionOrNotFound from './withFullTransactionOrNotFound'; import withWritableReportOrNotFound from './withWritableReportOrNotFound'; @@ -89,8 +88,7 @@ function IOURequestStepConfirmation({ const receiptPath = transaction?.receipt?.source; const receiptType = transaction?.receipt?.type; const customUnitRateID = TransactionUtils.getRateID(transaction) ?? ''; - console.debug(`~~Monil logs customUnitRateID ${customUnitRateID}`); - const defaultTaxCode = isDistanceRequest ? DistanceRequestUtils.getTaxCodeFromRateID(policy, customUnitRateID) : TransactionUtils.getDefaultTaxCode(policy, transaction); + const defaultTaxCode = TransactionUtils.getDefaultTaxCode(policy, transaction); const transactionTaxCode = (transaction?.taxCode ? transaction?.taxCode : defaultTaxCode) ?? ''; const transactionTaxAmount = transaction?.taxAmount ?? 0; const isSharingTrackExpense = action === CONST.IOU.ACTION.SHARE; diff --git a/src/pages/iou/request/step/IOURequestStepDistanceRate.tsx b/src/pages/iou/request/step/IOURequestStepDistanceRate.tsx index 762db0ab811c..679ffb0c8eb9 100644 --- a/src/pages/iou/request/step/IOURequestStepDistanceRate.tsx +++ b/src/pages/iou/request/step/IOURequestStepDistanceRate.tsx @@ -11,7 +11,7 @@ import * as CurrencyUtils from '@libs/CurrencyUtils'; import type {MileageRate} from '@libs/DistanceRequestUtils'; import DistanceRequestUtils from '@libs/DistanceRequestUtils'; import Navigation from '@libs/Navigation/Navigation'; -import {getCustomUnit, isTaxTrackingEnabled} from '@libs/PolicyUtils'; +import {getCustomUnitRate, isTaxTrackingEnabled} from '@libs/PolicyUtils'; import * as ReportUtils from '@libs/ReportUtils'; import * as TransactionUtils from '@libs/TransactionUtils'; import CONST from '@src/CONST'; @@ -50,8 +50,6 @@ function IOURequestStepDistanceRate({ const styles = useThemeStyles(); const {translate, toLocaleDigit} = useLocalize(); const isDistanceRequest = TransactionUtils.isDistanceRequest(transaction); - const distanceUnit = getCustomUnit(policy); - const customUnitID = distanceUnit?.customUnitID; const isPolicyExpenseChat = ReportUtils.isReportInGroupPolicy(report); const shouldShowTax = isTaxTrackingEnabled(isPolicyExpenseChat, policy, isDistanceRequest); @@ -78,10 +76,12 @@ function IOURequestStepDistanceRate({ const initiallyFocusedOption = sections.find((item) => item.isSelected)?.keyForList; function selectDistanceRate(customUnitRateID: string) { - if (policy?.customUnits && customUnitID && shouldShowTax) { - const policyCustomUnitRate = policy?.customUnits[customUnitID].rates[customUnitRateID]; + if (shouldShowTax) { + const policyCustomUnitRate = getCustomUnitRate(policy, customUnitRateID); const taxRateExternalID = policyCustomUnitRate.attributes?.taxRateExternalID ?? ''; - const taxAmount = CurrencyUtils.convertToBackendAmount(DistanceRequestUtils.calculateTaxAmount(policy, transaction, customUnitRateID)); + const taxableAmount = DistanceRequestUtils.getTaxableAmount(policy, customUnitRateID, TransactionUtils.getDistance(transaction)); + const taxPercentage = TransactionUtils.getTaxValue(policy, transaction, taxRateExternalID) ?? ''; + const taxAmount = CurrencyUtils.convertToBackendAmount(TransactionUtils.calculateTaxAmount(taxPercentage, taxableAmount)); IOU.setMoneyRequestTaxAmount(transactionID, taxAmount, true); IOU.setMoneyRequestTaxRate(transactionID, taxRateExternalID); } From 7f1f408f401aeb60bcd39d94439191adefd05c73 Mon Sep 17 00:00:00 2001 From: Monil Bhavsar Date: Wed, 5 Jun 2024 19:56:29 +0530 Subject: [PATCH 5/7] Fix style --- src/pages/iou/request/step/IOURequestStepConfirmation.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx index 402e7c0e0e96..6f4b06f5b818 100644 --- a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx +++ b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx @@ -572,7 +572,7 @@ function IOURequestStepConfirmation({ iouMerchant={transaction?.merchant} iouCreated={transaction?.created} isDistanceRequest={isDistanceRequest} - shouldShowSmartScanFields={isMovingTransactionFromTrackExpense ? transaction?.amount !== 0 : requestType !== CONST.IOU.REQUEST_TYPE.SCAN} + shouldShowSmartScanFields={isMovingTransactionFromTrackExpense ? transaction?.amount !== 0 : requestType !== CONST.IOU.REQUEST_TYPE.SCAN} action={action} payeePersonalDetails={payeePersonalDetails} /> From 941dd37137f9c8a7a1a2d875c06827a780af0fdd Mon Sep 17 00:00:00 2001 From: Monil Bhavsar Date: Wed, 5 Jun 2024 20:13:03 +0530 Subject: [PATCH 6/7] Revert a change and edit docs --- src/libs/TransactionUtils.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/libs/TransactionUtils.ts b/src/libs/TransactionUtils.ts index 7abc8f22bb14..10ee87b11cba 100644 --- a/src/libs/TransactionUtils.ts +++ b/src/libs/TransactionUtils.ts @@ -695,8 +695,9 @@ function getRateID(transaction: OnyxEntry): string | undefined { } /** - * Gets the tax code based on selected currency. - * Returns policy default tax rate if transaction is in policy default currency, otherwise returns foreign default tax rate + * Gets the tax code based on the type of transaction and selected currency. + * If it is distance request, then returns the tax code corresponding to the custom unit rate + * Else returns policy default tax rate if transaction is in policy default currency, otherwise foreign default tax rate */ function getDefaultTaxCode(policy: OnyxEntry, transaction: OnyxEntry, currency?: string | undefined) { if (isDistanceRequest(transaction)) { @@ -748,12 +749,11 @@ function getWorkspaceTaxesSettingsName(policy: OnyxEntry, taxCode: strin } /** - * Gets the tax name for the given transaction. - * If it is distance request, then returns the tax name corresponding to the custom unit rate + * Gets the name corresponding to the taxCode that is displayed to the user */ function getTaxName(policy: OnyxEntry, transaction: OnyxEntry) { - const taxCode = getDefaultTaxCode(policy, transaction) ?? ''; - return Object.values(transformedTaxRates(policy, transaction)).find((taxRate) => taxRate.code === (transaction?.taxCode ?? taxCode))?.modifiedName; + const defaultTaxCode = getDefaultTaxCode(policy, transaction); + return Object.values(transformedTaxRates(policy, transaction)).find((taxRate) => taxRate.code === (transaction?.taxCode ?? defaultTaxCode))?.modifiedName; } export { From 3744ffb0b09958ce205f9e67fdf7617852ab92a1 Mon Sep 17 00:00:00 2001 From: Monil Bhavsar Date: Thu, 6 Jun 2024 11:56:29 +0530 Subject: [PATCH 7/7] Fix lint --- src/components/MoneyRequestConfirmationList.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/components/MoneyRequestConfirmationList.tsx b/src/components/MoneyRequestConfirmationList.tsx index 0db02127af2f..fcb415fc193f 100755 --- a/src/components/MoneyRequestConfirmationList.tsx +++ b/src/components/MoneyRequestConfirmationList.tsx @@ -374,7 +374,6 @@ function MoneyRequestConfirmationList({ let taxableAmount: number; let taxCode: string; if (isDistanceRequest) { - const customUnitRateID = TransactionUtils.getRateID(transaction) ?? ''; const customUnitRate = getCustomUnitRate(policy, customUnitRateID); taxCode = customUnitRate?.attributes?.taxRateExternalID ?? ''; taxableAmount = DistanceRequestUtils.getTaxableAmount(policy, customUnitRateID, TransactionUtils.getDistance(transaction)); @@ -386,7 +385,7 @@ function MoneyRequestConfirmationList({ const taxAmount = TransactionUtils.calculateTaxAmount(taxPercentage, taxableAmount); const taxAmountInSmallestCurrencyUnits = CurrencyUtils.convertToBackendAmount(Number.parseFloat(taxAmount.toString())); IOU.setMoneyRequestTaxAmount(transaction?.transactionID ?? '', taxAmountInSmallestCurrencyUnits); - }, [policy, shouldShowTax, previousTransactionAmount, previousTransactionCurrency, transaction, isDistanceRequest]); + }, [policy, shouldShowTax, previousTransactionAmount, previousTransactionCurrency, transaction, isDistanceRequest, customUnitRateID]); // If completing a split expense fails, set didConfirm to false to allow the user to edit the fields again if (isEditingSplitBill && didConfirm) {