-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
[GL] Tax - Add tax code #43156
[GL] Tax - Add tax code #43156
Changes from 38 commits
5d711c4
f66b22b
9b1b630
8703337
235158b
6e9009b
02e0428
093711b
d3f86b7
1eca1e1
da57f9e
d046fe0
ed414d7
451548c
a69ff83
1131d5d
f782d3f
bee9d44
6dedec2
69ddb27
2c17a1f
78a4aeb
5d00b9f
7336fa1
f49b915
9f95f89
2f8b223
5bd6329
b167d5c
702e1d2
36e8d9c
08e7fbd
9ca398b
828d62f
62f5256
07067a6
04f31e0
81bc5b0
a78d2b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
type UpdatePolicyTaxCodeParams = { | ||
policyID: string; | ||
oldTaxCode: string; | ||
newTaxCode: string; | ||
taxID: string; | ||
}; | ||
|
||
export default UpdatePolicyTaxCodeParams; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,23 @@ import type {OnyxCollection} from 'react-native-onyx'; | |
import Onyx from 'react-native-onyx'; | ||
import type {FormOnyxValues} from '@components/Form/types'; | ||
import * as API from '@libs/API'; | ||
import type {CreatePolicyTaxParams, DeletePolicyTaxesParams, RenamePolicyTaxParams, SetPolicyTaxesEnabledParams, UpdatePolicyTaxValueParams} from '@libs/API/parameters'; | ||
import type { | ||
CreatePolicyTaxParams, | ||
DeletePolicyTaxesParams, | ||
RenamePolicyTaxParams, | ||
SetPolicyTaxesEnabledParams, | ||
UpdatePolicyTaxCodeParams, | ||
UpdatePolicyTaxValueParams, | ||
} from '@libs/API/parameters'; | ||
import {WRITE_COMMANDS} from '@libs/API/types'; | ||
import {translateLocal} from '@libs/Localize'; | ||
import * as ValidationUtils from '@libs/ValidationUtils'; | ||
import CONST from '@src/CONST'; | ||
import * as ErrorUtils from '@src/libs/ErrorUtils'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import INPUT_IDS from '@src/types/form/WorkspaceNewTaxForm'; | ||
// eslint-disable-next-line import/no-named-default | ||
import {default as INPUT_IDS_TAX_CODE} from '@src/types/form/WorkspaceTaxCodeForm'; | ||
import type {Policy, TaxRate, TaxRates} from '@src/types/onyx'; | ||
import type * as OnyxCommon from '@src/types/onyx/OnyxCommon'; | ||
import type {OnyxData} from '@src/types/onyx/Request'; | ||
|
@@ -46,6 +55,17 @@ const validateTaxName = (policy: Policy, values: FormOnyxValues<typeof ONYXKEYS. | |
return errors; | ||
}; | ||
|
||
const validateTaxCode = (policy: Policy, values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_TAX_CODE_FORM>) => { | ||
const errors = ValidationUtils.getFieldRequiredErrors(values, [INPUT_IDS_TAX_CODE.TAX_CODE]); | ||
|
||
const taxCode = values[INPUT_IDS_TAX_CODE.TAX_CODE]; | ||
if (policy?.taxRates?.taxes && ValidationUtils.isExistingTaxCode(taxCode, policy.taxRates.taxes)) { | ||
errors[INPUT_IDS_TAX_CODE.TAX_CODE] = translateLocal('workspace.taxes.error.taxCodeAlreadyExists'); | ||
} | ||
|
||
return errors; | ||
}; | ||
|
||
/** | ||
* Function to validate tax value | ||
*/ | ||
|
@@ -463,6 +483,82 @@ function renamePolicyTax(policyID: string, taxID: string, newName: string) { | |
API.write(WRITE_COMMANDS.RENAME_POLICY_TAX, parameters, onyxData); | ||
} | ||
|
||
function setPolicyTaxCode(policyID: string, oldTaxCode: string, newTaxCode: string) { | ||
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]; | ||
const originalTaxRate = {...policy?.taxRates?.taxes[oldTaxCode]}; | ||
const onyxData: OnyxData = { | ||
optimisticData: [ | ||
{ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, | ||
value: { | ||
taxRates: { | ||
taxes: { | ||
[oldTaxCode]: null, | ||
[newTaxCode]: { | ||
...originalTaxRate, | ||
pendingFields: {code: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}, | ||
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE, | ||
errorFields: {code: null}, | ||
previousTaxCode: oldTaxCode, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NAB - are we using this anywhere? not sure we need it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, if the action fails then we need to reset the taxCode. The previousTaxCode will be used for it |
||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
], | ||
successData: [ | ||
{ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, | ||
value: { | ||
taxRates: { | ||
taxes: { | ||
[oldTaxCode]: null, | ||
[newTaxCode]: { | ||
...originalTaxRate, | ||
code: newTaxCode, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here - do we need this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. im using it for pending fields and showing errors. we need a key inside the object for which an error or pending action can be associated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah right, thanks! |
||
pendingFields: {code: null}, | ||
pendingAction: null, | ||
errorFields: {code: null}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
], | ||
failureData: [ | ||
{ | ||
onyxMethod: Onyx.METHOD.MERGE, | ||
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, | ||
value: { | ||
taxRates: { | ||
taxes: { | ||
[newTaxCode]: null, | ||
[oldTaxCode]: { | ||
...originalTaxRate, | ||
code: originalTaxRate.code, | ||
pendingFields: {code: null}, | ||
pendingAction: null, | ||
errorFields: {code: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('workspace.taxes.error.updateFailureMessage')}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
const parameters: UpdatePolicyTaxCodeParams = { | ||
policyID, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'll need to pass the taxID (the name) here. We only really use it if the newTaxCode is clear, but you might s well pass it always, for simplicity There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. taxCode is same as taxID right. do you mean that the api expects ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i.e. just a different variable name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
oldTaxCode, | ||
newTaxCode, | ||
taxID: originalTaxRate.name ?? '', | ||
}; | ||
|
||
API.write(WRITE_COMMANDS.UPDATE_POLICY_TAX_CODE, parameters, onyxData); | ||
} | ||
|
||
export { | ||
createPolicyTax, | ||
getNextTaxCode, | ||
|
@@ -471,8 +567,10 @@ export { | |
getTaxValueWithPercentage, | ||
setPolicyTaxesEnabled, | ||
validateTaxName, | ||
validateTaxCode, | ||
validateTaxValue, | ||
deletePolicyTaxes, | ||
updatePolicyTaxValue, | ||
renamePolicyTax, | ||
setPolicyTaxCode, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why can't we use
import INPUT_IDS_TAX_CODE from '@src/types/form/WorkspaceTaxCodeForm';
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because it'll conflict with
import INPUT_IDS from '@src/types/form/WorkspaceNewTaxForm
.The default import name is
INPUT_IDS
for forms