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

support for offline tax tracking #40443

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
7574fc9
add taxAmount param to updateMoneyRequestAmountAndCurrency
teneeto Apr 18, 2024
1ed12df
should optimistically update tax amount when updating expense amount
teneeto Apr 18, 2024
1c16b1a
add UpdateMoneyRequestTaxRateParams types to updateMoneyRequestTaxRate
teneeto Apr 18, 2024
8dce58a
add taxAmount to UpdateMoneyRequestTaxRateParams types
teneeto Apr 18, 2024
228c477
should optimistically update tax amount when updating tax rate
teneeto Apr 18, 2024
a0ac9ba
fix lint
teneeto Apr 18, 2024
7b79f15
Merge branch 'main' of github.com:teneeto/Expensify into feature/3961…
teneeto Apr 19, 2024
3ba09e8
fix lint
teneeto Apr 19, 2024
59a57b0
Merge branch 'main' of github.com:teneeto/Expensify into feature/3961…
teneeto Apr 22, 2024
1e8a4a4
Merge branch 'main' of github.com:teneeto/Expensify into feature/3961…
teneeto Apr 22, 2024
3cd43a4
update policy getTaxRatesSection
teneeto Apr 24, 2024
31d6d0d
update TaxPicker
teneeto Apr 24, 2024
b2829fc
update IOURequestStepTaxRatePage
teneeto Apr 24, 2024
d87b8cc
Merge branch 'main' of github.com:teneeto/Expensify into feature/3961…
teneeto Apr 24, 2024
e9a2ebd
set taxCode and use foreignTaxDefault and defaultExternalId based on …
teneeto Apr 24, 2024
26e108f
set taxcode and not taxRate
teneeto Apr 24, 2024
7855c95
modify default TaxName props
teneeto Apr 24, 2024
36e731f
update defaultTaxKey
teneeto Apr 24, 2024
f66df7b
use modified getDefaultTaxName
teneeto Apr 24, 2024
7c29f96
set default tax code
teneeto Apr 24, 2024
6330d7d
update defaultTaxKey to defaultTaxCode
teneeto Apr 24, 2024
cdeb5bd
update defaultTaxKey to defaultTaxCode
teneeto Apr 24, 2024
8e08bb9
add getDefaultTaxCode function
teneeto Apr 24, 2024
dd6f0a6
use getDefaultTaxCode function
teneeto Apr 24, 2024
042bf2e
use getDefaultTaxCode function
teneeto Apr 24, 2024
45fd4f5
remove unused var
teneeto Apr 24, 2024
5365262
use getDefaultTaxCode function
teneeto Apr 24, 2024
57425e4
use getDefaultTaxCode function
teneeto Apr 24, 2024
930b070
fix lint
teneeto Apr 24, 2024
eec7b89
fix lint
teneeto Apr 24, 2024
9d0b843
fix lint
teneeto Apr 24, 2024
dc13632
update getTaxName
teneeto Apr 24, 2024
16b32c6
use update getTaxName
teneeto Apr 24, 2024
2fd6906
use update getTaxName and getDefaultTaxName
teneeto Apr 24, 2024
2850e39
cleanup taxRateTitle
teneeto Apr 24, 2024
da28145
add getTaxRateTitle
teneeto Apr 24, 2024
2213bc0
use getTaxRateTitle
teneeto Apr 24, 2024
f157b2d
use getTaxRateTitle
teneeto Apr 24, 2024
9be9960
update getDefaultTaxName
teneeto Apr 24, 2024
381fd11
use getTaxRateTitle
teneeto Apr 24, 2024
f23a397
fix cycle dependency
teneeto Apr 24, 2024
f2f529d
remove transactionCode
teneeto Apr 24, 2024
fc15227
fix cycle dependency
teneeto Apr 24, 2024
1341be5
fix type import
teneeto Apr 24, 2024
a5d2386
update getTaxRateTitle
teneeto Apr 24, 2024
bdb39b5
use getTaxRateTitle
teneeto Apr 24, 2024
521c669
use getWorkspaceTaxesSettingsName
teneeto Apr 24, 2024
9ef5ee5
merge conflicts
teneeto Apr 24, 2024
f8d31a6
should not show default for workspace settings options
teneeto Apr 25, 2024
a584cf0
use isSelected
teneeto Apr 25, 2024
e262453
remove transaction check
teneeto Apr 25, 2024
2e2da04
use code from selected tax
teneeto Apr 25, 2024
3522c4e
remove data type
teneeto Apr 25, 2024
2ae2836
fix OptionsListUtilsTest
teneeto Apr 25, 2024
1a0a317
fix lint
teneeto Apr 25, 2024
1c99e0f
Merge branch 'main' of github.com:teneeto/Expensify into feature/3961…
teneeto Apr 25, 2024
05f0784
Merge branch 'main' of github.com:teneeto/Expensify into feature/3961…
teneeto Apr 25, 2024
7872da9
update getTaxAmount for MoneyRequestConfirmationList
teneeto Apr 29, 2024
799182f
update getTaxAmount for IOURequestStepAmount
teneeto Apr 29, 2024
98f3180
update getTaxAmount for IOURequestStepTaxAmountPage
teneeto Apr 29, 2024
483f5d3
use transactionTaxCode var
teneeto Apr 29, 2024
dab26d8
add getTaxValue
teneeto Apr 29, 2024
ffe1a80
use getTaxValue MoneyRequestConfirmationList
teneeto Apr 29, 2024
61f31e1
use getTaxValue IOURequestStepAmount
teneeto Apr 29, 2024
7e7bcc0
use getTaxValue IOURequestStepTaxAmountPage
teneeto Apr 29, 2024
3011f30
fix lint
teneeto Apr 29, 2024
aed16c7
add getModifiedTaxName
teneeto Apr 29, 2024
9518d73
update tax name and default tax name
teneeto Apr 29, 2024
b27f215
update getTaxAmount for IOURequestStepTaxRatePage
teneeto Apr 29, 2024
072d10d
Merge branch 'main' of github.com:teneeto/Expensify into feature/3961…
teneeto Apr 29, 2024
1b41f8c
update getTaxAmount for MoneyRequestConfirmationList
teneeto Apr 29, 2024
57882de
update getTaxAmount for IOURequestStepAmount
teneeto Apr 29, 2024
25c2003
fix lint
teneeto Apr 29, 2024
18bc37c
fix comment review
teneeto Apr 29, 2024
f9a745c
Update src/libs/TransactionUtils.ts
teneeto Apr 29, 2024
5e6336c
Merge branch 'main' of github.com:teneeto/Expensify into feature/3961…
teneeto Apr 30, 2024
c25eca1
combine getTaxName and getDefaultTaxName
teneeto Apr 30, 2024
79bee2b
replace getTaxRateTitle with getTaxName
teneeto Apr 30, 2024
843d33c
use getTaxName
teneeto Apr 30, 2024
e93b1de
Merge branch 'main' of github.com:teneeto/Expensify into feature/3961…
teneeto Apr 30, 2024
bf0ba54
Merge branch 'main' of github.com:teneeto/Expensify into feature/3961…
teneeto May 1, 2024
a5768f8
Merge branch 'main' of github.com:teneeto/Expensify into feature/3961…
teneeto May 2, 2024
398e421
Merge branch 'main' of github.com:teneeto/Expensify into feature/3961…
teneeto May 2, 2024
0102a3f
add tax code and tax amount to buildOptimisticTransaction
teneeto May 2, 2024
4305c13
add taxCode and taxAmount types to CreateDistanceRequestParams
teneeto May 2, 2024
876ee9a
update optimistic data for track expense
teneeto May 2, 2024
e7e8886
should submit taxCode and taxAmount for distance request
teneeto May 2, 2024
764a76d
pass empty taxCode and taxAmount values to IOU.createDistanceRequest
teneeto May 2, 2024
09bbe46
Merge branch 'main' of github.com:teneeto/Expensify into feature/3961…
teneeto May 2, 2024
81e53e8
update error field
teneeto May 3, 2024
25178e9
Merge branch 'main' of github.com:teneeto/Expensify into feature/3961…
teneeto May 6, 2024
0ca3634
undo: update error field
teneeto May 6, 2024
1184706
Merge branch 'main' of github.com:teneeto/Expensify into feature/3961…
teneeto May 7, 2024
b5824e8
update tax amount when transaction is selected
teneeto May 7, 2024
923a2ab
fix lint
teneeto May 7, 2024
0a27694
Merge branch 'main' of github.com:teneeto/Expensify into feature/3961…
teneeto May 8, 2024
144746d
fix lint
teneeto May 8, 2024
52e8cc7
revert: add backTo
teneeto May 9, 2024
bade846
Merge branch 'main' of github.com:teneeto/Expensify into feature/3961…
teneeto May 10, 2024
ea26dc9
use navigateBack
teneeto May 10, 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
17 changes: 10 additions & 7 deletions src/components/MoneyRequestConfirmationList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,11 @@ type MoneyRequestConfirmationListProps = MoneyRequestConfirmationListOnyxProps &
action?: IOUAction;
};

const getTaxAmount = (transaction: OnyxEntry<OnyxTypes.Transaction>, defaultTaxValue: string) => {
const percentage = (transaction?.taxRate ? transaction?.taxRate?.data?.value : defaultTaxValue) ?? '';
return TransactionUtils.calculateTaxAmount(percentage, transaction?.amount ?? 0);
const getTaxAmount = (transaction: OnyxEntry<OnyxTypes.Transaction>, policy: OnyxEntry<OnyxTypes.Policy>) => {
const defaultTaxCode = TransactionUtils.getDefaultTaxCode(policy, transaction) ?? '';

const taxPercentage = TransactionUtils.getTaxValue(policy, transaction, transaction?.taxCode ?? defaultTaxCode) ?? '';
Copy link
Contributor

Choose a reason for hiding this comment

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

When the currency differs from the current transaction currency, the taxCode should have also changed to the default (i.e. local or foreign based on the newly updated currency) tax rate. And, the tax amount should also have been recomputed. Missing these changes here resulted in issue #42049.

return TransactionUtils.calculateTaxAmount(taxPercentage, transaction?.amount ?? 0);
};

function MoneyRequestConfirmationList({
Expand Down Expand Up @@ -317,9 +319,10 @@ function MoneyRequestConfirmationList({
isDistanceRequest ? currency : iouCurrencyCode,
);
const formattedTaxAmount = CurrencyUtils.convertToDisplayString(transaction?.taxAmount, iouCurrencyCode);
const taxRateTitle = taxRates && transaction ? TransactionUtils.getDefaultTaxName(taxRates, transaction) : '';
const taxRateTitle = TransactionUtils.getTaxName(policy, transaction);

const previousTransactionAmount = usePrevious(transaction?.amount);
const previousTransactionCurrency = usePrevious(transaction?.currency);

const isFocused = useIsFocused();
const [formError, debouncedFormError, setFormError] = useDebouncedState('');
Expand Down Expand Up @@ -373,15 +376,15 @@ function MoneyRequestConfirmationList({

// Calculate and set tax amount in transaction draft
useEffect(() => {
const taxAmount = getTaxAmount(transaction, taxRates?.defaultValue ?? '').toString();
const taxAmount = getTaxAmount(transaction, policy).toString();
const amountInSmallestCurrencyUnits = CurrencyUtils.convertToBackendAmount(Number.parseFloat(taxAmount));

if (transaction?.taxAmount && previousTransactionAmount === transaction?.amount) {
if (transaction?.taxAmount && previousTransactionAmount === transaction?.amount && previousTransactionCurrency === transaction?.currency) {
return IOU.setMoneyRequestTaxAmount(transaction?.transactionID, transaction?.taxAmount, true);
}
Comment on lines +382 to 384
Copy link
Contributor

@aldo-expensify aldo-expensify May 31, 2024

Choose a reason for hiding this comment

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

What is this block doing here? is it here in case the currency didn't change?

if transaction?.taxAmount && previousTransactionAmount, what is the purpose of calling IOU.setMoneyRequestTaxAmount? is the draft in a different state?


IOU.setMoneyRequestTaxAmount(transactionID, amountInSmallestCurrencyUnits, true);
}, [taxRates?.defaultValue, transaction, transactionID, previousTransactionAmount]);
}, [policy, transaction, transactionID, previousTransactionAmount, previousTransactionCurrency]);

// If completing a split expense fails, set didConfirm to false to allow the user to edit the fields again
if (isEditingSplitBill && didConfirm) {
Expand Down
7 changes: 1 addition & 6 deletions src/components/ReportActionItem/MoneyRequestView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ function MoneyRequestView({
created: transactionDate,
amount: transactionAmount,
taxAmount: transactionTaxAmount,
taxCode: transactionTaxCode,
currency: transactionCurrency,
comment: transactionDescription,
merchant: transactionMerchant,
Expand All @@ -134,11 +133,7 @@ function MoneyRequestView({
const formattedTaxAmount = CurrencyUtils.convertToDisplayString(transactionTaxAmount, transactionCurrency);

const taxRatesDescription = taxRates?.name;
const taxRateTitle =
taxRates &&
(transactionTaxCode === taxRates?.defaultExternalID
? transaction && TransactionUtils.getDefaultTaxName(taxRates, transaction)
: transactionTaxCode && TransactionUtils.getTaxName(taxRates?.taxes, transactionTaxCode));
const taxRateTitle = TransactionUtils.getTaxName(policy, transaction);

// Flags for allowing or disallowing editing an expense
const isSettled = ReportUtils.isSettled(moneyRequestReport?.reportID);
Expand Down
30 changes: 27 additions & 3 deletions src/components/TaxPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ import type {OnyxEntry} from 'react-native-onyx';
import type {EdgeInsets} from 'react-native-safe-area-context';
import useLocalize from '@hooks/useLocalize';
import useStyleUtils from '@hooks/useStyleUtils';
import * as IOUUtils from '@libs/IOUUtils';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import * as TransactionUtils from '@libs/TransactionUtils';
import CONST from '@src/CONST';
import type {IOUAction} from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy} from '@src/types/onyx';
import type {Policy, Transaction} from '@src/types/onyx';
import SelectionList from './SelectionList';
import RadioListItem from './SelectionList/RadioListItem';

type TaxPickerOnyxProps = {
/** The policy which the user has access to and which the report is tied to */
policy: OnyxEntry<Policy>;

/** All the data for the transaction */
transaction: OnyxEntry<Transaction>;
};

type TaxPickerProps = TaxPickerOnyxProps & {
Expand All @@ -25,6 +30,10 @@ type TaxPickerProps = TaxPickerOnyxProps & {
// eslint-disable-next-line react/no-unused-prop-types
policyID?: string;

/** ID of the transaction */
// eslint-disable-next-line react/no-unused-prop-types
transactionID?: string;

/**
* Safe area insets required for reflecting the portion of the view,
* that is not covered by navigation bars, tab bars, toolbars, and other ancestor views.
Expand All @@ -33,9 +42,13 @@ type TaxPickerProps = TaxPickerOnyxProps & {

/** Callback to fire when a tax is pressed */
onSubmit: (tax: OptionsListUtils.TaxRatesOption) => void;

/** The action to take */
// eslint-disable-next-line react/no-unused-prop-types
action?: IOUAction;
};

function TaxPicker({selectedTaxRate = '', policy, insets, onSubmit}: TaxPickerProps) {
function TaxPicker({selectedTaxRate = '', policy, transaction, insets, onSubmit}: TaxPickerProps) {
const StyleUtils = useStyleUtils();
const {translate} = useLocalize();
const [searchValue, setSearchValue] = useState('');
Expand All @@ -60,7 +73,10 @@ function TaxPicker({selectedTaxRate = '', policy, insets, onSubmit}: TaxPickerPr
];
}, [selectedTaxRate]);

const sections = useMemo(() => OptionsListUtils.getTaxRatesSection(taxRates, selectedOptions as OptionsListUtils.Tax[], searchValue), [taxRates, searchValue, selectedOptions]);
const sections = useMemo(
() => OptionsListUtils.getTaxRatesSection(policy, selectedOptions as OptionsListUtils.Tax[], searchValue, transaction),
[searchValue, selectedOptions, policy, transaction],
);

const headerMessage = OptionsListUtils.getHeaderMessageForNonUserList(sections[0].data.length > 0, searchValue);

Expand Down Expand Up @@ -88,4 +104,12 @@ export default withOnyx<TaxPickerProps, TaxPickerOnyxProps>({
policy: {
key: ({policyID}) => `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
},
transaction: {
key: ({transactionID, action}) => {
if (action === CONST.IOU.ACTION.CREATE || IOUUtils.isMovingTransactionFromTrackExpense(action)) {
return `${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}` as `${typeof ONYXKEYS.COLLECTION.TRANSACTION}${string}`;
}
return `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`;
},
},
})(TaxPicker);
2 changes: 2 additions & 0 deletions src/libs/API/parameters/CreateDistanceRequestParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type CreateDistanceRequestParams = {
created: string;
category?: string;
tag?: string;
taxCode?: string;
taxAmount?: number;
billable?: boolean;
transactionThreadReportID: string;
createdReportActionIDForThread: string;
Expand Down
44 changes: 16 additions & 28 deletions src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ type TaxRatesOption = {
tooltipText?: string;
isDisabled?: boolean;
keyForList?: string;
data: Partial<TaxRate>;
isSelected?: boolean;
};

type TaxSection = {
Expand Down Expand Up @@ -165,6 +165,8 @@ type GetOptionsConfig = {
includeSelectedOptions?: boolean;
includeTaxRates?: boolean;
taxRates?: TaxRatesWithDefault;
policy?: OnyxEntry<Policy>;
transaction?: OnyxEntry<Transaction>;
includePolicyReportFieldOptions?: boolean;
policyReportFieldOptions?: string[];
recentlyUsedPolicyReportFieldOptions?: string[];
Expand Down Expand Up @@ -1339,20 +1341,6 @@ function getReportFieldOptionsSection(options: string[], recentlyUsedOptions: st
return reportFieldOptionsSections;
}

/**
* Transforms tax rates to a new object format - to add codes and new name with concatenated name and value.
*
* @param taxRates - The original tax rates object.
* @returns The transformed tax rates object.g
*/
function transformedTaxRates(taxRates: TaxRatesWithDefault | undefined): Record<string, TaxRate> {
const defaultTaxKey = taxRates?.defaultExternalID;
const getModifiedName = (data: TaxRate, code: string) =>
`${data.name} (${data.value})${defaultTaxKey === code ? ` ${CONST.DOT_SEPARATOR} ${Localize.translateLocal('common.default')}` : ''}`;
const taxes = Object.fromEntries(Object.entries(taxRates?.taxes ?? {}).map(([code, data]) => [code, {...data, code, modifiedName: getModifiedName(data, code), name: data.name}]));
return taxes;
}

/**
* Sorts tax rates alphabetically by name.
*/
Expand All @@ -1365,24 +1353,24 @@ function sortTaxRates(taxRates: TaxRates): TaxRate[] {
* Builds the options for taxRates
*/
function getTaxRatesOptions(taxRates: Array<Partial<TaxRate>>): TaxRatesOption[] {
return taxRates.map((taxRate) => ({
text: taxRate.modifiedName,
keyForList: taxRate.modifiedName,
searchText: taxRate.modifiedName,
tooltipText: taxRate.modifiedName,
isDisabled: taxRate.isDisabled,
data: taxRate,
isSelected: taxRate.isSelected,
return taxRates.map(({code, modifiedName, isDisabled, isSelected}) => ({
code,
text: modifiedName,
keyForList: modifiedName,
searchText: modifiedName,
tooltipText: modifiedName,
isDisabled,
isSelected,
}));
}

/**
* Builds the section list for tax rates
*/
function getTaxRatesSection(taxRates: TaxRatesWithDefault | undefined, selectedOptions: Tax[], searchInputValue: string): TaxSection[] {
function getTaxRatesSection(policy: OnyxEntry<Policy> | undefined, selectedOptions: Tax[], searchInputValue: string, transaction?: OnyxEntry<Transaction>): TaxSection[] {
const policyRatesSections = [];

const taxes = transformedTaxRates(taxRates);
const taxes = TransactionUtils.transformedTaxRates(policy, transaction);

const sortedTaxRates = sortTaxRates(taxes);
const selectedOptionNames = selectedOptions.map((selectedOption) => selectedOption.modifiedName);
Expand Down Expand Up @@ -1669,7 +1657,8 @@ function getOptions(
includeSelectedOptions = false,
transactionViolations = {},
includeTaxRates,
taxRates,
policy,
transaction,
includeSelfDM = false,
includePolicyReportFieldOptions = false,
policyReportFieldOptions = [],
Expand Down Expand Up @@ -1705,7 +1694,7 @@ function getOptions(
}

if (includeTaxRates) {
const taxRatesOptions = getTaxRatesSection(taxRates, selectedOptions as Tax[], searchInputValue);
const taxRatesOptions = getTaxRatesSection(policy, selectedOptions as Tax[], searchInputValue, transaction);

return {
recentReports: [],
Expand Down Expand Up @@ -2435,7 +2424,6 @@ export {
hasEnabledTags,
formatMemberForList,
formatSectionsFromSearchTerm,
transformedTaxRates,
getShareLogOptions,
filterOptions,
createOptionList,
Expand Down
3 changes: 1 addition & 2 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import type {
ReportMetadata,
Session,
Task,
TaxRate,
Transaction,
TransactionViolation,
UserWallet,
Expand Down Expand Up @@ -446,7 +445,7 @@ type OptionData = {
isSelfDM?: boolean;
reportID?: string;
enabled?: boolean;
data?: Partial<TaxRate>;
code?: string;
transactionThreadReportID?: string | null;
shouldShowAmountInput?: boolean;
amountInputProps?: MoneyRequestAmountInputProps;
Expand Down
68 changes: 56 additions & 12 deletions src/libs/TransactionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Onyx from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {RecentWaypoint, Report, TaxRate, TaxRates, TaxRatesWithDefault, Transaction, TransactionViolation} from '@src/types/onyx';
import type {Policy, RecentWaypoint, Report, TaxRate, TaxRates, Transaction, TransactionViolation} 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 type {IOURequestType} from './actions/IOU';
Expand Down Expand Up @@ -96,6 +96,8 @@ function buildOptimisticTransaction(
existingTransactionID: string | null = null,
category = '',
tag = '',
taxCode = '',
taxAmount = 0,
billable = false,
pendingFields: Partial<{[K in TransactionPendingFieldsKey]: ValueOf<typeof CONST.RED_BRICK_ROAD_PENDING_ACTION>}> | undefined = undefined,
reimbursable = true,
Expand Down Expand Up @@ -126,6 +128,8 @@ function buildOptimisticTransaction(
filename,
category,
tag,
taxCode,
taxAmount,
Comment on lines +131 to +132
Copy link
Contributor

Choose a reason for hiding this comment

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

This is causing the deploy blocker: #42114 (comment)

billable,
reimbursable,
};
Expand Down Expand Up @@ -651,29 +655,69 @@ function getRateID(transaction: OnyxEntry<Transaction>): string | undefined {
}

/**
* Gets the default tax name
* 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
*/
function getDefaultTaxName(taxRates: TaxRatesWithDefault, transaction?: Transaction) {
const defaultTaxKey = taxRates.defaultExternalID;
const defaultTaxName =
(defaultTaxKey && `${taxRates.taxes[defaultTaxKey]?.name} (${taxRates.taxes[defaultTaxKey]?.value}) ${CONST.DOT_SEPARATOR} ${Localize.translateLocal('common.default')}`) || '';
return transaction?.taxRate?.text ?? defaultTaxName;
function getDefaultTaxCode(policy: OnyxEntry<Policy>, transaction: OnyxEntry<Transaction>) {
const defaultExternalID = policy?.taxRates?.defaultExternalID;
const foreignTaxDefault = policy?.taxRates?.foreignTaxDefault;
return policy?.outputCurrency === getCurrency(transaction) ? defaultExternalID : foreignTaxDefault;
}

/**
* Transforms tax rates to a new object format - to add codes and new name with concatenated name and value.
*
* @param policy - The policy which the user has access to and which the report is tied to.
* @returns The transformed tax rates object.g
*/
function transformedTaxRates(policy: OnyxEntry<Policy> | undefined, transaction?: OnyxEntry<Transaction>): Record<string, TaxRate> {
const taxRates = policy?.taxRates;
const defaultExternalID = taxRates?.defaultExternalID;

const defaultTaxCode = () => {
if (!transaction) {
return defaultExternalID;
}

return policy && getDefaultTaxCode(policy, transaction);
};
Comment on lines +677 to +683
Copy link
Contributor

Choose a reason for hiding this comment

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

@teneeto Why did we wrap this code in a function? Why didn't we just do something like:

const defaultTaxCode = !transaction ? defaultExternalID : policy && getDefaultTaxCode(policy, transaction);

Are you expecting that policy or transaction can mutate?


const getModifiedName = (data: TaxRate, code: string) =>
`${data.name} (${data.value})${defaultTaxCode() === code ? ` ${CONST.DOT_SEPARATOR} ${Localize.translateLocal('common.default')}` : ''}`;
const taxes = Object.fromEntries(Object.entries(taxRates?.taxes ?? {}).map(([code, data]) => [code, {...data, code, modifiedName: getModifiedName(data, code), name: data.name}]));
return taxes;
}

/**
* Gets the tax value of a selected tax
*/
function getTaxValue(policy: OnyxEntry<Policy>, transaction: OnyxEntry<Transaction>, taxCode: string) {
return Object.values(transformedTaxRates(policy, transaction)).find((taxRate) => taxRate.code === taxCode)?.value;
}

/**
* Gets the tax name for Workspace Taxes Settings
*/
function getWorkspaceTaxesSettingsName(policy: OnyxEntry<Policy>, taxCode: string) {
return Object.values(transformedTaxRates(policy)).find((taxRate) => taxRate.code === taxCode)?.modifiedName;
}

/**
* Gets the tax name
*/
function getTaxName(taxes: TaxRates, transactionTaxCode: string) {
const taxName = taxes[transactionTaxCode]?.name ?? '';
const taxValue = taxes[transactionTaxCode]?.value ?? '';
return transactionTaxCode && taxName && taxValue ? `${taxName} (${taxValue})` : '';
function getTaxName(policy: OnyxEntry<Policy>, transaction: OnyxEntry<Transaction>) {
const defaultTaxCode = getDefaultTaxCode(policy, transaction);
return Object.values(transformedTaxRates(policy, transaction)).find((taxRate) => taxRate.code === (transaction?.taxCode ?? defaultTaxCode))?.modifiedName;
}

export {
buildOptimisticTransaction,
calculateTaxAmount,
getWorkspaceTaxesSettingsName,
getDefaultTaxCode,
transformedTaxRates,
getTaxValue,
getTaxName,
getDefaultTaxName,
getEnabledTaxRateCount,
getUpdatedTransaction,
getDescription,
Expand Down
Loading
Loading