fix: items entries total calculation.

This commit is contained in:
a.bouhuolia
2021-08-03 16:04:31 +02:00
parent 0783fb26f2
commit b2c892b649
15 changed files with 222 additions and 175 deletions

View File

@@ -89,6 +89,7 @@ function EstimateForm({
);
const totalQuantity = sumBy(entries, (entry) => parseInt(entry.quantity));
// Validate the entries quantity should be bigger than zero.
if (totalQuantity === 0) {
AppToaster.show({
message: intl.get('quantity_cannot_be_zero_or_empty'),

View File

@@ -1,14 +1,15 @@
import React, { useMemo } from 'react';
import classNames from 'classnames';
import { sumBy } from 'lodash';
import { useFormikContext } from 'formik';
import intl from 'react-intl-universal';
import { CLASSES } from 'common/classes';
import EstimateFormHeaderFields from './EstimateFormHeaderFields';
import { PageFormBigNumber } from 'components';
import withSettings from 'containers/Settings/withSettings';
import { getEntriesTotal } from 'containers/Entries/utils';
import { PageFormBigNumber } from 'components';
import { compose } from 'utils';
// Estimate form top header.
@@ -20,7 +21,7 @@ function EstimateFormHeader({
// Calculate the total due amount of bill entries.
const totalDueAmount = useMemo(
() => sumBy(values.entries, 'total'),
() => getEntriesTotal(values.entries),
[values.entries],
);

View File

@@ -8,6 +8,7 @@ import {
useCreateEstimate,
useEditEstimate,
} from 'hooks/query';
import { ITEMS_FILTER_ROLES } from './utils';
const EstimateFormContext = createContext();
@@ -18,31 +19,24 @@ function EstimateFormProvider({ estimateId, ...props }) {
const {
data: estimate,
isFetching: isEstimateFetching,
isLoading: isEstimateLoading,
} = useEstimate(estimateId, { enabled: !!estimateId });
// Filter all sellable items only.
const stringifiedFilterRoles = React.useMemo(
() =>
JSON.stringify([
{ index: 1, fieldKey: 'sellable', value: true, condition: '&&', comparator: 'equals', },
{ index: 2, fieldKey: 'active', value: true, condition: '&&', comparator: 'equals' },
]),
[],
);
// Handle fetch Items data table or list
const {
data: { items },
isFetching: isItemsFetching,
isLoading: isItemsLoading,
} = useItems({
page_size: 10000,
stringified_filter_roles: stringifiedFilterRoles,
stringified_filter_roles: ITEMS_FILTER_ROLES,
});
// Handle fetch customers data table or list
const {
data: { customers },
isFetch: isCustomersFetching,
isLoading: isCustomersLoading,
} = useCustomers({ page_size: 10000 });
// Handle fetch settings.
@@ -68,6 +62,10 @@ function EstimateFormProvider({ estimateId, ...props }) {
isItemsFetching,
isEstimateFetching,
isCustomersLoading,
isItemsLoading,
isEstimateLoading,
submitPayload,
setSubmitPayload,
@@ -77,7 +75,7 @@ function EstimateFormProvider({ estimateId, ...props }) {
return (
<DashboardInsider
loading={isCustomersFetching || isItemsFetching || isEstimateFetching}
loading={isCustomersLoading || isItemsLoading || isEstimateLoading}
name={'estimate-form'}
>
<EstimateFormContext.Provider value={provider} {...props} />

View File

@@ -17,6 +17,7 @@ export const defaultEstimateEntry = {
discount: '',
quantity: '',
description: '',
amount: '',
};
export const defaultEstimate = {
@@ -55,7 +56,7 @@ export const useObserveEstimateNoSettings = (prefix, nextNumber) => {
setFieldValue('estimate_number', estimateNo);
}, [setFieldValue, prefix, nextNumber]);
};
/**
* Detarmines customers fast field when update.
*/
@@ -75,3 +76,20 @@ export const entriesFieldShouldUpdate = (newProps, oldProps) => {
defaultFastFieldShouldUpdate(newProps, oldProps)
);
};
export const ITEMS_FILTER_ROLES = JSON.stringify([
{
index: 1,
fieldKey: 'sellable',
value: true,
condition: '&&',
comparator: 'equals',
},
{
index: 2,
fieldKey: 'active',
value: true,
condition: '&&',
comparator: 'equals',
},
]);

View File

@@ -1,13 +1,14 @@
import React, { useMemo } from 'react';
import classNames from 'classnames';
import { sumBy } from 'lodash';
import { useFormikContext } from 'formik';
import intl from 'react-intl-universal';
import { CLASSES } from 'common/classes';
import InvoiceFormHeaderFields from './InvoiceFormHeaderFields';
import { getEntriesTotal } from 'containers/Entries/utils';
import { PageFormBigNumber } from 'components';
import withSettings from 'containers/Settings/withSettings';
import { compose } from 'redux';
@@ -23,7 +24,7 @@ function InvoiceFormHeader({
// Calculate the total due amount of invoice entries.
const totalDueAmount = useMemo(
() => sumBy(values.entries, 'total'),
() => getEntriesTotal(values.entries),
[values.entries],
);

View File

@@ -2,7 +2,7 @@ import React, { createContext, useState } from 'react';
import { isEmpty, pick } from 'lodash';
import { useLocation } from 'react-router-dom';
import DashboardInsider from 'components/Dashboard/DashboardInsider';
import { transformToEditForm } from './utils';
import { transformToEditForm, ITEMS_FILTER_ROLES_QUERY } from './utils';
import {
useInvoice,
useItems,
@@ -27,10 +27,10 @@ function InvoiceFormProvider({ invoiceId, ...props }) {
});
// Fetches the estimate by the given id.
const {
data: estimate,
isLoading: isEstimateLoading,
} = useEstimate(estimateId, { enabled: !!estimateId });
const { data: estimate, isLoading: isEstimateLoading } = useEstimate(
estimateId,
{ enabled: !!estimateId },
);
const newInvoice = !isEmpty(estimate)
? transformToEditForm({
@@ -38,23 +38,13 @@ function InvoiceFormProvider({ invoiceId, ...props }) {
})
: [];
// Filter all sellable items only.
const stringifiedFilterRoles = React.useMemo(
() =>
JSON.stringify([
{ index: 1, fieldKey: 'sellable', value: true, condition: '&&', comparator: 'equals', },
{ index: 2, fieldKey: 'active', value: true, condition: '&&', comparator: 'equals' },
]),
[],
);
// Handle fetching the items table based on the given query.
const {
data: { items },
isLoading: isItemsLoading,
} = useItems({
page_size: 10000,
stringified_filter_roles: stringifiedFilterRoles,
stringified_filter_roles: ITEMS_FILTER_ROLES_QUERY,
});
// Handle fetch customers data table or list

View File

@@ -26,7 +26,7 @@ export const defaultInvoiceEntry = {
discount: '',
quantity: '',
description: '',
total: 0,
amount: '',
};
// Default invoice object.
@@ -63,6 +63,9 @@ export function transformToEditForm(invoice) {
};
}
/**
* Transformes the response errors types.
*/
export const transformErrors = (errors, { setErrors }) => {
if (errors.some((e) => e.type === ERROR.SALE_INVOICE_NUMBER_IS_EXISTS)) {
setErrors({
@@ -102,6 +105,9 @@ export const useObserveInvoiceNoSettings = (prefix, nextNumber) => {
}, [setFieldValue, prefix, nextNumber]);
};
/**
* Detarmines customer name field when should update.
*/
export const customerNameFieldShouldUpdate = (newProps, oldProps) => {
return (
newProps.customers !== oldProps.customers ||
@@ -109,9 +115,29 @@ export const customerNameFieldShouldUpdate = (newProps, oldProps) => {
);
};
/**
* Detarmines invoice entries field when should update.
*/
export const entriesFieldShouldUpdate = (newProps, oldProps) => {
return (
newProps.items !== oldProps.items ||
defaultFastFieldShouldUpdate(newProps, oldProps)
);
};
export const ITEMS_FILTER_ROLES_QUERY = JSON.stringify([
{
index: 1,
fieldKey: 'sellable',
value: true,
condition: '&&',
comparator: 'equals',
},
{
index: 2,
fieldKey: 'active',
value: true,
condition: '&&',
comparator: 'equals',
},
]);

View File

@@ -1,15 +1,15 @@
import React, { useMemo } from 'react';
import classNames from 'classnames';
import { sumBy } from 'lodash';
import { useFormikContext } from 'formik';
import { CLASSES } from 'common/classes';
import ReceiptFormHeaderFields from './ReceiptFormHeaderFields';
import { PageFormBigNumber } from 'components';
import intl from 'react-intl-universal';
import { CLASSES } from 'common/classes';
import { PageFormBigNumber } from 'components';
import ReceiptFormHeaderFields from './ReceiptFormHeaderFields';
import withSettings from 'containers/Settings/withSettings';
import { getEntriesTotal } from 'containers/Entries/utils';
import { compose } from 'redux';
/**
@@ -25,7 +25,7 @@ function ReceiptFormHeader({
// Calculate the total due amount of bill entries.
const totalDueAmount = useMemo(
() => sumBy(values.entries, 'total'),
() => getEntriesTotal(values.entries),
[values.entries],
);

View File

@@ -17,6 +17,7 @@ export const defaultReceiptEntry = {
discount: '',
quantity: '',
description: '',
amount: '',
};
export const defaultReceipt = {