feat(refund credit):add exchange rate muted & branch.

This commit is contained in:
elforjani13
2022-03-07 20:57:20 +02:00
parent 2501626d70
commit b8b95c7929
7 changed files with 169 additions and 68 deletions

View File

@@ -19,6 +19,7 @@ const defaultInitialValues = {
reference_no: '', reference_no: '',
description: '', description: '',
amount: '', amount: '',
exchange_rate: 1,
}; };
/** /**

View File

@@ -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;

View File

@@ -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,16 +38,50 @@ 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}>
<FeatureCan feature={Features.Branches}>
<Row>
<Col xs={5}>
<FormGroup
label={<T id={'branch'} />}
className={classNames('form-group--select-list', Classes.FILL)}
>
<BranchSelect
name={'branch_id'}
branches={branches}
input={BranchSelectButton}
popoverProps={{ minimal: true }}
/>
</FormGroup>
</Col>
</Row>
<BranchRowDivider />
</FeatureCan>
<Row>
<Col xs={5}>
{/* ------------- Refund date ------------- */} {/* ------------- Refund date ------------- */}
<FastField name={'date'}> <FastField name={'date'}>
{({ form, field: { value }, meta: { error, touched } }) => ( {({ form, field: { value }, meta: { error, touched } }) => (
@@ -63,55 +107,8 @@ function RefundCreditNoteFormFields() {
</FormGroup> </FormGroup>
)} )}
</FastField> </FastField>
{/* ------------- Amount ------------- */} </Col>
<FastField name={'amount'}> <Col xs={5}>
{({
form: { values, setFieldValue },
field: { value },
meta: { error, touched },
}) => (
<FormGroup
label={<T id={'refund_credit_note.dialog.amount'} />}
labelInfo={<FieldRequiredHint />}
className={classNames('form-group--amount', CLASSES.FILL)}
intent={inputIntent({ error, touched })}
helperText={<ErrorMessage name="amount" />}
// inline={true}
>
<ControlGroup>
<InputPrependText text={values.currency_code} />
<MoneyInputGroup
value={value}
minimal={true}
onChange={(amount) => {
setFieldValue('amount', amount);
}}
intent={inputIntent({ error, touched })}
inputRef={(ref) => (amountFieldRef.current = ref)}
/>
</ControlGroup>
</FormGroup>
)}
</FastField>
{/* ------------ Reference No. ------------ */}
<FastField name={'reference_no'}>
{({ form, field, meta: { error, touched } }) => (
<FormGroup
label={<T id={'reference_no'} />}
className={classNames('form-group--reference', CLASSES.FILL)}
intent={inputIntent({ error, touched })}
helperText={<ErrorMessage name="reference" />}
// inline={true}
>
<InputGroup
intent={inputIntent({ error, touched })}
minimal={true}
{...field}
/>
</FormGroup>
)}
</FastField>
{/* ------------ Form account ------------ */} {/* ------------ Form account ------------ */}
<FastField name={'from_account_id'}> <FastField name={'from_account_id'}>
{({ form, field: { value }, meta: { error, touched } }) => ( {({ form, field: { value }, meta: { error, touched } }) => (
@@ -125,7 +122,6 @@ function RefundCreditNoteFormFields() {
labelInfo={<FieldRequiredHint />} labelInfo={<FieldRequiredHint />}
intent={inputIntent({ error, touched })} intent={inputIntent({ error, touched })}
helperText={<ErrorMessage name={'from_account_id'} />} helperText={<ErrorMessage name={'from_account_id'} />}
// inline={true}
> >
<AccountsSuggestField <AccountsSuggestField
selectedAccountId={value} selectedAccountId={value}
@@ -145,13 +141,74 @@ function RefundCreditNoteFormFields() {
</FormGroup> </FormGroup>
)} )}
</FastField> </FastField>
</Col>
</Row>
{/* ------------- Amount ------------- */}
<FastField name={'amount'}>
{({
form: { values, setFieldValue },
field: { value },
meta: { error, touched },
}) => (
<FormGroup
label={<T id={'refund_credit_note.dialog.amount'} />}
labelInfo={<FieldRequiredHint />}
className={classNames('form-group--amount', CLASSES.FILL)}
intent={inputIntent({ error, touched })}
helperText={<ErrorMessage name="amount" />}
>
<ControlGroup>
<InputPrependText text={values.currency_code} />
<MoneyInputGroup
value={value}
minimal={true}
onChange={(amount) => {
setFieldValue('amount', amount);
}}
intent={inputIntent({ error, touched })}
inputRef={(ref) => (amountFieldRef.current = ref)}
/>
</ControlGroup>
</FormGroup>
)}
</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. ------------ */}
<FastField name={'reference_no'}>
{({ form, field, meta: { error, touched } }) => (
<FormGroup
label={<T id={'reference_no'} />}
className={classNames('form-group--reference', CLASSES.FILL)}
intent={inputIntent({ error, touched })}
helperText={<ErrorMessage name="reference" />}
>
<InputGroup
intent={inputIntent({ error, touched })}
minimal={true}
{...field}
/>
</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;
`;

View File

@@ -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>
); );

View File

@@ -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]);
};

View File

@@ -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 {

View File

@@ -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 {