mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 04:40:32 +00:00
WIP / Features / Sate
This commit is contained in:
259
client/src/containers/Sales/Estimate/EntriesItemsTable.js
Normal file
259
client/src/containers/Sales/Estimate/EntriesItemsTable.js
Normal file
@@ -0,0 +1,259 @@
|
||||
import React, { useState, useMemo, useEffect, useCallback } from 'react';
|
||||
import { Button, Intent, Position, Tooltip } from '@blueprintjs/core';
|
||||
import { FormattedMessage as T, useIntl } from 'react-intl';
|
||||
import DataTable from 'components/DataTable';
|
||||
import Icon from 'components/Icon';
|
||||
|
||||
import { compose, formattedAmount, transformUpdatedRows } from 'utils';
|
||||
import {
|
||||
InputGroupCell,
|
||||
MoneyFieldCell,
|
||||
EstimatesListFieldCell,
|
||||
PercentFieldCell,
|
||||
DivFieldCell,
|
||||
} from 'components/DataTableCells';
|
||||
|
||||
import withItems from 'containers/Items/withItems';
|
||||
import { omit } from 'lodash';
|
||||
|
||||
const ActionsCellRenderer = ({
|
||||
row: { index },
|
||||
column: { id },
|
||||
cell: { value },
|
||||
data,
|
||||
payload,
|
||||
}) => {
|
||||
if (data.length <= index + 1) {
|
||||
return '';
|
||||
}
|
||||
const onRemoveRole = () => {
|
||||
payload.removeRow(index);
|
||||
};
|
||||
return (
|
||||
<Tooltip content={<T id={'remove_the_line'} />} position={Position.LEFT}>
|
||||
<Button
|
||||
icon={<Icon icon={'times-circle'} iconSize={14} />}
|
||||
iconSize={14}
|
||||
className="m12"
|
||||
intent={Intent.DANGER}
|
||||
onClick={onRemoveRole}
|
||||
/>
|
||||
</Tooltip>
|
||||
);
|
||||
};
|
||||
|
||||
const TotalEstimateCellRederer = (content, type) => (props) => {
|
||||
if (props.data.length === props.row.index + 1) {
|
||||
const total = props.data.reduce((total, entry) => {
|
||||
const amount = parseInt(entry[type], 10);
|
||||
const computed = amount ? total + amount : total;
|
||||
|
||||
return computed;
|
||||
}, 0);
|
||||
return <span>{formattedAmount(total, 'USD')}</span>;
|
||||
}
|
||||
return content(props);
|
||||
};
|
||||
|
||||
const calculateDiscount = (discount, quantity, rate) =>
|
||||
quantity * rate - (quantity * rate * discount) / 100;
|
||||
|
||||
const CellRenderer = (content, type) => (props) => {
|
||||
if (props.data.length === props.row.index + 1) {
|
||||
return '';
|
||||
}
|
||||
return content(props);
|
||||
};
|
||||
|
||||
function EstimateTable({
|
||||
//#withitems
|
||||
itemsCurrentPage,
|
||||
|
||||
//#ownProps
|
||||
onClickRemoveRow,
|
||||
onClickAddNewRow,
|
||||
onClickClearAllLines,
|
||||
entries,
|
||||
formik: { errors, setFieldValue, values },
|
||||
}) {
|
||||
const [rows, setRows] = useState([]);
|
||||
const { formatMessage } = useIntl();
|
||||
|
||||
useEffect(() => {
|
||||
setRows([...entries.map((e) => ({ ...e }))]);
|
||||
}, [entries]);
|
||||
|
||||
const columns = useMemo(
|
||||
() => [
|
||||
{
|
||||
Header: '#',
|
||||
accessor: 'index',
|
||||
Cell: ({ row: { index } }) => <span>{index + 1}</span>,
|
||||
width: 40,
|
||||
disableResizing: true,
|
||||
disableSortBy: true,
|
||||
},
|
||||
{
|
||||
Header: formatMessage({ id: 'product_and_service' }),
|
||||
id: 'item_id',
|
||||
accessor: 'item_id',
|
||||
Cell: EstimatesListFieldCell,
|
||||
disableSortBy: true,
|
||||
disableResizing: true,
|
||||
width: 250,
|
||||
},
|
||||
{
|
||||
Header: formatMessage({ id: 'description' }),
|
||||
accessor: 'description',
|
||||
Cell: InputGroupCell,
|
||||
disableSortBy: true,
|
||||
className: 'description',
|
||||
},
|
||||
|
||||
{
|
||||
Header: formatMessage({ id: 'quantity' }),
|
||||
accessor: 'quantity',
|
||||
Cell: CellRenderer(InputGroupCell, 'quantity'),
|
||||
disableSortBy: true,
|
||||
width: 100,
|
||||
className: 'quantity',
|
||||
},
|
||||
{
|
||||
Header: formatMessage({ id: 'rate' }),
|
||||
accessor: 'rate',
|
||||
Cell: TotalEstimateCellRederer(MoneyFieldCell, 'rate'),
|
||||
disableSortBy: true,
|
||||
width: 150,
|
||||
className: 'rate',
|
||||
},
|
||||
{
|
||||
Header: formatMessage({ id: 'discount' }),
|
||||
accessor: 'discount',
|
||||
Cell: CellRenderer(PercentFieldCell, InputGroupCell),
|
||||
disableSortBy: true,
|
||||
disableResizing: true,
|
||||
width: 100,
|
||||
className: 'discount',
|
||||
},
|
||||
{
|
||||
Header: formatMessage({ id: 'total' }),
|
||||
accessor: (row) =>
|
||||
calculateDiscount(row.discount, row.quantity, row.rate),
|
||||
Cell: TotalEstimateCellRederer(DivFieldCell, 'total'),
|
||||
disableSortBy: true,
|
||||
width: 150,
|
||||
className: 'total',
|
||||
},
|
||||
{
|
||||
Header: '',
|
||||
accessor: 'action',
|
||||
Cell: ActionsCellRenderer,
|
||||
className: 'actions',
|
||||
disableSortBy: true,
|
||||
disableResizing: true,
|
||||
width: 45,
|
||||
},
|
||||
],
|
||||
[formatMessage],
|
||||
);
|
||||
|
||||
const handleUpdateData = useCallback(
|
||||
(rowIndex, columnId, value) => {
|
||||
const newRow = rows.map((row, index) => {
|
||||
if (index === rowIndex) {
|
||||
const newRow = { ...rows[rowIndex], [columnId]: value };
|
||||
return {
|
||||
...newRow,
|
||||
total: calculateDiscount(
|
||||
newRow.discount,
|
||||
newRow.quantity,
|
||||
newRow.rate,
|
||||
),
|
||||
};
|
||||
}
|
||||
return row;
|
||||
});
|
||||
setFieldValue(
|
||||
'entries',
|
||||
newRow.map((row) => ({
|
||||
...omit(row),
|
||||
})),
|
||||
);
|
||||
},
|
||||
[rows, setFieldValue],
|
||||
);
|
||||
|
||||
const handleRemoveRow = useCallback(
|
||||
(rowIndex) => {
|
||||
if (rows.length <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
const removeIndex = parseInt(rowIndex, 10);
|
||||
const newRows = rows.filter((row, index) => index !== removeIndex);
|
||||
setFieldValue(
|
||||
'entries',
|
||||
newRows.map((row, index) => ({
|
||||
...omit(row),
|
||||
index: index + 1,
|
||||
})),
|
||||
);
|
||||
onClickRemoveRow && onClickRemoveRow(removeIndex);
|
||||
},
|
||||
[rows, setFieldValue, onClickRemoveRow],
|
||||
);
|
||||
|
||||
const onClickNewRow = () => {
|
||||
onClickAddNewRow && onClickAddNewRow();
|
||||
};
|
||||
|
||||
const handleClickClearAllLines = () => {
|
||||
onClickClearAllLines && onClickClearAllLines();
|
||||
};
|
||||
|
||||
const rowClassNames = useCallback(
|
||||
(row) => ({
|
||||
'row--total': rows.length === row.index + 1,
|
||||
}),
|
||||
[rows],
|
||||
);
|
||||
|
||||
return (
|
||||
<div className={'estimate-form__table'}>
|
||||
<DataTable
|
||||
columns={columns}
|
||||
data={rows}
|
||||
rowClassNames={rowClassNames}
|
||||
payload={{
|
||||
products: itemsCurrentPage,
|
||||
errors: errors.entries || [],
|
||||
updateData: handleUpdateData,
|
||||
removeRow: handleRemoveRow,
|
||||
}}
|
||||
/>
|
||||
<div className={'mt1'}>
|
||||
<Button
|
||||
small={true}
|
||||
className={'button--secondary button--new-line'}
|
||||
onClick={onClickNewRow}
|
||||
>
|
||||
<T id={'new_lines'} />
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
small={true}
|
||||
className={'button--secondary button--clear-lines ml1'}
|
||||
onClick={handleClickClearAllLines}
|
||||
>
|
||||
<T id={'clear_all_lines'} />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(
|
||||
withItems(({ itemsCurrentPage }) => ({
|
||||
itemsCurrentPage,
|
||||
})),
|
||||
)(EstimateTable);
|
||||
140
client/src/containers/Sales/Estimate/EstimateActionsBar.js
Normal file
140
client/src/containers/Sales/Estimate/EstimateActionsBar.js
Normal file
@@ -0,0 +1,140 @@
|
||||
import React, { useMemo, useCallback } from 'react';
|
||||
import Icon from 'components/Icon';
|
||||
import {
|
||||
Button,
|
||||
Classes,
|
||||
Menu,
|
||||
MenuItem,
|
||||
Popover,
|
||||
NavbarDivider,
|
||||
NavbarGroup,
|
||||
PopoverInteractionKind,
|
||||
Position,
|
||||
Intent,
|
||||
} from '@blueprintjs/core';
|
||||
import classNames from 'classnames';
|
||||
import { useRouteMatch, useHistory } from 'react-router-dom';
|
||||
import { FormattedMessage as T } from 'react-intl';
|
||||
|
||||
import { If } from 'components';
|
||||
import FilterDropdown from 'components/FilterDropdown';
|
||||
import DashboardActionsBar from 'components/Dashboard/DashboardActionsBar';
|
||||
|
||||
import withResourceDetail from 'containers/Resources/withResourceDetails';
|
||||
import withDialogActions from 'containers/Dialog/withDialogActions';
|
||||
import withEstimateActions from './withEstimateActions';
|
||||
|
||||
import withEstimates from './withEstimates';
|
||||
|
||||
import { compose } from 'utils';
|
||||
import { connect } from 'react-redux';
|
||||
|
||||
function EstimateActionsBar({
|
||||
// #withResourceDetail
|
||||
resourceFields,
|
||||
|
||||
//#withEstimates
|
||||
estimateViews,
|
||||
|
||||
// #withEstimateActions
|
||||
addEstimatesTableQueries,
|
||||
|
||||
// #own Porps
|
||||
onFilterChanged,
|
||||
selectedRows,
|
||||
}) {
|
||||
const { path } = useRouteMatch();
|
||||
const history = useHistory();
|
||||
|
||||
const onClickNewEstimate = useCallback(() => {
|
||||
// history.push('/estimates/new');
|
||||
}, [history]);
|
||||
|
||||
const filterDropdown = FilterDropdown({
|
||||
initialCondition: {
|
||||
fieldKey: '',
|
||||
compatator: '',
|
||||
value: '',
|
||||
},
|
||||
fields: resourceFields,
|
||||
onFilterChange: (filterConditions) => {
|
||||
addEstimatesTableQueries({
|
||||
filter_roles: filterConditions || '',
|
||||
});
|
||||
onFilterChanged && onFilterChange(filterConditions);
|
||||
},
|
||||
});
|
||||
|
||||
const hasSelectedRows = useMemo(() => selectedRows.length > 0, [
|
||||
selectedRows,
|
||||
]);
|
||||
|
||||
return (
|
||||
<DashboardActionsBar>
|
||||
<NavbarGroup>
|
||||
<NavbarDivider />
|
||||
<Button
|
||||
className={Classes.MINIMAL}
|
||||
icon={<Icon icon={'plus'} />}
|
||||
text={<T id={'new_estimate'} />}
|
||||
onClick={onClickNewEstimate}
|
||||
/>
|
||||
<Popover
|
||||
minimal={true}
|
||||
content={filterDropdown}
|
||||
interactionKind={PopoverInteractionKind.CLICK}
|
||||
position={Position.BOTTOM_LEFT}
|
||||
>
|
||||
<Button
|
||||
className={classNames(Classes.MINIMAL, 'button--filter')}
|
||||
text={'Filter'}
|
||||
icon={<Icon icon={'filter-16'} iconSize={16} />}
|
||||
/>
|
||||
</Popover>
|
||||
<If condition={hasSelectedRows}>
|
||||
<Button
|
||||
className={Classes.MINIMAL}
|
||||
icon={<Icon icon={'trash-16'} iconSize={16} />}
|
||||
text={<T id={'delete'} />}
|
||||
intent={Intent.DANGER}
|
||||
// onClick={handleBulkDelete}
|
||||
/>
|
||||
</If>
|
||||
<Button
|
||||
className={Classes.MINIMAL}
|
||||
icon={<Icon icon={'print-16'} iconSize={'16'} />}
|
||||
text={<T id={'print'} />}
|
||||
/>
|
||||
|
||||
<Button
|
||||
className={Classes.MINIMAL}
|
||||
icon={<Icon icon={'file-import-16'} />}
|
||||
text={<T id={'import'} />}
|
||||
/>
|
||||
<Button
|
||||
className={Classes.MINIMAL}
|
||||
icon={<Icon icon={'file-export-16'} iconSize={'16'} />}
|
||||
text={<T id={'export'} />}
|
||||
/>
|
||||
</NavbarGroup>
|
||||
</DashboardActionsBar>
|
||||
);
|
||||
}
|
||||
|
||||
const mapStateToProps = (state, props) => ({
|
||||
resourceName: 'estimates',
|
||||
});
|
||||
|
||||
const withEstimateActionsBar = connect(mapStateToProps);
|
||||
|
||||
export default compose(
|
||||
withEstimateActionsBar,
|
||||
withDialogActions,
|
||||
withResourceDetail(({ resourceFields }) => ({
|
||||
resourceFields,
|
||||
})),
|
||||
// withEstimate(({ estimateViews }) => ({
|
||||
// estimateViews,
|
||||
// })),
|
||||
withEstimateActions,
|
||||
)(EstimateActionsBar);
|
||||
334
client/src/containers/Sales/Estimate/EstimateForm.js
Normal file
334
client/src/containers/Sales/Estimate/EstimateForm.js
Normal file
@@ -0,0 +1,334 @@
|
||||
import React, {
|
||||
useMemo,
|
||||
useCallback,
|
||||
useEffect,
|
||||
useRef,
|
||||
useState,
|
||||
} from 'react';
|
||||
|
||||
import * as Yup from 'yup';
|
||||
import { useFormik } from 'formik';
|
||||
import moment from 'moment';
|
||||
import { Intent, FormGroup, TextArea, Button } from '@blueprintjs/core';
|
||||
import { FormattedMessage as T, useIntl } from 'react-intl';
|
||||
import { pick, omitBy, omit } from 'lodash';
|
||||
|
||||
import EstimateFormHeader from './EstimateFormHeader';
|
||||
import EstimatesItemsTable from './EntriesItemsTable';
|
||||
import EstimateFormFooter from './EstimateFormFooter';
|
||||
|
||||
import withEstimateActions from './withEstimateActions';
|
||||
import withDashboardActions from 'containers/Dashboard/withDashboardActions';
|
||||
import withMediaActions from 'containers/Media/withMediaActions';
|
||||
|
||||
import AppToaster from 'components/AppToaster';
|
||||
import Dragzone from 'components/Dragzone';
|
||||
|
||||
import useMedia from 'hooks/useMedia';
|
||||
import { compose, repeatValue } from 'utils';
|
||||
|
||||
const MIN_LINES_NUMBER = 4;
|
||||
|
||||
const EstimateForm = ({
|
||||
//#WithMedia
|
||||
requestSubmitMedia,
|
||||
requestDeleteMedia,
|
||||
|
||||
//#WithEstimateActions
|
||||
requestSubmitEstimate,
|
||||
requestEditEstimate,
|
||||
|
||||
//#withDashboard
|
||||
changePageTitle,
|
||||
changePageSubtitle,
|
||||
|
||||
//#withEstimateDetail
|
||||
estimate,
|
||||
|
||||
//#own Props
|
||||
estimateId,
|
||||
onFormSubmit,
|
||||
onCancelForm,
|
||||
}) => {
|
||||
const { formatMessage } = useIntl();
|
||||
const [payload, setPaload] = useState({});
|
||||
|
||||
const {
|
||||
setFiles,
|
||||
saveMedia,
|
||||
deletedFiles,
|
||||
setDeletedFiles,
|
||||
deleteMedia,
|
||||
} = useMedia({
|
||||
saveCallback: requestSubmitMedia,
|
||||
deleteCallback: requestDeleteMedia,
|
||||
});
|
||||
|
||||
const handleDropFiles = useCallback((_files) => {
|
||||
setFiles(_files.filter((file) => file.uploaded === false));
|
||||
}, []);
|
||||
|
||||
const savedMediaIds = useRef([]);
|
||||
const clearSavedMediaIds = () => {
|
||||
savedMediaIds.current = [];
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (estimate && estimate.id) {
|
||||
changePageTitle(formatMessage({ id: 'edit_estimate' }));
|
||||
} else {
|
||||
changePageTitle(formatMessage({ id: 'new_estimate' }));
|
||||
}
|
||||
}, [changePageTitle, estimate, formatMessage]);
|
||||
|
||||
const validationSchema = Yup.object().shape({
|
||||
customer_id: Yup.number()
|
||||
.label(formatMessage({ id: 'customer_name_' }))
|
||||
.required(),
|
||||
estimate_date: Yup.date()
|
||||
.required()
|
||||
.label(formatMessage({ id: 'estimate_date_' })),
|
||||
expiration_date: Yup.date()
|
||||
.required()
|
||||
.label(formatMessage({ id: 'expiration_date_' })),
|
||||
estimate_number: Yup.number()
|
||||
.required()
|
||||
.label(formatMessage({ id: 'estimate_number_' })),
|
||||
reference: Yup.string().min(1).max(255),
|
||||
note: Yup.string()
|
||||
.trim()
|
||||
.min(1)
|
||||
.max(1024)
|
||||
.label(formatMessage({ id: 'note' })),
|
||||
|
||||
terms_conditions: Yup.string()
|
||||
.trim()
|
||||
.min(1)
|
||||
.max(1024)
|
||||
.label(formatMessage({ id: 'note' })),
|
||||
|
||||
entries: Yup.array().of(
|
||||
Yup.object().shape({
|
||||
quantity: Yup.number().nullable(),
|
||||
//Cyclic dependency
|
||||
rate: Yup.number().nullable(),
|
||||
// .when(['item_id'], {
|
||||
// is: (item_id) => item_id,
|
||||
// then: Yup.number().required(),
|
||||
// }),
|
||||
|
||||
// rate: Yup.number().test((value) => {
|
||||
// const { item_id } = this.parent;
|
||||
// if (!item_id) return value != null;
|
||||
// return false;
|
||||
// }),
|
||||
item_id: Yup.number()
|
||||
.nullable()
|
||||
.when(['quantity', 'rate'], {
|
||||
is: (quantity, rate) => quantity || rate,
|
||||
then: Yup.number().required(),
|
||||
}),
|
||||
discount: Yup.number().nullable(),
|
||||
description: Yup.string().nullable(),
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
const saveInvokeSubmit = useCallback(
|
||||
(payload) => {
|
||||
onFormSubmit && onFormSubmit(payload);
|
||||
},
|
||||
[onFormSubmit],
|
||||
);
|
||||
|
||||
const defaultEstimate = useMemo(
|
||||
() => ({
|
||||
index: 0,
|
||||
item_id: null,
|
||||
rate: null,
|
||||
discount: null,
|
||||
quantity: null,
|
||||
description: '',
|
||||
}),
|
||||
[],
|
||||
);
|
||||
|
||||
const defaultInitialValues = useMemo(
|
||||
() => ({
|
||||
customer_id: null,
|
||||
estimate_date: moment(new Date()).format('YYYY-MM-DD'),
|
||||
expiration_date: moment(new Date()).format('YYYY-MM-DD'),
|
||||
estimate_number: null,
|
||||
reference: '',
|
||||
note: '',
|
||||
terms_conditions: '',
|
||||
entries: [...repeatValue(defaultEstimate, MIN_LINES_NUMBER)],
|
||||
}),
|
||||
[defaultEstimate],
|
||||
);
|
||||
|
||||
const orderingProductsIndex = (_entries) => {
|
||||
return _entries.map((item, index) => ({
|
||||
...item,
|
||||
index: index + 1,
|
||||
}));
|
||||
};
|
||||
|
||||
const initialValues = useMemo(
|
||||
() => ({
|
||||
...defaultInitialValues,
|
||||
entries: orderingProductsIndex(defaultInitialValues.entries),
|
||||
}),
|
||||
[defaultEstimate, defaultInitialValues, estimate],
|
||||
);
|
||||
|
||||
const initialAttachmentFiles = useMemo(() => {
|
||||
return estimate && estimate.media
|
||||
? estimate.media.map((attach) => ({
|
||||
preview: attach.attachment_file,
|
||||
uploaded: true,
|
||||
metadata: { ...attach },
|
||||
}))
|
||||
: [];
|
||||
}, [estimate]);
|
||||
|
||||
const formik = useFormik({
|
||||
enableReinitialize: true,
|
||||
validationSchema,
|
||||
initialValues: {
|
||||
...initialValues,
|
||||
},
|
||||
onSubmit: async (values, { setSubmitting, setErrors, resetForm }) => {
|
||||
const entries = values.entries.map((item) => omit(item, ['total']));
|
||||
|
||||
const form = {
|
||||
...values,
|
||||
entries,
|
||||
};
|
||||
const saveEstimate = (mediaIds) =>
|
||||
new Promise((resolve, reject) => {
|
||||
const requestForm = { ...form, media_ids: mediaIds };
|
||||
|
||||
requestSubmitEstimate(requestForm)
|
||||
.then((response) => {
|
||||
AppToaster.show({
|
||||
message: formatMessage(
|
||||
{
|
||||
id: 'the_estimate_has_been_successfully_created',
|
||||
},
|
||||
{
|
||||
number: values.estimate_number,
|
||||
},
|
||||
),
|
||||
intent: Intent.SUCCESS,
|
||||
});
|
||||
setSubmitting(false);
|
||||
resetForm();
|
||||
saveInvokeSubmit({ action: 'new', ...payload });
|
||||
clearSavedMediaIds();
|
||||
})
|
||||
.catch((errors) => {
|
||||
setSubmitting(false);
|
||||
});
|
||||
});
|
||||
Promise.all([saveMedia(), deleteMedia()])
|
||||
.then(([savedMediaResponses]) => {
|
||||
const mediaIds = savedMediaResponses.map((res) => res.data.media.id);
|
||||
savedMediaIds.current = mediaIds;
|
||||
return savedMediaResponses;
|
||||
})
|
||||
.then(() => {
|
||||
return saveEstimate(saveEstimate.current);
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
const handleSubmitClick = useCallback(
|
||||
(payload) => {
|
||||
setPaload(payload);
|
||||
formik.submitForm();
|
||||
},
|
||||
[setPaload, formik],
|
||||
);
|
||||
|
||||
const handleCancelClick = useCallback(
|
||||
(payload) => {
|
||||
onCancelForm && onCancelForm(payload);
|
||||
},
|
||||
[onCancelForm],
|
||||
);
|
||||
|
||||
const handleDeleteFile = useCallback(
|
||||
(_deletedFiles) => {
|
||||
_deletedFiles.forEach((deletedFile) => {
|
||||
if (deletedFile.uploaded && deletedFile.metadata.id) {
|
||||
setDeletedFiles([...deletedFiles, deletedFile.metadata.id]);
|
||||
}
|
||||
});
|
||||
},
|
||||
[setDeletedFiles, deletedFiles],
|
||||
);
|
||||
|
||||
const handleClickAddNewRow = () => {
|
||||
formik.setFieldValue(
|
||||
'entries',
|
||||
orderingProductsIndex([...formik.values.entries, defaultEstimate]),
|
||||
);
|
||||
};
|
||||
|
||||
const handleClearAllLines = () => {
|
||||
formik.setFieldValue(
|
||||
'entries',
|
||||
orderingProductsIndex([
|
||||
...repeatValue(defaultEstimate, MIN_LINES_NUMBER),
|
||||
]),
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<form onSubmit={formik.handleSubmit}>
|
||||
<EstimateFormHeader formik={formik} />
|
||||
<EstimatesItemsTable
|
||||
entries={formik.values.entries}
|
||||
onClickAddNewRow={handleClickAddNewRow}
|
||||
onClickClearAllLines={handleClearAllLines}
|
||||
formik={formik}
|
||||
/>
|
||||
<FormGroup
|
||||
label={<T id={'customer_note'} />}
|
||||
className={'form-group--customer_note'}
|
||||
>
|
||||
<TextArea growVertically={true} {...formik.getFieldProps('note')} />
|
||||
</FormGroup>
|
||||
<FormGroup
|
||||
label={<T id={'terms_conditions'} />}
|
||||
className={'form-group--terms_conditions'}
|
||||
>
|
||||
<TextArea
|
||||
growVertically={true}
|
||||
{...formik.getFieldProps('terms_conditions')}
|
||||
/>
|
||||
</FormGroup>
|
||||
<Dragzone
|
||||
initialFiles={initialAttachmentFiles}
|
||||
onDrop={handleDropFiles}
|
||||
onDeleteFile={handleDeleteFile}
|
||||
hint={'Attachments: Maxiumum size: 20MB'}
|
||||
/>
|
||||
|
||||
<EstimateFormFooter
|
||||
formik={formik}
|
||||
onSubmitClick={handleSubmitClick}
|
||||
onCancelClick={handleCancelClick}
|
||||
/>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default compose(
|
||||
withEstimateActions,
|
||||
withDashboardActions,
|
||||
withMediaActions,
|
||||
)(EstimateForm);
|
||||
41
client/src/containers/Sales/Estimate/EstimateFormFooter.js
Normal file
41
client/src/containers/Sales/Estimate/EstimateFormFooter.js
Normal file
@@ -0,0 +1,41 @@
|
||||
import React from 'react';
|
||||
import { Intent, Button } from '@blueprintjs/core';
|
||||
import { FormattedMessage as T } from 'react-intl';
|
||||
|
||||
export default function EstimateFormFooter({
|
||||
formik: { isSubmitting },
|
||||
onSubmitClick,
|
||||
onCancelClick,
|
||||
}) {
|
||||
return (
|
||||
<div className={'estimate-form__floating-footer'}>
|
||||
<Button disabled={isSubmitting} intent={Intent.PRIMARY} type="submit">
|
||||
<T id={'save_send'} />
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
disabled={isSubmitting}
|
||||
intent={Intent.PRIMARY}
|
||||
className={'ml1'}
|
||||
name={'save'}
|
||||
type="submit"
|
||||
>
|
||||
<T id={'save'} />
|
||||
</Button>
|
||||
|
||||
<Button className={'ml1'} disabled={isSubmitting}>
|
||||
<T id={'clear'} />
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
className={'ml1'}
|
||||
type="submit"
|
||||
onClick={() => {
|
||||
onCancelClick && onCancelClick();
|
||||
}}
|
||||
>
|
||||
<T id={'close'} />
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
192
client/src/containers/Sales/Estimate/EstimateFormHeader.js
Normal file
192
client/src/containers/Sales/Estimate/EstimateFormHeader.js
Normal file
@@ -0,0 +1,192 @@
|
||||
import React, { useMemo, useCallback, useState } from 'react';
|
||||
import {
|
||||
FormGroup,
|
||||
InputGroup,
|
||||
Intent,
|
||||
Position,
|
||||
MenuItem,
|
||||
Classes,
|
||||
} from '@blueprintjs/core';
|
||||
import { DateInput } from '@blueprintjs/datetime';
|
||||
import { FormattedMessage as T } from 'react-intl';
|
||||
import { Row, Col } from 'react-grid-system';
|
||||
import moment from 'moment';
|
||||
import { momentFormatter, compose, tansformDateValue } from 'utils';
|
||||
import classNames from 'classnames';
|
||||
import { ListSelect, ErrorMessage, FieldRequiredHint, Hint } from 'components';
|
||||
|
||||
import withCustomers from 'containers/Customers/withCustomers';
|
||||
|
||||
function EstimateFormHeader({
|
||||
formik: { errors, touched, setFieldValue, getFieldProps, values },
|
||||
|
||||
//#withCustomers
|
||||
customers,
|
||||
}) {
|
||||
const handleDateChange = useCallback(
|
||||
(date_filed) => (date) => {
|
||||
const formatted = moment(date).format('YYYY-MM-DD');
|
||||
setFieldValue(date_filed, formatted);
|
||||
},
|
||||
[setFieldValue],
|
||||
);
|
||||
|
||||
const CustomerRenderer = useCallback(
|
||||
(cutomer, { handleClick }) => (
|
||||
<MenuItem
|
||||
key={cutomer.id}
|
||||
text={cutomer.display_name}
|
||||
onClick={handleClick}
|
||||
/>
|
||||
),
|
||||
[],
|
||||
);
|
||||
|
||||
// Filter Customer
|
||||
const filterCustomer = (query, customer, _index, exactMatch) => {
|
||||
const normalizedTitle = customer.display_name.toLowerCase();
|
||||
const normalizedQuery = query.toLowerCase();
|
||||
if (exactMatch) {
|
||||
return normalizedTitle === normalizedQuery;
|
||||
} else {
|
||||
return (
|
||||
`${customer.display_name} ${normalizedTitle}`.indexOf(
|
||||
normalizedQuery,
|
||||
) >= 0
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
// handle change customer
|
||||
const onChangeCustomer = useCallback(
|
||||
(filedName) => {
|
||||
return (customer) => {
|
||||
setFieldValue(filedName, customer.id);
|
||||
};
|
||||
},
|
||||
[setFieldValue],
|
||||
);
|
||||
|
||||
return (
|
||||
<div className={'estimate-form'}>
|
||||
<div className={'estimate-form__primary-section'}>
|
||||
{/* customer name */}
|
||||
<FormGroup
|
||||
label={<T id={'customer_name'} />}
|
||||
inline={true}
|
||||
className={classNames('form-group--select-list', Classes.FILL)}
|
||||
labelInfo={<FieldRequiredHint />}
|
||||
intent={errors.customer_id && touched.customer_id && Intent.DANGER}
|
||||
helperText={
|
||||
<ErrorMessage name={'customer_id'} {...{ errors, touched }} />
|
||||
}
|
||||
>
|
||||
<ListSelect
|
||||
items={customers}
|
||||
noResults={<MenuItem disabled={true} text="No results." />}
|
||||
itemRenderer={CustomerRenderer}
|
||||
itemPredicate={filterCustomer}
|
||||
popoverProps={{ minimal: true }}
|
||||
onItemSelect={onChangeCustomer('customer_id')}
|
||||
selectedItem={values.customer_id}
|
||||
selectedItemProp={'id'}
|
||||
defaultText={<T id={'select_customer_account'} />}
|
||||
labelProp={'display_name'}
|
||||
/>
|
||||
</FormGroup>
|
||||
{/* estimate_date */}
|
||||
<Row>
|
||||
<Col
|
||||
|
||||
// md={9} push={{ md: 3 }}
|
||||
>
|
||||
<FormGroup
|
||||
label={<T id={'estimate_date'} />}
|
||||
inline={true}
|
||||
labelInfo={<FieldRequiredHint />}
|
||||
className={classNames('form-group--select-list', Classes.FILL)}
|
||||
intent={
|
||||
errors.estimate_date && touched.estimate_date && Intent.DANGER
|
||||
}
|
||||
helperText={
|
||||
<ErrorMessage name="estimate_date" {...{ errors, touched }} />
|
||||
}
|
||||
>
|
||||
<DateInput
|
||||
{...momentFormatter('YYYY/MM/DD')}
|
||||
value={tansformDateValue(values.estimate_date)}
|
||||
onChange={handleDateChange('estimate_date')}
|
||||
popoverProps={{ position: Position.BOTTOM, minimal: true }}
|
||||
/>
|
||||
</FormGroup>
|
||||
</Col>
|
||||
<Col
|
||||
|
||||
// md={3} pull={{ md: 9 }}
|
||||
>
|
||||
<FormGroup
|
||||
label={<T id={'expiration_date'} />}
|
||||
inline={true}
|
||||
className={classNames('form-group--select-list', Classes.FILL)}
|
||||
intent={
|
||||
errors.expiration_date &&
|
||||
touched.expiration_date &&
|
||||
Intent.DANGER
|
||||
}
|
||||
helperText={
|
||||
<ErrorMessage name="expiration_date" {...{ errors, touched }} />
|
||||
}
|
||||
>
|
||||
<DateInput
|
||||
{...momentFormatter('YYYY/MM/DD')}
|
||||
value={tansformDateValue(values.expiration_date)}
|
||||
onChange={handleDateChange('expiration_date')}
|
||||
popoverProps={{ position: Position.BOTTOM, minimal: true }}
|
||||
/>
|
||||
</FormGroup>
|
||||
</Col>
|
||||
</Row>
|
||||
</div>
|
||||
{/* Estimate */}
|
||||
<FormGroup
|
||||
label={<T id={'estimate'} />}
|
||||
inline={true}
|
||||
className={('form-group--estimate', Classes.FILL)}
|
||||
labelInfo={<FieldRequiredHint />}
|
||||
intent={
|
||||
errors.estimate_number && touched.estimate_number && Intent.DANGER
|
||||
}
|
||||
helperText={
|
||||
<ErrorMessage name="estimate_number" {...{ errors, touched }} />
|
||||
}
|
||||
>
|
||||
<InputGroup
|
||||
intent={
|
||||
errors.estimate_number && touched.estimate_number && Intent.DANGER
|
||||
}
|
||||
minimal={true}
|
||||
{...getFieldProps('estimate_number')}
|
||||
/>
|
||||
</FormGroup>
|
||||
<FormGroup
|
||||
label={<T id={'reference'} />}
|
||||
inline={true}
|
||||
className={classNames('form-group--reference', Classes.FILL)}
|
||||
intent={errors.reference && touched.reference && Intent.DANGER}
|
||||
helperText={<ErrorMessage name="reference" {...{ errors, touched }} />}
|
||||
>
|
||||
<InputGroup
|
||||
intent={errors.reference && touched.reference && Intent.DANGER}
|
||||
minimal={true}
|
||||
{...getFieldProps('reference')}
|
||||
/>
|
||||
</FormGroup>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(
|
||||
withCustomers(({ customers }) => ({
|
||||
customers,
|
||||
})),
|
||||
)(EstimateFormHeader);
|
||||
120
client/src/containers/Sales/Estimate/EstimateList.js
Normal file
120
client/src/containers/Sales/Estimate/EstimateList.js
Normal file
@@ -0,0 +1,120 @@
|
||||
import React, { useEffect, useCallback, useMemo } from 'react';
|
||||
import { Route, Switch, useHistory } from 'react-router-dom';
|
||||
import { useQuery, queryCache } from 'react-query';
|
||||
import { Alert, Intent } from '@blueprintjs/core';
|
||||
|
||||
import AppToaster from 'components/AppToaster';
|
||||
import { FormattedMessage as T, useIntl } from 'react-intl';
|
||||
import DashboardPageContent from 'components/Dashboard/DashboardPageContent';
|
||||
import DashboardInsider from 'components/Dashboard/DashboardInsider';
|
||||
|
||||
import withDashboardActions from 'containers/Dashboard/withDashboardActions';
|
||||
import withEstimateActions from './withEstimateActions';
|
||||
|
||||
import EstimateActionsBar from './EstimateActionsBar';
|
||||
|
||||
import { compose } from 'utils';
|
||||
|
||||
function EstimateList({
|
||||
// #withDashboardActions
|
||||
changePageTitle,
|
||||
|
||||
// #withViewsActions
|
||||
|
||||
// #withEstimate
|
||||
|
||||
//#withEistimateActions
|
||||
requestFetchEstimatesTable,
|
||||
requestDeleteEstimate,
|
||||
addEstimatesTableQueries,
|
||||
}) {
|
||||
const history = useHistory();
|
||||
const { formatMessage } = useIntl();
|
||||
const [deleteEstimate, setDeleteEstimate] = useState(false);
|
||||
const [selectedRows, setSelectedRows] = useState([]);
|
||||
|
||||
const fetchEstimate = useQuery(['estimate-table'], () =>
|
||||
requestFetchEstimatesTable(),
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
changePageTitle(formatMessage({ id: 'estimate_list' }));
|
||||
}, [changePageTitle, formatMessage]);
|
||||
|
||||
// handle delete estimate click
|
||||
|
||||
const handleDeleteEstimate = useCallback(
|
||||
(estimate) => {
|
||||
setDeleteEstimate(estimate);
|
||||
},
|
||||
[setDeleteEstimate],
|
||||
);
|
||||
|
||||
// handle cancel estimate
|
||||
const handleCancelEstimateDelete = useCallback(() => {
|
||||
setDeleteEstimate(false);
|
||||
}, [setDeleteEstimate]);
|
||||
|
||||
// handle confirm delete estimate
|
||||
const handleConfirmEstimateDelete = useCallback(() => {
|
||||
requestDeleteEstimate(deleteEstimate.id).then((response) => {
|
||||
AppToaster.show({
|
||||
message: formatMessage({
|
||||
id: 'the_estimate_has_been_successfully_deleted',
|
||||
}),
|
||||
intent: Intent.SUCCESS,
|
||||
});
|
||||
setDeleteEstimate(false);
|
||||
});
|
||||
}, [deleteEstimate, requestDeleteEstimate, formatMessage]);
|
||||
|
||||
// Calculates the selected rows
|
||||
const selectedRowsCount = useMemo(() => Object.values(selectedRows).length, [
|
||||
selectedRows,
|
||||
]);
|
||||
|
||||
const handleEidtEstimate = useCallback(
|
||||
(estimate) => {
|
||||
history.push(`/estimates/${estimate.id}/edit`);
|
||||
},
|
||||
[history],
|
||||
);
|
||||
const handleFetchData = useCallback(
|
||||
({ pageIndex, pageSize, sortBy }) => {
|
||||
const page = pageIndex + 1;
|
||||
|
||||
addEstimatesTableQueries({
|
||||
...(sortBy.length > 0
|
||||
? {
|
||||
column_sort_by: sortBy[0].id,
|
||||
sort_order: sortBy[0].desc ? 'desc' : 'asc',
|
||||
}
|
||||
: {}),
|
||||
page_size: pageSize,
|
||||
page,
|
||||
});
|
||||
},
|
||||
[addEstimatesTableQueries],
|
||||
);
|
||||
|
||||
const handleSelectedRowsChange = useCallback((estimate) => {
|
||||
selectedRows(estimate);
|
||||
});
|
||||
|
||||
return (
|
||||
<DashboardInsider name={'estimates'}>
|
||||
<EstimateActionsBar
|
||||
// onBulkDelete={}
|
||||
selectedRows={selectedRows}
|
||||
// onFilterChanged={}
|
||||
/>
|
||||
<DashboardPageContent>
|
||||
<Switch>
|
||||
<Route></Route>
|
||||
</Switch>
|
||||
</DashboardPageContent>
|
||||
</DashboardInsider>
|
||||
);
|
||||
}
|
||||
|
||||
export default EstimateList;
|
||||
49
client/src/containers/Sales/Estimate/Estimates.js
Normal file
49
client/src/containers/Sales/Estimate/Estimates.js
Normal file
@@ -0,0 +1,49 @@
|
||||
import React, { useCallback } from 'react';
|
||||
import { useParams, useHistory } from 'react-router-dom';
|
||||
import { useQuery } from 'react-query';
|
||||
|
||||
import EstimateForm from './EstimateForm';
|
||||
import DashboardInsider from 'components/Dashboard/DashboardInsider';
|
||||
|
||||
import withCustomersActions from 'containers/Customers/withCustomersActions';
|
||||
import withItemsActions from 'containers/Items/withItemsActions';
|
||||
import withEstimateActions from './withEstimateActions';
|
||||
|
||||
import { compose } from 'utils';
|
||||
|
||||
function Estimates({ requestFetchCustomers, requestFetchItems }) {
|
||||
const history = useHistory();
|
||||
const { id } = useParams();
|
||||
|
||||
// Handle fetch customers data table or list
|
||||
const fetchCustomers = useQuery('customers-table', () =>
|
||||
requestFetchCustomers({}),
|
||||
);
|
||||
|
||||
// Handle fetch Items data table or list
|
||||
const fetchItems = useQuery('items-table', () => requestFetchItems({}));
|
||||
|
||||
const handleFormSubmit = useCallback((payload) => {}, [history]);
|
||||
|
||||
const handleCancel = useCallback(() => {
|
||||
history.goBack();
|
||||
}, [history]);
|
||||
|
||||
return (
|
||||
<DashboardInsider
|
||||
loading={fetchCustomers.isFetching || fetchItems.isFetching}
|
||||
>
|
||||
<EstimateForm
|
||||
onFormSubmit={handleFormSubmit}
|
||||
// estimateId={id}
|
||||
onCancelForm={handleCancel}
|
||||
/>
|
||||
</DashboardInsider>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(
|
||||
withEstimateActions,
|
||||
withCustomersActions,
|
||||
withItemsActions,
|
||||
)(Estimates);
|
||||
196
client/src/containers/Sales/Estimate/EstimatesDataTable.js
Normal file
196
client/src/containers/Sales/Estimate/EstimatesDataTable.js
Normal file
@@ -0,0 +1,196 @@
|
||||
import React, { useEffect, useCallback, useState, useMemo } from 'react';
|
||||
import {
|
||||
Intent,
|
||||
Button,
|
||||
Classes,
|
||||
Popover,
|
||||
Tooltip,
|
||||
Menu,
|
||||
MenuItem,
|
||||
MenuDivider,
|
||||
Position,
|
||||
Tag,
|
||||
} from '@blueprintjs/core';
|
||||
import { useParams } from 'react-router-dom';
|
||||
import { withRouter } from 'react-router';
|
||||
import { FormattedMessage as T, useIntl } from 'react-intl';
|
||||
import moment from 'moment';
|
||||
|
||||
import Icon from 'components/Icon';
|
||||
import { compose } from 'utils';
|
||||
import { useUpdateEffect } from 'hooks';
|
||||
|
||||
import LoadingIndicator from 'components/LoadingIndicator';
|
||||
import { If } from 'components';
|
||||
import DataTable from 'components/DataTable';
|
||||
|
||||
import withDialogActions from 'containers/Dialog/withDialogActions';
|
||||
import withDashboardActions from 'containers/Dashboard/withDashboardActions';
|
||||
import withViewDetails from 'containers/Views/withViewDetails';
|
||||
import withEstimates from './withEstimates';
|
||||
import withEstimateActions from './withEstimateActions';
|
||||
|
||||
function EstimatesDataTable({
|
||||
//#withEitimates
|
||||
|
||||
// #withDashboardActions
|
||||
changeCurrentView,
|
||||
changePageSubtitle,
|
||||
setTopbarEditView,
|
||||
|
||||
// #withView
|
||||
viewMeta,
|
||||
|
||||
//#OwnProps
|
||||
loading,
|
||||
onFetchData,
|
||||
onEditEstimate,
|
||||
onDeleteEstimate,
|
||||
onSelectedRowsChange,
|
||||
}) {
|
||||
const [initialMount, setInitialMount] = useState(false);
|
||||
const { custom_view_id: customViewId } = useParams();
|
||||
const { formatMessage } = useIntl();
|
||||
|
||||
useEffect(() => {
|
||||
setInitialMount(false);
|
||||
}, []);
|
||||
|
||||
// useUpdateEffect(() => {
|
||||
// if (!estimateLoading) {
|
||||
// setInitialMount(true);
|
||||
// }
|
||||
// }, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (customViewId) {
|
||||
changeCurrentView(customViewId);
|
||||
setTopbarEditView(customViewId);
|
||||
}
|
||||
changePageSubtitle(customViewId && viewMeta ? viewMeta.name : '');
|
||||
}, [
|
||||
customViewId,
|
||||
changeCurrentView,
|
||||
changePageSubtitle,
|
||||
setTopbarEditView,
|
||||
viewMeta,
|
||||
]);
|
||||
|
||||
const handleEditEstimate = useCallback(
|
||||
(estimate) => {
|
||||
onEditEstimate && onEditEstimate(estimate);
|
||||
},
|
||||
[onEditEstimate],
|
||||
);
|
||||
|
||||
const handleDeleteEstimate = useCallback(() => {
|
||||
onDeleteEstimate && onDeleteEstimate();
|
||||
}, [onDeleteEstimate]);
|
||||
|
||||
const actionMenuList = useCallback(
|
||||
() => (
|
||||
<Menu>
|
||||
<MenuItem text={formatMessage({ id: 'view_details' })} />
|
||||
<MenuDivider />
|
||||
<MenuItem
|
||||
text={formatMessage({ id: 'edit_estimate' })}
|
||||
// onClick={handleEditEstimate(estimate)}
|
||||
/>
|
||||
<MenuItem
|
||||
text={formatMessage({ id: 'delete_estimate' })}
|
||||
intent={Intent.DANGER}
|
||||
// onClick={handleDeleteEstimate(estimate)}
|
||||
/>
|
||||
</Menu>
|
||||
),
|
||||
[handleDeleteEstimate, handleEditEstimate, formatMessage],
|
||||
);
|
||||
|
||||
const columns = useMemo(
|
||||
() => [
|
||||
{
|
||||
id: '',
|
||||
Header: formatMessage({ id: '' }),
|
||||
accessor: '',
|
||||
className: '',
|
||||
},
|
||||
{
|
||||
id: '',
|
||||
Header: formatMessage({ id: '' }),
|
||||
accessor: '',
|
||||
className: '',
|
||||
},
|
||||
{
|
||||
id: '',
|
||||
Header: formatMessage({ id: '' }),
|
||||
accessor: '',
|
||||
className: '',
|
||||
},
|
||||
{
|
||||
id: '',
|
||||
Header: formatMessage({ id: '' }),
|
||||
accessor: '',
|
||||
className: '',
|
||||
},
|
||||
{
|
||||
id: '',
|
||||
Header: formatMessage({ id: '' }),
|
||||
accessor: '',
|
||||
className: '',
|
||||
},
|
||||
{
|
||||
id: 'actions',
|
||||
Header: '',
|
||||
Cell: ({ cell }) => (
|
||||
<Popover
|
||||
content={actionMenuList(cell.row.original)}
|
||||
position={Position.RIGHT_BOTTOM}
|
||||
>
|
||||
<Button icon={<Icon icon="more-h-16" iconSize={16} />} />
|
||||
</Popover>
|
||||
),
|
||||
className: 'actions',
|
||||
width: 50,
|
||||
disableResizing: true,
|
||||
},
|
||||
],
|
||||
[actionMenuList, formatMessage],
|
||||
);
|
||||
|
||||
const handleDataTableFetchData = useCallback(
|
||||
(...arguments) => {
|
||||
onFetchData && onFetchData(...arguments);
|
||||
},
|
||||
[onFetchData],
|
||||
);
|
||||
|
||||
const handleSelectedRowsChange = useCallback(
|
||||
(selectedRows) => {
|
||||
onSelectedRowsChange &&
|
||||
onSelectedRowsChange(selectedRows.map((s) => s.original));
|
||||
},
|
||||
[onSelectedRowsChange],
|
||||
);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<LoadingIndicator loading={loading} mount={false}>
|
||||
<DataTable
|
||||
columns={columns}
|
||||
data={[]}
|
||||
onFetchData={handleDataTableFetchData}
|
||||
manualSortBy={true}
|
||||
/>
|
||||
</LoadingIndicator>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(
|
||||
withRouter,
|
||||
withDialogActions,
|
||||
withDashboardActions,
|
||||
withEstimateActions,
|
||||
// withEstimates(({}) => ({})),
|
||||
withViewDetails(),
|
||||
)(EstimatesDataTable);
|
||||
32
client/src/containers/Sales/Estimate/withEstimateActions.js
Normal file
32
client/src/containers/Sales/Estimate/withEstimateActions.js
Normal file
@@ -0,0 +1,32 @@
|
||||
import { connect } from 'react-redux';
|
||||
import {
|
||||
submitEstimate,
|
||||
editEstimate,
|
||||
deleteEstimate,
|
||||
fetchEstimate,
|
||||
fetchEstimatesTable,
|
||||
} from 'store/Estimate/estimates.actions';
|
||||
import t from 'store/types';
|
||||
|
||||
const mapDipatchToProps = (dispatch) => ({
|
||||
requestSubmitEstimate: (form) => dispatch(submitEstimate({ form })),
|
||||
requsetFetchEstimate: (id) => dispatch(fetchEstimate({ id })),
|
||||
requestEditEstimate: (id, form) => dispatch(editEstimate({ id, form })),
|
||||
requestFetchEstimatesTable: (query = {}) =>
|
||||
dispatch(fetchEstimatesTable({ query: { ...query } })),
|
||||
requestDeleteEstimate: (id) => dispatch(deleteEstimate({ id })),
|
||||
|
||||
changeEstimateView: (id) =>
|
||||
dispatch({
|
||||
type: t.ESTIMATES_SET_CURRENT_VIEW,
|
||||
currentViewId: parseInt(id, 10),
|
||||
}),
|
||||
|
||||
addEstimatesTableQueries: (queries) =>
|
||||
dispatch({
|
||||
type: t.ESTIMATES_TABLE_QUERIES_ADD,
|
||||
queries,
|
||||
}),
|
||||
});
|
||||
|
||||
export default connect(null, mapDipatchToProps);
|
||||
11
client/src/containers/Sales/Estimate/withEstimateDetail.js
Normal file
11
client/src/containers/Sales/Estimate/withEstimateDetail.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import { connect } from 'react-redux';
|
||||
import { getEstimateByIdFactory } from 'store/Estimate/estimates.selectors';
|
||||
|
||||
export default () => {
|
||||
const getEstimateById = getEstimateByIdFactory();
|
||||
|
||||
const mapStateToProps = (state, props) => ({
|
||||
estimate: getEstimateById(state, props),
|
||||
});
|
||||
return connect(mapStateToProps);
|
||||
};
|
||||
24
client/src/containers/Sales/Estimate/withEstimates.js
Normal file
24
client/src/containers/Sales/Estimate/withEstimates.js
Normal file
@@ -0,0 +1,24 @@
|
||||
import { connect } from 'react-redux';
|
||||
import { getResourceViews } from 'store/customViews/customViews.selectors';
|
||||
import {
|
||||
getEstimateCurrentPage,
|
||||
getEstimatesTableQuery,
|
||||
getEstimatesPaginationMetaFactory,
|
||||
} from 'store/Estimate/estimates.selectors';
|
||||
|
||||
function withEstimates(mapSate) {
|
||||
const mapStateToProps = (state, props) => {
|
||||
const query = getEstimatesTableQuery(state, props);
|
||||
const mapped = {
|
||||
estimateViews: getResourceViews(state, props, 'estimates'),
|
||||
estimateItems: state.estiamte.items,
|
||||
estimateTableQuery: query,
|
||||
estimatesLoading: state.estiamte.loading,
|
||||
};
|
||||
return mapSate ? mapSate(mapped, state, props) : mapped;
|
||||
};
|
||||
|
||||
return connect(mapStateToProps);
|
||||
}
|
||||
|
||||
export default withEstimates;
|
||||
133
client/src/containers/Sales/Invoice/InvoiceActionsBar.js
Normal file
133
client/src/containers/Sales/Invoice/InvoiceActionsBar.js
Normal file
@@ -0,0 +1,133 @@
|
||||
import React, { useMemo, useCallback } from 'react';
|
||||
import Icon from 'components/Icon';
|
||||
import {
|
||||
Button,
|
||||
Classes,
|
||||
Menu,
|
||||
MenuItem,
|
||||
Popover,
|
||||
NavbarDivider,
|
||||
NavbarGroup,
|
||||
PopoverInteractionKind,
|
||||
Position,
|
||||
Intent,
|
||||
} from '@blueprintjs/core';
|
||||
|
||||
import classNames from 'classnames';
|
||||
import { useRouteMatch, useHistory } from 'react-router-dom';
|
||||
import { FormattedMessage as T } from 'react-intl';
|
||||
|
||||
import { If } from 'components';
|
||||
import FilterDropdown from 'components/FilterDropdown';
|
||||
import DashboardActionsBar from 'components/Dashboard/DashboardActionsBar';
|
||||
|
||||
import withResourceDetail from 'containers/Resources/withResourceDetails';
|
||||
import withDialogActions from 'containers/Dialog/withDialogActions';
|
||||
import withInvoiceActions from './withInvoices';
|
||||
|
||||
import { compose } from 'utils';
|
||||
import { connect } from 'react-redux';
|
||||
|
||||
function InvoiceActionsBar({
|
||||
// #withResourceDetail
|
||||
resourceFields,
|
||||
|
||||
//#withInvoice
|
||||
InvoiceViews,
|
||||
|
||||
// #withInvoiceActions
|
||||
addInvoiceTableQueries,
|
||||
|
||||
// #own Porps
|
||||
onFilterChanged,
|
||||
selectedRows,
|
||||
}) {
|
||||
const history = useHistory();
|
||||
|
||||
const FilterDropdown = FilterDropdown({
|
||||
initialCondition: {
|
||||
fieldKey: '',
|
||||
compatator: '',
|
||||
value: '',
|
||||
},
|
||||
fields: resourceFields,
|
||||
onFilterChange: (filterConditions) => {
|
||||
addInvoiceTableQueries({
|
||||
filter_roles: filterConditions || '',
|
||||
});
|
||||
onFilterChanged && onFilterChanged(filterConditions);
|
||||
},
|
||||
});
|
||||
|
||||
const hasSelectedRows = useMemo(() => selectedRows.length > 0, [
|
||||
selectedRows,
|
||||
]);
|
||||
|
||||
return (
|
||||
<DashboardActionsBar>
|
||||
<NavbarGroup>
|
||||
<NavbarDivider />
|
||||
<Button
|
||||
className={Classes.MINIMAL}
|
||||
icon={<Icon icon={'plus'} />}
|
||||
text={<T id={'new_invoice'} />}
|
||||
onClick={onClickNewInvoice}
|
||||
/>
|
||||
<Popover
|
||||
minimal={true}
|
||||
content={FilterDropdown}
|
||||
interactionKind={PopoverInteractionKind.CLICK}
|
||||
position={Position.BOTTOM_LEFT}
|
||||
>
|
||||
<Button
|
||||
className={classNames(Classes.MINIMAL)}
|
||||
text={'Filter'}
|
||||
icon={<Icon icon={'filter-16'} iconSize={16} />}
|
||||
/>
|
||||
</Popover>
|
||||
<If condition={hasSelectedRows}>
|
||||
<Button
|
||||
className={Classes.MINIMAL}
|
||||
icon={<Icon icon={'trash-16'} iconSize={16} />}
|
||||
text={<T id={'delete'} />}
|
||||
intent={Intent.DANGER}
|
||||
// onClick={handleBulkDelete}
|
||||
/>
|
||||
</If>
|
||||
<Button
|
||||
className={Classes.MINIMAL}
|
||||
icon={<Icon icon={'print-16'} iconSize={'16'} />}
|
||||
text={<T id={'print'} />}
|
||||
/>
|
||||
<Button
|
||||
className={Classes.MINIMAL}
|
||||
icon={<Icon icon={'file-import-16'} />}
|
||||
text={<T id={'import'} />}
|
||||
/>
|
||||
<Button
|
||||
className={Classes.MINIMAL}
|
||||
icon={<Icon icon={'file-export-16'} iconSize={'16'} />}
|
||||
text={<T id={'export'} />}
|
||||
/>
|
||||
</NavbarGroup>
|
||||
</DashboardActionsBar>
|
||||
);
|
||||
}
|
||||
|
||||
const mapStateToProps = (state, props) => ({
|
||||
resourceName: 'invoice',
|
||||
});
|
||||
|
||||
const withInvoiceActionsBar = connect(mapStateToProps);
|
||||
|
||||
export default compose(
|
||||
withInvoiceActionsBar,
|
||||
withDialogActions,
|
||||
withResourceDetail(({ resourceFields }) => ({
|
||||
resourceFields,
|
||||
})),
|
||||
// withInvoices(({ invoiceViews }) => ({
|
||||
// invoiceViews,
|
||||
// })),
|
||||
withInvoiceActions,
|
||||
)(InvoiceActionsBar);
|
||||
321
client/src/containers/Sales/Invoice/InvoiceForm.js
Normal file
321
client/src/containers/Sales/Invoice/InvoiceForm.js
Normal file
@@ -0,0 +1,321 @@
|
||||
import React, {
|
||||
useMemo,
|
||||
useCallback,
|
||||
useEffect,
|
||||
useRef,
|
||||
useState,
|
||||
} from 'react';
|
||||
import * as Yup from 'yup';
|
||||
import { useFormik } from 'formik';
|
||||
import moment from 'moment';
|
||||
import { Intent, FormGroup, TextArea, Button } from '@blueprintjs/core';
|
||||
import { FormattedMessage as T, useIntl } from 'react-intl';
|
||||
import { pick, omit } from 'lodash';
|
||||
|
||||
import InvoiceFormHeader from './InvoiceFormHeader';
|
||||
import EstimatesItemsTable from 'containers/Sales/Estimate/EntriesItemsTable';
|
||||
import InvoiceFormFooter from './InvoiceFormFooter';
|
||||
|
||||
import withDashboardActions from 'containers/Dashboard/withDashboardActions';
|
||||
import withMediaActions from 'containers/Media/withMediaActions';
|
||||
import withInvoiceActions from './withInvoiceActions';
|
||||
|
||||
import { AppToaster } from 'components';
|
||||
import Dragzone from 'components/Dragzone';
|
||||
import useMedia from 'hooks/useMedia';
|
||||
|
||||
import { compose, repeatValue } from 'utils';
|
||||
|
||||
const MIN_LINES_NUMBER = 4;
|
||||
|
||||
function InvoiceForm({
|
||||
//#WithMedia
|
||||
requestSubmitMedia,
|
||||
requestDeleteMedia,
|
||||
|
||||
//#WithInvoiceActions
|
||||
requestSubmitInvoice,
|
||||
|
||||
//#withDashboard
|
||||
changePageTitle,
|
||||
changePageSubtitle,
|
||||
|
||||
//#withInvoiceDetail
|
||||
invoice,
|
||||
|
||||
//#own Props
|
||||
InvoiceId,
|
||||
onFormSubmit,
|
||||
onCancelForm,
|
||||
}) {
|
||||
const { formatMessage } = useIntl();
|
||||
const [payload, setPaload] = useState({});
|
||||
|
||||
const {
|
||||
setFiles,
|
||||
saveMedia,
|
||||
deletedFiles,
|
||||
setDeletedFiles,
|
||||
deleteMedia,
|
||||
} = useMedia({
|
||||
saveCallback: requestSubmitMedia,
|
||||
deleteCallback: requestDeleteMedia,
|
||||
});
|
||||
|
||||
const handleDropFiles = useCallback((_files) => {
|
||||
setFiles(_files.filter((file) => file.uploaded === false));
|
||||
}, []);
|
||||
|
||||
const savedMediaIds = useRef([]);
|
||||
const clearSavedMediaIds = () => {
|
||||
savedMediaIds.current = [];
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (invoice && invoice.id) {
|
||||
changePageTitle(formatMessage({ id: 'edit_invoice' }));
|
||||
} else {
|
||||
changePageTitle(formatMessage({ id: 'new_invoice' }));
|
||||
}
|
||||
}, [changePageTitle, invoice, formatMessage]);
|
||||
|
||||
const validationSchema = Yup.object().shape({
|
||||
customer_id: Yup.string()
|
||||
.label(formatMessage({ id: 'customer_name_' }))
|
||||
.required(),
|
||||
invoice_date: Yup.date()
|
||||
.required()
|
||||
.label(formatMessage({ id: 'invoice_date_' })),
|
||||
due_date: Yup.date()
|
||||
.required()
|
||||
.label(formatMessage({ id: 'due_date_' })),
|
||||
invoice_no: Yup.number()
|
||||
.required()
|
||||
.label(formatMessage({ id: 'invoice_no_' })),
|
||||
reference_no: Yup.string().min(1).max(255),
|
||||
status: Yup.string().required(),
|
||||
invoice_message: Yup.string()
|
||||
.trim()
|
||||
.min(1)
|
||||
.max(1024)
|
||||
.label(formatMessage({ id: 'note' })),
|
||||
terms_conditions: Yup.string()
|
||||
.trim()
|
||||
.min(1)
|
||||
.max(1024)
|
||||
.label(formatMessage({ id: 'note' })),
|
||||
|
||||
entries: Yup.array().of(
|
||||
Yup.object().shape({
|
||||
quantity: Yup.number().nullable(),
|
||||
rate: Yup.number().nullable(),
|
||||
item_id: Yup.number()
|
||||
.nullable()
|
||||
.when(['quantity', 'rate'], {
|
||||
is: (quantity, rate) => quantity || rate,
|
||||
then: Yup.number().required(),
|
||||
}),
|
||||
total: Yup.number().nullable(),
|
||||
discount: Yup.number().nullable(),
|
||||
description: Yup.string().nullable(),
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
const saveInvokeSubmit = useCallback(
|
||||
(payload) => {
|
||||
onFormSubmit && onFormSubmit(payload);
|
||||
},
|
||||
[onFormSubmit],
|
||||
);
|
||||
|
||||
const defaultInvoice = useMemo(
|
||||
() => ({
|
||||
index: 0,
|
||||
item_id: null,
|
||||
rate: null,
|
||||
discount: null,
|
||||
quantity: null,
|
||||
description: '',
|
||||
}),
|
||||
[],
|
||||
);
|
||||
const defaultInitialValues = useMemo(
|
||||
() => ({
|
||||
customer_id: '',
|
||||
invoice_date: moment(new Date()).format('YYYY-MM-DD'),
|
||||
due_date: moment(new Date()).format('YYYY-MM-DD'),
|
||||
status: 'status',
|
||||
invoice_no: '',
|
||||
reference_no: '',
|
||||
invoice_message: '',
|
||||
terms_conditions: '',
|
||||
entries: [...repeatValue(defaultInvoice, MIN_LINES_NUMBER)],
|
||||
}),
|
||||
[defaultInvoice],
|
||||
);
|
||||
|
||||
const orderingIndex = (_invoice) => {
|
||||
return _invoice.map((item, index) => ({
|
||||
...item,
|
||||
index: index + 1,
|
||||
}));
|
||||
};
|
||||
|
||||
const initialValues = useMemo(
|
||||
() => ({
|
||||
...defaultInitialValues,
|
||||
entries: orderingIndex(defaultInitialValues.entries),
|
||||
}),
|
||||
[defaultInvoice, defaultInitialValues, invoice],
|
||||
);
|
||||
|
||||
const initialAttachmentFiles = useMemo(() => {
|
||||
return invoice && invoice.media
|
||||
? invoice.media.map((attach) => ({
|
||||
preview: attach.attachment_file,
|
||||
uploaded: true,
|
||||
metadata: { ...attach },
|
||||
}))
|
||||
: [];
|
||||
}, [invoice]);
|
||||
|
||||
const formik = useFormik({
|
||||
enableReinitialize: true,
|
||||
validationSchema,
|
||||
initialValues: {
|
||||
...initialValues,
|
||||
},
|
||||
onSubmit: async (values, { setSubmitting, setErrors, resetForm }) => {
|
||||
setSubmitting(true);
|
||||
const entries = values.entries.map((item) => omit(item, ['total']));
|
||||
|
||||
const form = {
|
||||
...values,
|
||||
entries,
|
||||
};
|
||||
const saveInvoice = (mediaIds) =>
|
||||
new Promise((resolve, reject) => {
|
||||
const requestForm = { ...form, media_ids: mediaIds };
|
||||
|
||||
requestSubmitInvoice(requestForm)
|
||||
.then((response) => {
|
||||
AppToaster.show({
|
||||
message: formatMessage(
|
||||
{ id: 'the_invocie_has_been_successfully_created' },
|
||||
{ number: values.invoice_no },
|
||||
),
|
||||
intent: Intent.SUCCESS,
|
||||
});
|
||||
setSubmitting(false);
|
||||
resetForm();
|
||||
saveInvokeSubmit({ action: 'new', ...payload });
|
||||
clearSavedMediaIds();
|
||||
})
|
||||
.catch((errors) => {
|
||||
setSubmitting(false);
|
||||
});
|
||||
});
|
||||
|
||||
Promise.all([saveMedia(), deleteMedia()])
|
||||
.then(([savedMediaResponses]) => {
|
||||
const mediaIds = savedMediaResponses.map((res) => res.data.media.id);
|
||||
savedMediaIds.current = mediaIds;
|
||||
return savedMediaResponses;
|
||||
})
|
||||
.then(() => {
|
||||
return saveInvoice(savedMediaIds.current);
|
||||
});
|
||||
},
|
||||
});
|
||||
const handleSubmitClick = useCallback(
|
||||
(payload) => {
|
||||
setPaload(payload);
|
||||
formik.submitForm();
|
||||
},
|
||||
[setPaload, formik],
|
||||
);
|
||||
|
||||
const handleCancelClick = useCallback(
|
||||
(payload) => {
|
||||
onCancelForm && onCancelForm(payload);
|
||||
},
|
||||
[onCancelForm],
|
||||
);
|
||||
|
||||
console.log(formik.errors, 'Errors');
|
||||
const handleDeleteFile = useCallback(
|
||||
(_deletedFiles) => {
|
||||
_deletedFiles.forEach((deletedFile) => {
|
||||
if (deletedFile.uploaded && deletedFile.metadata.id) {
|
||||
setDeletedFiles([...deletedFiles, deletedFile.metadata.id]);
|
||||
}
|
||||
});
|
||||
},
|
||||
[setDeletedFiles, deletedFiles],
|
||||
);
|
||||
|
||||
const handleClickAddNewRow = () => {
|
||||
formik.setFieldValue(
|
||||
'entries',
|
||||
orderingIndex([...formik.values.entries, defaultInvoice]),
|
||||
);
|
||||
};
|
||||
|
||||
const handleClearAllLines = () => {
|
||||
formik.setFieldValue(
|
||||
'entries',
|
||||
orderingIndex([...repeatValue(defaultInvoice, MIN_LINES_NUMBER)]),
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={'invoice-form'}>
|
||||
<form onSubmit={formik.handleSubmit}>
|
||||
<InvoiceFormHeader formik={formik} />
|
||||
<EstimatesItemsTable
|
||||
entries={formik.values.entries}
|
||||
onClickAddNewRow={handleClickAddNewRow}
|
||||
onClickClearAllLines={handleClearAllLines}
|
||||
formik={formik}
|
||||
/>
|
||||
<FormGroup
|
||||
label={<T id={'invoice_message'} />}
|
||||
className={'form-group--customer_note'}
|
||||
>
|
||||
<TextArea
|
||||
growVertically={true}
|
||||
{...formik.getFieldProps('invoice_message')}
|
||||
/>
|
||||
</FormGroup>
|
||||
<FormGroup
|
||||
label={<T id={'terms_conditions'} />}
|
||||
className={'form-group--terms_conditions'}
|
||||
>
|
||||
<TextArea
|
||||
growVertically={true}
|
||||
{...formik.getFieldProps('terms_conditions')}
|
||||
/>
|
||||
</FormGroup>
|
||||
<Dragzone
|
||||
initialFiles={initialAttachmentFiles}
|
||||
onDrop={handleDropFiles}
|
||||
onDeleteFile={handleDeleteFile}
|
||||
hint={'Attachments: Maxiumum size: 20MB'}
|
||||
/>
|
||||
|
||||
<InvoiceFormFooter
|
||||
formik={formik}
|
||||
onSubmitClick={handleSubmitClick}
|
||||
onCancelClick={handleCancelClick}
|
||||
/>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(
|
||||
withInvoiceActions,
|
||||
withDashboardActions,
|
||||
withMediaActions,
|
||||
)(InvoiceForm);
|
||||
41
client/src/containers/Sales/Invoice/InvoiceFormFooter.js
Normal file
41
client/src/containers/Sales/Invoice/InvoiceFormFooter.js
Normal file
@@ -0,0 +1,41 @@
|
||||
import React from 'react';
|
||||
import { Intent, Button } from '@blueprintjs/core';
|
||||
import { FormattedMessage as T } from 'react-intl';
|
||||
|
||||
export default function EstimateFormFooter({
|
||||
formik: { isSubmitting },
|
||||
onSubmitClick,
|
||||
onCancelClick,
|
||||
}) {
|
||||
return (
|
||||
<div className={'estimate-form__floating-footer'}>
|
||||
<Button disabled={isSubmitting} intent={Intent.PRIMARY} type="submit">
|
||||
<T id={'save_send'} />
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
disabled={isSubmitting}
|
||||
intent={Intent.PRIMARY}
|
||||
className={'ml1'}
|
||||
name={'save'}
|
||||
type="submit"
|
||||
>
|
||||
<T id={'save'} />
|
||||
</Button>
|
||||
|
||||
<Button className={'ml1'} disabled={isSubmitting}>
|
||||
<T id={'clear'} />
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
className={'ml1'}
|
||||
type="submit"
|
||||
onClick={() => {
|
||||
onCancelClick && onCancelClick();
|
||||
}}
|
||||
>
|
||||
<T id={'close'} />
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
177
client/src/containers/Sales/Invoice/InvoiceFormHeader.js
Normal file
177
client/src/containers/Sales/Invoice/InvoiceFormHeader.js
Normal file
@@ -0,0 +1,177 @@
|
||||
import React, { useMemo, useCallback, useState } from 'react';
|
||||
import {
|
||||
FormGroup,
|
||||
InputGroup,
|
||||
Intent,
|
||||
Position,
|
||||
MenuItem,
|
||||
Classes,
|
||||
} from '@blueprintjs/core';
|
||||
import { DateInput } from '@blueprintjs/datetime';
|
||||
import { FormattedMessage as T } from 'react-intl';
|
||||
import { Row, Col } from 'react-grid-system';
|
||||
import moment from 'moment';
|
||||
import { momentFormatter, compose, tansformDateValue } from 'utils';
|
||||
import classNames from 'classnames';
|
||||
import { ListSelect, ErrorMessage, FieldRequiredHint, Hint } from 'components';
|
||||
|
||||
import withCustomers from 'containers/Customers/withCustomers';
|
||||
|
||||
function InvoiceFormHeader({
|
||||
formik: { errors, touched, setFieldValue, getFieldProps, values },
|
||||
|
||||
//#withCustomers
|
||||
customers,
|
||||
}) {
|
||||
const handleDateChange = useCallback(
|
||||
(date_filed) => (date) => {
|
||||
const formatted = moment(date).format('YYYY-MM-DD');
|
||||
setFieldValue(date_filed, formatted);
|
||||
},
|
||||
[setFieldValue],
|
||||
);
|
||||
|
||||
const CustomerRenderer = useCallback(
|
||||
(cutomer, { handleClick }) => (
|
||||
<MenuItem
|
||||
key={cutomer.id}
|
||||
text={cutomer.display_name}
|
||||
onClick={handleClick}
|
||||
/>
|
||||
),
|
||||
[],
|
||||
);
|
||||
|
||||
// Filter Customer
|
||||
const filterCustomer = (query, customer, _index, exactMatch) => {
|
||||
const normalizedTitle = customer.display_name.toLowerCase();
|
||||
const normalizedQuery = query.toLowerCase();
|
||||
if (exactMatch) {
|
||||
return normalizedTitle === normalizedQuery;
|
||||
} else {
|
||||
return (
|
||||
`${customer.display_name} ${normalizedTitle}`.indexOf(
|
||||
normalizedQuery,
|
||||
) >= 0
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
// handle change customer
|
||||
const onChangeCustomer = useCallback(
|
||||
(filedName) => {
|
||||
return (customer) => {
|
||||
setFieldValue(filedName, customer.id);
|
||||
};
|
||||
},
|
||||
[setFieldValue],
|
||||
);
|
||||
|
||||
return (
|
||||
<div className={'invoice-form'}>
|
||||
<div className={'invoice__primary-section'}>
|
||||
{/* customer name */}
|
||||
<FormGroup
|
||||
label={<T id={'customer_name'} />}
|
||||
inline={true}
|
||||
className={classNames('form-group--select-list', Classes.FILL)}
|
||||
labelInfo={<FieldRequiredHint />}
|
||||
intent={errors.customer_id && touched.customer_id && Intent.DANGER}
|
||||
helperText={
|
||||
<ErrorMessage name={'customer_id'} {...{ errors, touched }} />
|
||||
}
|
||||
>
|
||||
<ListSelect
|
||||
items={customers}
|
||||
noResults={<MenuItem disabled={true} text="No results." />}
|
||||
itemRenderer={CustomerRenderer}
|
||||
itemPredicate={filterCustomer}
|
||||
popoverProps={{ minimal: true }}
|
||||
onItemSelect={onChangeCustomer('customer_id')}
|
||||
selectedItem={values.customer_id}
|
||||
selectedItemProp={'id'}
|
||||
defaultText={<T id={'select_customer_account'} />}
|
||||
labelProp={'display_name'}
|
||||
/>
|
||||
</FormGroup>
|
||||
<Row>
|
||||
<Col>
|
||||
<FormGroup
|
||||
label={<T id={'invoice_date'} />}
|
||||
inline={true}
|
||||
labelInfo={<FieldRequiredHint />}
|
||||
className={classNames('form-group--select-list', Classes.FILL)}
|
||||
intent={
|
||||
errors.invoice_date && touched.invoice_date && Intent.DANGER
|
||||
}
|
||||
helperText={
|
||||
<ErrorMessage name="invoice_date" {...{ errors, touched }} />
|
||||
}
|
||||
>
|
||||
<DateInput
|
||||
{...momentFormatter('YYYY/MM/DD')}
|
||||
value={tansformDateValue(values.invoice_date)}
|
||||
onChange={handleDateChange('invoice_date')}
|
||||
popoverProps={{ position: Position.BOTTOM, minimal: true }}
|
||||
/>
|
||||
</FormGroup>
|
||||
</Col>
|
||||
<Col>
|
||||
<FormGroup
|
||||
label={<T id={'due_date'} />}
|
||||
inline={true}
|
||||
className={classNames('form-group--select-list', Classes.FILL)}
|
||||
intent={errors.due_date && touched.due_date && Intent.DANGER}
|
||||
helperText={
|
||||
<ErrorMessage name="due_date" {...{ errors, touched }} />
|
||||
}
|
||||
>
|
||||
<DateInput
|
||||
{...momentFormatter('YYYY/MM/DD')}
|
||||
value={tansformDateValue(values.due_date)}
|
||||
onChange={handleDateChange('due_date')}
|
||||
popoverProps={{ position: Position.BOTTOM, minimal: true }}
|
||||
/>
|
||||
</FormGroup>
|
||||
</Col>
|
||||
</Row>
|
||||
{/* invoice */}
|
||||
<FormGroup
|
||||
label={<T id={'invoice_no'} />}
|
||||
inline={true}
|
||||
className={('form-group--estimate', Classes.FILL)}
|
||||
labelInfo={<FieldRequiredHint />}
|
||||
intent={errors.invoice_no && touched.invoice_no && Intent.DANGER}
|
||||
helperText={
|
||||
<ErrorMessage name="invoice_no" {...{ errors, touched }} />
|
||||
}
|
||||
>
|
||||
<InputGroup
|
||||
intent={errors.invoice_no && touched.invoice_no && Intent.DANGER}
|
||||
minimal={true}
|
||||
{...getFieldProps('invoice_no')}
|
||||
/>
|
||||
</FormGroup>
|
||||
</div>
|
||||
<FormGroup
|
||||
label={<T id={'reference'} />}
|
||||
inline={true}
|
||||
className={classNames('form-group--reference', Classes.FILL)}
|
||||
intent={errors.reference_no && touched.reference_no && Intent.DANGER}
|
||||
helperText={<ErrorMessage name="reference" {...{ errors, touched }} />}
|
||||
>
|
||||
<InputGroup
|
||||
intent={errors.reference_no && touched.reference_no && Intent.DANGER}
|
||||
minimal={true}
|
||||
{...getFieldProps('reference_no')}
|
||||
/>
|
||||
</FormGroup>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(
|
||||
withCustomers(({ customers }) => ({
|
||||
customers,
|
||||
})),
|
||||
)(InvoiceFormHeader);
|
||||
109
client/src/containers/Sales/Invoice/InvoiceList.js
Normal file
109
client/src/containers/Sales/Invoice/InvoiceList.js
Normal file
@@ -0,0 +1,109 @@
|
||||
import React, { useEffect, useCallback, useMemo, useState } from 'react';
|
||||
import { Route, Switch, useHistory } from 'react-router-dom';
|
||||
import { useQuery, queryCache } from 'react-query';
|
||||
import { Alert, Intent } from '@blueprintjs/core';
|
||||
|
||||
import AppToaster from 'components/AppToaster';
|
||||
import { FormattedMessage as T, useIntl } from 'react-intl';
|
||||
import DashboardPageContent from 'components/Dashboard/DashboardPageContent';
|
||||
import DashboardInsider from 'components/Dashboard/DashboardInsider';
|
||||
|
||||
import withDashboardActions from 'containers/Dashboard/withDashboardActions';
|
||||
// import withInvoiceActions from './withInvoiceActions'
|
||||
|
||||
// import InvoiceActionsBar from './InvoiceActionsBar';
|
||||
|
||||
import { compose } from 'utils';
|
||||
import InvoiceActionsBar from './InvoiceActionsBar';
|
||||
|
||||
function InvoiceList({
|
||||
// #withDashboardActions
|
||||
changePageTitle,
|
||||
|
||||
// #withViewsActions
|
||||
|
||||
//#withInvoice
|
||||
|
||||
//#withInvoiceActions
|
||||
}) {
|
||||
const history = useHistory();
|
||||
const { formatMessage } = useIntl();
|
||||
const [deleteInvoice, setDeleteInvoice] = useState(false);
|
||||
const [selectedRows, setSelectedRows] = useState([]);
|
||||
|
||||
useEffect(() => {
|
||||
changePageTitle(formatMessage({ id: 'invoice_list' }));
|
||||
}, [changePageTitle, formatMessage]);
|
||||
|
||||
//handle dalete Invoice
|
||||
const handleDeleteInvoice = useCallback(
|
||||
(invoice) => {
|
||||
setDeleteInvoice(invoice);
|
||||
},
|
||||
[setDeleteInvoice],
|
||||
);
|
||||
|
||||
// handle cancel Invoice
|
||||
const handleCancelInvoiceDelete = useCallback(() => {
|
||||
setDeleteInvoice(false);
|
||||
}, [setDeleteInvoice]);
|
||||
|
||||
// handleConfirm delete invoice
|
||||
const handleConfirmInvoiceDelete = useCallback(() => {
|
||||
requestDeleteInvoice(deleteInvoice.id).then(() => {
|
||||
AppToaster.show({
|
||||
message: formatMessage({
|
||||
id: 'the_invocie_has_been_successfully_deleted',
|
||||
}),
|
||||
intent: Intent.SUCCESS,
|
||||
});
|
||||
setDeleteInvoice(false);
|
||||
});
|
||||
}, [setDeleteInvoice, requestDeleteInvoice]);
|
||||
|
||||
const handleEditInvoice = useCallback((invoice) => {
|
||||
history.push(`/invoices/${invoice.id}/edit`);
|
||||
});
|
||||
|
||||
const fetchInvoice = useQuery(['invoice-table'], () =>
|
||||
requsetFetchInvoiceTable(),
|
||||
);
|
||||
|
||||
const handleFetchData = useCallback(
|
||||
({ pageIndex, pageSize, sortBy }) => {
|
||||
const page = pageIndex + 1;
|
||||
|
||||
addInvoiceTableQueries({
|
||||
...(sortBy.length > 0
|
||||
? {
|
||||
column_sort_by: sortBy[0].id,
|
||||
sort_order: sortBy[0].desc ? 'desc' : 'asc',
|
||||
}
|
||||
: {}),
|
||||
page_size: pageSize,
|
||||
page,
|
||||
});
|
||||
},
|
||||
[addInvoiceTableQueries],
|
||||
);
|
||||
const handleSelectedRowsChange = useCallback((_invoice) => {
|
||||
selectedRows(_invoice);
|
||||
});
|
||||
|
||||
return (
|
||||
<DashboardInsider name={'invoices'}>
|
||||
<InvoiceActionsBar
|
||||
// onBulkDelete={}
|
||||
selectedRows={selectedRows}
|
||||
// onFilterChanged={}
|
||||
/>
|
||||
<DashboardPageContent>
|
||||
<Switch>
|
||||
<Route></Route>
|
||||
</Switch>
|
||||
</DashboardPageContent>
|
||||
</DashboardInsider>
|
||||
);
|
||||
}
|
||||
|
||||
export default InvoiceList;
|
||||
49
client/src/containers/Sales/Invoice/Invoices.js
Normal file
49
client/src/containers/Sales/Invoice/Invoices.js
Normal file
@@ -0,0 +1,49 @@
|
||||
import React, { useCallback } from 'react';
|
||||
import { useParams, useHistory } from 'react-router-dom';
|
||||
import { useQuery } from 'react-query';
|
||||
|
||||
import InvoiceForm from './InvoiceForm';
|
||||
import DashboardInsider from 'components/Dashboard/DashboardInsider';
|
||||
|
||||
import withCustomersActions from 'containers/Customers/withCustomersActions';
|
||||
import withItemsActions from 'containers/Items/withItemsActions';
|
||||
import withInvoiceActions from './withInvoiceActions';
|
||||
|
||||
import { compose } from 'utils';
|
||||
|
||||
function Invoices({ requestFetchCustomers, requestFetchItems }) {
|
||||
const history = useHistory();
|
||||
const { id } = useParams();
|
||||
|
||||
// Handle fetch Items data table or list
|
||||
const fetchItems = useQuery('items-table', () => requestFetchItems({}));
|
||||
|
||||
const handleFormSubmit = useCallback((payload) => {}, [history]);
|
||||
|
||||
// Handle fetch customers data table or list
|
||||
const fetchCustomers = useQuery('customers-table', () =>
|
||||
requestFetchCustomers({}),
|
||||
);
|
||||
|
||||
const handleCancel = useCallback(() => {
|
||||
history.goBack();
|
||||
}, [history]);
|
||||
|
||||
return (
|
||||
<DashboardInsider
|
||||
loading={fetchCustomers.isFetching || fetchItems.isFetching}
|
||||
>
|
||||
<InvoiceForm
|
||||
onFormSubmit={handleFormSubmit}
|
||||
// InvoiceId={id}
|
||||
onCancelForm={handleCancel}
|
||||
/>
|
||||
</DashboardInsider>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(
|
||||
withInvoiceActions,
|
||||
withCustomersActions,
|
||||
withItemsActions,
|
||||
)(Invoices);
|
||||
189
client/src/containers/Sales/Invoice/InvoicesDataTable.js
Normal file
189
client/src/containers/Sales/Invoice/InvoicesDataTable.js
Normal file
@@ -0,0 +1,189 @@
|
||||
import React, { useEffect, useCallback, useState, useMemo } from 'react';
|
||||
import {
|
||||
Intent,
|
||||
Button,
|
||||
Classes,
|
||||
Popover,
|
||||
Tooltip,
|
||||
Menu,
|
||||
MenuItem,
|
||||
MenuDivider,
|
||||
Position,
|
||||
Tag,
|
||||
} from '@blueprintjs/core';
|
||||
import { useParams } from 'react-router-dom';
|
||||
import { withRouter } from 'react-router';
|
||||
import { FormattedMessage as T, useIntl } from 'react-intl';
|
||||
import moment from 'moment';
|
||||
|
||||
import Icon from 'components/Icon';
|
||||
import { compose } from 'utils';
|
||||
import { useUpdateEffect } from 'hooks';
|
||||
|
||||
import LoadingIndicator from 'components/LoadingIndicator';
|
||||
import { If } from 'components';
|
||||
import DataTable from 'components/DataTable';
|
||||
|
||||
import withDialogActions from 'containers/Dialog/withDialogActions';
|
||||
import withDashboardActions from 'containers/Dashboard/withDashboardActions';
|
||||
import withViewDetails from 'containers/Views/withViewDetails';
|
||||
|
||||
import witInvoice from './withInvoice';
|
||||
import withInvoiceActions from './withInvoiceActions';
|
||||
|
||||
function InvoicesDataTable({
|
||||
//#withInvoices
|
||||
|
||||
// #withDashboardActions
|
||||
changeCurrentView,
|
||||
changePageSubtitle,
|
||||
setTopbarEditView,
|
||||
|
||||
// #withView
|
||||
viewMeta,
|
||||
|
||||
//#OwnProps
|
||||
loading,
|
||||
onFetchData,
|
||||
onEditEstimate,
|
||||
onDeleteEstimate,
|
||||
onSelectedRowsChange,
|
||||
}) {
|
||||
const [initialMount, setInitialMount] = useState(false);
|
||||
const { custom_view_id: customViewId } = useParams();
|
||||
const { formatMessage } = useIntl();
|
||||
|
||||
useEffect(() => {
|
||||
setInitialMount(false);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (customViewId) {
|
||||
changeCurrentView(customViewId);
|
||||
setTopbarEditView(customViewId);
|
||||
}
|
||||
changePageSubtitle(customViewId && viewMeta ? viewMeta.name : '');
|
||||
}, [
|
||||
customViewId,
|
||||
changeCurrentView,
|
||||
changePageSubtitle,
|
||||
setTopbarEditView,
|
||||
viewMeta,
|
||||
]);
|
||||
|
||||
const handleEditInvoice = useCallback(
|
||||
(_invoice) => {
|
||||
onEditInvoice && onEditInvoice(_invoice);
|
||||
},
|
||||
[onEditInvoice],
|
||||
);
|
||||
|
||||
const handleDeleteInvoice = useCallback(() => {
|
||||
onDeleteInvoice && onDeleteInvoice();
|
||||
}, [onDeleteInvoice]);
|
||||
|
||||
const actionsMenuList = useCallback(
|
||||
(invoice) => {
|
||||
<Menu>
|
||||
<MenuItem text={formatMessage({ id: 'view_details' })} />
|
||||
<MenuDivider />
|
||||
|
||||
<MenuItem
|
||||
text={formatMessage({ id: 'edit_invoice' })}
|
||||
// onClick={handleEditInvoice(invoice)}
|
||||
/>
|
||||
</Menu>;
|
||||
},
|
||||
[handleDeleteInvoice, handleEditInvoice, formatMessage],
|
||||
);
|
||||
|
||||
const columns = useMemo(
|
||||
() => [
|
||||
{
|
||||
id: '',
|
||||
Header: formatMessage({ id: '' }),
|
||||
accessor: '',
|
||||
className: '',
|
||||
},
|
||||
{
|
||||
id: '',
|
||||
Header: formatMessage({ id: '' }),
|
||||
accessor: '',
|
||||
className: '',
|
||||
},
|
||||
{
|
||||
id: '',
|
||||
Header: formatMessage({ id: '' }),
|
||||
accessor: '',
|
||||
className: '',
|
||||
},
|
||||
{
|
||||
id: '',
|
||||
Header: formatMessage({ id: '' }),
|
||||
accessor: '',
|
||||
className: '',
|
||||
},
|
||||
{
|
||||
id: '',
|
||||
Header: formatMessage({ id: '' }),
|
||||
accessor: '',
|
||||
className: '',
|
||||
},
|
||||
{
|
||||
id: 'actions',
|
||||
Header: '',
|
||||
Cell: ({ cell }) => (
|
||||
<Popover
|
||||
content={actionMenuList(cell.row.original)}
|
||||
position={Position.RIGHT_BOTTOM}
|
||||
>
|
||||
<Button icon={<Icon icon="more-h-16" iconSize={16} />} />
|
||||
</Popover>
|
||||
),
|
||||
className: 'actions',
|
||||
width: 50,
|
||||
disableResizing: true,
|
||||
},
|
||||
],
|
||||
[actionMenuList, formatMessage],
|
||||
);
|
||||
|
||||
const handleDataTableFetchData = useCallback(
|
||||
(...arguments) => {
|
||||
onFetchData && onFetchData(...arguments);
|
||||
},
|
||||
[onFetchData],
|
||||
);
|
||||
|
||||
const handleSelectedRowsChange = useCallback(
|
||||
(selectedRows) => {
|
||||
onSelectedRowsChange &&
|
||||
onSelectedRowsChange(selectedRows.map((s) => s.original));
|
||||
},
|
||||
[onSelectedRowsChange],
|
||||
);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<LoadingIndicator>
|
||||
<DataTable
|
||||
columns={columns}
|
||||
data={[]}
|
||||
onFetchData={handleDataTableFetchData}
|
||||
manualSortBy={true}
|
||||
/>
|
||||
</LoadingIndicator>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(
|
||||
withRouter,
|
||||
withDialogActions,
|
||||
withDashboardActions,
|
||||
withInvoiceActions,
|
||||
// withInvoices(({})=>({
|
||||
|
||||
// }))
|
||||
withViewDetails(),
|
||||
)(InvoicesDataTable);
|
||||
31
client/src/containers/Sales/Invoice/withInvoiceActions.js
Normal file
31
client/src/containers/Sales/Invoice/withInvoiceActions.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import { connect } from 'react-redux';
|
||||
import {
|
||||
submitInvoice,
|
||||
editInvoice,
|
||||
deleteInvoice,
|
||||
fetchInvoice,
|
||||
fetchInvoicesTable,
|
||||
} from 'store/Invoice/invoices.actions';
|
||||
import t from 'store/types';
|
||||
|
||||
const mapDipatchToProps = (dispatch) => ({
|
||||
requestSubmitInvoice: (form) => dispatch(submitInvoice({ form })),
|
||||
requsetFetchInvoice: (id) => dispatch(fetchInvoice({ id })),
|
||||
requestEditInvoice: (id, form) => dispatch(editInvoice({ id, form })),
|
||||
requestFetchInvoiceTable: (query = {}) =>
|
||||
dispatch(fetchInvoicesTable({ query: { ...query } })),
|
||||
requestDeleteInvoice: (id) => dispatch(deleteInvoice({ id })),
|
||||
|
||||
changeInvoiceView: (id) =>
|
||||
dispatch({
|
||||
type: t.INVOICES_SET_CURREMT_VIEW,
|
||||
currentViewId: parseInt(id, 10),
|
||||
}),
|
||||
addInvoiceTableQueries: (_queries) =>
|
||||
dispatch({
|
||||
type: t.INVOICES_TABLE_QUERIES_ADD,
|
||||
_queries,
|
||||
}),
|
||||
});
|
||||
|
||||
export default connect(null, mapDipatchToProps);
|
||||
11
client/src/containers/Sales/Invoice/withInvoiceDetail.js
Normal file
11
client/src/containers/Sales/Invoice/withInvoiceDetail.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import { connect } from 'react-redux';
|
||||
import { getInvoiceById } from 'store/Invoice/invoices.selector';
|
||||
|
||||
export default () => {
|
||||
const getInvoiceById = getInvoiceById();
|
||||
|
||||
const mapStateToProps = (state, props) => ({
|
||||
invoice: getInvoiceById(state, props),
|
||||
});
|
||||
return connect(mapStateToProps);
|
||||
};
|
||||
325
client/src/containers/Sales/Receipt/ReceiptForm.js
Normal file
325
client/src/containers/Sales/Receipt/ReceiptForm.js
Normal file
@@ -0,0 +1,325 @@
|
||||
import React, {
|
||||
useMemo,
|
||||
useCallback,
|
||||
useEffect,
|
||||
useRef,
|
||||
useState,
|
||||
} from 'react';
|
||||
import * as Yup from 'yup';
|
||||
import { useFormik } from 'formik';
|
||||
import moment from 'moment';
|
||||
import { Intent, FormGroup, TextArea } from '@blueprintjs/core';
|
||||
|
||||
import { FormattedMessage as T, useIntl } from 'react-intl';
|
||||
import { pick } from 'lodash';
|
||||
|
||||
import ReceiptFromHeader from './ReceiptFormHeader';
|
||||
import EstimatesItemsTable from 'containers/Sales/Estimate/EntriesItemsTable';
|
||||
import ReceiptFormFooter from './ReceiptFormFooter';
|
||||
|
||||
import withDashboardActions from 'containers/Dashboard/withDashboardActions';
|
||||
import withMediaActions from 'containers/Media/withMediaActions';
|
||||
import withReceipActions from './withReceipActions';
|
||||
|
||||
import { AppToaster } from 'components';
|
||||
import Dragzone from 'components/Dragzone';
|
||||
import useMedia from 'hooks/useMedia';
|
||||
|
||||
import { compose, repeatValue } from 'utils';
|
||||
|
||||
const MIN_LINES_NUMBER = 4;
|
||||
|
||||
function ReceiptForm({
|
||||
//#withMedia
|
||||
requestSubmitMedia,
|
||||
requestDeleteMedia,
|
||||
|
||||
//#withReceiptActions
|
||||
requestSubmitReceipt,
|
||||
|
||||
//#withDashboard
|
||||
changePageTitle,
|
||||
changePageSubtitle,
|
||||
|
||||
//#withReceiptDetail
|
||||
receipt,
|
||||
|
||||
//#own Props
|
||||
receiptId,
|
||||
onFormSubmit,
|
||||
onCancelForm,
|
||||
}) {
|
||||
const { formatMessage } = useIntl();
|
||||
const [payload, setPaload] = useState({});
|
||||
|
||||
const {
|
||||
setFiles,
|
||||
saveMedia,
|
||||
deletedFiles,
|
||||
setDeletedFiles,
|
||||
deleteMedia,
|
||||
} = useMedia({
|
||||
saveCallback: requestSubmitMedia,
|
||||
deleteCallback: requestDeleteMedia,
|
||||
});
|
||||
|
||||
const handleDropFiles = useCallback((_files) => {
|
||||
setFiles(_files.filter((file) => file.uploaded === false));
|
||||
}, []);
|
||||
|
||||
const savedMediaIds = useRef([]);
|
||||
const clearSavedMediaIds = () => {
|
||||
savedMediaIds.current = [];
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (receipt && receipt.id) {
|
||||
changePageTitle(formatMessage({ id: 'edit_receipt' }));
|
||||
} else {
|
||||
changePageTitle(formatMessage({ id: 'new_receipt' }));
|
||||
}
|
||||
}, [changePageTitle, receipt, formatMessage]);
|
||||
|
||||
const validationSchema = Yup.object().shape({
|
||||
customer_id: Yup.string()
|
||||
.label(formatMessage({ id: 'customer_name_' }))
|
||||
.required(),
|
||||
receipt_date: Yup.date()
|
||||
.required()
|
||||
.label(formatMessage({ id: 'receipt_date_' })),
|
||||
receipt_no: Yup.number()
|
||||
.required()
|
||||
.label(formatMessage({ id: 'receipt_no_' })),
|
||||
deposit_account_id: Yup.number()
|
||||
.required()
|
||||
.label(formatMessage({ id: 'deposit_account_' })),
|
||||
reference_no: Yup.string().min(1).max(255),
|
||||
receipt_message: Yup.string()
|
||||
.trim()
|
||||
.min(1)
|
||||
.max(1024)
|
||||
.label(formatMessage({ id: 'receipt_message_' })),
|
||||
send_to_email: Yup.string().email(),
|
||||
statement: Yup.string()
|
||||
.trim()
|
||||
.min(1)
|
||||
.max(1024)
|
||||
.label(formatMessage({ id: 'note' })),
|
||||
|
||||
entries: Yup.array().of(
|
||||
Yup.object().shape({
|
||||
quantity: Yup.number().nullable(),
|
||||
rate: Yup.number().nullable(),
|
||||
item_id: Yup.number()
|
||||
.nullable()
|
||||
.when(['quantity', 'rate'], {
|
||||
is: (quantity, rate) => quantity || rate,
|
||||
then: Yup.number().required(),
|
||||
}),
|
||||
discount: Yup.number().nullable(),
|
||||
description: Yup.string().nullable(),
|
||||
}),
|
||||
),
|
||||
});
|
||||
const saveReceiptSubmit = useCallback(
|
||||
(payload) => {
|
||||
onFormSubmit && onFormSubmit(payload);
|
||||
},
|
||||
[onFormSubmit],
|
||||
);
|
||||
|
||||
const defaultReceipt = useMemo(
|
||||
() => ({
|
||||
index: 0,
|
||||
item_id: null,
|
||||
rate: null,
|
||||
discount: null,
|
||||
quantity: null,
|
||||
description: '',
|
||||
}),
|
||||
[],
|
||||
);
|
||||
|
||||
const defaultInitialValues = useMemo(
|
||||
() => ({
|
||||
customer_id: '',
|
||||
deposit_account_id: '',
|
||||
receipt_date: moment(new Date()).format('YYYY-MM-DD'),
|
||||
send_to_email: '',
|
||||
reference_no: '',
|
||||
receipt_message: '',
|
||||
statement: '',
|
||||
entries: [...repeatValue(defaultReceipt, MIN_LINES_NUMBER)],
|
||||
}),
|
||||
[defaultReceipt],
|
||||
);
|
||||
|
||||
const orderingIndex = (_receipt) => {
|
||||
return _receipt.map((item, index) => ({
|
||||
...item,
|
||||
index: index + 1,
|
||||
}));
|
||||
};
|
||||
|
||||
const initialValues = useMemo(
|
||||
() => ({
|
||||
...defaultInitialValues,
|
||||
entries: orderingIndex(defaultInitialValues.entries),
|
||||
}),
|
||||
[defaultReceipt, defaultInitialValues, receipt],
|
||||
);
|
||||
|
||||
const initialAttachmentFiles = useMemo(() => {
|
||||
return receipt && receipt.media
|
||||
? receipt.media.map((attach) => ({
|
||||
preview: attach.attachment_file,
|
||||
uploaded: true,
|
||||
metadata: { ...attach },
|
||||
}))
|
||||
: [];
|
||||
}, [receipt]);
|
||||
|
||||
const formik = useFormik({
|
||||
enableReinitialize: true,
|
||||
validationSchema,
|
||||
initialValues: {
|
||||
...initialValues,
|
||||
},
|
||||
onSubmit: async (values, { setErrors, setSubmitting, resetForm }) => {
|
||||
const entries = values.entries.map(
|
||||
({ item_id, quantity, rate, description }) => ({
|
||||
item_id,
|
||||
quantity,
|
||||
rate,
|
||||
description,
|
||||
}),
|
||||
);
|
||||
const form = {
|
||||
...values,
|
||||
entries,
|
||||
};
|
||||
|
||||
const saveReceipt = (mediaIds) =>
|
||||
new Promise((resolve, reject) => {
|
||||
const requestForm = { ...form, media_ids: mediaIds };
|
||||
|
||||
requestSubmitReceipt(requestForm)
|
||||
.then((resposne) => {
|
||||
AppToaster.show({
|
||||
message: formatMessage({
|
||||
id: 'the_receipt_has_been_successfully_created',
|
||||
}),
|
||||
intent: Intent.SUCCESS,
|
||||
});
|
||||
setSubmitting(false);
|
||||
resetForm();
|
||||
saveReceiptSubmit({ action: 'new', ...payload });
|
||||
clearSavedMediaIds();
|
||||
})
|
||||
.catch((errors) => {
|
||||
setSubmitting(false);
|
||||
});
|
||||
});
|
||||
Promise.all([saveMedia(), deleteMedia()])
|
||||
.then(([savedMediaResponses]) => {
|
||||
const mediaIds = savedMediaResponses.map((res) => res.data.media.id);
|
||||
savedMediaIds.current = mediaIds;
|
||||
return savedMediaResponses;
|
||||
})
|
||||
.then(() => {
|
||||
return saveReceipt(saveReceipt.current);
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
const handleDeleteFile = useCallback(
|
||||
(_deletedFiles) => {
|
||||
_deletedFiles.forEach((deletedFile) => {
|
||||
if (deletedFile.uploaded && deletedFile.metadata.id) {
|
||||
setDeletedFiles([...deletedFiles, deletedFile.metadata.id]);
|
||||
}
|
||||
});
|
||||
},
|
||||
[setDeletedFiles, deletedFiles],
|
||||
);
|
||||
|
||||
const handleSubmitClick = useCallback(
|
||||
(payload) => {
|
||||
setPaload(payload);
|
||||
formik.submitForm();
|
||||
},
|
||||
[setPaload, formik],
|
||||
);
|
||||
|
||||
const handleCancelClick = useCallback(
|
||||
(payload) => {
|
||||
onCancelForm && onCancelForm(payload);
|
||||
},
|
||||
[onCancelForm],
|
||||
);
|
||||
|
||||
const handleClickAddNewRow = () => {
|
||||
formik.setFieldValue(
|
||||
'entries',
|
||||
orderingIndex([...formik.values.entries, defaultReceipt]),
|
||||
);
|
||||
};
|
||||
|
||||
const handleClearAllLines = () => {
|
||||
formik.setFieldValue(
|
||||
'entries',
|
||||
orderingIndex([...repeatValue(defaultReceipt, MIN_LINES_NUMBER)]),
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={'receipt-form'}>
|
||||
<form onSubmit={formik.handleSubmit}>
|
||||
<ReceiptFromHeader formik={formik} />
|
||||
|
||||
<EstimatesItemsTable
|
||||
entries={formik.values.entries}
|
||||
onClickAddNewRow={handleClickAddNewRow}
|
||||
onClickClearAllLines={handleClearAllLines}
|
||||
formik={formik}
|
||||
/>
|
||||
<FormGroup
|
||||
label={<T id={'receipt_message'} />}
|
||||
className={'form-group--'}
|
||||
>
|
||||
<TextArea
|
||||
growVertically={true}
|
||||
{...formik.getFieldProps('receipt_message')}
|
||||
/>
|
||||
</FormGroup>
|
||||
<FormGroup
|
||||
label={<T id={'statement'} />}
|
||||
className={'form-group--statement'}
|
||||
>
|
||||
<TextArea
|
||||
growVertically={true}
|
||||
{...formik.getFieldProps('statement')}
|
||||
/>
|
||||
</FormGroup>
|
||||
|
||||
<Dragzone
|
||||
initialFiles={initialAttachmentFiles}
|
||||
onDrop={handleDropFiles}
|
||||
onDeleteFile={handleDeleteFile}
|
||||
hint={'Attachments: Maxiumum size: 20MB'}
|
||||
/>
|
||||
<ReceiptFormFooter
|
||||
formik={formik}
|
||||
onSubmit={handleSubmitClick}
|
||||
onCancelForm={handleCancelClick}
|
||||
/>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(
|
||||
withReceipActions,
|
||||
withDashboardActions,
|
||||
withMediaActions,
|
||||
)(ReceiptForm);
|
||||
41
client/src/containers/Sales/Receipt/ReceiptFormFooter.js
Normal file
41
client/src/containers/Sales/Receipt/ReceiptFormFooter.js
Normal file
@@ -0,0 +1,41 @@
|
||||
import React from 'react';
|
||||
import { Intent, Button } from '@blueprintjs/core';
|
||||
import { FormattedMessage as T } from 'react-intl';
|
||||
|
||||
export default function ReceiptFormFooter({
|
||||
formik: { isSubmitting },
|
||||
onSubmitClick,
|
||||
onCancelClick,
|
||||
}) {
|
||||
return (
|
||||
<div className={'estimate-form__floating-footer'}>
|
||||
<Button disabled={isSubmitting} intent={Intent.PRIMARY} type="submit">
|
||||
<T id={'save_send'} />
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
disabled={isSubmitting}
|
||||
intent={Intent.PRIMARY}
|
||||
className={'ml1'}
|
||||
name={'save'}
|
||||
type="submit"
|
||||
>
|
||||
<T id={'save'} />
|
||||
</Button>
|
||||
|
||||
<Button className={'ml1'} disabled={isSubmitting}>
|
||||
<T id={'clear'} />
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
className={'ml1'}
|
||||
type="submit"
|
||||
onClick={() => {
|
||||
onCancelClick && onCancelClick();
|
||||
}}
|
||||
>
|
||||
<T id={'close'} />
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
214
client/src/containers/Sales/Receipt/ReceiptFormHeader.js
Normal file
214
client/src/containers/Sales/Receipt/ReceiptFormHeader.js
Normal file
@@ -0,0 +1,214 @@
|
||||
import React, { useMemo, useCallback, useState } from 'react';
|
||||
import {
|
||||
FormGroup,
|
||||
InputGroup,
|
||||
Intent,
|
||||
Position,
|
||||
MenuItem,
|
||||
Classes,
|
||||
} from '@blueprintjs/core';
|
||||
|
||||
import { DateInput } from '@blueprintjs/datetime';
|
||||
import { FormattedMessage as T } from 'react-intl';
|
||||
import moment from 'moment';
|
||||
import { momentFormatter, compose, tansformDateValue } from 'utils';
|
||||
import classNames from 'classnames';
|
||||
import {
|
||||
AccountsSelectList,
|
||||
ListSelect,
|
||||
ErrorMessage,
|
||||
FieldRequiredHint,
|
||||
Hint,
|
||||
} from 'components';
|
||||
|
||||
import withCustomers from 'containers/Customers/withCustomers';
|
||||
import withAccounts from 'containers/Accounts/withAccounts';
|
||||
|
||||
function ReceiptFormHeader({
|
||||
formik: { errors, touched, setFieldValue, getFieldProps, values },
|
||||
|
||||
//#withCustomers
|
||||
customers,
|
||||
//#withAccouts
|
||||
accountsList,
|
||||
}) {
|
||||
const handleDateChange = useCallback(
|
||||
(date) => {
|
||||
const formatted = moment(date).format('YYYY-MM-DD');
|
||||
setFieldValue('receipt_date', formatted);
|
||||
},
|
||||
[setFieldValue],
|
||||
);
|
||||
|
||||
const CustomerRenderer = useCallback(
|
||||
(cutomer, { handleClick }) => (
|
||||
<MenuItem
|
||||
key={cutomer.id}
|
||||
text={cutomer.display_name}
|
||||
onClick={handleClick}
|
||||
/>
|
||||
),
|
||||
[],
|
||||
);
|
||||
|
||||
// Filter Customer
|
||||
const filterCustomer = (query, customer, _index, exactMatch) => {
|
||||
const normalizedTitle = customer.display_name.toLowerCase();
|
||||
const normalizedQuery = query.toLowerCase();
|
||||
if (exactMatch) {
|
||||
return normalizedTitle === normalizedQuery;
|
||||
} else {
|
||||
return (
|
||||
`${customer.display_name} ${normalizedTitle}`.indexOf(
|
||||
normalizedQuery,
|
||||
) >= 0
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
// handle change
|
||||
const onChangeSelect = useCallback(
|
||||
(filedName) => {
|
||||
return (item) => {
|
||||
setFieldValue(filedName, item.id);
|
||||
};
|
||||
},
|
||||
[setFieldValue],
|
||||
);
|
||||
|
||||
// Filter deposit accounts.
|
||||
const depositAccounts = useMemo(
|
||||
() => accountsList.filter((a) => a?.type?.key === 'current_asset'),
|
||||
[accountsList],
|
||||
);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div>
|
||||
{/* customer name */}
|
||||
<FormGroup
|
||||
label={<T id={'customer_name'} />}
|
||||
inline={true}
|
||||
className={classNames('form-group--select-list', Classes.FILL)}
|
||||
labelInfo={<FieldRequiredHint />}
|
||||
intent={errors.customer_id && touched.customer_id && Intent.DANGER}
|
||||
helperText={
|
||||
<ErrorMessage name={'customer_id'} {...{ errors, touched }} />
|
||||
}
|
||||
>
|
||||
<ListSelect
|
||||
items={customers}
|
||||
noResults={<MenuItem disabled={true} text="No results." />}
|
||||
itemRenderer={CustomerRenderer}
|
||||
itemPredicate={filterCustomer}
|
||||
popoverProps={{ minimal: true }}
|
||||
onItemSelect={onChangeSelect('customer_id')}
|
||||
selectedItem={values.customer_id}
|
||||
selectedItemProp={'id'}
|
||||
defaultText={<T id={'select_customer_account'} />}
|
||||
labelProp={'display_name'}
|
||||
/>
|
||||
</FormGroup>
|
||||
|
||||
<FormGroup
|
||||
label={<T id={'deposit_account'} />}
|
||||
className={classNames(
|
||||
'form-group--deposit_account_id',
|
||||
'form-group--select-list',
|
||||
Classes.FILL,
|
||||
)}
|
||||
inline={true}
|
||||
labelInfo={<FieldRequiredHint />}
|
||||
intent={
|
||||
errors.deposit_account_id &&
|
||||
touched.deposit_account_id &&
|
||||
Intent.DANGER
|
||||
}
|
||||
helperText={
|
||||
<ErrorMessage
|
||||
name={'deposit_account_id'}
|
||||
{...{ errors, touched }}
|
||||
/>
|
||||
}
|
||||
>
|
||||
<AccountsSelectList
|
||||
accounts={depositAccounts}
|
||||
onAccountSelected={onChangeSelect('deposit_account_id')}
|
||||
defaultSelectText={<T id={'select_deposit_account'} />}
|
||||
selectedAccountId={values.deposit_account_id}
|
||||
/>
|
||||
</FormGroup>
|
||||
|
||||
<FormGroup
|
||||
label={<T id={'receipt_date'} />}
|
||||
inline={true}
|
||||
className={classNames('form-group--select-list', Classes.FILL)}
|
||||
intent={errors.receipt_date && touched.receipt_date && Intent.DANGER}
|
||||
helperText={
|
||||
<ErrorMessage name="receipt_date" {...{ errors, touched }} />
|
||||
}
|
||||
>
|
||||
<DateInput
|
||||
{...momentFormatter('YYYY/MM/DD')}
|
||||
value={tansformDateValue(values.receipt_date)}
|
||||
onChange={handleDateChange}
|
||||
popoverProps={{ position: Position.BOTTOM, minimal: true }}
|
||||
/>
|
||||
</FormGroup>
|
||||
</div>
|
||||
{/* receipt_no */}
|
||||
<FormGroup
|
||||
label={<T id={'receipt'} />}
|
||||
inline={true}
|
||||
className={('form-group--receipt_no', Classes.FILL)}
|
||||
labelInfo={<FieldRequiredHint />}
|
||||
intent={errors.receipt_no && touched.receipt_no && Intent.DANGER}
|
||||
helperText={<ErrorMessage name="receipt_no" {...{ errors, touched }} />}
|
||||
>
|
||||
<InputGroup
|
||||
intent={errors.receipt_no && touched.receipt_no && Intent.DANGER}
|
||||
minimal={true}
|
||||
{...getFieldProps('receipt_no')}
|
||||
/>
|
||||
</FormGroup>
|
||||
|
||||
<FormGroup
|
||||
label={<T id={'reference'} />}
|
||||
inline={true}
|
||||
className={classNames('form-group--reference', Classes.FILL)}
|
||||
intent={errors.reference_no && touched.reference_no && Intent.DANGER}
|
||||
helperText={<ErrorMessage name="reference" {...{ errors, touched }} />}
|
||||
>
|
||||
<InputGroup
|
||||
intent={errors.reference_no && touched.reference_no && Intent.DANGER}
|
||||
minimal={true}
|
||||
{...getFieldProps('reference_no')}
|
||||
/>
|
||||
</FormGroup>
|
||||
<FormGroup
|
||||
label={<T id={'send_to_email'} />}
|
||||
inline={true}
|
||||
className={classNames('form-group--send_to_email', Classes.FILL)}
|
||||
intent={errors.send_to_email && touched.send_to_email && Intent.DANGER}
|
||||
helperText={<ErrorMessage name="reference" {...{ errors, touched }} />}
|
||||
>
|
||||
<InputGroup
|
||||
intent={
|
||||
errors.send_to_email && touched.send_to_email && Intent.DANGER
|
||||
}
|
||||
minimal={true}
|
||||
{...getFieldProps('send_to_email')}
|
||||
/>
|
||||
</FormGroup>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(
|
||||
withCustomers(({ customers }) => ({
|
||||
customers,
|
||||
})),
|
||||
withAccounts(({ accountsList }) => ({
|
||||
accountsList,
|
||||
})),
|
||||
)(ReceiptFormHeader);
|
||||
65
client/src/containers/Sales/Receipt/Receipts.js
Normal file
65
client/src/containers/Sales/Receipt/Receipts.js
Normal file
@@ -0,0 +1,65 @@
|
||||
import React, { useCallback } from 'react';
|
||||
import { useParams, useHistory } from 'react-router-dom';
|
||||
import { useQuery } from 'react-query';
|
||||
|
||||
import ReceiptFrom from './ReceiptForm';
|
||||
import DashboardInsider from 'components/Dashboard/DashboardInsider';
|
||||
|
||||
import withCustomersActions from 'containers/Customers/withCustomersActions';
|
||||
import withAccountsActions from 'containers/Accounts/withAccountsActions';
|
||||
import withItemsActions from 'containers/Items/withItemsActions';
|
||||
|
||||
import { compose } from 'utils';
|
||||
|
||||
function Receipts({
|
||||
//#withwithAccountsActions
|
||||
requestFetchAccounts,
|
||||
|
||||
//#withCustomersActions
|
||||
requestFetchCustomers,
|
||||
|
||||
//#withItemsActions
|
||||
requestFetchItems,
|
||||
}) {
|
||||
const history = useHistory();
|
||||
const { id } = useParams();
|
||||
|
||||
const fetchAccounts = useQuery('accounts-list', (key) =>
|
||||
requestFetchAccounts(),
|
||||
);
|
||||
|
||||
const fetchCustomers = useQuery('customers-table', () =>
|
||||
requestFetchCustomers({}),
|
||||
);
|
||||
|
||||
// Handle fetch Items data table or list
|
||||
const fetchItems = useQuery('items-table', () => requestFetchItems({}));
|
||||
|
||||
const handleFormSubmit = useCallback((payload) => {}, [history]);
|
||||
|
||||
const handleCancel = useCallback(() => {
|
||||
history.goBack();
|
||||
}, [history]);
|
||||
|
||||
return (
|
||||
<DashboardInsider
|
||||
loading={
|
||||
fetchCustomers.isFetching ||
|
||||
fetchItems.isFetching ||
|
||||
fetchAccounts.isFetching
|
||||
}
|
||||
>
|
||||
<ReceiptFrom
|
||||
onFormSubmit={handleFormSubmit}
|
||||
// ReceiptId={id}
|
||||
onCancelForm={handleCancel}
|
||||
/>
|
||||
</DashboardInsider>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(
|
||||
withCustomersActions,
|
||||
withItemsActions,
|
||||
withAccountsActions,
|
||||
)(Receipts);
|
||||
33
client/src/containers/Sales/Receipt/withReceipActions.js
Normal file
33
client/src/containers/Sales/Receipt/withReceipActions.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import { connect } from 'react-redux';
|
||||
import {
|
||||
submitReceipt,
|
||||
deleteReceipt,
|
||||
fetchReceipt,
|
||||
fetchReceiptsTable,
|
||||
editReceipt,
|
||||
} from 'store/receipt/receipt.actions';
|
||||
import t from 'store/types';
|
||||
|
||||
const mapDispatchToProps = (dispatch) => ({
|
||||
requestSubmitReceipt: (form) => dispatch(submitReceipt({ form })),
|
||||
requestFetchReceipt: (id) => dispatch(fetchReceipt({ id })),
|
||||
requestEditTeceipt: (id, form) => dispatch(editReceipt({ id, form })),
|
||||
requestDeleteReceipt: (id) => dispatch(deleteReceipt({ id })),
|
||||
requestFetchReceiptsTable: (query = {}) =>
|
||||
dispatch(fetchReceiptsTable({ query: { ...query } })),
|
||||
// requestDeleteBulkReceipt: (ids) => dispatch(deleteBulkReceipt({ ids })),
|
||||
|
||||
changeReceiptView: (id) =>
|
||||
dispatch({
|
||||
type: t.RECEIPT_SET_CURRENT_VIEW,
|
||||
currentViewId: parseInt(id, 10),
|
||||
}),
|
||||
|
||||
addReceiptsTableQueries: (queries) =>
|
||||
dispatch({
|
||||
type: t.RECEIPTS_TABLE_QUERIES_ADD,
|
||||
queries,
|
||||
}),
|
||||
});
|
||||
|
||||
export default connect(null, mapDispatchToProps);
|
||||
Reference in New Issue
Block a user