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',
};

View File

@@ -1,135 +1,10 @@
import ApiService from 'services/ApiService';
import t from 'store/types';
export const submitEstimate = ({ form }) => {
return (dispatch) =>
new Promise((resolve, reject) => {
ApiService.post('sales/estimates', form)
.then((response) => {
resolve(response);
})
.catch((error) => {
const { response } = error;
const { data } = response;
reject(data?.errors);
});
});
export const setEstimatesTableState = (queries) => {
return {
type: t.ESTIMATES_TABLE_STATE_SET,
payload: { queries },
};
};
export const editEstimate = (id, form) => {
return (dispatch) =>
new Promise((resolve, reject) => {
ApiService.post(`sales/estimates/${id}`, form)
.then((response) => {
resolve(response);
})
.catch((error) => {
const { response } = error;
const { data } = response;
reject(data?.errors);
});
});
};
export const deleteEstimate = ({ id }) => {
return (dispatch) =>
new Promise((resovle, reject) => {
ApiService.delete(`sales/estimates/${id}`)
.then((response) => {
dispatch({
type: t.ESTIMATE_DELETE,
payload: { id },
});
resovle(response);
})
.catch((error) => {
reject(error.response.data.errors || []);
});
});
};
export const fetchEstimate = ({ id }) => {
return (dispatch) =>
new Promise((resovle, reject) => {
ApiService.get(`sales/estimates/${id}`)
.then((response) => {
const { estimate } = response.data;
dispatch({
type: t.ESTIMATE_SET,
payload: {
id,
estimate,
},
});
resovle(response);
})
.catch((error) => {
const { response } = error;
const { data } = response;
reject(data?.errors);
});
});
};
export const fetchEstimatesTable = ({ query = {} }) => {
return (dispatch, getState) =>
new Promise((resolve, reject) => {
const pageQuery = getState().salesEstimates.tableQuery;
dispatch({
type: t.ESTIMATES_TABLE_LOADING,
payload: {
loading: true,
},
});
ApiService.get('sales/estimates', {
params: { ...pageQuery, ...query },
})
.then((response) => {
dispatch({
type: t.ESTIMATES_PAGE_SET,
payload: {
sales_estimates: response.data.sales_estimates,
pagination: response.data.pagination,
customViewId:
response.data?.filter_meta?.view?.custom_view_id || -1,
},
});
dispatch({
type: t.ESTIMATES_ITEMS_SET,
payload: {
sales_estimates: response.data.sales_estimates,
},
});
dispatch({
type: t.ESTIMATES_PAGINATION_SET,
payload: {
pagination: response.data.pagination,
customViewId:
response.data?.filter_meta?.view?.custom_view_id || -1,
},
});
dispatch({
type: t.ESTIMATES_TABLE_LOADING,
payload: {
loading: false,
},
});
resolve(response);
})
.catch((error) => {
reject(error);
});
});
};
export const deliverEstimate = ({ id }) => {
return (dispatch) => ApiService.post(`sales/estimates/${id}/deliver`);
};
export const approveEstimate = ({ id }) => {
return (dispatch) => ApiService.post(`sales/estimates/${id}/approve`);
};
export const rejectEstimate = ({ id }) => {
return (dispatch) => ApiService.post(`sales/estimates/${id}/reject`);
};
export const setSelectedRowsItems = () => {};

View File

@@ -1,116 +1,15 @@
import { createReducer } from '@reduxjs/toolkit';
import {
journalNumberChangedReducer,
createTableQueryReducers,
} from 'store/journalNumber.reducer';
import t from 'store/types';
createTableStateReducers,
} from 'store/tableState.reducer';
const initialState = {
items: {},
views: {},
loading: false,
tableQuery: {
page_size: 12,
page: 1,
tableState: {
pageSize: 12,
pageIndex: 0,
},
currentViewId: -1,
selectedRows: [],
};
const defaultEstimate = {
entries: [],
};
export default createReducer(initialState, {
[t.ESTIMATE_SET]: (state, action) => {
const { id, estimate } = action.payload;
const _estimate = state.items[id] || {};
state.items[id] = { ...defaultEstimate, ..._estimate, ...estimate };
},
[t.ESTIMATES_ITEMS_SET]: (state, action) => {
const { sales_estimates } = action.payload;
const _estimates = {};
sales_estimates.forEach((estimate) => {
_estimates[estimate.id] = {
...defaultEstimate,
...estimate,
};
});
state.items = {
...state.items,
..._estimates,
};
},
[t.ESTIMATES_TABLE_LOADING]: (state, action) => {
const { loading } = action.payload;
state.loading = loading;
},
[t.ESTIMATES_SET_CURRENT_VIEW]: (state, action) => {
state.currentViewId = action.currentViewId;
},
[t.ESTIMATE_DELETE]: (state, action) => {
const { id } = action.payload;
if (typeof state.items[id] !== 'undefined') {
delete state.items[id];
}
},
[t.ESTIMATES_PAGE_SET]: (state, action) => {
// @todo camelCase keys.
const { customViewId, sales_estimates, 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_estimates.map((i) => i.id),
},
},
};
},
[t.ESTIMATES_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,
},
};
},
[t.ESTIMATES_SELECTED_ROWS_SET]: (state, action) => {
const { selectedRows } = action.payload;
state.selectedRows = selectedRows;
},
...journalNumberChangedReducer(t.ESTIMATE_NUMBER_CHANGED),
...createTableQueryReducers('ESTIMATES'),
...createTableStateReducers('ESTIMATES'),
});
export const getEstimateById = (state, id) => {
return state.sales_estimates.items[id];
};

View File

@@ -1,73 +1,17 @@
import { createSelector } from '@reduxjs/toolkit';
import { pickItemsFromIds, paginationLocationQuery, defaultPaginationMeta } from 'store/selectors';
const estimateTableQuery = (state) => state.salesEstimates.tableQuery;
const estimateByIdSelector = (state, props) =>
state.salesEstimates.items[props.estimateId];
const estimatesCurrentViewSelector = (state, props) => {
const viewId = state.salesEstimates.currentViewId;
return state.salesEstimates.views?.[viewId];
};
const estimateItemsSelector = (state) => state.salesEstimates.items;
const estimatesPageSelector = (state, props, query) => {
const viewId = state.salesEstimates.currentViewId;
const currentPageId = state.salesEstimates.views?.[viewId]?.paginationMeta?.page;
return state.salesEstimates.views?.[viewId]?.pages?.[currentPageId];
};
const getEstimatesCurrentViewIdSelector = (state, props) => {
return state.salesEstimates.currentViewId;
};
import { createDeepEqualSelector } from 'utils';
import { paginationLocationQuery } from 'store/selectors';
const estimatesTableState = (state) => state.salesEstimates.tableState;
// Retrieve estimates table query.
export const getEstimatesTableQueryFactory = () =>
createSelector(
export const getEstimatesTableStateFactory = () =>
createDeepEqualSelector(
paginationLocationQuery,
estimateTableQuery,
(locationQuery, tableQuery) => {
estimatesTableState,
(locationQuery, tableState) => {
return {
...locationQuery,
...tableQuery,
...tableState,
};
},
);
// Retreive estimate results of the current page.
export const getEstimateCurrentPageFactory = () =>
createSelector(
estimatesPageSelector,
estimateItemsSelector,
(estimatePage, estimateItems) => {
return typeof estimatePage === 'object'
? pickItemsFromIds(estimateItems, estimatePage.ids) || []
: [];
},
);
// Retrieve specific estimate by the passed estimate id.
export const getEstimateByIdFactory = () =>
createSelector(estimateByIdSelector, (estimate) => {
return estimate;
});
// Retrieve estimates pagination meta.
export const getEstimatesPaginationMetaFactory = () =>
createSelector(estimatesCurrentViewSelector, (estimateView) => {
return {
...defaultPaginationMeta(),
...(estimateView?.paginationMeta || {}),
};
});
// Retrieve estimates current view id.
export const getEstimatesCurrentViewIdFactory = () =>
createSelector(
getEstimatesCurrentViewIdSelector,
(currentViewId) => {
return currentViewId;
});

View File

@@ -1,14 +1,3 @@
export default {
ESTIMATES_LIST_SET: 'ESTIMATES_LIST_SET',
ESTIMATE_SET: 'ESTIMATE_SET',
ESTIMATE_DELETE: 'ESTIMATE_DELETE',
ESTIMATES_BULK_DELETE: 'ESTIMATES_BULK_DELETE',
ESTIMATES_SET_CURRENT_VIEW: 'ESTIMATES/SET_CURRENT_VIEW',
ESTIMATES_TABLE_QUERIES_ADD: 'ESTIMATES/TABLE_QUERIES_ADD',
ESTIMATES_TABLE_LOADING: 'ESTIMATES_TABLE_LOADING',
ESTIMATES_PAGINATION_SET: 'ESTIMATES_PAGINATION_SET',
ESTIMATES_PAGE_SET: 'ESTIMATES_PAGE_SET',
ESTIMATES_ITEMS_SET: 'ESTIMATES_ITEMS_SET',
ESTIMATE_NUMBER_CHANGED: 'ESTIMATE_NUMBER_CHANGED',
ESTIMATES_SELECTED_ROWS_SET: 'ESTIMATES_SELECTED_ROWS_SET',
ESTIMATES_TABLE_STATE_SET: 'ESTIMATES/TABLE_STATE_SET',
};

View File

@@ -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 = () => {};

View File

@@ -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];
};

View File

@@ -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;
}
);

View File

@@ -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',
};

View File

@@ -1,160 +0,0 @@
import ApiService from 'services/ApiService';
import t from 'store/types';
export const submitPaymentMade = ({ form }) => {
return (dispatch) =>
new Promise((resolve, reject) => {
ApiService.post('purchases/bill_payments', form)
.then((response) => {
resolve(response);
})
.catch((error) => {
const { response } = error;
const { data } = response;
reject(data?.errors);
});
});
};
export const deletePaymentMade = ({ id }) => {
return (dispatch) =>
new Promise((resovle, reject) => {
ApiService.delete(`purchases/bill_payments/${id}`)
.then((response) => {
dispatch({ type: t.PAYMENT_MADE_DELETE, payload: { id } });
resovle(response);
})
.catch((error) => {
reject(error.response.data.errors || []);
});
});
};
export const editPaymentMade = (id, form) => {
return (dispatch) =>
new Promise((resolve, rejcet) => {
ApiService.post(`purchases/bill_payments/${id}`, form)
.then((response) => {
resolve(response);
})
.catch((error) => {
const { response } = error;
const { data } = response;
rejcet(data?.errors);
});
});
};
export const fetchPaymentMadesTable = ({ query = {} }) => {
return (dispatch, getState) =>
new Promise((resolve, rejcet) => {
const pageQuery = getState().paymentMades.tableQuery;
dispatch({
type: t.PAYMENT_MADES_TABLE_LOADING,
payload: {
loading: true,
},
});
ApiService.get('purchases/bill_payments', {
params: { ...pageQuery, ...query },
})
.then((response) => {
dispatch({
type: t.PAYMENT_MADES_PAGE_SET,
payload: {
bill_payments: response.data.bill_payments,
pagination: response.data.pagination,
customViewId: response.data.customViewId || -1,
},
});
dispatch({
type: t.PAYMENT_MADES_ITEMS_SET,
payload: {
bill_payments: response.data.bill_payments,
},
});
dispatch({
type: t.PAYMENT_MADES_PAGINATION_SET,
payload: {
pagination: response.data.pagination,
customViewId: response.data.customViewId || -1,
},
});
dispatch({
type: t.PAYMENT_MADES_TABLE_LOADING,
payload: {
loading: false,
},
});
resolve(response);
})
.catch((error) => {
rejcet(error);
});
});
};
export const fetchPaymentMade = ({ id }) => {
return (dispatch) =>
new Promise((resovle, reject) => {
ApiService.get(`purchases/bill_payments/${id}`, {})
.then((response) => {
dispatch({
type: t.PAYMENT_MADE_SET,
payload: {
id,
paymentMade: response.data.bill_payment,
},
});
dispatch({
type: t.BILLS_ITEMS_SET,
payload: {
bills: response.data.payable_bills,
},
});
dispatch({
type: t.BILLS_PAYABLE_BY_PAYMENT_ID,
payload: {
billPaymentId: response.data.bill_payment.id,
bills: response.data.payable_bills,
},
});
dispatch({
type: t.BILLS_ITEMS_SET,
payload: {
bills: response.data.payment_bills,
},
});
dispatch({
type: t.BILLS_BY_PAYMENT_ID,
payload: {
billPaymentId: response.data.bill_payment.id,
bills: response.data.payment_bills,
},
});
resovle(response);
})
.catch((error) => {
const { response } = error;
const { data } = response;
reject(data?.errors);
});
});
};
export const fetchPaymentMadeBills = ({ paymentMadeId }) => (dispatch) => {
return new Promise((resolve, reject) => {
ApiService.get(`purchases/bill_payments/${paymentMadeId}/bills`).then((response) => {
dispatch({
type: t.BILLS_ITEMS_SET,
payload: {
bills: response.data.bills,
},
});
resolve(response);
}).catch((error) => { reject(error) });
});
}

View File

@@ -1,88 +0,0 @@
import { createReducer } from '@reduxjs/toolkit';
import {
createTableQueryReducers,
viewPaginationSetReducer,
} from 'store/journalNumber.reducer';
import t from 'store/types';
const initialState = {
items: {},
views: {},
loading: false,
currentViewId: -1,
tableQuery: {
page_size: 12,
page: 1,
},
nextPaymentNumberChanged: false,
};
const defaultPaymentMade = {
entries: [],
};
export default createReducer(initialState, {
[t.PAYMENT_MADES_TABLE_LOADING]: (state, action) => {
const { loading } = action.payload;
state.loading = loading;
},
[t.PAYMENT_MADES_ITEMS_SET]: (state, action) => {
const { bill_payments } = action.payload;
const _bill_payments = {};
bill_payments.forEach((billPayment) => {
_bill_payments[billPayment.id] = {
...defaultPaymentMade,
...billPayment,
};
});
state.items = {
...state.items,
..._bill_payments,
};
},
[t.PAYMENT_MADE_SET]: (state, action) => {
const { id, paymentMade } = action.payload;
const _oldPaymentMade = state.items[id] || {};
state.items[id] = {
...defaultPaymentMade,
..._oldPaymentMade,
...paymentMade,
};
},
[t.PAYMENT_MADE_DELETE]: (state, action) => {
const { id } = action.payload;
if (typeof state.items[id] !== 'undefined') {
delete state.items[id];
}
},
[t.PAYMENT_MADES_PAGE_SET]: (state, action) => {
const { customViewId, bill_payments, pagination } = action.payload;
const viewId = customViewId || -1;
const view = state.views[viewId] || {};
state.views[viewId] = {
...view,
pages: {
...(state.views?.[viewId]?.pages || {}),
[pagination.page]: {
ids: bill_payments.map((i) => i.id),
},
},
};
},
[t.PAYMENT_MADES_NUMBER_CHANGED]: (state, action) => {
const { isChanged } = action.payload;
state.nextPaymentNumberChanged = isChanged;
},
...viewPaginationSetReducer(t.PAYMENT_MADES_PAGINATION_SET),
...createTableQueryReducers('PAYMENT_MADES'),
});

View File

@@ -1,129 +0,0 @@
import { createSelector } from '@reduxjs/toolkit';
import {
pickItemsFromIds,
paginationLocationQuery,
defaultPaginationMeta,
} from 'store/selectors';
import { transformToObject } from 'utils';
const paymentMadeTableQuery = (state) => state.paymentMades.tableQuery;
const paymentMadesPageSelector = (state) => {
const viewId = state.paymentMades.currentViewId;
const currentView = state.paymentMades.views?.[viewId];
const currentPageId = currentView?.paginationMeta?.page;
return currentView?.pages?.[currentPageId];
};
const paymentMadesItemsSelector = (state) => state.paymentMades.items;
const PaymentMadePaginationSelector = (state, props) => {
const viewId = state.paymentMades.currentViewId;
return state.paymentMades.views?.[viewId];
};
const paymentMadeById = (state, props) =>
state.paymentMades.items[props.paymentMadeId];
const paymentMadeEntries = (state, props) => props.paymentMadeEntries;
const billsItemsSelector = (state, props) => state.bills.items;
const billsPayableByPaymentMadeSelector = (state, props) =>
state.bills.payable.byBillPayamentId[props.paymentMadeId];
const paymentMadeBillsSelector = (state, props) =>
state.bills.byBillPayamentId[props.paymentMadeId];
const paymentMadesCurrentViewIdSelector = (state) => state.paymentMades.currentViewId;
export const getPaymentMadeCurrentPageFactory = () =>
createSelector(
paymentMadesPageSelector,
paymentMadesItemsSelector,
(Page, Items) => {
return typeof Page === 'object'
? pickItemsFromIds(Items, Page.ids) || []
: [];
},
);
export const getPaymentMadeTableQuery = createSelector(
paginationLocationQuery,
paymentMadeTableQuery,
(locationQuery, tableQuery) => {
return {
...locationQuery,
...tableQuery,
};
},
);
export const getPaymentMadePaginationMetaFactory = () =>
createSelector(PaymentMadePaginationSelector, (page) => {
return {
...defaultPaginationMeta(),
...(page?.paginationMeta || {}),
};
});
export const getPaymentMadeByIdFactory = () =>
createSelector(paymentMadeById, (payment_Made) => {
return payment_Made;
});
export const getPaymentMadeEntriesDataFactory = () =>
createSelector(
billsItemsSelector,
paymentMadeEntries,
(billsItems, paymentEntries) => {
return Array.isArray(paymentEntries)
? paymentEntries.map((entry) => ({
...entry,
...(billsItems[entry.bill_id] || {}),
}))
: [];
},
);
// Retrieve payment made entries.
export const getPaymentMadeEntriesFactory = () =>
createSelector(
billsItemsSelector,
billsPayableByPaymentMadeSelector,
paymentMadeBillsSelector,
paymentMadeById,
(billsItems, paymentPayableBillsIds, paymentMadeBillsIds, paymentMade) => {
const billsIds = [
...(paymentPayableBillsIds || []),
...(paymentMadeBillsIds || []),
];
const bills = pickItemsFromIds(billsItems, billsIds);
const billEntries = transformToObject(
paymentMade?.entries || [],
'bill_id',
);
return bills.map((bill) => {
const paymentMadeEntry = billEntries?.[bill.id] || {};
return {
...bill,
bill_id: bill.id,
total_payment_amount: bill.payment_amount,
id: paymentMadeEntry?.id,
payment_amount: paymentMadeEntry?.payment_amount,
};
});
},
);
// Retrieve payment mades current view id.
export const getPaymentMadesCurrentViewIdFactory = () =>
createSelector(
paymentMadesCurrentViewIdSelector,
(currentViewId) => {
return currentViewId;
}
);

View File

@@ -1,12 +0,0 @@
export default {
PAYMENT_MADE_LIST_SET: 'PAYMENT_MADE_LIST_SET',
PAYMENT_MADE_DELETE: 'PAYMENT_MADE_DELETE',
PAYMENT_MADE_SET: 'PAYMENT_MADE_SET',
PAYMENT_MADE_SET_CURRENT_VIEW: 'PAYMENT_MADE_SET_CURRENT_VIEW',
PAYMENT_MADES_TABLE_QUERIES_ADD: 'PAYMENT_MADES/TABLE_QUERIES_ADD',
PAYMENT_MADES_TABLE_LOADING: 'PAYMENT_MADES_TABLE_LOADING',
PAYMENT_MADES_PAGE_SET: 'PAYMENT_MADES_PAGE_SET',
PAYMENT_MADES_ITEMS_SET: 'PAYMENT_MADES_ITEMS_SET',
PAYMENT_MADES_PAGINATION_SET: 'PAYMENT_MADES_PAGINATION_SET',
PAYMENT_MADES_NUMBER_CHANGED:'PAYMENT_MADES_NUMBER_CHANGED'
};

View File

@@ -0,0 +1,10 @@
import t from 'store/types';
export const setPaymentMadesTableState = (queries) => {
return {
type: t.PAYMENT_MADES_TABLE_STATE_SET,
payload: { queries },
};
};
export const setSelectedRowsItems = () => {};

View File

@@ -0,0 +1,16 @@
import { createReducer } from '@reduxjs/toolkit';
import {
createTableStateReducers,
} from 'store/tableState.reducer';
const initialState = {
tableState: {
pageSize: 12,
pageIndex: 1,
sortBy: [],
},
};
export default createReducer(initialState, {
...createTableStateReducers('ITEMS'),
});

View File

@@ -0,0 +1,17 @@
import { paginationLocationQuery } from 'store/selectors';
import { createDeepEqualSelector } from 'utils';
const paymentMadesTableStateSelector = (state) => state.paymentMades.tableState;
// Get payment mades table state marged with location query.
export const getPaymentMadesTableStateFactory = () =>
createDeepEqualSelector(
paginationLocationQuery,
paymentMadesTableStateSelector,
(locationQuery, tableState) => {
return {
...locationQuery,
...tableState,
};
},
);

View File

@@ -0,0 +1,3 @@
export default {
PAYMENT_MADES_TABLE_STATE_SET: 'PAYMENT_MADES/TABLE_STATE_SET',
};

View File

@@ -1,144 +0,0 @@
import ApiService from 'services/ApiService';
import t from 'store/types';
export const submitPaymentReceive = ({ form }) => {
return (dispatch) =>
new Promise((resolve, reject) => {
ApiService.post('sales/payment_receives', form)
.then((response) => {
resolve(response);
})
.catch((error) => {
const { response } = error;
const { data } = response;
reject(data?.errors);
});
});
};
export const editPaymentReceive = (id, form) => {
return (dispatch) =>
new Promise((resolve, rejcet) => {
ApiService.post(`sales/payment_receives/${id}`, form)
.then((response) => {
resolve(response);
})
.catch((error) => {
const { response } = error;
const { data } = response;
rejcet(data?.errors);
});
});
};
export const deletePaymentReceive = ({ id }) => {
return (dispatch) =>
new Promise((resovle, reject) => {
ApiService.delete(`sales/payment_receives/${id}`)
.then((response) => {
dispatch({ type: t.PAYMENT_RECEIVE_DELETE, payload: { id } });
resovle(response);
})
.catch((error) => {
reject(error.response.data.errors || []);
});
});
};
export const fetchPaymentReceive = ({ id }) => {
return (dispatch) =>
new Promise((resovle, reject) => {
ApiService.get(`sales/payment_receives/${id}`, {})
.then((response) => {
dispatch({
type: t.PAYMENT_RECEIVE_SET,
payload: {
id,
paymentReceive: response.data.payment_receive,
},
});
dispatch({
type: t.INVOICES_ITEMS_SET,
payload: {
sales_invoices: response.data.receivable_invoices,
},
});
dispatch({
type: t.INVOICES_ITEMS_SET,
payload: {
sales_invoices: response.data.payment_invoices,
},
});
dispatch({
type: t.INVOICES_RECEIVABLE_BY_PAYMENT_ID,
payload: {
paymentReceiveId: response.data.payment_receive.id,
saleInvoices: response.data.receivable_invoices,
},
});
dispatch({
type: t.INVOICES_BY_PAYMENT_ID,
payload: {
paymentReceiveId: response.data.payment_receive.id,
saleInvoices: response.data.payment_invoices,
},
});
resovle(response);
})
.catch((error) => {
reject(error);
});
});
};
export const fetchPaymentReceivesTable = ({ query = {} }) => {
return (dispatch, getState) =>
new Promise((resolve, rejcet) => {
const pageQuery = getState().paymentReceives.tableQuery;
dispatch({
type: t.PAYMENT_RECEIVES_TABLE_LOADING,
payload: {
loading: true,
},
});
ApiService.get('sales/payment_receives', {
params: { ...pageQuery, ...query },
})
.then((response) => {
dispatch({
type: t.PAYMENT_RECEIVES_PAGE_SET,
payload: {
payment_receives: response.data.payment_receives,
pagination: response.data.pagination,
customViewId: response.data.customViewId || -1,
},
});
dispatch({
type: t.PAYMENT_RECEIVES_ITEMS_SET,
payload: {
payment_receives: response.data.payment_receives,
},
});
dispatch({
type: t.PAYMENT_RECEIVES_PAGINATION_SET,
payload: {
pagination: response.data.pagination,
customViewId: response.data.customViewId || -1,
},
});
dispatch({
type: t.PAYMENT_RECEIVES_TABLE_LOADING,
payload: {
loading: false,
},
});
resolve(response);
})
.catch((error) => {
rejcet(error);
});
});
};

View File

@@ -1,95 +0,0 @@
import { createReducer } from '@reduxjs/toolkit';
import { omit } from 'lodash';
import {
journalNumberChangedReducer,
viewPaginationSetReducer,
createTableQueryReducers,
} from 'store/journalNumber.reducer';
import t from 'store/types';
const initialState = {
items: {},
views: {},
loading: false,
currentViewId: -1,
tableQuery: {
page_size: 12,
page: 1,
},
};
const defaultPaymentReceive = {
entries: [],
};
export default createReducer(initialState, {
[t.PAYMENT_RECEIVE_SET]: (state, action) => {
const { id, paymentReceive } = action.payload;
const _paymentReceive = {
...paymentReceive,
entries: paymentReceive.entries.map((e) => {
return { ...omit(e, ['invoice']) };
}),
};
const oldPaymentReceive = state.items[id] || {};
state.items[id] = {
...defaultPaymentReceive,
...oldPaymentReceive,
..._paymentReceive,
};
},
[t.PAYMENT_RECEIVES_TABLE_LOADING]: (state, action) => {
const { loading } = action.payload;
state.loading = loading;
},
[t.PAYMENT_RECEIVES_ITEMS_SET]: (state, action) => {
const { payment_receives } = action.payload;
const _payment_receives = {};
payment_receives.forEach((payment_receive) => {
_payment_receives[payment_receive.id] = {
...defaultPaymentReceive,
...payment_receive,
};
});
state.items = {
...state.items,
..._payment_receives,
};
},
[t.PAYMENT_RECEIVE_SET_CURRENT_VIEW]: (state, action) => {
state.currentViewId = action.currentViewId;
},
[t.PAYMENT_RECEIVE_DELETE]: (state, action) => {
const { id } = action.payload;
if (typeof state.items[id] !== 'undefined') {
delete state.items[id];
}
},
[t.PAYMENT_RECEIVES_PAGE_SET]: (state, action) => {
const { customViewId, payment_receives, pagination } = action.payload;
const viewId = customViewId || -1;
const view = state.views[viewId] || {};
state.views[viewId] = {
...view,
pages: {
...(state.views?.[viewId]?.pages || {}),
[pagination.page]: {
ids: payment_receives.map((i) => i.id),
},
},
};
},
...journalNumberChangedReducer(t.PAYMENT_RECEIVE_NUMBER_CHANGED),
...viewPaginationSetReducer(t.PAYMENT_RECEIVES_PAGINATION_SET),
...createTableQueryReducers('PAYMENT_RECEIVES'),
});

View File

@@ -1,136 +0,0 @@
import { createSelector } from '@reduxjs/toolkit';
import {
pickItemsFromIds,
paginationLocationQuery,
defaultPaginationMeta,
} from 'store/selectors';
import { transformToObject } from 'utils';
const paymentReceivesPageSelector = (state) => {
const viewId = state.paymentReceives.currentViewId;
const viewMeta = state.paymentReceives.views?.[viewId];
const currentPageId = viewMeta?.paginationMeta?.page;
return viewMeta?.pages?.[currentPageId];
};
const paymentReceivesItemsSelector = (state) => state.paymentReceives.items;
const paymentReceiveTableQuery = (state) => state.paymentReceives.tableQuery;
const PaymentReceivePaginationSelector = (state, props) => {
const viewId = state.paymentReceives.currentViewId;
return state.paymentReceives.views?.[viewId];
};
const invoicesItemsSelector = (state) => state.salesInvoices.items;
const payemntReceiveById = (state, props) =>
state.paymentReceives.items[props.paymentReceiveId];
const invoicesReceivableByPaymentReceiveSelector = (state, props) =>
state.salesInvoices.receivable.byPaymentReceiveId[props.paymentReceiveId];
const paymentReceiveInvoicesSelector = (state, props) =>
state.salesInvoices.byPaymentReceiveId[props.paymentReceiveId];
const paymentReceiveByIdSelector = (state, props) =>
state.paymentReceives.items[props.paymentReceiveId];
const paymentReceivesCurrentViewIdSelector = (state) =>
state.paymentReceives.currentViewId;
// Retrieve payment receive current page results.
export const getPaymentReceiveCurrentPageFactory = () =>
createSelector(
paymentReceivesPageSelector,
paymentReceivesItemsSelector,
(expensesPage, expensesItems) => {
return typeof expensesPage === 'object'
? pickItemsFromIds(expensesItems, expensesPage.ids) || []
: [];
},
);
// Retrieve payment receives table fetch query.
export const getPaymentReceiveTableQuery = createSelector(
paginationLocationQuery,
paymentReceiveTableQuery,
(locationQuery, tableQuery) => {
return {
...locationQuery,
...tableQuery,
};
},
);
// Retrieve payment receive pagination meta.
export const getPaymentReceivePaginationMetaFactory = () =>
createSelector(PaymentReceivePaginationSelector, (page) => {
return {
...defaultPaginationMeta(),
...(page?.paginationMeta || {}),
};
});
// Retrieve payment receive based on the passed payment receive id.
export const getPaymentReceiveByIdFactory = () =>
createSelector(payemntReceiveById, (payment_receive) => {
return payment_receive;
});
// Retrieve the payment receive associated invoices.
export const getPaymentReceiveInvoices = createSelector(
payemntReceiveById,
invoicesItemsSelector,
(paymentRecieve, items) => {
return typeof paymentRecieve === 'object'
? pickItemsFromIds(
items,
paymentRecieve.entries.map((entry) => entry.invoice_id),
)
: [];
},
);
// Retrieve payment receive invoices entries.
export const getPaymentReceiveEntriesFactory = () =>
createSelector(
invoicesItemsSelector,
invoicesReceivableByPaymentReceiveSelector,
paymentReceiveInvoicesSelector,
paymentReceiveByIdSelector,
(
invoicesItems,
paymentReceivableInvoicesIds,
paymentReceiveInvoicesIds,
paymentReceive,
) => {
const invoicesIds = [
...(paymentReceivableInvoicesIds || []),
...(paymentReceiveInvoicesIds || []),
];
const invoices = pickItemsFromIds(invoicesItems, invoicesIds);
const invoicesEntries = transformToObject(
paymentReceive?.entries || [],
'invoice_id',
);
return invoices.map((invoice) => {
const paymentReceiveEntry = invoicesEntries?.[invoice.id] || {};
return {
...invoice,
invoice_id: invoice.id,
total_payment_amount: invoice.payment_amount,
id: paymentReceiveEntry?.id,
payment_amount: paymentReceiveEntry?.payment_amount,
};
});
},
);
// Retrieve payment receives current view id.
export const getPaymentReceivesCurrentViewIdFactory = () =>
createSelector(
paymentReceivesCurrentViewIdSelector,
(currentViewId) => currentViewId,
);

View File

@@ -1,12 +0,0 @@
export default {
PAYMENT_RECEIVE_LIST_SET: 'PAYMENT_RECEIVE_LIST_SET',
PAYMENT_RECEIVE_DELETE: 'PAYMENT_RECEIVE_DELETE',
PAYMENT_RECEIVE_SET: 'PAYMENT_RECEIVE_SET',
PAYMENT_RECEIVES_SET_CURRENT_VIEW: 'PAYMENT_RECEIVES_SET_CURRENT_VIEW',
PAYMENT_RECEIVES_TABLE_QUERIES_ADD: 'PAYMENT_RECEIVES/TABLE_QUERIES_ADD',
PAYMENT_RECEIVES_TABLE_LOADING: 'PAYMENT_RECEIVES_TABLE_LOADING',
PAYMENT_RECEIVES_PAGE_SET: 'PAYMENT_RECEIVES_PAGE_SET',
PAYMENT_RECEIVES_ITEMS_SET: 'PAYMENT_RECEIVES_ITEMS_SET',
PAYMENT_RECEIVES_PAGINATION_SET: 'PAYMENT_RECEIVES_PAGINATION_SET',
PAYMENT_RECEIVE_NUMBER_CHANGED: 'PAYMENT_RECEIVE_NUMBER_CHANGED',
};

View File

@@ -0,0 +1,10 @@
import t from 'store/types';
export const setPaymentReceivesTableState = (queries) => {
return {
type: t.PAYMENT_RECEIVES_TABLE_STATE_SET,
payload: { queries },
};
};
export const setSelectedRowsItems = () => {};

View File

@@ -0,0 +1,15 @@
import { createReducer } from '@reduxjs/toolkit';
import {
createTableStateReducers,
} from 'store/tableState.reducer';
const initialState = {
tableState: {
pageSize: 12,
pageIndex: 0,
},
};
export default createReducer(initialState, {
...createTableStateReducers('PAYMENT_RECEIVES'),
});

View File

@@ -0,0 +1,18 @@
import { createSelector } from '@reduxjs/toolkit';
import {
paginationLocationQuery,
} from 'store/selectors';
const paymentReceiveTableState = (state) => state.paymentReceives.tableState;
// Retrieve payment receives table fetch query.
export const getPaymentReceiveTableStateFactory = () => createSelector(
paginationLocationQuery,
paymentReceiveTableState,
(locationQuery, tableState) => {
return {
...locationQuery,
...tableState,
};
},
);

View File

@@ -0,0 +1,3 @@
export default {
PAYMENT_RECEIVES_TABLE_STATE_SET: 'PAYMENT_RECEIVES/TABLE_STATE_SET',
};

View File

@@ -1,124 +0,0 @@
import ApiService from 'services/ApiService';
import t from 'store/types';
export const submitReceipt = ({ form }) => {
return (dispatch) =>
new Promise((resolve, reject) => {
ApiService.post('sales/receipts', form)
.then((response) => {
resolve(response);
})
.catch((error) => {
const { response } = error;
const { data } = response;
reject(data?.errors);
});
});
};
export const deleteReceipt = ({ id }) => {
return (dispatch) =>
new Promise((resovle, reject) => {
ApiService.delete(`sales/receipts/${id}`)
.then((response) => {
dispatch({
type: t.RECEIPT_DELETE,
payload: { id },
});
resovle(response);
})
.catch((error) => {
reject(error.response.data.errors || []);
});
});
};
export const editReceipt = (id, form) => {
return (dispatch) =>
new Promise((resolve, rejcet) => {
ApiService.post(`sales/receipts/${id}`, form)
.then((response) => {
resolve(response);
})
.catch((error) => {
const { response } = error;
const { data } = response;
rejcet(data?.errors);
});
});
};
export const fetchReceipt = ({ id }) => {
return (dispatch) =>
new Promise((resovle, reject) => {
ApiService.get(`sales/receipts/${id}`)
.then((response) => {
dispatch({
type: t.RECEIPT_SET,
payload: { id, sale_receipt: response.data.sale_receipt },
});
resovle(response);
})
.catch((error) => {
const { response } = error;
const { data } = response;
reject(data?.errors);
});
});
};
export const fetchReceiptsTable = ({ query = {} }) => {
return (dispatch, getState) =>
new Promise((resolve, rejcet) => {
const pageQuery = getState().salesReceipts.tableQuery;
dispatch({
type: t.RECEIPTS_TABLE_LOADING,
payload: {
loading: true,
},
});
ApiService.get('sales/receipts', {
params: { ...pageQuery, ...query },
})
.then((response) => {
dispatch({
type: t.RECEIPTS_PAGE_SET,
payload: {
sales_receipts: response.data.sale_receipts,
pagination: response.data.pagination,
customViewId:
response.data?.filter_meta?.view?.custom_view_id || -1,
},
});
dispatch({
type: t.RECEIPTS_ITEMS_SET,
payload: {
sales_receipts: response.data.sale_receipts,
},
});
dispatch({
type: t.RECEIPTS_PAGINATION_SET,
payload: {
pagination: response.data.pagination,
customViewId:
response.data?.filter_meta?.view?.custom_view_id || -1,
},
});
dispatch({
type: t.RECEIPTS_TABLE_LOADING,
payload: {
loading: false,
},
});
resolve(response);
})
.catch((error) => {
rejcet(error);
});
});
};
export const closeReceipt = ({ id }) => {
return (dispatch) => ApiService.post(`sales/receipts/${id}/close`);
};

View File

@@ -1,101 +0,0 @@
import { createReducer } from '@reduxjs/toolkit';
import t from 'store/types';
import { journalNumberChangedReducer, createTableQueryReducers } from 'store/journalNumber.reducer';
const initialState = {
items: {},
views: {},
loading: false,
tableQuery: {
page_size: 12,
page: 1,
},
currentViewId: -1,
};
const defaultReceipt = {
entries: [],
};
export default createReducer(initialState, {
[t.RECEIPT_SET]: (state, action) => {
const { id, sale_receipt } = action.payload;
const _receipt = state.items[id] || {};
state.items[id] = { ...defaultReceipt, ..._receipt, ...sale_receipt };
},
[t.RECEIPTS_ITEMS_SET]: (state, action) => {
const { sales_receipts } = action.payload;
const _receipts = {};
sales_receipts.forEach((receipt) => {
_receipts[receipt.id] = {
...defaultReceipt,
...receipt,
};
});
state.items = {
...state.items,
..._receipts,
};
},
[t.RECEIPTS_TABLE_LOADING]: (state, action) => {
const { loading } = action.payload;
state.loading = loading;
},
[t.RECEIPT_DELETE]: (state, action) => {
const { id } = action.payload;
if (typeof state.items[id] !== 'undefined') {
delete state.items[id];
}
},
[t.RECEIPTS_SET_CURRENT_VIEW]: (state, action) => {
state.currentViewId = action.currentViewId;
},
[t.RECEIPTS_PAGE_SET]: (state, action) => {
const { customViewId, sales_receipts, 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_receipts.map((i) => i.id),
},
},
};
},
[t.RECEIPTS_PAGINATION_SET]: (state, action) => {
const { pagination, customViewId } = action.payload;
const mapped = {
pageSize: parseInt(pagination.page_size, 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,
},
};
},
...journalNumberChangedReducer(t.RECEIPT_NUMBER_CHANGED),
...createTableQueryReducers('RECEIPTS'),
});
export const getReceiptById = (state, id) => {
return state.receipts.items[id];
};

View File

@@ -1,66 +0,0 @@
import { createSelector } from '@reduxjs/toolkit';
import { pickItemsFromIds, paginationLocationQuery } from 'store/selectors';
const receiptsPageSelector = (state) => {
const viewId = state.salesReceipts.currentViewId;
const currentView = state.salesReceipts.views?.[viewId];
const currentPageId = currentView?.paginationMeta?.page;
return currentView?.pages?.[currentPageId];
};
const receiptsPaginationSelector = (state, props) => {
const viewId = state.salesReceipts.currentViewId;
return state.salesReceipts.views?.[viewId];
};
const receiptItemsSelector = (state) => state.salesReceipts.items;
const receiptTableQuery = (state) => state.salesReceipts.tableQuery;
const receiptByIdSelector = (state, props) => state.salesReceipts.items[props.receiptId];
const receiptsCurrentViewIdSelector = (state) => state.salesReceipts.currentViewId;
// Retrieve current page sale receipts results.
export const getReceiptCurrentPageFactory = () =>
createSelector(
receiptsPageSelector,
receiptItemsSelector,
(receiptPage, receiptItems) => {
return typeof receiptPage === 'object'
? pickItemsFromIds(receiptItems, receiptPage.ids) || []
: [];
},
);
// Retrieve receipts table query.
export const getReceiptsTableQueryFactory = () =>
createSelector(
paginationLocationQuery,
receiptTableQuery,
(locationQuery, tableQuery) => {
return {
...locationQuery,
...tableQuery,
};
},
);
// Retrieve specific receipts by the passed receipt id.
export const getReceiptByIdFactory = () =>
createSelector(receiptByIdSelector, (receipt) => {
return receipt;
});
// Retrieve receipts pagination meta.
export const getReceiptsPaginationMetaFactory = () =>
createSelector(receiptsPaginationSelector, (receiptPage) => {
return receiptPage?.paginationMeta || {};
});
// Retrieve receipts current view id.
export const getReceiptsCurrentViewIdFactory = () =>
createSelector(
receiptsCurrentViewIdSelector,
(currentViewId) => currentViewId);

View File

@@ -1,13 +0,0 @@
export default {
RECEIPT_DELETE: 'RECEIPT_DELETE',
RECEIPTS_BULK_DELETE: 'RECEIPTS_BULK_DELETE',
RECEIPTS_LIST_SET: 'RECEIPTS_LIST_SET',
RECEIPT_SET: 'RECEIPT_SET',
RECEIPTS_SET_CURRENT_VIEW: 'RECEIPTS_SET_CURRENT_VIEW',
RECEIPTS_TABLE_QUERIES_ADD: 'RECEIPTS/TABLE_QUERIES_ADD',
RECEIPTS_TABLE_LOADING: 'RECEIPTS_TABLE_LOADING',
RECEIPTS_PAGINATION_SET: 'RECEIPTS_PAGINATION_SET',
RECEIPTS_PAGE_SET: 'RECEIPTS_PAGE_SET',
RECEIPTS_ITEMS_SET: 'RECEIPTS_ITEMS_SET',
RECEIPT_NUMBER_CHANGED:'RECEIPT_NUMBER_CHANGED'
};

View File

@@ -0,0 +1,10 @@
import t from 'store/types';
export const setReceiptsTableState = (queries) => {
return {
type: t.RECEIPTS_TABLE_STATE_SET,
payload: { queries },
};
};
export const setSelectedRowsItems = () => {};

View File

@@ -0,0 +1,15 @@
import { createReducer } from '@reduxjs/toolkit';
import {
createTableStateReducers,
} from 'store/tableState.reducer';
const initialState = {
tableState: {
pageSize: 12,
pageIndex: 0,
},
};
export default createReducer(initialState, {
...createTableStateReducers('RECEIPTS'),
});

View File

@@ -0,0 +1,17 @@
import { paginationLocationQuery } from 'store/selectors';
import { createDeepEqualSelector } from 'utils';
const receiptTableStateSelector = (state) => state.salesReceipts.tableState;
// Retrieve receipts table query.
export const getReceiptsTableStateFactory = () =>
createDeepEqualSelector(
paginationLocationQuery,
receiptTableStateSelector,
(locationQuery, tableState) => {
return {
...locationQuery,
...tableState,
};
},
);

View File

@@ -0,0 +1,4 @@
export default {
RECEIPTS_TABLE_STATE_SET: 'RECEIPTS/TABLE_STATE_SET',
};

View File

@@ -20,11 +20,11 @@ import globalErrors from './globalErrors/globalErrors.reducer';
import customers from './customers/customers.reducer';
import salesEstimates from './Estimate/estimates.reducer';
import salesInvoices from './Invoice/invoices.reducer';
import salesReceipts from './receipt/receipt.reducer';
import salesReceipts from './receipts/receipts.reducer';
import bills from './Bills/bills.reducer';
import vendors from './vendors/vendors.reducer';
import paymentReceives from './PaymentReceive/paymentReceive.reducer';
import paymentMades from './PaymentMades/paymentMade.reducer';
import paymentReceives from './PaymentReceives/paymentReceives.reducer';
import paymentMades from './PaymentMades/paymentMades.reducer';
import organizations from './organizations/organizations.reducers';
import subscriptions from './subscription/subscription.reducer';
import inventoryAdjustments from './inventoryAdjustments/inventoryAdjustment.reducer';

View File

@@ -19,11 +19,11 @@ import exchangeRate from './ExchangeRate/exchange.type';
import customer from './customers/customers.type';
import estimates from './Estimate/estimates.types';
import invoices from './Invoice/invoices.types';
import receipts from './receipt/receipt.type';
import receipts from './receipts/receipts.type';
import bills from './Bills/bills.type';
import vendors from './vendors/vendors.types';
import paymentReceives from './PaymentReceive/paymentReceive.type';
import paymentMades from './PaymentMades/paymentMade.type';
import paymentReceives from './PaymentReceives/paymentReceives.type';
import paymentMades from './PaymentMades/paymentMades.type';
import organizations from './organizations/organizations.types';
import subscription from './subscription/subscription.types';
import inventoryAdjustments from './inventoryAdjustments/inventoryAdjustment.type';