mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 22:00:31 +00:00
fix(WarehouseTransfer): set item cost to form entries.
This commit is contained in:
@@ -7,15 +7,18 @@ import WarehouseTransferFormEntriesTable from './WarehouseTransferFormEntriesTab
|
|||||||
import {
|
import {
|
||||||
entriesFieldShouldUpdate,
|
entriesFieldShouldUpdate,
|
||||||
defaultWarehouseTransferEntry,
|
defaultWarehouseTransferEntry,
|
||||||
|
useWatchItemsCostSetCostEntries
|
||||||
} from './utils';
|
} from './utils';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Warehouse transafer editor field.
|
* Warehouse transafer editor field.
|
||||||
*/
|
*/
|
||||||
export default function WarehouseTransferEditorField() {
|
export default function WarehouseTransferEditorField() {
|
||||||
const { items } = useWarehouseTransferFormContext();
|
const { items } = useWarehouseTransferFormContext();
|
||||||
|
|
||||||
|
// Watches inventory items cost and sets cost to form entries.
|
||||||
|
useWatchItemsCostSetCostEntries();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={classNames(CLASSES.PAGE_FORM_BODY)}>
|
<div className={classNames(CLASSES.PAGE_FORM_BODY)}>
|
||||||
<FastField
|
<FastField
|
||||||
|
|||||||
@@ -45,7 +45,6 @@ function WarehouseTransferForm({
|
|||||||
createWarehouseTransferMutate,
|
createWarehouseTransferMutate,
|
||||||
editWarehouseTransferMutate,
|
editWarehouseTransferMutate,
|
||||||
submitPayload,
|
submitPayload,
|
||||||
setItemCostQuery,
|
|
||||||
} = useWarehouseTransferFormContext();
|
} = useWarehouseTransferFormContext();
|
||||||
|
|
||||||
// WarehouseTransfer number.
|
// WarehouseTransfer number.
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import React from 'react';
|
|||||||
|
|
||||||
import { DataTableEditable } from 'components';
|
import { DataTableEditable } from 'components';
|
||||||
|
|
||||||
|
import { useWarehouseTransferFormContext } from './WarehouseTransferFormProvider';
|
||||||
import { useWarehouseTransferTableColumns } from '../utils';
|
import { useWarehouseTransferTableColumns } from '../utils';
|
||||||
import { useFetchItemWarehouseQuantity } from './hooks';
|
import { useFetchItemWarehouseQuantity } from './hooks';
|
||||||
import { useDeepCompareEffect } from 'hooks/utils';
|
import { useDeepCompareEffect } from 'hooks/utils';
|
||||||
@@ -24,8 +25,15 @@ export default function WarehouseTransferFormEntriesTable({
|
|||||||
sourceWarehouseId,
|
sourceWarehouseId,
|
||||||
}) {
|
}) {
|
||||||
// Fetch the table row.
|
// Fetch the table row.
|
||||||
const { newRowMeta, setTableRow, resetTableRow, cellsLoading } =
|
const {
|
||||||
useFetchItemWarehouseQuantity();
|
newRowMeta,
|
||||||
|
setTableRow,
|
||||||
|
resetTableRow,
|
||||||
|
cellsLoading,
|
||||||
|
} = useFetchItemWarehouseQuantity();
|
||||||
|
|
||||||
|
// Warehouse transfer provider context.
|
||||||
|
const { isItemsCostFetching } = useWarehouseTransferFormContext();
|
||||||
|
|
||||||
// Retrieve the warehouse transfer table columns.
|
// Retrieve the warehouse transfer table columns.
|
||||||
const columns = useWarehouseTransferTableColumns();
|
const columns = useWarehouseTransferTableColumns();
|
||||||
@@ -87,6 +95,7 @@ export default function WarehouseTransferFormEntriesTable({
|
|||||||
data={entries}
|
data={entries}
|
||||||
cellsLoading={!!cellsLoading}
|
cellsLoading={!!cellsLoading}
|
||||||
cellsLoadingCoords={cellsLoading}
|
cellsLoadingCoords={cellsLoading}
|
||||||
|
progressBarLoading={isItemsCostFetching || cellsLoading}
|
||||||
payload={{
|
payload={{
|
||||||
items,
|
items,
|
||||||
errors: errors || [],
|
errors: errors || [],
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ function WarehouseTransferFormProvider({ warehouseTransferId, ...props }) {
|
|||||||
useWarehouseTransfer(warehouseTransferId, {
|
useWarehouseTransfer(warehouseTransferId, {
|
||||||
enabled: !!warehouseTransferId,
|
enabled: !!warehouseTransferId,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Fetch warehouses list.
|
// Fetch warehouses list.
|
||||||
const {
|
const {
|
||||||
data: warehouses,
|
data: warehouses,
|
||||||
@@ -54,7 +53,12 @@ function WarehouseTransferFormProvider({ warehouseTransferId, ...props }) {
|
|||||||
!isEmpty(itemCostQuery?.date) && !isEmpty(itemCostQuery?.itemsIds);
|
!isEmpty(itemCostQuery?.date) && !isEmpty(itemCostQuery?.itemsIds);
|
||||||
|
|
||||||
// Retrieves the inventory item cost.
|
// Retrieves the inventory item cost.
|
||||||
const { data: inventoryItemsCost } = useItemInventoryCost(
|
const {
|
||||||
|
data: inventoryItemsCost,
|
||||||
|
isLoading: isItemsCostLoading,
|
||||||
|
isFetching: isItemsCostFetching,
|
||||||
|
isSuccess: isItemsCostSuccess
|
||||||
|
} = useItemInventoryCost(
|
||||||
{
|
{
|
||||||
date: itemCostQuery?.date,
|
date: itemCostQuery?.date,
|
||||||
items_ids: itemCostQuery?.itemsIds,
|
items_ids: itemCostQuery?.itemsIds,
|
||||||
@@ -91,6 +95,9 @@ function WarehouseTransferFormProvider({ warehouseTransferId, ...props }) {
|
|||||||
editWarehouseTransferMutate,
|
editWarehouseTransferMutate,
|
||||||
|
|
||||||
inventoryItemsCost,
|
inventoryItemsCost,
|
||||||
|
isItemsCostLoading,
|
||||||
|
isItemsCostFetching,
|
||||||
|
isItemsCostSuccess,
|
||||||
itemCostQuery,
|
itemCostQuery,
|
||||||
setItemCostQuery,
|
setItemCostQuery,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import { chain } from 'lodash';
|
||||||
import { FormikObserver } from 'components';
|
import { FormikObserver } from 'components';
|
||||||
import { useWarehouseTransferFormContext } from './WarehouseTransferFormProvider';
|
import { useWarehouseTransferFormContext } from './WarehouseTransferFormProvider';
|
||||||
|
|
||||||
@@ -7,14 +8,14 @@ export function WarehouseTransferObserveItemsCost() {
|
|||||||
|
|
||||||
// Handle the form change.
|
// Handle the form change.
|
||||||
const handleFormChange = (values) => {
|
const handleFormChange = (values) => {
|
||||||
const itemsIds = values.entries
|
const { date } = values;
|
||||||
|
const itemsIds = chain(values.entries)
|
||||||
.filter((e) => e.item_id)
|
.filter((e) => e.item_id)
|
||||||
.map((e) => e.item_id);
|
.map((e) => e.item_id)
|
||||||
|
.uniq()
|
||||||
|
.value();
|
||||||
|
|
||||||
setItemCostQuery({
|
setItemCostQuery({ date, itemsIds });
|
||||||
date: values.date,
|
|
||||||
itemsIds,
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
return <FormikObserver onChange={handleFormChange} />;
|
return <FormikObserver onChange={handleFormChange} />;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,13 @@ import React from 'react';
|
|||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
import intl from 'react-intl-universal';
|
import intl from 'react-intl-universal';
|
||||||
import { Intent } from '@blueprintjs/core';
|
import { Intent } from '@blueprintjs/core';
|
||||||
import { omit } from 'lodash';
|
import { keyBy, omit } from 'lodash';
|
||||||
import { useFormikContext } from 'formik';
|
import { useFormikContext } from 'formik';
|
||||||
import * as R from 'ramda';
|
import * as R from 'ramda';
|
||||||
|
|
||||||
|
import { useWatch } from 'hooks/utils';
|
||||||
|
import { useWarehouseTransferFormContext } from './WarehouseTransferFormProvider';
|
||||||
|
|
||||||
import { AppToaster } from 'components';
|
import { AppToaster } from 'components';
|
||||||
import {
|
import {
|
||||||
orderingLinesIndexes,
|
orderingLinesIndexes,
|
||||||
@@ -119,6 +122,7 @@ export function transformValueToRequest(values) {
|
|||||||
'warehouses',
|
'warehouses',
|
||||||
'destination_warehouse',
|
'destination_warehouse',
|
||||||
'source_warehouse',
|
'source_warehouse',
|
||||||
|
'cost',
|
||||||
]),
|
]),
|
||||||
})),
|
})),
|
||||||
};
|
};
|
||||||
@@ -178,3 +182,39 @@ export const deleteTableRow = R.curry((rowIndex, defaultEntry, rows) => {
|
|||||||
updateRemoveLineByIndex(rowIndex),
|
updateRemoveLineByIndex(rowIndex),
|
||||||
)(rows);
|
)(rows);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Watches the inventory items cost and sets the cost to form entries.
|
||||||
|
*/
|
||||||
|
export function useWatchItemsCostSetCostEntries() {
|
||||||
|
const { isItemsCostSuccess, inventoryItemsCost } =
|
||||||
|
useWarehouseTransferFormContext();
|
||||||
|
|
||||||
|
const {
|
||||||
|
setFieldValue,
|
||||||
|
values: { entries },
|
||||||
|
} = useFormikContext();
|
||||||
|
|
||||||
|
// Transformes items cost map by item id.
|
||||||
|
const itemsCostByItemId = React.useMemo(
|
||||||
|
() => keyBy(inventoryItemsCost, 'item_id'),
|
||||||
|
[inventoryItemsCost],
|
||||||
|
);
|
||||||
|
|
||||||
|
// Observes the inventory items cost and set form entries with cost.
|
||||||
|
useWatch(() => {
|
||||||
|
if (!isItemsCostSuccess) return;
|
||||||
|
|
||||||
|
const newEntries = entries.map((entry) => {
|
||||||
|
const costEntry = itemsCostByItemId[entry.item_id];
|
||||||
|
|
||||||
|
return entry.item_id
|
||||||
|
? {
|
||||||
|
...entry,
|
||||||
|
cost: costEntry?.average,
|
||||||
|
}
|
||||||
|
: entry;
|
||||||
|
});
|
||||||
|
setFieldValue('entries', newEntries);
|
||||||
|
}, inventoryItemsCost);
|
||||||
|
}
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ export const useWarehouseTransferTableColumns = () => {
|
|||||||
disableSortBy: true,
|
disableSortBy: true,
|
||||||
Cell: SourceWarehouseAccessorCell,
|
Cell: SourceWarehouseAccessorCell,
|
||||||
align: 'right',
|
align: 'right',
|
||||||
width: 120,
|
width: 100,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'destination_warehouse',
|
id: 'destination_warehouse',
|
||||||
@@ -107,7 +107,7 @@ export const useWarehouseTransferTableColumns = () => {
|
|||||||
Cell: DistentionWarehouseAccessorCell,
|
Cell: DistentionWarehouseAccessorCell,
|
||||||
disableSortBy: true,
|
disableSortBy: true,
|
||||||
align: 'right',
|
align: 'right',
|
||||||
width: 120,
|
width: 100,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Header: intl.get('warehouse_transfer.column.transfer_quantity'),
|
Header: intl.get('warehouse_transfer.column.transfer_quantity'),
|
||||||
@@ -120,10 +120,9 @@ export const useWarehouseTransferTableColumns = () => {
|
|||||||
{
|
{
|
||||||
Header: intl.get('warehouse_transfer.column.cost_price'),
|
Header: intl.get('warehouse_transfer.column.cost_price'),
|
||||||
accessor: 'cost',
|
accessor: 'cost',
|
||||||
Cell: MoneyFieldCell,
|
|
||||||
disableSortBy: true,
|
disableSortBy: true,
|
||||||
align: 'right',
|
align: 'right',
|
||||||
width: 100,
|
width: 80,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Header: '',
|
Header: '',
|
||||||
@@ -136,4 +135,4 @@ export const useWarehouseTransferTableColumns = () => {
|
|||||||
],
|
],
|
||||||
[],
|
[],
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user