fix: allocate landed cost on inventory items.

This commit is contained in:
a.bouhuolia
2021-12-04 16:32:37 +02:00
parent 008faaece6
commit d3c5131020
15 changed files with 277 additions and 77 deletions

View File

@@ -1,6 +1,16 @@
import React from 'react';
import { defaultTo, get } from 'lodash';
import { DialogContent } from 'components';
import { useBill, useCreateLandedCost } from 'hooks/query';
import {
useBill,
useCreateLandedCost,
useLandedCostTransaction,
} from 'hooks/query';
import {
getEntriesByTransactionId,
getCostTransactionById,
getTransactionEntryById,
} from './utils';
const AllocateLandedCostDialogContext = React.createContext();
@@ -13,22 +23,79 @@ function AllocateLandedCostDialogProvider({
dialogName,
...props
}) {
const [transactionsType, setTransactionsType] = React.useState(null);
const [transactionId, setTransactionId] = React.useState(null);
const [transactionEntryId, setTransactionEntryId] = React.useState(null);
// Handle fetch bill details.
const { isLoading: isBillLoading, data: bill } = useBill(billId, {
enabled: !!billId,
});
// Retrieve the landed cost transactions based on the given transactions type.
const {
data: { transactions: landedCostTransactions },
} = useLandedCostTransaction(transactionsType, {
enabled: !!transactionsType,
});
// Landed cost selected transaction.
const costTransaction = React.useMemo(
() =>
transactionId
? getCostTransactionById(transactionId, landedCostTransactions)
: null,
[transactionId, landedCostTransactions],
);
// Retrieve the cost transaction entry.
const costTransactionEntry = React.useMemo(
() =>
costTransaction && transactionEntryId
? getTransactionEntryById(costTransaction, transactionEntryId)
: null,
[costTransaction, transactionEntryId],
);
// Retrieve entries of the given transaction id.
const costTransactionEntries = React.useMemo(
() =>
transactionId
? getEntriesByTransactionId(landedCostTransactions, transactionId)
: [],
[landedCostTransactions, transactionId],
);
// Create landed cost mutations.
const { mutateAsync: createLandedCostMutate } = useCreateLandedCost();
// provider payload.
// Retrieve the unallocate cost amount of cost transaction.
const unallocatedCostAmount = defaultTo(
get(costTransactionEntry, 'unallocated_cost_amount'),
0,
);
// Retrieve the unallocate cost amount of cost transaction.
const formattedUnallocatedCostAmount = defaultTo(
get(costTransactionEntry, 'formatted_unallocated_cost_amount'),
0,
);
// Provider payload.
const provider = {
isBillLoading,
bill,
dialogName,
query,
createLandedCostMutate,
costTransaction,
costTransactionEntries,
transactionsType,
landedCostTransactions,
setTransactionsType,
setTransactionId,
setTransactionEntryId,
costTransactionEntry,
transactionEntryId,
transactionId,
billId,
unallocatedCostAmount,
formattedUnallocatedCostAmount,
};
return (