WIP: allocate landed cost.

This commit is contained in:
a.bouhuolia
2021-07-25 12:58:33 +02:00
parent 95edfe66b0
commit 77d987ef1f
13 changed files with 108 additions and 92 deletions

View File

@@ -8,10 +8,10 @@ import AllocateLandedCostForm from './AllocateLandedCostForm';
export default function AllocateLandedCostDialogContent({ export default function AllocateLandedCostDialogContent({
// #ownProps // #ownProps
dialogName, dialogName,
bill, billId,
}) { }) {
return ( return (
<AllocateLandedCostDialogProvider billId={bill} dialogName={dialogName}> <AllocateLandedCostDialogProvider billId={billId} dialogName={dialogName}>
<AllocateLandedCostForm /> <AllocateLandedCostForm />
</AllocateLandedCostDialogProvider> </AllocateLandedCostDialogProvider>
); );

View File

@@ -2,9 +2,6 @@ import React from 'react';
import { DialogContent } from 'components'; import { DialogContent } from 'components';
import { useBill, useCreateLandedCost } from 'hooks/query'; import { useBill, useCreateLandedCost } from 'hooks/query';
import { map, omit, pick } from 'lodash';
import * as R from 'ramda';
const AllocateLandedCostDialogContext = React.createContext(); const AllocateLandedCostDialogContext = React.createContext();
/** /**
@@ -24,21 +21,10 @@ function AllocateLandedCostDialogProvider({
// Create landed cost mutations. // Create landed cost mutations.
const { mutateAsync: createLandedCostMutate } = useCreateLandedCost(); const { mutateAsync: createLandedCostMutate } = useCreateLandedCost();
// const L = [bill].map(({ entries: items }) => ({
// items,
// }));
// let obj = { oldKey: 1, b: 2, c: 3 };
// const { oldKey: newKey, ...rest } = obj;
// obj = { newKey, ...rest };
// const obj = { ...pick(bill, ['entries']).map((index) => index) };
// provider payload. // provider payload.
const provider = { const provider = {
items: { isBillLoading,
...pick(bill, ['entries']), bill,
},
dialogName, dialogName,
query, query,
createLandedCostMutate, createLandedCostMutate,

View File

@@ -10,8 +10,7 @@ export default function AllocateLandedCostEntriesTable({
onUpdateData, onUpdateData,
entries, entries,
}) { }) {
// Allocate landed cost entries table columns.
// allocate landed cost entries table columns.
const columns = React.useMemo( const columns = React.useMemo(
() => [ () => [
{ {

View File

@@ -23,14 +23,14 @@ function AllocateLandedCostFloatingActions({
return ( return (
<div className={Classes.DIALOG_FOOTER}> <div className={Classes.DIALOG_FOOTER}>
<div className={Classes.DIALOG_FOOTER_ACTIONS}> <div className={Classes.DIALOG_FOOTER_ACTIONS}>
<Button onClick={handleCancelBtnClick} style={{ minWidth: '75px' }}> <Button onClick={handleCancelBtnClick} style={{ minWidth: '85px' }}>
<T id={'cancel'} /> <T id={'cancel'} />
</Button> </Button>
<Button <Button
intent={Intent.PRIMARY} intent={Intent.PRIMARY}
disabled={isSubmitting} style={{ minWidth: '85px' }}
style={{ minWidth: '75px' }}
type="submit" type="submit"
loading={isSubmitting}
> >
{<T id={'save'} />} {<T id={'save'} />}
</Button> </Button>

View File

@@ -3,7 +3,6 @@ import { Formik } from 'formik';
import { Intent } from '@blueprintjs/core'; import { Intent } from '@blueprintjs/core';
import intl from 'react-intl-universal'; import intl from 'react-intl-universal';
import moment from 'moment'; import moment from 'moment';
import { pick, omit } from 'lodash';
import 'style/pages/AllocateLandedCost/AllocateLandedCostForm.scss'; import 'style/pages/AllocateLandedCost/AllocateLandedCostForm.scss';
@@ -12,8 +11,9 @@ import { AllocateLandedCostFormSchema } from './AllocateLandedCostForm.schema';
import { useAllocateLandedConstDialogContext } from './AllocateLandedCostDialogProvider'; import { useAllocateLandedConstDialogContext } from './AllocateLandedCostDialogProvider';
import AllocateLandedCostFormContent from './AllocateLandedCostFormContent'; import AllocateLandedCostFormContent from './AllocateLandedCostFormContent';
import withDialogActions from 'containers/Dialog/withDialogActions'; import withDialogActions from 'containers/Dialog/withDialogActions';
import { compose } from 'utils'; import { compose, transformToForm } from 'utils';
// Default form initial values.
const defaultInitialValues = { const defaultInitialValues = {
transaction_type: 'Bill', transaction_type: 'Bill',
transaction_date: moment(new Date()).format('YYYY-MM-DD'), transaction_date: moment(new Date()).format('YYYY-MM-DD'),
@@ -21,10 +21,12 @@ const defaultInitialValues = {
transaction_entry_id: '', transaction_entry_id: '',
amount: '', amount: '',
allocation_method: 'quantity', allocation_method: 'quantity',
items: { items: [
{
entry_id: '', entry_id: '',
cost: '', cost: '',
}, },
],
}; };
/** /**
@@ -34,32 +36,36 @@ function AllocateLandedCostForm({
// #withDialogActions // #withDialogActions
closeDialog, closeDialog,
}) { }) {
const { items, dialogName, createLandedCostMutate } = const { dialogName, bill, billId, createLandedCostMutate } =
useAllocateLandedConstDialogContext(); useAllocateLandedConstDialogContext();
// Initial form values. // Initial form values.
const initialValues = { const initialValues = {
...defaultInitialValues, ...defaultInitialValues,
...items, items: bill.entries.map((entry) => ({
...entry,
entry_id: entry.id,
cost: '',
})),
}; };
// Handle form submit. // Handle form submit.
const handleFormSubmit = (values, { setSubmitting }) => { const handleFormSubmit = (values, { setSubmitting }) => {
setSubmitting(false); setSubmitting(false);
closeDialog(dialogName);
const entries = [values] // Filters the entries has no cost.
.filter((entry) => entry.id && entry.cost) const entries = values.items
.map((entry) => ({ .filter((entry) => entry.entry_id && entry.cost)
entry_id: entry.id, .map((entry) => transformToForm(entry, defaultInitialValues.items[0]));
...pick(entry, ['cost']),
}));
if (entries.length <= 0) {
AppToaster.show({ message: 'Something wrong!', intent: Intent.DANGER });
return;
}
const form = { const form = {
...values, ...values,
// items:{entries}, items: entries,
}; };
// Handle the request success. // Handle the request success.
const onSuccess = (response) => { const onSuccess = (response) => {
AppToaster.show({ AppToaster.show({
@@ -67,17 +73,15 @@ function AllocateLandedCostForm({
intent: Intent.SUCCESS, intent: Intent.SUCCESS,
}); });
setSubmitting(false); setSubmitting(false);
closeDialog(dialogName);
}; };
// Handle the request error. // Handle the request error.
const onError = ({ const onError = () => {
response: {
data: { errors },
},
}) => {
setSubmitting(false); setSubmitting(false);
AppToaster.show({ message: 'Something went wrong!', intent: Intent.DANGER });
}; };
createLandedCostMutate(form).then(onSuccess).catch(onError); createLandedCostMutate([billId, form]).then(onSuccess).catch(onError);
}; };
return ( return (

View File

@@ -8,7 +8,7 @@ const Schema = Yup.object().shape({
transaction_entry_id: Yup.string().label(intl.get('transaction_line')), transaction_entry_id: Yup.string().label(intl.get('transaction_line')),
amount: Yup.number().label(intl.get('amount')), amount: Yup.number().label(intl.get('amount')),
allocation_method: Yup.string().trim(), allocation_method: Yup.string().trim(),
entries: Yup.array().of( items: Yup.array().of(
Yup.object().shape({ Yup.object().shape({
entry_id: Yup.number().nullable(), entry_id: Yup.number().nullable(),
cost: Yup.number().nullable(), cost: Yup.number().nullable(),

View File

@@ -7,20 +7,18 @@ import AllocateLandedCostEntriesTable from './AllocateLandedCostEntriesTable';
export default function AllocateLandedCostFormBody() { export default function AllocateLandedCostFormBody() {
return ( return (
<div className={classNames(CLASSES.PAGE_FORM_BODY)}> <div className={classNames(CLASSES.PAGE_FORM_BODY)}>
<FastField name={'entries'}> <FastField name={'items'}>
{({ {({
form: { setFieldValue, values }, form: { setFieldValue, values },
field: { value }, field: { value },
meta: { error, touched }, meta: { error, touched },
}) => ( }) => (
<>
<AllocateLandedCostEntriesTable <AllocateLandedCostEntriesTable
entries={value} entries={value}
onUpdateData={(newEntries) => { onUpdateData={(newEntries) => {
setFieldValue('entries', newEntries); setFieldValue('items', newEntries);
}} }}
/> />
</>
)} )}
</FastField> </FastField>
</div> </div>

View File

@@ -8,7 +8,7 @@ import {
InputGroup, InputGroup,
} from '@blueprintjs/core'; } from '@blueprintjs/core';
import classNames from 'classnames'; import classNames from 'classnames';
import { FormattedMessage as T } from 'components'; import { FormattedMessage as T, If } from 'components';
import intl from 'react-intl-universal'; import intl from 'react-intl-universal';
import { inputIntent, handleStringChange } from 'utils'; import { inputIntent, handleStringChange } from 'utils';
import { FieldRequiredHint, ListSelect } from 'components'; import { FieldRequiredHint, ListSelect } from 'components';
@@ -29,10 +29,11 @@ export default function AllocateLandedCostFormFields() {
data: { transactions }, data: { transactions },
} = useLandedCostTransaction(values.transaction_type); } = useLandedCostTransaction(values.transaction_type);
const transactionEntry = getEntriesByTransactionId( // Retrieve entries of the given transaction id.
const transactionEntries = React.useMemo(() => getEntriesByTransactionId(
transactions, transactions,
values.transaction_id, values.transaction_id,
); ), [transactions, values.transaction_id]);
return ( return (
<div className={Classes.DIALOG_BODY}> <div className={Classes.DIALOG_BODY}>
@@ -71,7 +72,7 @@ export default function AllocateLandedCostFormFields() {
{({ form, field: { value }, meta: { error, touched } }) => ( {({ form, field: { value }, meta: { error, touched } }) => (
<FormGroup <FormGroup
label={<T id={'transaction_id'} />} label={<T id={'transaction_id'} />}
// labelInfo={<FieldRequiredHint />} labelInfo={<FieldRequiredHint />}
intent={inputIntent({ error, touched })} intent={inputIntent({ error, touched })}
helperText={<ErrorMessage name="transaction_id" />} helperText={<ErrorMessage name="transaction_id" />}
className={classNames(CLASSES.FILL, 'form-group--transaction_id')} className={classNames(CLASSES.FILL, 'form-group--transaction_id')}
@@ -95,6 +96,7 @@ export default function AllocateLandedCostFormFields() {
</Field> </Field>
{/*------------ Transaction line -----------*/} {/*------------ Transaction line -----------*/}
<If condition={transactionEntries.length > 0}>
<Field name={'transaction_entry_id'}> <Field name={'transaction_entry_id'}>
{({ form, field: { value }, meta: { error, touched } }) => ( {({ form, field: { value }, meta: { error, touched } }) => (
<FormGroup <FormGroup
@@ -108,8 +110,9 @@ export default function AllocateLandedCostFormFields() {
inline={true} inline={true}
> >
<ListSelect <ListSelect
items={transactionEntry} items={transactionEntries}
onItemSelect={({ id }) => { onItemSelect={({ id, amount }) => {
form.setFieldValue('amount', amount)
form.setFieldValue('transaction_entry_id', id); form.setFieldValue('transaction_entry_id', id);
}} }}
filterable={false} filterable={false}
@@ -123,6 +126,7 @@ export default function AllocateLandedCostFormFields() {
</FormGroup> </FormGroup>
)} )}
</Field> </Field>
</If>
{/*------------ Amount -----------*/} {/*------------ Amount -----------*/}
<FastField name={'amount'}> <FastField name={'amount'}>

View File

@@ -25,7 +25,7 @@ function AllocateLandedCostDialog({
> >
<DialogSuspense> <DialogSuspense>
<AllocateLandedCostDialogContent <AllocateLandedCostDialogContent
bill={payload.billId} billId={payload.billId}
dialogName={dialogName} dialogName={dialogName}
/> />
</DialogSuspense> </DialogSuspense>

View File

@@ -1,3 +1,6 @@
/**
* Retrieve transaction entries of the given transaction id.
*/
export function getEntriesByTransactionId(transactions, id) { export function getEntriesByTransactionId(transactions, id) {
const transaction = transactions.find((trans) => trans.id === id); const transaction = transactions.find((trans) => trans.id === id);
return transaction ? transaction.entries : []; return transaction ? transaction.entries : [];

View File

@@ -21,7 +21,8 @@ export function useCreateLandedCost(props) {
const apiRequest = useApiRequest(); const apiRequest = useApiRequest();
return useMutation( return useMutation(
(id) => apiRequest.post(`purchases/landed-cost/bills/${id}/allocate`), ([id, values]) =>
apiRequest.post(`purchases/landed-cost/bills/${id}/allocate`, values),
{ {
onSuccess: (res, id) => { onSuccess: (res, id) => {
// Common invalidate queries. // Common invalidate queries.

View File

@@ -1067,6 +1067,7 @@ export default {
cash_flow_statement: 'Cash Flow Statement', cash_flow_statement: 'Cash Flow Statement',
statement_of_cash_flow: 'Statement of Cash Flow ', statement_of_cash_flow: 'Statement of Cash Flow ',
inventory_item_details: 'Inventory Item Details', inventory_item_details: 'Inventory Item Details',
congratulations: 'Congratulations' congratulations: 'Congratulations',
"all_items"
}; };

View File

@@ -3,9 +3,12 @@
width: 700px; width: 700px;
.bp3-dialog-body { .bp3-dialog-body {
.bp3-form-group{
margin-bottom: 18px;
}
.bp3-form-group.bp3-inline { .bp3-form-group.bp3-inline {
.bp3-label { .bp3-label {
min-width: 140px; min-width: 150px;
} }
.bp3-form-content { .bp3-form-content {
width: 300px; width: 300px;
@@ -17,6 +20,10 @@
} }
} }
.bp3-dialog-footer{
padding-top: 10px;
}
.bigcapital-datatable { .bigcapital-datatable {
.table { .table {
// max-height: 300px; // max-height: 300px;
@@ -37,6 +44,19 @@
margin-left: -1px; margin-left: -1px;
border-left: 1px solid #ececec; border-left: 1px solid #ececec;
} }
.bp3-form-group{
margin-bottom: 0;
&:not(.bp3-intent-danger) .bp3-input{
border: 1px solid #d0dfe2;
&:focus{
box-shadow: 0 0 0 1px #116cd0;
border-color: #116cd0;
}
}
}
} }
} }
} }