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

Fix tax details are empty when categorizing tracked expense #42151

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ type GetOptionsConfig = {
includeSelectedOptions?: boolean;
includeTaxRates?: boolean;
taxRates?: TaxRatesWithDefault;
policy?: OnyxEntry<Policy>;
policy?: OnyxEntry<Policy> | EmptyObject;
transaction?: OnyxEntry<Transaction>;
includePolicyReportFieldOptions?: boolean;
policyReportFieldOptions?: string[];
Expand Down Expand Up @@ -1383,7 +1383,7 @@ function getTaxRatesOptions(taxRates: Array<Partial<TaxRate>>): TaxRatesOption[]
/**
* Builds the section list for tax rates
*/
function getTaxRatesSection(policy: OnyxEntry<Policy> | undefined, selectedOptions: Tax[], searchInputValue: string, transaction?: OnyxEntry<Transaction>): TaxSection[] {
function getTaxRatesSection(policy: OnyxEntry<Policy> | EmptyObject, selectedOptions: Tax[], searchInputValue: string, transaction?: OnyxEntry<Transaction>): TaxSection[] {
const policyRatesSections = [];

const taxes = TransactionUtils.transformedTaxRates(policy, transaction);
Expand Down Expand Up @@ -1705,7 +1705,7 @@ function getOptions(
}

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

return {
recentReports: [],
Expand Down
2 changes: 1 addition & 1 deletion src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ function isPaidGroupPolicy(policy: OnyxEntry<Policy> | EmptyObject): boolean {
return policy?.type === CONST.POLICY.TYPE.TEAM || policy?.type === CONST.POLICY.TYPE.CORPORATE;
}

function isTaxTrackingEnabled(isPolicyExpenseChat: boolean, policy: OnyxEntry<Policy>): boolean {
function isTaxTrackingEnabled(isPolicyExpenseChat: boolean, policy: OnyxEntry<Policy> | EmptyObject): boolean {
return (isPolicyExpenseChat && (policy?.tax?.trackingEnabled ?? policy?.isTaxTrackingEnabled)) ?? false;
}

Expand Down
23 changes: 16 additions & 7 deletions src/libs/TransactionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
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 type {EmptyObject} from '@src/types/utils/EmptyObject';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import type {IOURequestType} from './actions/IOU';
import {isCorporateCard, isExpensifyCard} from './CardUtils';
Expand Down Expand Up @@ -658,7 +659,7 @@ function getRateID(transaction: OnyxEntry<Transaction>): 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
*/
function getDefaultTaxCode(policy: OnyxEntry<Policy>, transaction: OnyxEntry<Transaction>, currency?: string | undefined) {
function getDefaultTaxCode(policy: OnyxEntry<Policy> | EmptyObject, transaction: OnyxEntry<Transaction>, currency?: string | undefined) {
const defaultExternalID = policy?.taxRates?.defaultExternalID;
const foreignTaxDefault = policy?.taxRates?.foreignTaxDefault;
return policy?.outputCurrency === (currency ?? getCurrency(transaction)) ? defaultExternalID : foreignTaxDefault;
Expand All @@ -667,10 +668,9 @@ function getDefaultTaxCode(policy: OnyxEntry<Policy>, transaction: OnyxEntry<Tra
/**
* 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
* @returns The transformed tax rates object
*/
function transformedTaxRates(policy: OnyxEntry<Policy> | undefined, transaction?: OnyxEntry<Transaction>): Record<string, TaxRate> {
function transformedTaxRates(policy: OnyxEntry<Policy> | EmptyObject, transaction?: OnyxEntry<Transaction>): Record<string, TaxRate> {
const taxRates = policy?.taxRates;
const defaultExternalID = taxRates?.defaultExternalID;

Expand All @@ -684,14 +684,23 @@ function transformedTaxRates(policy: OnyxEntry<Policy> | undefined, transaction?

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;
return Object.fromEntries(
Object.entries(taxRates?.taxes ?? {}).map(([code, data]) => [
code,
{
...data,
code,
modifiedName: getModifiedName(data, code),
name: data.name,
},
]),
);
}

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

Expand Down
4 changes: 2 additions & 2 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6517,8 +6517,8 @@ function setMoneyRequestParticipantsFromReport(transactionID: string, report: On
return participants;
}

function setMoneyRequestTaxRate(transactionID: string, taxCode: string) {
Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`, {taxCode});
function setMoneyRequestTaxRate(transactionID: string, taxCode: string, isDraft: boolean) {
Onyx.merge(`${isDraft ? ONYXKEYS.COLLECTION.TRANSACTION_DRAFT : ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, {taxCode});
}

function setMoneyRequestTaxAmount(transactionID: string, taxAmount: number, isDraft: boolean) {
Expand Down
32 changes: 31 additions & 1 deletion src/pages/iou/request/step/IOURequestStepParticipants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import {withOnyx} from 'react-native-onyx';
import FormHelpMessage from '@components/FormHelpMessage';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import DistanceRequestUtils from '@libs/DistanceRequestUtils';
import * as IOUUtils from '@libs/IOUUtils';
import Navigation from '@libs/Navigation/Navigation';
import {getPolicy, isTaxTrackingEnabled} from '@libs/PolicyUtils';
import * as TransactionUtils from '@libs/TransactionUtils';
import MoneyRequestParticipantsSelector from '@pages/iou/request/MoneyRequestParticipantsSelector';
import * as IOU from '@userActions/IOU';
Expand Down Expand Up @@ -81,6 +83,31 @@ function IOURequestStepParticipants({
IOU.navigateToStartStepIfScanFileCannotBeRead(receiptFilename ?? '', receiptPath ?? '', () => {}, iouRequestType, iouType, transactionID, reportID, receiptType ?? '');
}, [receiptType, receiptPath, receiptFilename, iouRequestType, iouType, transactionID, reportID, action]);

const setTaxDetailsOnCategorizing = useCallback(
(participantsArray: Participant[]) => {
if (action !== CONST.IOU.ACTION.CATEGORIZE) {
return;
}

const isPolicyExpenseChat = participantsArray.length === 1 && participantsArray[0]?.isPolicyExpenseChat;
if (!isPolicyExpenseChat) {
return;
}

const policyID = participantsArray[0].policyID;
const policy = getPolicy(policyID);
if (!isTaxTrackingEnabled(isPolicyExpenseChat, policy)) {
return;
}
const taxCode = TransactionUtils.getDefaultTaxCode(policy, transaction) ?? '';
const taxPercentage = TransactionUtils.getTaxValue(policy, transaction, taxCode) ?? '';
const taxAmount = CurrencyUtils.convertToBackendAmount(TransactionUtils.calculateTaxAmount(taxPercentage, transaction?.amount ?? 0));
IOU.setMoneyRequestTaxRate(transactionID, taxCode, true);
IOU.setMoneyRequestTaxAmount(transactionID, taxAmount, true);
},
[action, transaction, transactionID],
);

const addParticipant = useCallback(
(val: Participant[]) => {
IOU.setMoneyRequestParticipants(transactionID, val);
Expand All @@ -96,10 +123,13 @@ function IOURequestStepParticipants({
return;
}

// If user is categorizing tracked expense with respective workspace on policy expense chat then update tax details on draft transaction
setTaxDetailsOnCategorizing(val);

// When a participant is selected, the reportID needs to be saved because that's the reportID that will be used in the confirmation step.
selectedReportID.current = val[0]?.reportID ?? reportID;
},
[reportID, transactionID],
[reportID, transactionID, setTaxDetailsOnCategorizing],
);

const goToNextStep = useCallback(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/iou/request/step/IOURequestStepTaxRatePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function IOURequestStepTaxRatePage({
return;
}
const amountInSmallestCurrencyUnits = CurrencyUtils.convertToBackendAmount(taxAmount);
IOU.setMoneyRequestTaxRate(transaction?.transactionID, taxes?.code ?? '');
IOU.setMoneyRequestTaxRate(transaction?.transactionID, taxes?.code ?? '', true);
IOU.setMoneyRequestTaxAmount(transaction.transactionID, amountInSmallestCurrencyUnits, true);

navigateBack();
Expand Down
Loading