Skip to content

Commit

Permalink
don't apply the category tax rules for distance request
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhardoj committed Dec 26, 2024
1 parent 918488a commit 6734a5a
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/libs/TransactionUtils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ function buildTransactionsMergeParams(reviewDuplicates: OnyxEntry<ReviewDuplicat

function getCategoryTaxCodeAndAmount(category: string, transaction: OnyxEntry<Transaction>, policy: OnyxEntry<Policy>) {
const taxRules = policy?.rules?.expenseRules?.filter((rule) => rule.tax);
if (!taxRules || taxRules?.length === 0) {
if (!taxRules || taxRules?.length === 0 || isDistanceRequest(transaction)) {
return {categoryTaxCode: undefined, categoryTaxAmount: undefined};
}

Expand Down
201 changes: 145 additions & 56 deletions tests/actions/IOUTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3520,40 +3520,82 @@ describe('actions/IOU', () => {
});
});

it('should not change the tax if there are no tax expense rules', async () => {
// Given a policy without tax expense rules
const transactionID = '1';
const category = 'Advertising';
const policyID = '2';
const taxCode = 'id_TAX_EXEMPT';
const taxAmount = 0;
const fakePolicy: OnyxTypes.Policy = {
...createRandomPolicy(Number(policyID)),
taxRates: CONST.DEFAULT_TAX,
rules: {},
};
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`, {
taxCode,
taxAmount,
amount: 100,
describe('should not change the tax', () => {
it('if the transaction type is distance', async () => {
// Given a policy with tax expense rules associated with category and a distance transaction
const transactionID = '1';
const category = 'Advertising';
const policyID = '2';
const taxCode = 'id_TAX_EXEMPT';
const ruleTaxCode = 'id_TAX_RATE_1';
const taxAmount = 0;
const fakePolicy: OnyxTypes.Policy = {
...createRandomPolicy(Number(policyID)),
taxRates: CONST.DEFAULT_TAX,
rules: {expenseRules: createCategoryTaxExpenseRules(category, ruleTaxCode)},
};
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`, {
taxCode,
taxAmount,
amount: 100,
iouRequestType: CONST.IOU.REQUEST_TYPE.DISTANCE,
});
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, fakePolicy);

// When setting the money request category
IOU.setMoneyRequestCategory(transactionID, category, policyID);

await waitForBatchedUpdates();

// Then the transaction tax rate and amount shouldn't be updated
await new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`,
callback: (transaction) => {
Onyx.disconnect(connection);
expect(transaction?.taxCode).toBe(taxCode);
expect(transaction?.taxAmount).toBe(taxAmount);
resolve();
},
});
});
});
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, fakePolicy);

// When setting the money request category
IOU.setMoneyRequestCategory(transactionID, category, policyID);
it('if there are no tax expense rules', async () => {
// Given a policy without tax expense rules
const transactionID = '1';
const category = 'Advertising';
const policyID = '2';
const taxCode = 'id_TAX_EXEMPT';
const taxAmount = 0;
const fakePolicy: OnyxTypes.Policy = {
...createRandomPolicy(Number(policyID)),
taxRates: CONST.DEFAULT_TAX,
rules: {},
};
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`, {
taxCode,
taxAmount,
amount: 100,
});
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, fakePolicy);

await waitForBatchedUpdates();
// When setting the money request category
IOU.setMoneyRequestCategory(transactionID, category, policyID);

// Then the transaction tax rate and amount shouldn't be updated
await new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`,
callback: (transaction) => {
Onyx.disconnect(connection);
expect(transaction?.taxCode).toBe(taxCode);
expect(transaction?.taxAmount).toBe(taxAmount);
resolve();
},
await waitForBatchedUpdates();

// Then the transaction tax rate and amount shouldn't be updated
await new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`,
callback: (transaction) => {
Onyx.disconnect(connection);
expect(transaction?.taxCode).toBe(taxCode);
expect(transaction?.taxAmount).toBe(taxAmount);
resolve();
},
});
});
});
});
Expand Down Expand Up @@ -3627,34 +3669,81 @@ describe('actions/IOU', () => {
});
});

it('should not update the tax when there are no tax expense rules', async () => {
// Given a policy without tax expense rules
const transactionID = '1';
const policyID = '2';
const category = 'Advertising';
const fakePolicy: OnyxTypes.Policy = {
...createRandomPolicy(Number(policyID)),
taxRates: CONST.DEFAULT_TAX,
rules: {},
};
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, {amount: 100});
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, fakePolicy);
describe('should not update the tax', () => {
it('if the transaction type is distance', async () => {
// Given a policy with tax expense rules associated with category and a distance transaction
const transactionID = '1';
const policyID = '2';
const category = 'Advertising';
const taxCode = 'id_TAX_EXEMPT';
const taxAmount = 0;
const ruleTaxCode = 'id_TAX_RATE_1';
const fakePolicy: OnyxTypes.Policy = {
...createRandomPolicy(Number(policyID)),
taxRates: CONST.DEFAULT_TAX,
rules: {expenseRules: createCategoryTaxExpenseRules(category, ruleTaxCode)},
};
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, {
taxCode,
taxAmount,
amount: 100,
comment: {
type: CONST.TRANSACTION.TYPE.CUSTOM_UNIT,
customUnit: {
name: CONST.CUSTOM_UNITS.NAME_DISTANCE,
},
},
});
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, fakePolicy);

// When updating the money request category
IOU.updateMoneyRequestCategory(transactionID, '3', category, fakePolicy, undefined, undefined);
// When updating a money request category
IOU.updateMoneyRequestCategory(transactionID, '3', category, fakePolicy, undefined, undefined);

await waitForBatchedUpdates();
await waitForBatchedUpdates();

// Then the transaction tax rate and amount shouldn't be updated
await new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`,
callback: (transaction) => {
Onyx.disconnect(connection);
expect(transaction?.taxCode).toBeUndefined();
expect(transaction?.taxAmount).toBeUndefined();
resolve();
},
// Then the transaction tax rate and amount shouldn't be updated
await new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`,
callback: (transaction) => {
Onyx.disconnect(connection);
expect(transaction?.taxCode).toBe(taxCode);
expect(transaction?.taxAmount).toBe(taxAmount);
resolve();
},
});
});
});

it('if there are no tax expense rules', async () => {
// Given a policy without tax expense rules
const transactionID = '1';
const policyID = '2';
const category = 'Advertising';
const fakePolicy: OnyxTypes.Policy = {
...createRandomPolicy(Number(policyID)),
taxRates: CONST.DEFAULT_TAX,
rules: {},
};
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, {amount: 100});
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, fakePolicy);

// When updating the money request category
IOU.updateMoneyRequestCategory(transactionID, '3', category, fakePolicy, undefined, undefined);

await waitForBatchedUpdates();

// Then the transaction tax rate and amount shouldn't be updated
await new Promise<void>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`,
callback: (transaction) => {
Onyx.disconnect(connection);
expect(transaction?.taxCode).toBeUndefined();
expect(transaction?.taxAmount).toBeUndefined();
resolve();
},
});
});
});
});
Expand Down Expand Up @@ -3700,7 +3789,7 @@ describe('actions/IOU', () => {
});

describe('should not change the tax', () => {
it('if there is no tax expense rules', async () => {
it('if there are no tax expense rules', async () => {
// Given a policy without tax expense rules
const transactionID = '1';
const category = 'Advertising';
Expand Down

0 comments on commit 6734a5a

Please sign in to comment.