WIP/ Feature : Estimate & Receipt & Bill & Invoice

This commit is contained in:
elforjani3
2020-08-12 21:19:52 +02:00
parent 58c6b5466c
commit c86f0b29bf
66 changed files with 4288 additions and 535 deletions

View File

@@ -4,7 +4,7 @@ import t from 'store/types';
export const fetchBillsTable = ({ query = {} }) => {
return (dispatch, getState) =>
new Promise((resolve, rejcet) => {
const pageQuery = getState().bill.tableQuery;
const pageQuery = getState().bills.tableQuery;
dispatch({
type: t.BILLS_TABLE_LOADING,
@@ -12,7 +12,7 @@ export const fetchBillsTable = ({ query = {} }) => {
loading: true,
},
});
ApiService.get('bills', {
ApiService.get('purchases/bills', {
params: { ...pageQuery, ...query },
})
.then((response) => {
@@ -54,9 +54,9 @@ export const fetchBillsTable = ({ query = {} }) => {
export const deleteBill = ({ id }) => {
return (dispatch) =>
new Promise((resovle, reject) => {
ApiService.delete(`bills/${id}`)
ApiService.delete(`purchases/bills/${id}`)
.then((response) => {
dispatch({ type: t.BILL_DELETE });
dispatch({ type: t.BILL_DELETE, payload: { id } });
resovle(response);
})
.catch((error) => {
@@ -71,7 +71,7 @@ export const submitBill = ({ form }) => {
dispatch({
type: t.SET_DASHBOARD_REQUEST_LOADING,
});
ApiService.post('bills', form)
ApiService.post('purchases/bills', form)
.then((response) => {
dispatch({
type: t.SET_DASHBOARD_REQUEST_COMPLETED,
@@ -92,7 +92,7 @@ export const submitBill = ({ form }) => {
export const fetchBill = ({ id }) => {
return (dispatch) =>
new Promise((resovle, reject) => {
ApiService.get(`bills/${id}`)
ApiService.get(`purchases/bills/${id}`)
.then((response) => {
dispatch({
type: t.BILL_SET,
@@ -117,7 +117,7 @@ export const editBill = (id, form) => {
dispatch({
type: t.SET_DASHBOARD_REQUEST_LOADING,
});
ApiService.post(`bills/${id}`, form)
ApiService.post(`purchases/bills/${id}`, form)
.then((response) => {
dispatch({
type: t.SET_DASHBOARD_REQUEST_COMPLETED,

View File

@@ -0,0 +1,102 @@
import { createReducer } from '@reduxjs/toolkit';
import { createTableQueryReducers } from 'store/queryReducers';
import t from 'store/types';
const initialState = {
items: {},
views: {},
loading: false,
currentViewId: -1,
tableQuery: {
page_size: 5,
page: 1,
},
};
const defaultBill = {
entries: [],
};
const reducer = 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) => {
_bills[bill.id] = {
...defaultBill,
...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.BILLS_PAGINATION_SET]: (state, action) => {
const { pagination, customViewId } = action.payload;
const mapped = {
pageSize: parseInt(pagination.pageSize, 10),
page: parseInt(pagination.page, 10),
total: parseInt(pagination.total, 10),
};
const paginationMeta = {
...mapped,
pagesCount: Math.ceil(mapped.total / mapped.pageSize),
pageIndex: Math.max(mapped.page - 1, 0),
};
state.views = {
...state.views,
[customViewId]: {
...(state.views?.[customViewId] || {}),
paginationMeta,
},
};
},
});
export default createTableQueryReducers('bills', reducer);

View File

@@ -1,6 +1,52 @@
import { createSelector } from '@reduxjs/toolkit';
import { pickItemsFromIds, paginationLocationQuery } from 'store/selectors';
const billByIdSelector = (state, props) => state.bills.items[props.billId];
const billTableQuery = (state) => {
return state.bills.tableQuery;
};
export const getBillById = () =>
createSelector(billByIdSelector, (_bill) => _bill);
export const getBillTableQuery = createSelector(
paginationLocationQuery,
billTableQuery,
(locationQuery, tableQuery) => {
return {
...locationQuery,
...tableQuery,
};
},
);
const billPageSelector = (state, props, query) => {
const viewId = state.bills.currentViewId;
return state.bills.views?.[viewId]?.pages?.[query.page];
};
const billItemsSelector = (state) => {
return state.bills.items;
};
export const getBillCurrentPageFactory = () =>
createSelector(billPageSelector, billItemsSelector, (billPage, billItems) => {
return typeof billPage === 'object'
? pickItemsFromIds(billItems, billPage.ids) || []
: [];
});
const billByIdSelector = (state, props) => {
return state.bills.items[props.billId];
};
export const getBillByIdFactory = () =>
createSelector(billByIdSelector, (bill) => {
return bill;
});
const billPaginationSelector = (state, props) => {
const viewId = state.bills.currentViewId;
return state.bills.views?.[viewId];
};
export const getBillPaginationMetaFactory = () =>
createSelector(billPaginationSelector, (billPage) => {
return billPage?.paginationMeta || {};
});