mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 13:20:31 +00:00
refactoring: sales tables.
refacoring: purchases tables.
This commit is contained in:
@@ -1,156 +1,10 @@
|
||||
import ApiService from 'services/ApiService';
|
||||
import t from 'store/types';
|
||||
|
||||
export const submitInvoice = ({ form }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resolve, reject) => {
|
||||
ApiService.post('sales/invoices', form)
|
||||
.then((response) => {
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
|
||||
reject(data?.errors);
|
||||
});
|
||||
});
|
||||
export const setInvoicesTableState = (queries) => {
|
||||
return {
|
||||
type: t.INVOICES_TABLE_STATE_SET,
|
||||
payload: { queries },
|
||||
};
|
||||
};
|
||||
|
||||
export const deleteInvoice = ({ id }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resovle, reject) => {
|
||||
ApiService.delete(`sales/invoices/${id}`)
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.INVOICE_DELETE,
|
||||
payload: { id },
|
||||
});
|
||||
resovle(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error.response.data.errors || []);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const editInvoice = (id, form) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resolve, reject) => {
|
||||
ApiService.post(`sales/invoices/${id}`, form)
|
||||
.then((response) => {
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
|
||||
reject(data?.errors);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const fetchInvoicesTable = ({ query } = {}) => {
|
||||
return (dispatch, getState) =>
|
||||
new Promise((resolve, reject) => {
|
||||
const pageQuery = getState().salesInvoices.tableQuery;
|
||||
dispatch({
|
||||
type: t.INVOICES_TABLE_LOADING,
|
||||
payload: {
|
||||
loading: true,
|
||||
},
|
||||
});
|
||||
ApiService.get('sales/invoices', {
|
||||
params: { ...pageQuery, ...query },
|
||||
})
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.INVOICES_PAGE_SET,
|
||||
payload: {
|
||||
sales_invoices: response.data.sales_invoices,
|
||||
pagination: response.data.pagination,
|
||||
customViewId:
|
||||
response.data?.filter_meta?.view?.custom_view_id || -1,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.INVOICES_ITEMS_SET,
|
||||
payload: {
|
||||
sales_invoices: response.data.sales_invoices,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.INVOICES_PAGINATION_SET,
|
||||
payload: {
|
||||
pagination: response.data.pagination,
|
||||
customViewId: response.data?.filter_meta?.view?.custom_view_id ||-1
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.INVOICES_TABLE_LOADING,
|
||||
payload: {
|
||||
loading: false,
|
||||
},
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const fetchInvoice = ({ id }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resovle, reject) => {
|
||||
ApiService.get(`sales/invoices/${id}`)
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.INVOICE_SET,
|
||||
payload: {
|
||||
id,
|
||||
sale_invoice: response.data.sale_invoice,
|
||||
},
|
||||
});
|
||||
resovle(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
reject(data?.errors);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const fetchDueInvoices = ({ customerId }) => (dispatch) =>
|
||||
new Promise((resovle, reject) => {
|
||||
ApiService.get(`sales/invoices/payable`, {
|
||||
params: { customer_id: customerId },
|
||||
})
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.INVOICES_ITEMS_SET,
|
||||
payload: {
|
||||
sales_invoices: response.data.sales_invoices,
|
||||
},
|
||||
});
|
||||
if (customerId) {
|
||||
dispatch({
|
||||
type: t.INVOICES_RECEIVABLE_BY_CUSTOMER_ID,
|
||||
payload: {
|
||||
customerId,
|
||||
saleInvoices: response.data.sales_invoices,
|
||||
},
|
||||
});
|
||||
}
|
||||
resovle(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
reject(data?.errors);
|
||||
});
|
||||
});
|
||||
export const deliverInvoice = ({ id }) => {
|
||||
return (dispatch) => ApiService.post(`sales/invoices/${id}/deliver`);
|
||||
};
|
||||
export const setSelectedRowsItems = () => {};
|
||||
|
||||
@@ -1,115 +1,15 @@
|
||||
import { createReducer } from '@reduxjs/toolkit';
|
||||
import {
|
||||
journalNumberChangedReducer,
|
||||
viewPaginationSetReducer,
|
||||
createTableQueryReducers,
|
||||
} from 'store/journalNumber.reducer';
|
||||
import t from 'store/types';
|
||||
createTableStateReducers,
|
||||
} from 'store/tableState.reducer';
|
||||
|
||||
const initialState = {
|
||||
items: {},
|
||||
views: {},
|
||||
loading: false,
|
||||
currentViewId: -1,
|
||||
tableQuery: {
|
||||
page_size: 12,
|
||||
page: 1,
|
||||
tableState: {
|
||||
pageSize: 12,
|
||||
pageIndex: 0,
|
||||
},
|
||||
dueInvoices: {},
|
||||
receivable: {
|
||||
byCustomerId: [],
|
||||
byPaymentReceiveId: [],
|
||||
},
|
||||
byPaymentReceiveId: {},
|
||||
};
|
||||
|
||||
const defaultInvoice = {
|
||||
entries: [],
|
||||
};
|
||||
|
||||
export default createReducer(initialState, {
|
||||
[t.INVOICE_SET]: (state, action) => {
|
||||
const { id, sale_invoice } = action.payload;
|
||||
const _invoice = state.items[id] || {};
|
||||
state.items[id] = { ...defaultInvoice, ..._invoice, ...sale_invoice };
|
||||
},
|
||||
|
||||
[t.INVOICES_ITEMS_SET]: (state, action) => {
|
||||
const { sales_invoices } = action.payload;
|
||||
const _invoices = {};
|
||||
|
||||
sales_invoices.forEach((invoice) => {
|
||||
_invoices[invoice.id] = {
|
||||
...defaultInvoice,
|
||||
...invoice,
|
||||
};
|
||||
});
|
||||
state.items = {
|
||||
...state.items,
|
||||
..._invoices,
|
||||
};
|
||||
},
|
||||
|
||||
[t.INVOICES_TABLE_LOADING]: (state, action) => {
|
||||
const { loading } = action.payload;
|
||||
state.loading = loading;
|
||||
},
|
||||
|
||||
[t.INVOICES_SET_CURRENT_VIEW]: (state, action) => {
|
||||
state.currentViewId = action.currentViewId;
|
||||
},
|
||||
|
||||
[t.INVOICE_DELETE]: (state, action) => {
|
||||
const { id } = action.payload;
|
||||
|
||||
if (typeof state.items[id] !== 'undefined') {
|
||||
delete state.items[id];
|
||||
}
|
||||
},
|
||||
|
||||
[t.INVOICES_PAGE_SET]: (state, action) => {
|
||||
const { customViewId, sales_invoices, pagination } = action.payload;
|
||||
|
||||
const viewId = customViewId || -1;
|
||||
const view = state.views[viewId] || {};
|
||||
|
||||
state.views[viewId] = {
|
||||
...view,
|
||||
pages: {
|
||||
...(state.views?.[viewId]?.pages || {}),
|
||||
[pagination.page]: {
|
||||
ids: sales_invoices.map((i) => i.id),
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
[t.INVOICES_RECEIVABLE_BY_PAYMENT_ID]: (state, action) => {
|
||||
const { paymentReceiveId, saleInvoices } = action.payload;
|
||||
const saleInvoicesIds = saleInvoices.map((saleInvoice) => saleInvoice.id);
|
||||
|
||||
state.receivable.byPaymentReceiveId[paymentReceiveId] = saleInvoicesIds;
|
||||
},
|
||||
|
||||
[t.INVOICES_RECEIVABLE_BY_CUSTOMER_ID]: (state, action) => {
|
||||
const { customerId, saleInvoices } = action.payload;
|
||||
const saleInvoiceIds = saleInvoices.map((saleInvoice) => saleInvoice.id);
|
||||
|
||||
state.receivable.byCustomerId[customerId] = saleInvoiceIds;
|
||||
},
|
||||
|
||||
[t.INVOICES_BY_PAYMENT_ID]: (state, action) => {
|
||||
const { paymentReceiveId, saleInvoices } = action.payload;
|
||||
const saleInvoiceIds = saleInvoices.map((saleInvoice) => saleInvoice.id);
|
||||
|
||||
state.byPaymentReceiveId[paymentReceiveId] = saleInvoiceIds;
|
||||
},
|
||||
|
||||
...journalNumberChangedReducer(t.INVOICE_NUMBER_CHANGED),
|
||||
...viewPaginationSetReducer(t.INVOICES_PAGINATION_SET),
|
||||
...createTableQueryReducers('INVOICES'),
|
||||
...createTableStateReducers('INVOICES'),
|
||||
});
|
||||
|
||||
export const getInvoiceById = (state, id) => {
|
||||
return state.sales_invoices.items[id];
|
||||
};
|
||||
|
||||
@@ -1,98 +1,19 @@
|
||||
import { createSelector } from '@reduxjs/toolkit';
|
||||
import {
|
||||
pickItemsFromIds,
|
||||
paginationLocationQuery,
|
||||
defaultPaginationMeta,
|
||||
} from 'store/selectors';
|
||||
import { paginationLocationQuery } from 'store/selectors';
|
||||
import { createDeepEqualSelector } from 'utils';
|
||||
|
||||
const invoiceTableQuery = (state) => state.salesInvoices.tableQuery;
|
||||
const invoicesTableStateSelector = (state) => state.salesInvoices.tableState;
|
||||
|
||||
const invoicesByIdSelector = (state, props) =>
|
||||
state.salesInvoices.items[props.invoiceId];
|
||||
|
||||
const invoicesPaginationSelector = (state, props) => {
|
||||
const viewId = state.salesInvoices.currentViewId;
|
||||
return state.salesInvoices.views?.[viewId];
|
||||
};
|
||||
|
||||
const invoicesPageSelector = (state, props, query) => {
|
||||
const viewId = state.salesInvoices.currentViewId;
|
||||
const currentView = state.salesInvoices.views?.[viewId];
|
||||
const currentPageId = currentView?.paginationMeta?.page;
|
||||
|
||||
return currentView?.pages?.[currentPageId];
|
||||
};
|
||||
|
||||
const invoicesItemsSelector = (state) => state.salesInvoices.items;
|
||||
const invoicesReceiableCustomerSelector = (state, props) =>
|
||||
state.salesInvoices.receivable.byCustomerId[props.customerId];
|
||||
|
||||
const getInvoicesCurrentViewIdSelector = (state) => state.salesInvoices.currentViewId;
|
||||
|
||||
export const getInvoiceTableQueryFactory = () =>
|
||||
createSelector(
|
||||
/**
|
||||
* Retrieve invoices table state.
|
||||
*/
|
||||
export const getInvoicesTableStateFactory = () =>
|
||||
createDeepEqualSelector(
|
||||
paginationLocationQuery,
|
||||
invoiceTableQuery,
|
||||
(locationQuery, tableQuery) => {
|
||||
invoicesTableStateSelector,
|
||||
(locationQuery, tableState) => {
|
||||
return {
|
||||
...locationQuery,
|
||||
...tableQuery,
|
||||
...tableState,
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
// Retrieve invoices of the current view and view.
|
||||
export const getInvoiceCurrentPageFactory = () =>
|
||||
createSelector(
|
||||
invoicesPageSelector,
|
||||
invoicesItemsSelector,
|
||||
(invoicePage, invoicesItems) => {
|
||||
return typeof invoicePage === 'object'
|
||||
? pickItemsFromIds(invoicesItems, invoicePage.ids) || []
|
||||
: [];
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
|
||||
// Retrieve specific invoice by the passed invoice id.
|
||||
export const getInvoiecsByIdFactory = () =>
|
||||
createSelector(invoicesByIdSelector, (invoice) => {
|
||||
return invoice;
|
||||
});
|
||||
|
||||
// Retrieve invoices pagination meta.
|
||||
export const getInvoicePaginationMetaFactory = () =>
|
||||
createSelector(invoicesPaginationSelector, (invoicePage) => {
|
||||
return {
|
||||
...defaultPaginationMeta(),
|
||||
...(invoicePage?.paginationMeta || {}),
|
||||
};
|
||||
});
|
||||
|
||||
export const getCustomerReceivableInvoicesEntriesFactory = () =>
|
||||
createSelector(
|
||||
invoicesItemsSelector,
|
||||
invoicesReceiableCustomerSelector,
|
||||
(invoicesItems, customerInvoicesIds) => {
|
||||
const invoicesIds = [...(customerInvoicesIds || [])];
|
||||
const invoices = pickItemsFromIds(invoicesItems, invoicesIds);
|
||||
|
||||
return invoices.map((invoice) => ({
|
||||
...invoice,
|
||||
invoice_id: invoice.id,
|
||||
total_payment_amount: invoice.payment_amount,
|
||||
id: null,
|
||||
payment_amount: 0,
|
||||
}));
|
||||
},
|
||||
);
|
||||
|
||||
// Retrieve sale invoices current view id.
|
||||
export const getInvoicesCurrentViewIdFactory = () =>
|
||||
createSelector(
|
||||
getInvoicesCurrentViewIdSelector,
|
||||
(currentViewId) => {
|
||||
return currentViewId;
|
||||
}
|
||||
);
|
||||
@@ -1,20 +1,4 @@
|
||||
|
||||
export default {
|
||||
INVOICE_DELETE: 'INVOICE_DELETE',
|
||||
INVOICES_BULK_DELETE: 'INVOICES_BULK_DELETE',
|
||||
INVOICES_LIST_SET: 'INVOICES_LIST_SET',
|
||||
INVOICE_SET: 'INVOICE_SET',
|
||||
INVOICES_SET_CURRENT_VIEW: 'INVOICES_SET_CURRENT_VIEW',
|
||||
INVOICES_TABLE_QUERIES_ADD: 'INVOICES/TABLE_QUERIES_ADD',
|
||||
INVOICES_TABLE_LOADING: 'INVOICES_TABLE_LOADING',
|
||||
INVOICES_PAGINATION_SET: 'INVOICES_PAGINATION_SET',
|
||||
INVOICES_PAGE_SET: 'INVOICES_PAGE_SET',
|
||||
INVOICES_ITEMS_SET: 'INVOICES_ITEMS_SET',
|
||||
DUE_INVOICES_SET: 'DUE_INVOICES_SET',
|
||||
RELOAD_INVOICES: 'RELOAD_INVOICES',
|
||||
|
||||
INVOICES_RECEIVABLE_BY_PAYMENT_ID: 'INVOICES_RECEIVABLE_BY_PAYMENT_ID',
|
||||
INVOICES_RECEIVABLE_BY_CUSTOMER_ID: 'INVOICES_RECEIVABLE_BY_CUSTOMER_ID',
|
||||
INVOICES_BY_PAYMENT_ID: 'INVOICES_BY_PAYMENT_ID',
|
||||
|
||||
INVOICE_NUMBER_CHANGED: 'INVOICE_NUMBER_CHANGED',
|
||||
};
|
||||
INVOICES_TABLE_STATE_SET: 'INVOICES/TABLE_STATE_SET',
|
||||
};
|
||||
Reference in New Issue
Block a user