refactoring: sales tables.

refacoring: purchases tables.
This commit is contained in:
a.bouhuolia
2021-02-11 20:45:06 +02:00
parent 3901c336df
commit d48532a7e6
210 changed files with 2799 additions and 5392 deletions

View File

@@ -1,152 +1,10 @@
import ApiService from 'services/ApiService';
import t from 'store/types';
export const fetchBillsTable = ({ query = {} }) => {
return (dispatch, getState) =>
new Promise((resolve, rejcet) => {
const pageQuery = getState().bills.tableQuery;
dispatch({
type: t.BILLS_TABLE_LOADING,
payload: {
loading: true,
},
});
ApiService.get('purchases/bills', {
params: { ...pageQuery, ...query },
})
.then((response) => {
dispatch({
type: t.BILLS_PAGE_SET,
payload: {
bills: response.data.bills,
pagination: response.data.pagination,
customViewId:
response.data?.filter_meta?.view?.custom_view_id || -1,
},
});
dispatch({
type: t.BILLS_ITEMS_SET,
payload: {
bills: response.data.bills,
},
});
dispatch({
type: t.BILLS_PAGINATION_SET,
payload: {
pagination: response.data.pagination,
customViewId:
response.data?.filter_meta?.view?.custom_view_id || -1,
},
});
dispatch({
type: t.BILLS_TABLE_LOADING,
payload: {
loading: false,
},
});
resolve(response);
})
.catch((error) => {
rejcet(error);
});
});
export const setBillsTableState = (queries) => {
return {
type: t.BILLS_TABLE_STATE_SET,
payload: { queries },
};
};
export const deleteBill = ({ id }) => {
return (dispatch) =>
new Promise((resovle, reject) => {
ApiService.delete(`purchases/bills/${id}`)
.then((response) => {
dispatch({ type: t.BILL_DELETE, payload: { id } });
resovle(response);
})
.catch((error) => {
reject(error.response.data.errors || []);
});
});
};
export const submitBill = ({ form }) => {
return (dispatch) =>
new Promise((resolve, reject) => {
ApiService.post('purchases/bills', form)
.then((response) => {
resolve(response);
})
.catch((error) => {
const { response } = error;
const { data } = response;
reject(data?.errors);
});
});
};
export const fetchBill = ({ id }) => {
return (dispatch) =>
new Promise((resovle, reject) => {
ApiService.get(`purchases/bills/${id}`)
.then((response) => {
const { bill } = response.data;
dispatch({
type: t.BILL_SET,
payload: { id, bill },
});
resovle(response);
})
.catch((error) => {
const { response } = error;
const { data } = response;
reject(data?.errors);
});
});
};
export const editBill = (id, form) => {
return (dispatch) =>
new Promise((resolve, reject) => {
ApiService.post(`purchases/bills/${id}`, form)
.then((response) => {
resolve(response);
})
.catch((error) => {
const { response } = error;
const { data } = response;
reject(data?.errors);
});
});
};
export const fetchDueBills = ({ vendorId }) => (dispatch) =>
new Promise((resolve, reject) => {
const params = { vendor_id: vendorId };
ApiService.get(`purchases/bills/due`, { params })
.then((response) => {
dispatch({
type: t.BILLS_ITEMS_SET,
payload: {
bills: response.data.bills,
},
});
if (vendorId) {
dispatch({
type: t.BILLS_PAYABLE_BY_VENDOR_ID,
payload: {
bills: response.data.bills,
},
});
}
resolve(response);
})
.catch((error) => {
reject(error);
});
});
export const openBill = ({ id }) => {
return (dispatch) => ApiService.post(`purchases/bills/${id}/open`);
};
export const setSelectedRowsItems = () => {};

View File

@@ -1,131 +1,15 @@
import { createReducer } from '@reduxjs/toolkit';
import {
createTableQueryReducers,
viewPaginationSetReducer,
} 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,
},
nextBillNumberChanged: false,
payable: {
byVendorId: [],
byBillPayamentId: [],
},
byBillPayamentId: {},
};
const defaultBill = {
entries: [],
};
export default createReducer(initialState, {
[t.BILL_SET]: (state, action) => {
const { id, bill } = action.payload;
const _bill = state.items[id] || {};
state.items[id] = { ...defaultBill, ..._bill, ...bill };
},
[t.BILLS_TABLE_LOADING]: (state, action) => {
const { loading } = action.payload;
state.loading = loading;
},
[t.BILLS_SET_CURRENT_VIEW]: (state, action) => {
state.currentViewId = action.currentViewId;
},
[t.BILLS_ITEMS_SET]: (state, action) => {
const { bills } = action.payload;
const _bills = {};
bills.forEach((bill) => {
const oldBill = state.items[bill.id] || {};
_bills[bill.id] = {
...defaultBill,
...oldBill,
...bill,
};
});
state.items = {
...state.items,
..._bills,
};
},
[t.BILL_DELETE]: (state, action) => {
const { id } = action.payload;
if (typeof state.items[id] !== 'undefined') {
delete state.items[id];
}
},
[t.BILLS_PAGE_SET]: (state, action) => {
const { customViewId, bills, pagination } = action.payload;
const viewId = customViewId || -1;
const view = state.views[viewId] || {};
state.views[viewId] = {
...view,
pages: {
...(state.views?.[viewId]?.pages || {}),
[pagination.page]: {
ids: bills.map((i) => i.id),
},
},
};
},
[t.BILL_NUMBER_CHANGED]: (state, action) => {
const { isChanged } = action.payload;
state.nextBillNumberChanged = isChanged;
},
[t.BILLS_PAYABLE_BY_VENDOR_ID]: (state, action) => {
const { bills } = action.payload;
const _data = {};
bills.forEach((bill) => {
if (!_data[bill.vendor_id]) {
_data[bill.vendor_id] = [];
}
_data[bill.vendor_id].push(bill.id);
});
state.payable.byVendorId = {
...state.payable.byVendorId,
..._data,
};
},
[t.BILLS_PAYABLE_BY_PAYMENT_ID]: (state, action) => {
const { bills, billPaymentId } = action.payload;
const _data = [];
bills.forEach((bill) => {
_data.push(bill.id);
});
state.payable.byBillPayamentId[billPaymentId] = _data;
},
[t.BILLS_BY_PAYMENT_ID]: (state, action) => {
const { bills, billPaymentId } = action.payload;
const billsIds = bills.map((bill) => bill.id);
state.byBillPayamentId[billPaymentId] = billsIds;
},
...viewPaginationSetReducer(t.BILLS_PAGINATION_SET),
...createTableQueryReducers('BILLS'),
...createTableStateReducers('BILLS'),
});

View File

@@ -1,121 +1,17 @@
import { createSelector } from '@reduxjs/toolkit';
import {
pickItemsFromIds,
paginationLocationQuery,
defaultPaginationMeta,
getCurrentPageResults,
} from 'store/selectors';
import { paginationLocationQuery } from 'store/selectors';
import { createDeepEqualSelector } from 'utils';
// Retreive bills table query.
const billTableQuery = (state) => state.bills.tableQuery;
const billsTableStateSelector = (state) => state.bills.tableState;
const billPageSelector = (state, props, query) => {
const viewId = state.bills.currentViewId;
const currentView = state.bills.views?.[viewId];
const currentPageId = currentView?.paginationMeta?.page;
return currentView?.pages?.[currentPageId];
};
// Retreive bills items.
const billItemsSelector = (state) => state.bills.items;
// Retrieve bill details.
const billByIdSelector = (state, props) => state.bills.items[props.billId];
// Retrieve vendor due bills ids.
const billsPayableVendorSelector = (state, props) =>
state.bills.payable.byVendorId[props.vendorId];
const billPaginationSelector = (state, props) => {
const viewId = state.bills.currentViewId;
return state.bills.views?.[viewId];
};
const getBillsCurrentViewIdSelector = (state) => state.bills.currentViewId;
export const getBillTableQueryFactory = () =>
createSelector(
// Get bills table state marged with location query.
export const getBillsTableStateFactory = () =>
createDeepEqualSelector(
paginationLocationQuery,
billTableQuery,
(locationQuery, tableQuery) => {
billsTableStateSelector,
(locationQuery, tableState) => {
return {
...locationQuery,
...tableQuery,
...tableState,
};
},
);
/**
* Get current page bills items.
* @return {Array}
*/
export const getBillCurrentPageFactory = () =>
createSelector(billPageSelector, billItemsSelector, (billPage, billItems) => {
return typeof billPage === 'object'
? pickItemsFromIds(billItems, billPage.ids) || []
: [];
});
/**
* Retrieve bill details of the given bill id.
*/
export const getBillByIdFactory = () =>
createSelector(billByIdSelector, (bill) => {
return bill;
});
/**
* Retrieve bills datatable pagination meta.
*/
export const getBillPaginationMetaFactory = () =>
createSelector(billPaginationSelector, (billPage) => {
return {
...defaultPaginationMeta(),
...(billPage?.paginationMeta || {}),
};
});
/**
* Retrieve vendor payable bills.
*/
export const getVendorPayableBillsFactory = () =>
createSelector(
billItemsSelector,
billsPayableVendorSelector,
(billsItems, payableBillsIds) => {
return Array.isArray(payableBillsIds)
? pickItemsFromIds(billsItems, payableBillsIds) || []
: [];
},
);
/**
* Retrieve vendor payable bills entries.
*/
export const getVendorPayableBillsEntriesFactory = () =>
createSelector(
billItemsSelector,
billsPayableVendorSelector,
(billsItems, payableBillsIds) => {
const bills = Array.isArray(payableBillsIds)
? pickItemsFromIds(billsItems, payableBillsIds) || []
: [];
return bills.map((bill) => ({
...bill,
bill_id: bill.id,
total_payment_amount: bill.payment_amount,
id: null,
payment_amount: null,
}));
},
);
// Retreive the current bills view id.
export const getBillsCurrentViewIdFactory = () =>
createSelector(
getBillsCurrentViewIdSelector,
(currentViewId) => {
return currentViewId;
}
);

View File

@@ -1,16 +1,4 @@
export default {
BILL_DELETE: 'BILL_DELETE',
BILLS_BULK_DELETE: 'BILLS_BULK_DELETE',
BILLS_LIST_SET: 'BILLS_LIST_SET',
BILL_SET: 'BILL_SET',
BILLS_SET_CURRENT_VIEW: 'BILLS_SET_CURRENT_VIEW',
BILLS_TABLE_QUERIES_ADD: 'BILLS/TABLE_QUERIES_ADD',
BILLS_TABLE_LOADING: 'BILLS_TABLE_LOADING',
BILLS_PAGINATION_SET: 'BILLS_PAGINATION_SET',
BILLS_PAGE_SET: 'BILLS_PAGE_SET',
BILLS_ITEMS_SET: 'BILLS_ITEMS_SET',
BILL_NUMBER_CHANGED: 'BILL_NUMBER_CHANGED',
BILLS_PAYABLE_BY_PAYMENT_ID: 'BILLS_PAYABLE_BY_PAYMENT_ID',
BILLS_PAYABLE_BY_VENDOR_ID: 'BILLS_PAYABLE_BY_VENDOR_ID',
BILLS_BY_PAYMENT_ID: 'BILLS_BY_PAYMENT_ID',
};
BILLS_TABLE_STATE_SET: 'BILLS/TABLE_STATE_SET',
};