diff --git a/client/src/containers/Purchases/Bill/withBills.js b/client/src/containers/Purchases/Bill/withBills.js index 5e552fc43..4c8a15547 100644 --- a/client/src/containers/Purchases/Bill/withBills.js +++ b/client/src/containers/Purchases/Bill/withBills.js @@ -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; }; diff --git a/client/src/store/Bills/bills.actions.js b/client/src/store/Bills/bills.actions.js index 1ff6e36ff..8e57fa4df 100644 --- a/client/src/store/Bills/bills.actions.js +++ b/client/src/store/Bills/bills.actions.js @@ -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) }); +}); \ No newline at end of file diff --git a/client/src/store/Bills/bills.reducer.js b/client/src/store/Bills/bills.reducer.js index a8f2d85f6..6f7f438e0 100644 --- a/client/src/store/Bills/bills.reducer.js +++ b/client/src/store/Bills/bills.reducer.js @@ -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); diff --git a/client/src/store/Bills/bills.selectors.js b/client/src/store/Bills/bills.selectors.js index 633690ee3..b2840dc6d 100644 --- a/client/src/store/Bills/bills.selectors.js +++ b/client/src/store/Bills/bills.selectors.js @@ -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) || [] + : []; + }, + ); \ No newline at end of file diff --git a/client/src/store/Bills/bills.type.js b/client/src/store/Bills/bills.type.js index 671478154..c8d3fae9d 100644 --- a/client/src/store/Bills/bills.type.js +++ b/client/src/store/Bills/bills.type.js @@ -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' }; diff --git a/server/src/models/Bill.js b/server/src/models/Bill.js index 26c0b88f2..c01df5e91 100644 --- a/server/src/models/Bill.js +++ b/server/src/models/Bill.js @@ -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); } /**