mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-20 06:40:31 +00:00
feat(refund credit):add exchange rate muted & branch.
This commit is contained in:
@@ -19,6 +19,7 @@ const defaultInitialValues = {
|
|||||||
reference_no: '',
|
reference_no: '',
|
||||||
description: '',
|
description: '',
|
||||||
amount: '',
|
amount: '',
|
||||||
|
exchange_rate: 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -8,5 +8,6 @@ const Schema = Yup.object().shape({
|
|||||||
reference_no: Yup.string().min(1).max(DATATYPES_LENGTH.STRING).nullable(),
|
reference_no: Yup.string().min(1).max(DATATYPES_LENGTH.STRING).nullable(),
|
||||||
from_account_id: Yup.number().required().label(intl.get('deposit_account_')),
|
from_account_id: Yup.number().required().label(intl.get('deposit_account_')),
|
||||||
description: Yup.string().nullable().max(DATATYPES_LENGTH.TEXT),
|
description: Yup.string().nullable().max(DATATYPES_LENGTH.TEXT),
|
||||||
|
exchange_rate: Yup.number(),
|
||||||
});
|
});
|
||||||
export const CreateRefundCreditNoteFormSchema = Schema;
|
export const CreateRefundCreditNoteFormSchema = Schema;
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import styled from 'styled-components';
|
||||||
import intl from 'react-intl-universal';
|
import intl from 'react-intl-universal';
|
||||||
import { FastField, ErrorMessage } from 'formik';
|
import { isEqual } from 'lodash';
|
||||||
|
import { FastField, ErrorMessage, useFormikContext } from 'formik';
|
||||||
import {
|
import {
|
||||||
Classes,
|
Classes,
|
||||||
FormGroup,
|
FormGroup,
|
||||||
@@ -11,14 +13,22 @@ import {
|
|||||||
} from '@blueprintjs/core';
|
} from '@blueprintjs/core';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import { CLASSES } from 'common/classes';
|
import { CLASSES } from 'common/classes';
|
||||||
|
import { Features } from 'common';
|
||||||
import { DateInput } from '@blueprintjs/datetime';
|
import { DateInput } from '@blueprintjs/datetime';
|
||||||
import {
|
import {
|
||||||
Icon,
|
Icon,
|
||||||
|
Col,
|
||||||
|
Row,
|
||||||
|
If,
|
||||||
FieldRequiredHint,
|
FieldRequiredHint,
|
||||||
AccountsSuggestField,
|
AccountsSuggestField,
|
||||||
InputPrependText,
|
InputPrependText,
|
||||||
MoneyInputGroup,
|
MoneyInputGroup,
|
||||||
FormattedMessage as T,
|
FormattedMessage as T,
|
||||||
|
ExchangeRateMutedField,
|
||||||
|
BranchSelect,
|
||||||
|
BranchSelectButton,
|
||||||
|
FeatureCan,
|
||||||
} from 'components';
|
} from 'components';
|
||||||
import {
|
import {
|
||||||
inputIntent,
|
inputIntent,
|
||||||
@@ -28,41 +38,111 @@ import {
|
|||||||
} from 'utils';
|
} from 'utils';
|
||||||
import { useAutofocus } from 'hooks';
|
import { useAutofocus } from 'hooks';
|
||||||
import { ACCOUNT_TYPE } from 'common/accountTypes';
|
import { ACCOUNT_TYPE } from 'common/accountTypes';
|
||||||
|
import { useSetPrimaryBranchToForm } from './utils';
|
||||||
import { useRefundCreditNoteContext } from './RefundCreditNoteFormProvider';
|
import { useRefundCreditNoteContext } from './RefundCreditNoteFormProvider';
|
||||||
|
|
||||||
|
import withCurrentOrganization from 'containers/Organization/withCurrentOrganization';
|
||||||
|
import { compose } from 'utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Refund credit note form fields.
|
* Refund credit note form fields.
|
||||||
*/
|
*/
|
||||||
function RefundCreditNoteFormFields() {
|
function RefundCreditNoteFormFields({
|
||||||
const { accounts } = useRefundCreditNoteContext();
|
// #withCurrentOrganization
|
||||||
|
organization: { base_currency },
|
||||||
|
}) {
|
||||||
|
const { accounts, branches } = useRefundCreditNoteContext();
|
||||||
|
const { values } = useFormikContext();
|
||||||
|
|
||||||
const amountFieldRef = useAutofocus();
|
const amountFieldRef = useAutofocus();
|
||||||
|
|
||||||
|
// Sets the primary branch to form.
|
||||||
|
useSetPrimaryBranchToForm();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={Classes.DIALOG_BODY}>
|
<div className={Classes.DIALOG_BODY}>
|
||||||
{/* ------------- Refund date ------------- */}
|
<FeatureCan feature={Features.Branches}>
|
||||||
<FastField name={'date'}>
|
<Row>
|
||||||
{({ form, field: { value }, meta: { error, touched } }) => (
|
<Col xs={5}>
|
||||||
<FormGroup
|
<FormGroup
|
||||||
label={<T id={'refund_credit_note.dialog.refund_date'} />}
|
label={<T id={'branch'} />}
|
||||||
labelInfo={<FieldRequiredHint />}
|
className={classNames('form-group--select-list', Classes.FILL)}
|
||||||
className={classNames('form-group--select-list', CLASSES.FILL)}
|
>
|
||||||
intent={inputIntent({ error, touched })}
|
<BranchSelect
|
||||||
helperText={<ErrorMessage name="date" />}
|
name={'branch_id'}
|
||||||
// inline={true}
|
branches={branches}
|
||||||
>
|
input={BranchSelectButton}
|
||||||
<DateInput
|
popoverProps={{ minimal: true }}
|
||||||
{...momentFormatter('YYYY/MM/DD')}
|
/>
|
||||||
value={tansformDateValue(value)}
|
</FormGroup>
|
||||||
onChange={handleDateChange((formattedDate) => {
|
</Col>
|
||||||
form.setFieldValue('date', formattedDate);
|
</Row>
|
||||||
})}
|
<BranchRowDivider />
|
||||||
popoverProps={{ position: Position.BOTTOM, minimal: true }}
|
</FeatureCan>
|
||||||
inputProps={{
|
|
||||||
leftIcon: <Icon icon={'date-range'} />,
|
<Row>
|
||||||
}}
|
<Col xs={5}>
|
||||||
/>
|
{/* ------------- Refund date ------------- */}
|
||||||
</FormGroup>
|
<FastField name={'date'}>
|
||||||
)}
|
{({ form, field: { value }, meta: { error, touched } }) => (
|
||||||
</FastField>
|
<FormGroup
|
||||||
|
label={<T id={'refund_credit_note.dialog.refund_date'} />}
|
||||||
|
labelInfo={<FieldRequiredHint />}
|
||||||
|
className={classNames('form-group--select-list', CLASSES.FILL)}
|
||||||
|
intent={inputIntent({ error, touched })}
|
||||||
|
helperText={<ErrorMessage name="date" />}
|
||||||
|
// inline={true}
|
||||||
|
>
|
||||||
|
<DateInput
|
||||||
|
{...momentFormatter('YYYY/MM/DD')}
|
||||||
|
value={tansformDateValue(value)}
|
||||||
|
onChange={handleDateChange((formattedDate) => {
|
||||||
|
form.setFieldValue('date', formattedDate);
|
||||||
|
})}
|
||||||
|
popoverProps={{ position: Position.BOTTOM, minimal: true }}
|
||||||
|
inputProps={{
|
||||||
|
leftIcon: <Icon icon={'date-range'} />,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</FormGroup>
|
||||||
|
)}
|
||||||
|
</FastField>
|
||||||
|
</Col>
|
||||||
|
<Col xs={5}>
|
||||||
|
{/* ------------ Form account ------------ */}
|
||||||
|
<FastField name={'from_account_id'}>
|
||||||
|
{({ form, field: { value }, meta: { error, touched } }) => (
|
||||||
|
<FormGroup
|
||||||
|
label={<T id={'refund_credit_note.dialog.from_account'} />}
|
||||||
|
className={classNames(
|
||||||
|
'form-group--from_account_id',
|
||||||
|
'form-group--select-list',
|
||||||
|
CLASSES.FILL,
|
||||||
|
)}
|
||||||
|
labelInfo={<FieldRequiredHint />}
|
||||||
|
intent={inputIntent({ error, touched })}
|
||||||
|
helperText={<ErrorMessage name={'from_account_id'} />}
|
||||||
|
>
|
||||||
|
<AccountsSuggestField
|
||||||
|
selectedAccountId={value}
|
||||||
|
accounts={accounts}
|
||||||
|
onAccountSelected={({ id }) =>
|
||||||
|
form.setFieldValue('from_account_id', id)
|
||||||
|
}
|
||||||
|
inputProps={{
|
||||||
|
placeholder: intl.get('select_account'),
|
||||||
|
}}
|
||||||
|
filterByTypes={[
|
||||||
|
ACCOUNT_TYPE.BANK,
|
||||||
|
ACCOUNT_TYPE.CASH,
|
||||||
|
ACCOUNT_TYPE.FIXED_ASSET,
|
||||||
|
]}
|
||||||
|
/>
|
||||||
|
</FormGroup>
|
||||||
|
)}
|
||||||
|
</FastField>
|
||||||
|
</Col>
|
||||||
|
</Row>
|
||||||
{/* ------------- Amount ------------- */}
|
{/* ------------- Amount ------------- */}
|
||||||
<FastField name={'amount'}>
|
<FastField name={'amount'}>
|
||||||
{({
|
{({
|
||||||
@@ -76,7 +156,6 @@ function RefundCreditNoteFormFields() {
|
|||||||
className={classNames('form-group--amount', CLASSES.FILL)}
|
className={classNames('form-group--amount', CLASSES.FILL)}
|
||||||
intent={inputIntent({ error, touched })}
|
intent={inputIntent({ error, touched })}
|
||||||
helperText={<ErrorMessage name="amount" />}
|
helperText={<ErrorMessage name="amount" />}
|
||||||
// inline={true}
|
|
||||||
>
|
>
|
||||||
<ControlGroup>
|
<ControlGroup>
|
||||||
<InputPrependText text={values.currency_code} />
|
<InputPrependText text={values.currency_code} />
|
||||||
@@ -93,6 +172,19 @@ function RefundCreditNoteFormFields() {
|
|||||||
</FormGroup>
|
</FormGroup>
|
||||||
)}
|
)}
|
||||||
</FastField>
|
</FastField>
|
||||||
|
|
||||||
|
<If condition={!isEqual(base_currency, values.currency_code)}>
|
||||||
|
{/*------------ exchange rate -----------*/}
|
||||||
|
<ExchangeRateMutedField
|
||||||
|
name={'exchange_rate'}
|
||||||
|
fromCurrency={base_currency}
|
||||||
|
toCurrency={values.currency_code}
|
||||||
|
formGroupProps={{ label: '', inline: false }}
|
||||||
|
date={values.date}
|
||||||
|
exchangeRate={values.exchange_rate}
|
||||||
|
/>
|
||||||
|
</If>
|
||||||
|
|
||||||
{/* ------------ Reference No. ------------ */}
|
{/* ------------ Reference No. ------------ */}
|
||||||
<FastField name={'reference_no'}>
|
<FastField name={'reference_no'}>
|
||||||
{({ form, field, meta: { error, touched } }) => (
|
{({ form, field, meta: { error, touched } }) => (
|
||||||
@@ -101,7 +193,6 @@ function RefundCreditNoteFormFields() {
|
|||||||
className={classNames('form-group--reference', CLASSES.FILL)}
|
className={classNames('form-group--reference', CLASSES.FILL)}
|
||||||
intent={inputIntent({ error, touched })}
|
intent={inputIntent({ error, touched })}
|
||||||
helperText={<ErrorMessage name="reference" />}
|
helperText={<ErrorMessage name="reference" />}
|
||||||
// inline={true}
|
|
||||||
>
|
>
|
||||||
<InputGroup
|
<InputGroup
|
||||||
intent={inputIntent({ error, touched })}
|
intent={inputIntent({ error, touched })}
|
||||||
@@ -112,46 +203,12 @@ function RefundCreditNoteFormFields() {
|
|||||||
)}
|
)}
|
||||||
</FastField>
|
</FastField>
|
||||||
|
|
||||||
{/* ------------ Form account ------------ */}
|
|
||||||
<FastField name={'from_account_id'}>
|
|
||||||
{({ form, field: { value }, meta: { error, touched } }) => (
|
|
||||||
<FormGroup
|
|
||||||
label={<T id={'refund_credit_note.dialog.from_account'} />}
|
|
||||||
className={classNames(
|
|
||||||
'form-group--from_account_id',
|
|
||||||
'form-group--select-list',
|
|
||||||
CLASSES.FILL,
|
|
||||||
)}
|
|
||||||
labelInfo={<FieldRequiredHint />}
|
|
||||||
intent={inputIntent({ error, touched })}
|
|
||||||
helperText={<ErrorMessage name={'from_account_id'} />}
|
|
||||||
// inline={true}
|
|
||||||
>
|
|
||||||
<AccountsSuggestField
|
|
||||||
selectedAccountId={value}
|
|
||||||
accounts={accounts}
|
|
||||||
onAccountSelected={({ id }) =>
|
|
||||||
form.setFieldValue('from_account_id', id)
|
|
||||||
}
|
|
||||||
inputProps={{
|
|
||||||
placeholder: intl.get('select_account'),
|
|
||||||
}}
|
|
||||||
filterByTypes={[
|
|
||||||
ACCOUNT_TYPE.BANK,
|
|
||||||
ACCOUNT_TYPE.CASH,
|
|
||||||
ACCOUNT_TYPE.FIXED_ASSET,
|
|
||||||
]}
|
|
||||||
/>
|
|
||||||
</FormGroup>
|
|
||||||
)}
|
|
||||||
</FastField>
|
|
||||||
{/* --------- Statement --------- */}
|
{/* --------- Statement --------- */}
|
||||||
<FastField name={'description'}>
|
<FastField name={'description'}>
|
||||||
{({ form, field, meta: { error, touched } }) => (
|
{({ form, field, meta: { error, touched } }) => (
|
||||||
<FormGroup
|
<FormGroup
|
||||||
label={<T id={'refund_credit_note.dialog.description'} />}
|
label={<T id={'refund_credit_note.dialog.description'} />}
|
||||||
className={'form-group--description'}
|
className={'form-group--description'}
|
||||||
// inline={true}
|
|
||||||
>
|
>
|
||||||
<TextArea growVertically={true} {...field} />
|
<TextArea growVertically={true} {...field} />
|
||||||
</FormGroup>
|
</FormGroup>
|
||||||
@@ -161,4 +218,10 @@ function RefundCreditNoteFormFields() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default RefundCreditNoteFormFields;
|
export default compose(withCurrentOrganization())(RefundCreditNoteFormFields);
|
||||||
|
|
||||||
|
export const BranchRowDivider = styled.div`
|
||||||
|
height: 1px;
|
||||||
|
background: #ebf1f6;
|
||||||
|
margin-bottom: 13px;
|
||||||
|
`;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { pick } from 'lodash';
|
|||||||
import {
|
import {
|
||||||
useAccounts,
|
useAccounts,
|
||||||
useCreditNote,
|
useCreditNote,
|
||||||
|
useBranches,
|
||||||
useCreateRefundCreditNote,
|
useCreateRefundCreditNote,
|
||||||
} from 'hooks/query';
|
} from 'hooks/query';
|
||||||
|
|
||||||
@@ -24,6 +25,14 @@ function RefundCreditNoteFormProvider({ creditNoteId, dialogName, ...props }) {
|
|||||||
enabled: !!creditNoteId,
|
enabled: !!creditNoteId,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Fetches the branches list.
|
||||||
|
const {
|
||||||
|
data: branches,
|
||||||
|
isLoading: isBranchesLoading,
|
||||||
|
isSuccess: isBranchesSuccess,
|
||||||
|
} = useBranches();
|
||||||
|
|
||||||
// Create and edit credit note mutations.
|
// Create and edit credit note mutations.
|
||||||
const { mutateAsync: createRefundCreditNoteMutate } =
|
const { mutateAsync: createRefundCreditNoteMutate } =
|
||||||
useCreateRefundCreditNote();
|
useCreateRefundCreditNote();
|
||||||
@@ -35,12 +44,17 @@ function RefundCreditNoteFormProvider({ creditNoteId, dialogName, ...props }) {
|
|||||||
amount: creditNote.credits_remaining,
|
amount: creditNote.credits_remaining,
|
||||||
},
|
},
|
||||||
accounts,
|
accounts,
|
||||||
|
branches,
|
||||||
dialogName,
|
dialogName,
|
||||||
|
isBranchesSuccess,
|
||||||
|
|
||||||
createRefundCreditNoteMutate,
|
createRefundCreditNoteMutate,
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DialogContent isLoading={isAccountsLoading || isCreditNoteLoading}>
|
<DialogContent
|
||||||
|
isLoading={isAccountsLoading || isCreditNoteLoading || isBranchesLoading}
|
||||||
|
>
|
||||||
<RefundCreditNoteContext.Provider value={provider} {...props} />
|
<RefundCreditNoteContext.Provider value={provider} {...props} />
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { useFormikContext } from 'formik';
|
||||||
|
import { isEqual, isUndefined, isNull, first } from 'lodash';
|
||||||
|
|
||||||
|
import { useRefundCreditNoteContext } from './RefundCreditNoteFormProvider';
|
||||||
|
|
||||||
|
export const useSetPrimaryBranchToForm = () => {
|
||||||
|
const { setFieldValue } = useFormikContext();
|
||||||
|
const { branches, isBranchesSuccess } = useRefundCreditNoteContext();
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
if (isBranchesSuccess) {
|
||||||
|
const primaryBranch = branches.find((b) => b.primary) || first(branches);
|
||||||
|
|
||||||
|
if (primaryBranch) {
|
||||||
|
setFieldValue('branch_id', primaryBranch.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [isBranchesSuccess, setFieldValue, branches]);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
.dialog--refund-credit-note {
|
.dialog--refund-credit-note {
|
||||||
.bp3-dialog-body {
|
.bp3-dialog-body {
|
||||||
.bp3-form-group {
|
.bp3-form-group {
|
||||||
margin-bottom: 16px;
|
margin-bottom: 13px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.form-group {
|
.form-group {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
.bp3-dialog-body {
|
.bp3-dialog-body {
|
||||||
.bp3-form-group {
|
.bp3-form-group {
|
||||||
margin-bottom: 16px;
|
margin-bottom: 13px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.form-group {
|
.form-group {
|
||||||
|
|||||||
Reference in New Issue
Block a user