feat: add due bills reducer and selector.

This commit is contained in:
Ahmed Bouhuolia
2020-10-28 18:29:51 +02:00
parent bc9638c9a2
commit 8a20deacf3
6 changed files with 64 additions and 1 deletions

View File

@@ -4,12 +4,14 @@ import {
getBillCurrentPageFactory,
getBillPaginationMetaFactory,
getBillTableQueryFactory,
getVendorDueBillsFactory
} from 'store/Bills/bills.selectors';
export default (mapState) => {
const getBillsItems = getBillCurrentPageFactory();
const getBillsPaginationMeta = getBillPaginationMetaFactory();
const getBillTableQuery = getBillTableQueryFactory();
const getVendorDueBills = getVendorDueBillsFactory();
const mapStateToProps = (state, props) => {
const tableQuery = getBillTableQuery(state, props);
@@ -23,6 +25,8 @@ export default (mapState) => {
billsPageination: getBillsPaginationMeta(state, props, tableQuery),
billsLoading: state.bills.loading,
nextBillNumberChanged: state.bills.nextBillNumberChanged,
vendorDueBills: getVendorDueBills(state, props),
};
return mapState ? mapState(mapped, state, props) : mapped;
};

View File

@@ -117,3 +117,17 @@ export const editBill = (id, form) => {
});
});
};
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.DUE_BILLS_SET,
payload: {
bills: response.data.bills,
}
});
resolve(response);
}).catch(error => { reject(error) });
});

View File

@@ -13,6 +13,7 @@ const initialState = {
page: 1,
},
nextBillNumberChanged: false,
dueBills: {},
};
const defaultBill = {
@@ -103,6 +104,25 @@ const reducer = createReducer(initialState, {
const { isChanged } = action.payload;
state.nextBillNumberChanged = isChanged;
},
[t.DUE_BILLS_SET]: (state, action) => {
const { bills } = action.payload;
const _dueBills = { ...state.dueBills };
const _bills = { ...state.items };
bills.forEach((bill) => {
_bills[bill.id] = { ...bill };
if (!_dueBills[bill.vendor_id]) {
_dueBills[bill.vendor_id] = []
}
_dueBills[bill.vendor_id].push(bill.id);
});
state.items = { ..._bills };
state.dueBills = { ..._dueBills };
}
});
export default createTableQueryReducers('bills', reducer);

View File

@@ -9,8 +9,18 @@ const billPageSelector = (state, props, query) => {
};
const billItemsSelector = (state) => state.bills.items;
/**
* Retrieve bill details.
* @return {IBill}
*/
const billByIdSelector = (state, props) => state.bills.items[props.billId];
/**
* Retrieve vendor due bills ids.
* @return {number[]}
*/
const billsDueVendorSelector = (state, props) => state.bills.dueBills[props.vendorId];
const billPaginationSelector = (state, props) => {
const viewId = state.bills.currentViewId;
return state.bills.views?.[viewId];
@@ -51,3 +61,17 @@ export const getBillPaginationMetaFactory = () =>
createSelector(billPaginationSelector, (billPage) => {
return billPage?.paginationMeta || {};
});
/**
* Retrieve due bills of specific vendor.
*/
export const getVendorDueBillsFactory = () =>
createSelector(
billItemsSelector,
billsDueVendorSelector,
(billsItems, dueBillsIds) => {
return Array.isArray(dueBillsIds)
? pickItemsFromIds(billsItems, dueBillsIds) || []
: [];
},
);

View File

@@ -10,4 +10,5 @@ export default {
BILLS_PAGE_SET: 'BILLS_PAGE_SET',
BILLS_ITEMS_SET: 'BILLS_ITEMS_SET',
BILL_NUMBER_CHANGED: 'BILL_NUMBER_CHANGED',
DUE_BILLS_SET: 'DUE_BILLS_SET'
};

View File

@@ -41,7 +41,7 @@ export default class Bill extends TenantModel {
* @return {number}
*/
get dueAmount() {
return this.amount - this.paymentAmount;
return Math.max(this.amount - this.paymentAmount, 0);
}
/**