mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 21:00:31 +00:00
re-structure to monorepo.
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import classNames from 'classnames';
|
||||
import { useHistory } from 'react-router-dom';
|
||||
import { useFormikContext } from 'formik';
|
||||
import {
|
||||
Intent,
|
||||
Button,
|
||||
ButtonGroup,
|
||||
Popover,
|
||||
PopoverInteractionKind,
|
||||
Position,
|
||||
Menu,
|
||||
MenuItem,
|
||||
} from '@blueprintjs/core';
|
||||
import { If, Icon, FormattedMessage as T } from '@/components';
|
||||
import { CLASSES } from '@/constants/classes';
|
||||
import { useVendorCreditNoteFormContext } from './VendorCreditNoteFormProvider';
|
||||
|
||||
/**
|
||||
* Purchases Credit note floating actions.
|
||||
*/
|
||||
export default function VendorCreditNoteFloatingActions() {
|
||||
const history = useHistory();
|
||||
|
||||
// Formik context.
|
||||
const { resetForm, submitForm, isSubmitting } = useFormikContext();
|
||||
|
||||
// Credit note form context.
|
||||
const { setSubmitPayload, vendorCredit } = useVendorCreditNoteFormContext();
|
||||
|
||||
// Handle submit as open button click.
|
||||
const handleSubmitOpenBtnClick = (event) => {
|
||||
setSubmitPayload({ redirect: true, open: true });
|
||||
submitForm();
|
||||
};
|
||||
|
||||
// Handle submit, open and anothe new button click.
|
||||
const handleSubmitOpenAndNewBtnClick = (event) => {
|
||||
setSubmitPayload({ redirect: false, open: true, resetForm: true });
|
||||
submitForm();
|
||||
};
|
||||
|
||||
// Handle submit as open & continue editing button click.
|
||||
const handleSubmitOpenContinueEditingBtnClick = (event) => {
|
||||
setSubmitPayload({ redirect: false, open: true });
|
||||
submitForm();
|
||||
};
|
||||
// Handle submit as draft button click.
|
||||
const handleSubmitDraftBtnClick = (event) => {
|
||||
setSubmitPayload({ redirect: true, open: false });
|
||||
submitForm();
|
||||
};
|
||||
|
||||
// handle submit as draft & new button click.
|
||||
const handleSubmitDraftAndNewBtnClick = (event) => {
|
||||
setSubmitPayload({ redirect: false, open: false, resetForm: true });
|
||||
submitForm();
|
||||
};
|
||||
|
||||
// Handle submit as draft & continue editing button click.
|
||||
const handleSubmitDraftContinueEditingBtnClick = (event) => {
|
||||
setSubmitPayload({ redirect: false, open: false });
|
||||
submitForm();
|
||||
};
|
||||
|
||||
// Handle cancel button click.
|
||||
const handleCancelBtnClick = (event) => {
|
||||
history.goBack();
|
||||
};
|
||||
|
||||
const handleClearBtnClick = (event) => {
|
||||
resetForm();
|
||||
};
|
||||
return (
|
||||
<div className={classNames(CLASSES.PAGE_FORM_FLOATING_ACTIONS)}>
|
||||
{/* ----------- Save And Open ----------- */}
|
||||
<If condition={!vendorCredit || !vendorCredit?.is_open}>
|
||||
<ButtonGroup>
|
||||
<Button
|
||||
disabled={isSubmitting}
|
||||
loading={isSubmitting}
|
||||
intent={Intent.PRIMARY}
|
||||
onClick={handleSubmitOpenBtnClick}
|
||||
text={<T id={'save_open'} />}
|
||||
/>
|
||||
<Popover
|
||||
content={
|
||||
<Menu>
|
||||
<MenuItem
|
||||
text={<T id={'open_and_new'} />}
|
||||
onClick={handleSubmitOpenAndNewBtnClick}
|
||||
/>
|
||||
<MenuItem
|
||||
text={<T id={'open_continue_editing'} />}
|
||||
onClick={handleSubmitOpenContinueEditingBtnClick}
|
||||
/>
|
||||
</Menu>
|
||||
}
|
||||
minimal={true}
|
||||
interactionKind={PopoverInteractionKind.CLICK}
|
||||
position={Position.BOTTOM_LEFT}
|
||||
>
|
||||
<Button
|
||||
disabled={isSubmitting}
|
||||
intent={Intent.PRIMARY}
|
||||
rightIcon={<Icon icon="arrow-drop-up-16" iconSize={20} />}
|
||||
/>
|
||||
</Popover>
|
||||
</ButtonGroup>
|
||||
{/* ----------- Save As Draft ----------- */}
|
||||
<ButtonGroup>
|
||||
<Button
|
||||
disabled={isSubmitting}
|
||||
className={'ml1'}
|
||||
onClick={handleSubmitDraftBtnClick}
|
||||
text={<T id={'save_as_draft'} />}
|
||||
/>
|
||||
<Popover
|
||||
content={
|
||||
<Menu>
|
||||
<MenuItem
|
||||
text={<T id={'save_and_new'} />}
|
||||
onClick={handleSubmitDraftAndNewBtnClick}
|
||||
/>
|
||||
<MenuItem
|
||||
text={<T id={'save_continue_editing'} />}
|
||||
onClick={handleSubmitDraftContinueEditingBtnClick}
|
||||
/>
|
||||
</Menu>
|
||||
}
|
||||
minimal={true}
|
||||
interactionKind={PopoverInteractionKind.CLICK}
|
||||
position={Position.BOTTOM_LEFT}
|
||||
>
|
||||
<Button
|
||||
disabled={isSubmitting}
|
||||
rightIcon={<Icon icon="arrow-drop-up-16" iconSize={20} />}
|
||||
/>
|
||||
</Popover>
|
||||
</ButtonGroup>
|
||||
</If>
|
||||
{/* ----------- Save and New ----------- */}
|
||||
<If condition={vendorCredit && vendorCredit?.is_open}>
|
||||
<ButtonGroup>
|
||||
<Button
|
||||
loading={isSubmitting}
|
||||
intent={Intent.PRIMARY}
|
||||
onClick={handleSubmitOpenBtnClick}
|
||||
style={{ minWidth: '85px' }}
|
||||
text={<T id={'save'} />}
|
||||
/>
|
||||
<Popover
|
||||
content={
|
||||
<Menu>
|
||||
<MenuItem
|
||||
text={<T id={'save_and_new'} />}
|
||||
onClick={handleSubmitOpenAndNewBtnClick}
|
||||
/>
|
||||
</Menu>
|
||||
}
|
||||
minimal={true}
|
||||
interactionKind={PopoverInteractionKind.CLICK}
|
||||
position={Position.BOTTOM_LEFT}
|
||||
>
|
||||
<Button
|
||||
disabled={isSubmitting}
|
||||
intent={Intent.PRIMARY}
|
||||
rightIcon={<Icon icon="arrow-drop-up-16" iconSize={20} />}
|
||||
/>
|
||||
</Popover>
|
||||
</ButtonGroup>
|
||||
</If>
|
||||
{/* ----------- Clear & Reset----------- */}
|
||||
<Button
|
||||
className={'ml1'}
|
||||
disabled={isSubmitting}
|
||||
onClick={handleClearBtnClick}
|
||||
text={vendorCredit ? <T id={'reset'} /> : <T id={'clear'} />}
|
||||
/>
|
||||
{/* ----------- Cancel ----------- */}
|
||||
<Button
|
||||
className={'ml1'}
|
||||
disabled={isSubmitting}
|
||||
onClick={handleCancelBtnClick}
|
||||
text={<T id={'cancel'} />}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// @ts-nocheck
|
||||
import * as Yup from 'yup';
|
||||
import intl from 'react-intl-universal';
|
||||
import { DATATYPES_LENGTH } from '@/constants/dataTypes';
|
||||
import { isBlank } from '@/utils';
|
||||
|
||||
const getSchema = Yup.object().shape({
|
||||
vendor_id: Yup.number().required().label(intl.get('vendor_name_')),
|
||||
vendor_credit_date: Yup.date().required().label(intl.get('bill_date_')),
|
||||
vendor_credit_number: Yup.string()
|
||||
.max(DATATYPES_LENGTH.STRING)
|
||||
.label(intl.get('bill_number_')),
|
||||
reference_no: Yup.string().nullable().min(1).max(DATATYPES_LENGTH.STRING),
|
||||
note: Yup.string()
|
||||
.trim()
|
||||
.min(1)
|
||||
.max(DATATYPES_LENGTH.TEXT)
|
||||
.label(intl.get('note')),
|
||||
open: Yup.boolean(),
|
||||
branch_id: Yup.string(),
|
||||
warehouse_id: Yup.string(),
|
||||
exchange_rate: Yup.number(),
|
||||
entries: Yup.array().of(
|
||||
Yup.object().shape({
|
||||
quantity: Yup.number()
|
||||
.nullable()
|
||||
.max(DATATYPES_LENGTH.INT_10)
|
||||
.when(['rate'], {
|
||||
is: (rate) => rate,
|
||||
then: Yup.number().required(),
|
||||
}),
|
||||
rate: Yup.number().nullable().max(DATATYPES_LENGTH.INT_10),
|
||||
item_id: Yup.number()
|
||||
.nullable()
|
||||
.when(['quantity', 'rate'], {
|
||||
is: (quantity, rate) => !isBlank(quantity) && !isBlank(rate),
|
||||
then: Yup.number().required(),
|
||||
}),
|
||||
discount: Yup.number().nullable().min(0).max(DATATYPES_LENGTH.INT_10),
|
||||
description: Yup.string().nullable().max(DATATYPES_LENGTH.TEXT),
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
export const CreateCreditNoteFormSchema = getSchema;
|
||||
export const EditCreditNoteFormSchema = getSchema;
|
||||
@@ -0,0 +1,176 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import intl from 'react-intl-universal';
|
||||
import classNames from 'classnames';
|
||||
import { useHistory } from 'react-router-dom';
|
||||
import { Formik, Form } from 'formik';
|
||||
import { Intent } from '@blueprintjs/core';
|
||||
import { isEmpty } from 'lodash';
|
||||
import { CLASSES } from '@/constants/classes';
|
||||
import {
|
||||
CreateCreditNoteFormSchema,
|
||||
EditCreditNoteFormSchema,
|
||||
} from './VendorCreditNoteForm.schema';
|
||||
|
||||
import VendorCreditNoteFormHeader from './VendorCreditNoteFormHeader';
|
||||
import VendorCreditNoteItemsEntriesEditor from './VendorCreditNoteItemsEntriesEditor';
|
||||
import VendorCreditNoteFormFooter from './VendorCreditNoteFormFooter';
|
||||
import VendorCreditNoteFloatingActions from './VendorCreditNoteFloatingActions';
|
||||
import VendorCreditNoteFormDialogs from './VendorCreditNoteFormDialogs';
|
||||
import VendorCreditNoteFormTopBar from './VendorCreditNoteFormTopBar';
|
||||
|
||||
import { useVendorCreditNoteFormContext } from './VendorCreditNoteFormProvider';
|
||||
|
||||
import { AppToaster } from '@/components';
|
||||
import { compose, safeSumBy, transactionNumber } from '@/utils';
|
||||
import {
|
||||
defaultVendorsCreditNote,
|
||||
filterNonZeroEntries,
|
||||
transformToEditForm,
|
||||
transformFormValuesToRequest,
|
||||
} from './utils';
|
||||
|
||||
import withSettings from '@/containers/Settings/withSettings';
|
||||
import withCurrentOrganization from '@/containers/Organization/withCurrentOrganization';
|
||||
|
||||
/**
|
||||
* Vendor Credit note form.
|
||||
*/
|
||||
function VendorCreditNoteForm({
|
||||
// #withSettings
|
||||
vendorcreditAutoIncrement,
|
||||
vendorcreditNumberPrefix,
|
||||
vendorcreditNextNumber,
|
||||
|
||||
// #withCurrentOrganization
|
||||
organization: { base_currency },
|
||||
}) {
|
||||
const history = useHistory();
|
||||
|
||||
// Vendor Credit note form context.
|
||||
const {
|
||||
isNewMode,
|
||||
submitPayload,
|
||||
vendorCredit,
|
||||
newVendorCredit,
|
||||
createVendorCreditMutate,
|
||||
editVendorCreditMutate,
|
||||
} = useVendorCreditNoteFormContext();
|
||||
|
||||
// Credit number.
|
||||
const vendorCreditNumber = transactionNumber(
|
||||
vendorcreditNumberPrefix,
|
||||
vendorcreditNextNumber,
|
||||
);
|
||||
|
||||
// Initial values.
|
||||
const initialValues = React.useMemo(
|
||||
() => ({
|
||||
...(!isEmpty(vendorCredit)
|
||||
? {
|
||||
...transformToEditForm(vendorCredit),
|
||||
}
|
||||
: {
|
||||
...defaultVendorsCreditNote,
|
||||
...(vendorcreditAutoIncrement && {
|
||||
vendor_credit_number: vendorCreditNumber,
|
||||
}),
|
||||
currency_code: base_currency,
|
||||
...newVendorCredit,
|
||||
}),
|
||||
}),
|
||||
[vendorCredit, base_currency],
|
||||
);
|
||||
|
||||
// Handles form submit.
|
||||
const handleFormSubmit = (
|
||||
values,
|
||||
{ setSubmitting, setErrors, resetForm },
|
||||
) => {
|
||||
const entries = filterNonZeroEntries(values.entries);
|
||||
const totalQuantity = safeSumBy(entries, 'quantity');
|
||||
|
||||
if (totalQuantity === 0) {
|
||||
AppToaster.show({
|
||||
message: intl.get('quantity_cannot_be_zero_or_empty'),
|
||||
intent: Intent.DANGER,
|
||||
});
|
||||
setSubmitting(false);
|
||||
return;
|
||||
}
|
||||
const form = {
|
||||
...transformFormValuesToRequest(values),
|
||||
open: submitPayload.open,
|
||||
};
|
||||
// Handle the request success.
|
||||
const onSuccess = (response) => {
|
||||
AppToaster.show({
|
||||
message: intl.get(
|
||||
isNewMode
|
||||
? 'vendor_credits.success_message'
|
||||
: 'vendor_credits.edit_success_message',
|
||||
),
|
||||
intent: Intent.SUCCESS,
|
||||
});
|
||||
setSubmitting(false);
|
||||
|
||||
if (submitPayload.redirect) {
|
||||
history.push('/vendor-credits');
|
||||
}
|
||||
if (submitPayload.resetForm) {
|
||||
resetForm();
|
||||
}
|
||||
};
|
||||
// Handle the request error.
|
||||
const onError = ({
|
||||
response: {
|
||||
data: { errors },
|
||||
},
|
||||
}) => {
|
||||
setSubmitting(false);
|
||||
};
|
||||
if (isNewMode) {
|
||||
createVendorCreditMutate(form).then(onSuccess).catch(onError);
|
||||
} else {
|
||||
editVendorCreditMutate([vendorCredit.id, form])
|
||||
.then(onSuccess)
|
||||
.catch(onError);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
className={classNames(
|
||||
CLASSES.PAGE_FORM,
|
||||
CLASSES.PAGE_FORM_STRIP_STYLE,
|
||||
CLASSES.PAGE_FORM_VENDOR_CREDIT_NOTE,
|
||||
)}
|
||||
>
|
||||
<Formik
|
||||
validationSchema={
|
||||
isNewMode ? CreateCreditNoteFormSchema : EditCreditNoteFormSchema
|
||||
}
|
||||
initialValues={initialValues}
|
||||
onSubmit={handleFormSubmit}
|
||||
>
|
||||
<Form>
|
||||
<VendorCreditNoteFormTopBar />
|
||||
<VendorCreditNoteFormHeader />
|
||||
<VendorCreditNoteItemsEntriesEditor />
|
||||
<VendorCreditNoteFormFooter />
|
||||
<VendorCreditNoteFloatingActions />
|
||||
<VendorCreditNoteFormDialogs />
|
||||
</Form>
|
||||
</Formik>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(
|
||||
withSettings(({ vendorsCreditNoteSetting }) => ({
|
||||
vendorcreditAutoIncrement: vendorsCreditNoteSetting?.autoIncrement,
|
||||
vendorcreditNextNumber: vendorsCreditNoteSetting?.nextNumber,
|
||||
vendorcreditNumberPrefix: vendorsCreditNoteSetting?.numberPrefix,
|
||||
})),
|
||||
withCurrentOrganization(),
|
||||
)(VendorCreditNoteForm);
|
||||
@@ -0,0 +1,22 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import { BaseCurrency, BaseCurrencyRoot } from '@/components';
|
||||
import { useVendorCreditNoteFormContext } from './VendorCreditNoteFormProvider';
|
||||
|
||||
/**
|
||||
* Vendor credit note currency tag.
|
||||
* @returns
|
||||
*/
|
||||
export default function VendorCreditNoteFormCurrencyTag() {
|
||||
const { isForeignVendor, selectVendor } = useVendorCreditNoteFormContext();
|
||||
|
||||
if (!isForeignVendor) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<BaseCurrencyRoot>
|
||||
<BaseCurrency currency={selectVendor?.currency_code} />
|
||||
</BaseCurrencyRoot>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import VendorCreditNumberDialog from '@/containers/Dialogs/VendorCreditNumberDialog';
|
||||
import { useFormikContext } from 'formik';
|
||||
|
||||
/**
|
||||
* Vendor credit form dialog.
|
||||
*/
|
||||
export default function VendorCreditNoteFormDialogs() {
|
||||
// Update the form once the vendor credit number form submit confirm.
|
||||
const handleVendorCreditNumberFormConfirm = ({
|
||||
incrementNumber,
|
||||
manually,
|
||||
}) => {
|
||||
setFieldValue('vendor_credit_number', incrementNumber || '');
|
||||
setFieldValue('vendor_credit_no_manually', manually);
|
||||
};
|
||||
const { setFieldValue } = useFormikContext();
|
||||
|
||||
return (
|
||||
<React.Fragment>
|
||||
<VendorCreditNumberDialog
|
||||
dialogName={'vendor-credit-form'}
|
||||
onConfirm={handleVendorCreditNumberFormConfirm}
|
||||
/>
|
||||
</React.Fragment>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import classNames from 'classnames';
|
||||
import styled from 'styled-components';
|
||||
|
||||
import { CLASSES } from '@/constants/classes';
|
||||
import { Row, Col, Paper } from '@/components';
|
||||
import { VendorCreditNoteFormFooterLeft } from './VendorCreditNoteFormFooterLeft';
|
||||
import { VendorCreditNoteFormFooterRight } from './VendorCreditNoteFormFooterRight';
|
||||
|
||||
/**
|
||||
* Vendor Credit note form footer.
|
||||
*/
|
||||
export default function VendorCreditNoteFormFooter() {
|
||||
return (
|
||||
<div class={classNames(CLASSES.PAGE_FORM_FOOTER)}>
|
||||
<VendorCreditNoteFooterPaper>
|
||||
<Row>
|
||||
<Col md={8}>
|
||||
<VendorCreditNoteFormFooterLeft />
|
||||
</Col>
|
||||
|
||||
<Col md={4}>
|
||||
<VendorCreditNoteFormFooterRight />
|
||||
</Col>
|
||||
</Row>
|
||||
</VendorCreditNoteFooterPaper>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const VendorCreditNoteFooterPaper = styled(Paper)`
|
||||
padding: 20px;
|
||||
`;
|
||||
@@ -0,0 +1,34 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import intl from 'react-intl-universal';
|
||||
import styled from 'styled-components';
|
||||
import { FFormGroup, FEditableText, FormattedMessage as T } from '@/components';
|
||||
|
||||
export function VendorCreditNoteFormFooterLeft() {
|
||||
return (
|
||||
<React.Fragment>
|
||||
{/* --------- Terms and conditions --------- */}
|
||||
<TermsConditsFormGroup
|
||||
label={<T id={'vendor_credit_form.label.note'} />}
|
||||
name={'note'}
|
||||
>
|
||||
<FEditableText
|
||||
name={'note'}
|
||||
placeholder={intl.get('vendor_credit_form.note.placeholder')}
|
||||
/>
|
||||
</TermsConditsFormGroup>
|
||||
</React.Fragment>
|
||||
);
|
||||
}
|
||||
|
||||
const TermsConditsFormGroup = styled(FFormGroup)`
|
||||
&.bp3-form-group {
|
||||
.bp3-label {
|
||||
font-size: 12px;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
.bp3-form-content {
|
||||
margin-left: 10px;
|
||||
}
|
||||
}
|
||||
`;
|
||||
@@ -0,0 +1,38 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import styled from 'styled-components';
|
||||
import {
|
||||
T,
|
||||
TotalLines,
|
||||
TotalLine,
|
||||
TotalLineBorderStyle,
|
||||
TotalLineTextStyle,
|
||||
} from '@/components';
|
||||
import { useVendorCrditNoteTotals } from './utils';
|
||||
|
||||
export function VendorCreditNoteFormFooterRight() {
|
||||
const { formattedSubtotal, formattedTotal } = useVendorCrditNoteTotals();
|
||||
|
||||
return (
|
||||
<VendorCreditNoteTotalLines
|
||||
labelColWidth={'180px'}
|
||||
amountColWidth={'180px'}
|
||||
>
|
||||
<TotalLine
|
||||
title={<T id={'vendor_credit_form.label.subtotal'} />}
|
||||
value={formattedSubtotal}
|
||||
borderStyle={TotalLineBorderStyle.None}
|
||||
/>
|
||||
<TotalLine
|
||||
title={<T id={'vendor_credit_form.label.total'} />}
|
||||
value={formattedTotal}
|
||||
textStyle={TotalLineTextStyle.Bold}
|
||||
/>
|
||||
</VendorCreditNoteTotalLines>
|
||||
);
|
||||
}
|
||||
|
||||
const VendorCreditNoteTotalLines = styled(TotalLines)`
|
||||
width: 100%;
|
||||
color: #555555;
|
||||
`;
|
||||
@@ -0,0 +1,37 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import intl from 'react-intl-universal';
|
||||
import classNames from 'classnames';
|
||||
import { useFormikContext } from 'formik';
|
||||
import { CLASSES } from '@/constants/classes';
|
||||
import VendorCreditNoteFormHeaderFields from './VendorCreditNoteFormHeaderFields';
|
||||
|
||||
import { getEntriesTotal } from '@/containers/Entries/utils';
|
||||
import { PageFormBigNumber } from '@/components';
|
||||
|
||||
|
||||
/**
|
||||
* Vendor Credit note header.
|
||||
*/
|
||||
function VendorCreditNoteFormHeader() {
|
||||
const { values:{entries ,currency_code} } = useFormikContext();
|
||||
|
||||
// Calculate the total amount.
|
||||
const totalAmount = React.useMemo(
|
||||
() => getEntriesTotal(entries),
|
||||
[entries],
|
||||
);
|
||||
|
||||
return (
|
||||
<div className={classNames(CLASSES.PAGE_FORM_HEADER)}>
|
||||
<VendorCreditNoteFormHeaderFields />
|
||||
<PageFormBigNumber
|
||||
label={intl.get('vendor_credits.label.amount_to_credit')}
|
||||
amount={totalAmount}
|
||||
currencyCode={currency_code}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default VendorCreditNoteFormHeader;
|
||||
@@ -0,0 +1,220 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import styled from 'styled-components';
|
||||
import classNames from 'classnames';
|
||||
import {
|
||||
FormGroup,
|
||||
InputGroup,
|
||||
Position,
|
||||
ControlGroup,
|
||||
} from '@blueprintjs/core';
|
||||
import { DateInput } from '@blueprintjs/datetime';
|
||||
import { FastField, ErrorMessage } from 'formik';
|
||||
import { CLASSES } from '@/constants/classes';
|
||||
|
||||
import {
|
||||
FFormGroup,
|
||||
VendorSelectField,
|
||||
FieldRequiredHint,
|
||||
InputPrependButton,
|
||||
Icon,
|
||||
FormattedMessage as T,
|
||||
VendorDrawerLink,
|
||||
} from '@/components';
|
||||
import {
|
||||
vendorsFieldShouldUpdate,
|
||||
useObserveVendorCreditNoSettings,
|
||||
} from './utils';
|
||||
|
||||
import { useVendorCreditNoteFormContext } from './VendorCreditNoteFormProvider';
|
||||
import { VendorCreditNoteExchangeRateInputField } from './components';
|
||||
import {
|
||||
momentFormatter,
|
||||
compose,
|
||||
tansformDateValue,
|
||||
inputIntent,
|
||||
handleDateChange,
|
||||
} from '@/utils';
|
||||
|
||||
import withSettings from '@/containers/Settings/withSettings';
|
||||
import withDialogActions from '@/containers/Dialog/withDialogActions';
|
||||
|
||||
/**
|
||||
* Vendor Credit note form header fields.
|
||||
*/
|
||||
function VendorCreditNoteFormHeaderFields({
|
||||
// #withDialogActions
|
||||
openDialog,
|
||||
|
||||
// #withSettings
|
||||
vendorcreditAutoIncrement,
|
||||
vendorcreditNumberPrefix,
|
||||
vendorcreditNextNumber,
|
||||
}) {
|
||||
// Vendor Credit form context.
|
||||
const { vendors } = useVendorCreditNoteFormContext();
|
||||
|
||||
// Handle vendor credit number changing.
|
||||
const handleVendorCreditNumberChange = () => {
|
||||
openDialog('vendor-credit-form');
|
||||
};
|
||||
|
||||
// Handle vendor credit no. field blur.
|
||||
const handleVendorCreditNoBlur = (form, field) => (event) => {
|
||||
const newValue = event.target.value;
|
||||
|
||||
if (field.value !== newValue && vendorcreditAutoIncrement) {
|
||||
openDialog('vendor-credit-form', {
|
||||
initialFormValues: {
|
||||
manualTransactionNo: newValue,
|
||||
incrementMode: 'manual-transaction',
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
// Syncs vendor credit number settings with form.
|
||||
useObserveVendorCreditNoSettings(
|
||||
vendorcreditNumberPrefix,
|
||||
vendorcreditNextNumber,
|
||||
);
|
||||
|
||||
return (
|
||||
<div className={classNames(CLASSES.PAGE_FORM_HEADER_FIELDS)}>
|
||||
{/* ----------- Vendor name ----------- */}
|
||||
<FastField
|
||||
name={'vendor_id'}
|
||||
vendors={vendors}
|
||||
shouldUpdate={vendorsFieldShouldUpdate}
|
||||
>
|
||||
{({ form, field: { value }, meta: { error, touched } }) => (
|
||||
<FFormGroup
|
||||
name={'vendor_id'}
|
||||
label={<T id={'vendor_name'} />}
|
||||
inline={true}
|
||||
className={classNames(CLASSES.FILL, 'form-group--vendor')}
|
||||
labelInfo={<FieldRequiredHint />}
|
||||
>
|
||||
<VendorSelectField
|
||||
contacts={vendors}
|
||||
selectedContactId={value}
|
||||
defaultSelectText={<T id={'select_vender_account'} />}
|
||||
onContactSelected={(contact) => {
|
||||
form.setFieldValue('vendor_id', contact.id);
|
||||
form.setFieldValue('currency_code', contact?.currency_code);
|
||||
}}
|
||||
popoverFill={true}
|
||||
allowCreate={true}
|
||||
/>
|
||||
|
||||
{value && (
|
||||
<VendorButtonLink vendorId={value}>
|
||||
<T id={'view_vendor_details'} />
|
||||
</VendorButtonLink>
|
||||
)}
|
||||
</FFormGroup>
|
||||
)}
|
||||
</FastField>
|
||||
|
||||
{/* ----------- Exchange rate ----------- */}
|
||||
<VendorCreditNoteExchangeRateInputField
|
||||
name={'exchange_rate'}
|
||||
formGroupProps={{ label: ' ', inline: true }}
|
||||
/>
|
||||
|
||||
{/* ------- Vendor Credit date ------- */}
|
||||
<FastField name={'vendor_credit_date'}>
|
||||
{({ form, field: { value }, meta: { error, touched } }) => (
|
||||
<FormGroup
|
||||
label={<T id={'credit_note.label_credit_note_date'} />}
|
||||
inline={true}
|
||||
labelInfo={<FieldRequiredHint />}
|
||||
className={classNames(
|
||||
'form-group--vendor_credit_date',
|
||||
CLASSES.FILL,
|
||||
)}
|
||||
intent={inputIntent({ error, touched })}
|
||||
helperText={<ErrorMessage name="vendor_credit_date" />}
|
||||
>
|
||||
<DateInput
|
||||
{...momentFormatter('YYYY/MM/DD')}
|
||||
value={tansformDateValue(value)}
|
||||
onChange={handleDateChange((formattedDate) => {
|
||||
form.setFieldValue('vendor_credit_date', formattedDate);
|
||||
})}
|
||||
popoverProps={{ position: Position.BOTTOM_LEFT, minimal: true }}
|
||||
inputProps={{
|
||||
leftIcon: <Icon icon={'date-range'} />,
|
||||
}}
|
||||
/>
|
||||
</FormGroup>
|
||||
)}
|
||||
</FastField>
|
||||
|
||||
{/* ----------- Vendor Credit No # ----------- */}
|
||||
<FastField name={'vendor_credit_number'}>
|
||||
{({ form, field, meta: { error, touched } }) => (
|
||||
<FormGroup
|
||||
label={<T id={'credit_note.label_credit_note'} />}
|
||||
inline={true}
|
||||
labelInfo={<FieldRequiredHint />}
|
||||
className={('form-group--vendor_credit_number', CLASSES.FILL)}
|
||||
intent={inputIntent({ error, touched })}
|
||||
helperText={<ErrorMessage name="vendor_credit_number" />}
|
||||
>
|
||||
<ControlGroup fill={true}>
|
||||
<InputGroup
|
||||
minimal={true}
|
||||
value={field.value}
|
||||
asyncControl={true}
|
||||
onBlur={handleVendorCreditNoBlur(form, field)}
|
||||
/>
|
||||
<InputPrependButton
|
||||
buttonProps={{
|
||||
onClick: handleVendorCreditNumberChange,
|
||||
icon: <Icon icon={'settings-18'} />,
|
||||
}}
|
||||
tooltip={true}
|
||||
tooltipProps={{
|
||||
content: (
|
||||
<T
|
||||
id={'setting_your_auto_generated_vendor_credit_number'}
|
||||
/>
|
||||
),
|
||||
position: Position.BOTTOM_LEFT,
|
||||
}}
|
||||
/>
|
||||
</ControlGroup>
|
||||
</FormGroup>
|
||||
)}
|
||||
</FastField>
|
||||
{/* ----------- Reference ----------- */}
|
||||
<FastField name={'reference_no'}>
|
||||
{({ field, meta: { error, touched } }) => (
|
||||
<FormGroup
|
||||
label={<T id={'reference_no'} />}
|
||||
inline={true}
|
||||
className={classNames('form-group--reference', CLASSES.FILL)}
|
||||
intent={inputIntent({ error, touched })}
|
||||
helperText={<ErrorMessage name="reference_no" />}
|
||||
>
|
||||
<InputGroup minimal={true} {...field} />
|
||||
</FormGroup>
|
||||
)}
|
||||
</FastField>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(
|
||||
withDialogActions,
|
||||
withSettings(({ vendorsCreditNoteSetting }) => ({
|
||||
vendorcreditAutoIncrement: vendorsCreditNoteSetting?.autoIncrement,
|
||||
vendorcreditNextNumber: vendorsCreditNoteSetting?.nextNumber,
|
||||
vendorcreditNumberPrefix: vendorsCreditNoteSetting?.numberPrefix,
|
||||
})),
|
||||
)(VendorCreditNoteFormHeaderFields);
|
||||
|
||||
const VendorButtonLink = styled(VendorDrawerLink)`
|
||||
font-size: 11px;
|
||||
margin-top: 6px;
|
||||
`;
|
||||
@@ -0,0 +1,22 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import { useParams } from 'react-router-dom';
|
||||
|
||||
import '@/style/pages/VendorsCreditNote/PageForm.scss';
|
||||
|
||||
import VendorCreditNoteForm from './VendorCreditNoteForm';
|
||||
import { VendorCreditNoteFormProvider } from './VendorCreditNoteFormProvider';
|
||||
|
||||
/**
|
||||
* Vendor Credit note form pages.
|
||||
*/
|
||||
export default function VendorCreditNoteFormPage() {
|
||||
const { id } = useParams();
|
||||
const idAsInteger = parseInt(id, 10);
|
||||
|
||||
return (
|
||||
<VendorCreditNoteFormProvider vendorCreditId={idAsInteger}>
|
||||
<VendorCreditNoteForm />
|
||||
</VendorCreditNoteFormProvider>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import { useLocation } from 'react-router-dom';
|
||||
import { isEmpty, pick } from 'lodash';
|
||||
import { DashboardInsider } from '@/components/Dashboard';
|
||||
import { transformToEditForm } from './utils';
|
||||
import { Features } from '@/constants';
|
||||
import { useFeatureCan } from '@/hooks/state';
|
||||
import {
|
||||
useCreateVendorCredit,
|
||||
useEditVendorCredit,
|
||||
useVendorCredit,
|
||||
useWarehouses,
|
||||
useBranches,
|
||||
useItems,
|
||||
useVendors,
|
||||
useSettingsVendorCredits,
|
||||
useBill,
|
||||
} from '@/hooks/query';
|
||||
|
||||
const VendorCreditNoteFormContext = React.createContext();
|
||||
|
||||
/**
|
||||
* Vendor Credit note data provider.
|
||||
*/
|
||||
function VendorCreditNoteFormProvider({ vendorCreditId, ...props }) {
|
||||
const { state } = useLocation();
|
||||
const billId = state?.billId;
|
||||
|
||||
// Features guard.
|
||||
const { featureCan } = useFeatureCan();
|
||||
const isBranchFeatureCan = featureCan(Features.Branches);
|
||||
const isWarehouseFeatureCan = featureCan(Features.Warehouses);
|
||||
|
||||
// Handle fetching the items table based on the given query.
|
||||
const {
|
||||
data: { items },
|
||||
isLoading: isItemsLoading,
|
||||
} = useItems({
|
||||
page_size: 10000,
|
||||
});
|
||||
|
||||
// Handle fetching settings.
|
||||
useSettingsVendorCredits();
|
||||
|
||||
// Handle fetch vendors data table or list
|
||||
const {
|
||||
data: { vendors },
|
||||
isLoading: isVendorsLoading,
|
||||
} = useVendors({ page_size: 10000 });
|
||||
|
||||
// Handle fetch vendor credit details.
|
||||
const { data: vendorCredit, isLoading: isVendorCreditLoading } =
|
||||
useVendorCredit(vendorCreditId, {
|
||||
enabled: !!vendorCreditId,
|
||||
});
|
||||
|
||||
// Handle fetch bill details.
|
||||
const { isLoading: isBillLoading, data: bill } = useBill(billId, {
|
||||
enabled: !!billId,
|
||||
});
|
||||
|
||||
// Fetch warehouses list.
|
||||
const {
|
||||
data: warehouses,
|
||||
isLoading: isWarehouesLoading,
|
||||
isSuccess: isWarehousesSuccess,
|
||||
} = useWarehouses({}, { enabled: isWarehouseFeatureCan });
|
||||
|
||||
// Fetches the branches list.
|
||||
const {
|
||||
data: branches,
|
||||
isLoading: isBranchesLoading,
|
||||
isSuccess: isBranchesSuccess,
|
||||
} = useBranches({}, { enabled: isBranchFeatureCan });
|
||||
|
||||
// Form submit payload.
|
||||
const [submitPayload, setSubmitPayload] = React.useState();
|
||||
|
||||
// Create and edit vendor credit mutations.
|
||||
const { mutateAsync: createVendorCreditMutate } = useCreateVendorCredit();
|
||||
const { mutateAsync: editVendorCreditMutate } = useEditVendorCredit();
|
||||
|
||||
// Determines whether the form in new mode.
|
||||
const isNewMode = !vendorCreditId;
|
||||
|
||||
// Determines whether the warehouse and branches are loading.
|
||||
const isFeatureLoading = isWarehouesLoading || isBranchesLoading;
|
||||
|
||||
const newVendorCredit = !isEmpty(bill)
|
||||
? transformToEditForm({
|
||||
...pick(bill, ['vendor_id', 'currency_code', 'entries']),
|
||||
})
|
||||
: [];
|
||||
|
||||
// Provider payload.
|
||||
const provider = {
|
||||
items,
|
||||
vendors,
|
||||
vendorCredit,
|
||||
warehouses,
|
||||
branches,
|
||||
submitPayload,
|
||||
isNewMode,
|
||||
newVendorCredit,
|
||||
|
||||
isVendorCreditLoading,
|
||||
isFeatureLoading,
|
||||
isBranchesSuccess,
|
||||
isWarehousesSuccess,
|
||||
|
||||
createVendorCreditMutate,
|
||||
editVendorCreditMutate,
|
||||
setSubmitPayload,
|
||||
};
|
||||
|
||||
return (
|
||||
<DashboardInsider
|
||||
loading={
|
||||
isVendorCreditLoading ||
|
||||
isItemsLoading ||
|
||||
isVendorsLoading ||
|
||||
isVendorCreditLoading ||
|
||||
isBillLoading
|
||||
}
|
||||
name={'vendor-credit-form'}
|
||||
>
|
||||
<VendorCreditNoteFormContext.Provider value={provider} {...props} />
|
||||
</DashboardInsider>
|
||||
);
|
||||
}
|
||||
|
||||
const useVendorCreditNoteFormContext = () =>
|
||||
React.useContext(VendorCreditNoteFormContext);
|
||||
|
||||
export { VendorCreditNoteFormProvider, useVendorCreditNoteFormContext };
|
||||
@@ -0,0 +1,115 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import intl from 'react-intl-universal';
|
||||
import {
|
||||
Alignment,
|
||||
NavbarGroup,
|
||||
NavbarDivider,
|
||||
Button,
|
||||
Classes,
|
||||
} from '@blueprintjs/core';
|
||||
import {
|
||||
useSetPrimaryBranchToForm,
|
||||
useSetPrimaryWarehouseToForm,
|
||||
} from './utils';
|
||||
import { useFeatureCan } from '@/hooks/state';
|
||||
import {
|
||||
Icon,
|
||||
BranchSelect,
|
||||
FeatureCan,
|
||||
WarehouseSelect,
|
||||
FormTopbar,
|
||||
DetailsBarSkeletonBase,
|
||||
} from '@/components';
|
||||
import { useVendorCreditNoteFormContext } from './VendorCreditNoteFormProvider';
|
||||
import { Features } from '@/constants';
|
||||
|
||||
/**
|
||||
* Vendor Credit note form topbar .
|
||||
* @returns {JSX.Element}
|
||||
*/
|
||||
export default function VendorCreditNoteFormTopBar() {
|
||||
// Features guard.
|
||||
const { featureCan } = useFeatureCan();
|
||||
|
||||
// Sets the primary warehouse to form.
|
||||
useSetPrimaryWarehouseToForm();
|
||||
|
||||
// Sets the primary branch to form.
|
||||
useSetPrimaryBranchToForm();
|
||||
|
||||
// Can't display the navigation bar if warehouses or branches feature is not enabled.
|
||||
if (!featureCan(Features.Warehouses) && !featureCan(Features.Branches)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<FormTopbar>
|
||||
<NavbarGroup align={Alignment.LEFT}>
|
||||
<FeatureCan feature={Features.Branches}>
|
||||
<VendorCreditNoteFormSelectBranch />
|
||||
</FeatureCan>
|
||||
{featureCan(Features.Warehouses) && featureCan(Features.Branches) && (
|
||||
<NavbarDivider />
|
||||
)}
|
||||
<FeatureCan feature={Features.Warehouses}>
|
||||
<VendorCreditFormSelectWarehouse />
|
||||
</FeatureCan>
|
||||
</NavbarGroup>
|
||||
</FormTopbar>
|
||||
);
|
||||
}
|
||||
|
||||
function VendorCreditNoteFormSelectBranch() {
|
||||
// Vendor credit note form context.
|
||||
const { branches, isBranchesLoading } = useVendorCreditNoteFormContext();
|
||||
|
||||
return isBranchesLoading ? (
|
||||
<DetailsBarSkeletonBase className={Classes.SKELETON} />
|
||||
) : (
|
||||
<BranchSelect
|
||||
name={'branch_id'}
|
||||
branches={branches}
|
||||
input={VendorCreditNoteBranchSelectButton}
|
||||
popoverProps={{ minimal: true }}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function VendorCreditFormSelectWarehouse() {
|
||||
// vendor credit note form context.
|
||||
const { warehouses, isWarehouesLoading } = useVendorCreditNoteFormContext();
|
||||
|
||||
return isWarehouesLoading ? (
|
||||
<DetailsBarSkeletonBase className={Classes.SKELETON} />
|
||||
) : (
|
||||
<WarehouseSelect
|
||||
name={'warehouse_id'}
|
||||
warehouses={warehouses}
|
||||
input={VendorCreditNoteWarehouseSelectButton}
|
||||
popoverProps={{ minimal: true }}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function VendorCreditNoteWarehouseSelectButton({ label }) {
|
||||
return (
|
||||
<Button
|
||||
text={intl.get('vendor_credit.warehouse_button.label', { label })}
|
||||
minimal={true}
|
||||
small={true}
|
||||
icon={<Icon icon={'warehouse-16'} iconSize={16} />}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function VendorCreditNoteBranchSelectButton({ label }) {
|
||||
return (
|
||||
<Button
|
||||
text={intl.get('vendor_credit.branch_button.label', { label })}
|
||||
minimal={true}
|
||||
small={true}
|
||||
icon={<Icon icon={'branch-16'} iconSize={16} />}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import classNames from 'classnames';
|
||||
import { FastField } from 'formik';
|
||||
import { CLASSES } from '@/constants/classes';
|
||||
import { entriesFieldShouldUpdate } from './utils';
|
||||
import { useVendorCreditNoteFormContext } from './VendorCreditNoteFormProvider';
|
||||
import ItemsEntriesTable from '@/containers/Entries/ItemsEntriesTable';
|
||||
|
||||
export default function VendorCreditNoteItemsEntriesEditor() {
|
||||
const { items } = useVendorCreditNoteFormContext();
|
||||
return (
|
||||
<div className={classNames(CLASSES.PAGE_FORM_BODY)}>
|
||||
<FastField
|
||||
name={'entries'}
|
||||
items={items}
|
||||
shouldUpdate={entriesFieldShouldUpdate}
|
||||
>
|
||||
{({
|
||||
form: { values, setFieldValue },
|
||||
field: { value },
|
||||
meta: { error, touched },
|
||||
}) => (
|
||||
<ItemsEntriesTable
|
||||
entries={value}
|
||||
onUpdateData={(entries) => {
|
||||
setFieldValue('entries', entries);
|
||||
}}
|
||||
items={items}
|
||||
errors={error}
|
||||
linesNumber={4}
|
||||
currencyCode={values.currency_code}
|
||||
/>
|
||||
)}
|
||||
</FastField>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import { useFormikContext } from 'formik';
|
||||
import { ExchangeRateInputGroup } from '@/components';
|
||||
import { useCurrentOrganization } from '@/hooks/state';
|
||||
import { useVendorNoteIsForeignCustomer } from './utils';
|
||||
|
||||
/**
|
||||
* vendor credit note exchange rate input field.
|
||||
* @returns {JSX.Element}
|
||||
*/
|
||||
export function VendorCreditNoteExchangeRateInputField({ ...props }) {
|
||||
const currentOrganization = useCurrentOrganization();
|
||||
const { values } = useFormikContext();
|
||||
|
||||
const isForeignCustomer = useVendorNoteIsForeignCustomer();
|
||||
|
||||
// Can't continue if the customer is not foreign.
|
||||
if (!isForeignCustomer) {
|
||||
return null;
|
||||
}
|
||||
return (
|
||||
<ExchangeRateInputGroup
|
||||
fromCurrency={values.currency_code}
|
||||
toCurrency={currentOrganization.base_currency}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
// @ts-nocheck
|
||||
import React from 'react';
|
||||
import * as R from 'ramda';
|
||||
import moment from 'moment';
|
||||
import { first } from 'lodash';
|
||||
|
||||
import {
|
||||
defaultFastFieldShouldUpdate,
|
||||
transformToForm,
|
||||
repeatValue,
|
||||
transactionNumber,
|
||||
orderingLinesIndexes,
|
||||
formattedAmount,
|
||||
} from '@/utils';
|
||||
import {
|
||||
updateItemsEntriesTotal,
|
||||
ensureEntriesHaveEmptyLine,
|
||||
} from '@/containers/Entries/utils';
|
||||
import { useFormikContext } from 'formik';
|
||||
import { useVendorCreditNoteFormContext } from './VendorCreditNoteFormProvider';
|
||||
import { useCurrentOrganization } from '@/hooks/state';
|
||||
import { getEntriesTotal } from '@/containers/Entries/utils';
|
||||
|
||||
export const MIN_LINES_NUMBER = 1;
|
||||
|
||||
// Default Vendors Credit Note entry.
|
||||
export const defaultCreditNoteEntry = {
|
||||
index: 0,
|
||||
item_id: '',
|
||||
rate: '',
|
||||
discount: '',
|
||||
quantity: '',
|
||||
description: '',
|
||||
amount: '',
|
||||
};
|
||||
|
||||
// Default Vendors Credit Note.
|
||||
export const defaultVendorsCreditNote = {
|
||||
vendor_id: '',
|
||||
vendor_credit_number: '',
|
||||
vendor_credit_no_manually: false,
|
||||
open: '',
|
||||
vendor_credit_date: moment(new Date()).format('YYYY-MM-DD'),
|
||||
reference_no: '',
|
||||
note: '',
|
||||
branch_id: '',
|
||||
warehouse_id: '',
|
||||
exchange_rate: 1,
|
||||
currency_code: '',
|
||||
entries: [...repeatValue(defaultCreditNoteEntry, MIN_LINES_NUMBER)],
|
||||
};
|
||||
|
||||
/**
|
||||
* Transformes the credit note to initial values of edit form.
|
||||
*/
|
||||
export const transformToEditForm = (creditNote) => {
|
||||
const initialEntries = [
|
||||
...creditNote.entries.map((entry) => ({
|
||||
...transformToForm(entry, defaultCreditNoteEntry),
|
||||
})),
|
||||
...repeatValue(
|
||||
defaultCreditNoteEntry,
|
||||
Math.max(MIN_LINES_NUMBER - creditNote.entries.length, 0),
|
||||
),
|
||||
];
|
||||
const entries = R.compose(
|
||||
ensureEntriesHaveEmptyLine(defaultCreditNoteEntry),
|
||||
updateItemsEntriesTotal,
|
||||
)(initialEntries);
|
||||
|
||||
return {
|
||||
...transformToForm(creditNote, defaultVendorsCreditNote),
|
||||
entries,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Transformes credit note entries to submit request.
|
||||
*/
|
||||
export const transformEntriesToSubmit = (entries) => {
|
||||
const transformCreditNoteEntry = R.compose(
|
||||
R.omit(['amount']),
|
||||
R.curry(transformToForm)(R.__, defaultCreditNoteEntry),
|
||||
);
|
||||
return R.compose(
|
||||
orderingLinesIndexes,
|
||||
R.map(transformCreditNoteEntry),
|
||||
)(entries);
|
||||
};
|
||||
|
||||
/**
|
||||
* Filters the givne non-zero entries.
|
||||
*/
|
||||
export const filterNonZeroEntries = (entries) => {
|
||||
return entries.filter((item) => item.item_id && item.quantity);
|
||||
};
|
||||
|
||||
/**
|
||||
* Transformes form values to request body.
|
||||
*/
|
||||
export const transformFormValuesToRequest = (values) => {
|
||||
const entries = filterNonZeroEntries(values.entries);
|
||||
|
||||
return {
|
||||
...values,
|
||||
entries: transformEntriesToSubmit(entries),
|
||||
open: false,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Detarmines vendors fast field should update
|
||||
*/
|
||||
export const vendorsFieldShouldUpdate = (newProps, oldProps) => {
|
||||
return (
|
||||
newProps.vendors !== oldProps.vendors ||
|
||||
defaultFastFieldShouldUpdate(newProps, oldProps)
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Detarmines entries fast field should update.
|
||||
*/
|
||||
export const entriesFieldShouldUpdate = (newProps, oldProps) => {
|
||||
return (
|
||||
newProps.items !== oldProps.items ||
|
||||
defaultFastFieldShouldUpdate(newProps, oldProps)
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Syncs invoice no. settings with form.
|
||||
*/
|
||||
export const useObserveVendorCreditNoSettings = (prefix, nextNumber) => {
|
||||
const { setFieldValue } = useFormikContext();
|
||||
|
||||
React.useEffect(() => {
|
||||
const creditNo = transactionNumber(prefix, nextNumber);
|
||||
setFieldValue('vendor_credit_number', creditNo);
|
||||
}, [setFieldValue, prefix, nextNumber]);
|
||||
};
|
||||
|
||||
export const useSetPrimaryBranchToForm = () => {
|
||||
const { setFieldValue } = useFormikContext();
|
||||
const { branches, isBranchesSuccess } = useVendorCreditNoteFormContext();
|
||||
|
||||
React.useEffect(() => {
|
||||
if (isBranchesSuccess) {
|
||||
const primaryBranch = branches.find((b) => b.primary) || first(branches);
|
||||
|
||||
if (primaryBranch) {
|
||||
setFieldValue('branch_id', primaryBranch.id);
|
||||
}
|
||||
}
|
||||
}, [isBranchesSuccess, setFieldValue, branches]);
|
||||
};
|
||||
|
||||
export const useSetPrimaryWarehouseToForm = () => {
|
||||
const { setFieldValue } = useFormikContext();
|
||||
const { warehouses, isWarehousesSuccess } = useVendorCreditNoteFormContext();
|
||||
|
||||
React.useEffect(() => {
|
||||
if (isWarehousesSuccess) {
|
||||
const primaryWarehouse =
|
||||
warehouses.find((b) => b.primary) || first(warehouses);
|
||||
|
||||
if (primaryWarehouse) {
|
||||
setFieldValue('warehouse_id', primaryWarehouse.id);
|
||||
}
|
||||
}
|
||||
}, [isWarehousesSuccess, setFieldValue, warehouses]);
|
||||
};
|
||||
|
||||
export const useVendorCrditNoteTotals = () => {
|
||||
const {
|
||||
values: { entries, currency_code: currencyCode },
|
||||
} = useFormikContext();
|
||||
|
||||
// Retrieves the invoice entries total.
|
||||
const total = React.useMemo(() => getEntriesTotal(entries), [entries]);
|
||||
|
||||
// Retrieves the formatted total money.
|
||||
const formattedTotal = React.useMemo(
|
||||
() => formattedAmount(total, currencyCode),
|
||||
[total, currencyCode],
|
||||
);
|
||||
// Retrieves the formatted subtotal.
|
||||
const formattedSubtotal = React.useMemo(
|
||||
() => formattedAmount(total, currencyCode, { money: false }),
|
||||
[total, currencyCode],
|
||||
);
|
||||
|
||||
return {
|
||||
total,
|
||||
formattedTotal,
|
||||
formattedSubtotal,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Detarmines whether the vendor note has foreign customer.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export const useVendorNoteIsForeignCustomer = () => {
|
||||
const { values } = useFormikContext();
|
||||
const currentOrganization = useCurrentOrganization();
|
||||
|
||||
const isForeignCustomer = React.useMemo(
|
||||
() => values.currency_code !== currentOrganization.base_currency,
|
||||
[values.currency_code, currentOrganization.base_currency],
|
||||
);
|
||||
return isForeignCustomer;
|
||||
};
|
||||
Reference in New Issue
Block a user