BIG-14: avoid allocate landed cost on non-inventory items.

This commit is contained in:
a.bouhuolia
2021-09-13 18:27:01 +02:00
parent bffc50a492
commit 4188f3f1b5
17 changed files with 188 additions and 111 deletions

View File

@@ -5,7 +5,7 @@ import classNames from 'classnames';
import * as R from 'ramda';
import intl from 'react-intl-universal';
import { useHistory } from 'react-router-dom';
import { isEmpty, omit } from 'lodash';
import { isEmpty } from 'lodash';
import { CLASSES } from 'common/classes';
import { EditBillFormSchema, CreateBillFormSchema } from './BillForm.schema';
@@ -18,8 +18,12 @@ import { AppToaster } from 'components';
import { ERROR } from 'common/errors';
import { useBillFormContext } from './BillFormProvider';
import { compose, orderingLinesIndexes, safeSumBy } from 'utils';
import { defaultBill, transformToEditForm } from './utils';
import { compose, safeSumBy } from 'utils';
import {
defaultBill,
transformToEditForm,
transformEntriesToSubmit,
} from './utils';
import withCurrentOrganization from 'containers/Organization/withCurrentOrganization';
/**
@@ -81,7 +85,7 @@ function BillForm({
const form = {
...values,
open: submitPayload.status,
entries: R.compose(orderingLinesIndexes)(entries),
entries: transformEntriesToSubmit(entries),
};
// Handle the request success.
const onSuccess = (response) => {

View File

@@ -12,6 +12,12 @@ import {
const BillFormContext = createContext();
// Filter all purchasable items only.
const stringifiedFilterRoles = JSON.stringify([
{ index: 1, fieldKey: 'purchasable', value: true, condition: '&&', comparator: 'equals' },
{ index: 2, fieldKey: 'active', value: true, condition: '&&', comparator: 'equals' },
]);
/**
* Bill form provider.
*/
@@ -25,16 +31,6 @@ function BillFormProvider({ billId, ...props }) {
isLoading: isVendorsLoading,
} = useVendors({ page_size: 10000 });
// Filter all purchasable items only.
const stringifiedFilterRoles = React.useMemo(
() =>
JSON.stringify([
{ index: 1, fieldKey: 'purchasable', value: true, condition: '&&', comparator: 'equals' },
{ index: 2, fieldKey: 'active', value: true, condition: '&&', comparator: 'equals' },
]),
[],
);
// Handle fetch Items data table or list
const {
data: { items },

View File

@@ -7,14 +7,17 @@ import {
defaultFastFieldShouldUpdate,
transformToForm,
repeatValue,
orderingLinesIndexes,
} from 'utils';
import {
updateItemsEntriesTotal,
ensureEntriesHaveEmptyLine,
} from 'containers/Entries/utils';
import { isLandedCostDisabled } from '../../../Entries/utils';
export const MIN_LINES_NUMBER = 4;
// Default bill entry.
export const defaultBillEntry = {
index: 0,
item_id: '',
@@ -26,6 +29,7 @@ export const defaultBillEntry = {
landed_cost: false,
};
// Default bill.
export const defaultBill = {
vendor_id: '',
bill_number: '',
@@ -37,10 +41,14 @@ export const defaultBill = {
entries: [...repeatValue(defaultBillEntry, MIN_LINES_NUMBER)],
};
/**
* Transformes the bill to initial values of edit form.
*/
export const transformToEditForm = (bill) => {
const initialEntries = [
...bill.entries.map((entry) => ({
...transformToForm(entry, defaultBillEntry),
landed_cost_disabled: isLandedCostDisabled(entry.item),
})),
...repeatValue(
defaultBillEntry,
@@ -58,7 +66,18 @@ export const transformToEditForm = (bill) => {
};
};
// handle delete errors.
/**
* Transformes bill entries to submit request.
*/
export const transformEntriesToSubmit = (entries) => {
const transformBillEntry = R.curry(transformToForm)(R.__, defaultBillEntry);
return R.compose(orderingLinesIndexes, R.map(transformBillEntry))(entries);
};
/**
* Handle delete errors.
*/
export const handleDeleteErrors = (errors) => {
if (
errors.find((error) => error.type === 'BILL_HAS_ASSOCIATED_PAYMENT_ENTRIES')

View File

@@ -8,7 +8,7 @@ import { DataTableEditable } from 'components';
import { usePaymentMadeEntriesTableColumns } from './components';
import { usePaymentMadeInnerContext } from './PaymentMadeInnerProvider';
import { compose, updateTableRow } from 'utils';
import { compose, updateTableCell } from 'utils';
import { useFormikContext } from 'formik';
/**
@@ -33,7 +33,7 @@ export default function PaymentMadeEntriesTable({
// Handle update data.
const handleUpdateData = useCallback(
(rowIndex, columnId, value) => {
const newRows = compose(updateTableRow(rowIndex, columnId, value))(
const newRows = compose(updateTableCell(rowIndex, columnId, value))(
entries,
);
onUpdateData(newRows);