Skip to content

Commit

Permalink
fix: add bank rule categories
Browse files Browse the repository at this point in the history
  • Loading branch information
abouolia committed Jul 8, 2024
1 parent 38d4122 commit 73acdb6
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 38 deletions.
9 changes: 9 additions & 0 deletions packages/webapp/src/constants/cashflowOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,12 @@ export const TRANSACRIONS_TYPE = [
'OtherExpense',
'TransferToAccount',
];

export const MoneyCategoryPerCreditAccountRootType = {
OwnerContribution: ['equity'],
OtherIncome: ['income'],
OwnerDrawing: ['equity'],
OtherExpense: ['expense'],
TransferToAccount: ['asset'],
TransferFromAccount: ['asset'],
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @ts-nocheck
import { useCallback, useMemo } from 'react';
import { Form, Formik, FormikHelpers, useFormikContext } from 'formik';
import { Button, Classes, Intent, Radio, Tag } from '@blueprintjs/core';
import * as R from 'ramda';
Expand All @@ -16,11 +17,11 @@ import {
} from '@/components';
import { useCreateBankRule, useEditBankRule } from '@/hooks/query/bank-rules';
import {
AssignTransactionTypeOptions,
FieldCondition,
Fields,
RuleFormValues,
TransactionTypeOptions,
getAccountRootFromMoneyCategory,
initialValues,
} from './_utils';
import { useRuleFormDialogBoot } from './RuleFormBoot';
Expand All @@ -31,6 +32,11 @@ import {
} from '@/utils';
import withDialogActions from '@/containers/Dialog/withDialogActions';
import { DialogsName } from '@/constants/dialogs';
import { getAddMoneyInOptions, getAddMoneyOutOptions } from '@/constants';

// Retrieves the add money in button options.
const MoneyInOptions = getAddMoneyInOptions();
const MoneyOutOptions = getAddMoneyOutOptions();

function RuleFormContentFormRoot({
// #withDialogActions
Expand All @@ -47,7 +53,6 @@ function RuleFormContentFormRoot({
...initialValues,
...transformToForm(transformToCamelCase(bankRule), initialValues),
};

// Handles the form submitting.
const handleSubmit = (
values: RuleFormValues,
Expand Down Expand Up @@ -92,38 +97,32 @@ function RuleFormContentFormRoot({
label={'Rule Name'}
labelInfo={<Tag minimal>Required</Tag>}
style={{ maxWidth: 300 }}
fastField
>
<FInputGroup name={'name'} />
<FInputGroup name={'name'} fastField />
</FFormGroup>

<FFormGroup
name={'applyIfAccountId'}
label={'Apply the rule to account'}
labelInfo={<Tag minimal>Required</Tag>}
style={{ maxWidth: 350 }}
fastField
>
<AccountsSelect
name={'applyIfAccountId'}
items={accounts}
filterByTypes={['cash', 'bank']}
fastField
/>
</FFormGroup>

<FFormGroup
name={'applyIfTransactionType'}
label={'Apply to transactions are'}
style={{ maxWidth: 350 }}
>
<FSelect
name={'applyIfTransactionType'}
items={TransactionTypeOptions}
popoverProps={{ minimal: true, inline: false }}
/>
</FFormGroup>
<RuleApplyIfTransactionTypeField />

<FFormGroup
name={'conditionsType'}
label={'Categorize the transactions when'}
fastField
>
<FRadioGroup name={'conditionsType'}>
<Radio value={'and'} label={'All the following criteria matches'} />
Expand All @@ -139,34 +138,16 @@ function RuleFormContentFormRoot({
Then Assign
</h3>

<FFormGroup
name={'assignCategory'}
label={'Transaction type'}
labelInfo={<Tag minimal>Required</Tag>}
style={{ maxWidth: 300 }}
>
<FSelect
name={'assignCategory'}
items={AssignTransactionTypeOptions}
popoverProps={{ minimal: true, inline: false }}
/>
</FFormGroup>

<FFormGroup
name={'assignAccountId'}
label={'Account category'}
labelInfo={<Tag minimal>Required</Tag>}
style={{ maxWidth: 300 }}
>
<AccountsSelect name={'assignAccountId'} items={accounts} />
</FFormGroup>
<RuleAssignCategoryField />
<RuleAssignCategoryAccountField />

<FFormGroup
name={'assignRef'}
label={'Reference'}
style={{ maxWidth: 300 }}
fastField
>
<FInputGroup name={'assignRef'} />
<FInputGroup name={'assignRef'} fastField />
</FFormGroup>

<RuleFormActions />
Expand Down Expand Up @@ -203,32 +184,37 @@ function RuleFormConditions() {
name={`conditions[${index}].field`}
label={'Field'}
style={{ marginBottom: 0, flex: '1 0' }}
fastField
>
<FSelect
name={`conditions[${index}].field`}
items={Fields}
popoverProps={{ minimal: true, inline: false }}
fastField
/>
</FFormGroup>

<FFormGroup
name={`conditions[${index}].comparator`}
label={'Condition'}
style={{ marginBottom: 0, flex: '1 0' }}
fastField
>
<FSelect
name={`conditions[${index}].comparator`}
items={FieldCondition}
popoverProps={{ minimal: true, inline: false }}
fastField
/>
</FFormGroup>

<FFormGroup
name={`conditions[${index}].value`}
label={'Value'}
style={{ marginBottom: 0, flex: '1 0 ', width: '40%' }}
fastField
>
<FInputGroup name={`conditions[${index}].value`} />
<FInputGroup name={`conditions[${index}].value`} fastField />
</FFormGroup>
</Group>
))}
Expand Down Expand Up @@ -284,3 +270,104 @@ function RuleFormActionsRoot({
}

const RuleFormActions = R.compose(withDialogActions)(RuleFormActionsRoot);

function RuleApplyIfTransactionTypeField() {
const { setFieldValue } = useFormikContext<RuleFormValues>();

const handleItemChange = useCallback(
(item: any) => {
setFieldValue('applyIfTransactionType', item.value);
setFieldValue('assignCategory', '');
setFieldValue('assignAccountId', '');
},
[setFieldValue],
);

return (
<FFormGroup
name={'applyIfTransactionType'}
label={'Apply to transactions are'}
style={{ maxWidth: 350 }}
fastField
>
<FSelect
name={'applyIfTransactionType'}
items={TransactionTypeOptions}
popoverProps={{ minimal: true, inline: false }}
onItemChange={handleItemChange}
fastField
/>
</FFormGroup>
);
}

function RuleAssignCategoryField() {
const { values, setFieldValue } = useFormikContext<RuleFormValues>();

// Retrieves the transaction types if it is deposit or withdrawal.
const transactionTypes = useMemo(
() =>
values?.applyIfTransactionType === 'deposit'
? MoneyInOptions
: MoneyOutOptions,
[values?.applyIfTransactionType],
);

// Handles the select item change.
const handleItemChange = useCallback(
(item: any) => {
setFieldValue('assignCategory', item.value);
setFieldValue('assignAccountId', '');
},
[setFieldValue],
);

return (
<FFormGroup
name={'assignCategory'}
label={'Transaction type'}
labelInfo={<Tag minimal>Required</Tag>}
style={{ maxWidth: 300 }}
fastField
>
<FSelect
name={'assignCategory'}
items={transactionTypes}
popoverProps={{ minimal: true, inline: false }}
valueAccessor={'value'}
textAccessor={'name'}
onItemChange={handleItemChange}
fastField
/>
</FFormGroup>
);
}

function RuleAssignCategoryAccountField() {
const { values } = useFormikContext<RuleFormValues>();
const { accounts } = useRuleFormDialogBoot();

const accountRoot = useMemo(
() => getAccountRootFromMoneyCategory(values.assignCategory),
[values.assignCategory],
);

return (
<FFormGroup
name={'assignAccountId'}
label={'Account category'}
labelInfo={<Tag minimal>Required</Tag>}
style={{ maxWidth: 300 }}
fastField
shouldUpdateDeps={{ accountRoot }}
>
<AccountsSelect
name={'assignAccountId'}
items={accounts}
filterByRootTypes={accountRoot}
shouldUpdateDeps={{ accountRoot }}
fastField
/>
</FFormGroup>
);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { camelCase, get, upperFirst } from 'lodash';
import { MoneyCategoryPerCreditAccountRootType } from '@/constants/cashflowOptions';

export const initialValues = {
name: '',
order: 0,
applyIfAccountId: '',
applyIfTransactionType: '',
applyIfTransactionType: 'deposit',
conditionsType: 'and',
conditions: [
{
Expand Down Expand Up @@ -47,3 +50,9 @@ export const FieldCondition = [
export const AssignTransactionTypeOptions = [
{ value: 'expense', text: 'Expense' },
];

export const getAccountRootFromMoneyCategory = (category: string): string[] => {
const _category = upperFirst(camelCase(category));

return get(MoneyCategoryPerCreditAccountRootType, _category) || [];
};

0 comments on commit 73acdb6

Please sign in to comment.