Skip to content

Commit

Permalink
Show the quantity and amount of each Mode of Payment (#141)
Browse files Browse the repository at this point in the history
* feat: mode of payment summary component

* feat: add number_of_invoices_per_voucher to check

* feat: currency format

* chore: fix setup, run formatters against repo

* fix: html formatting

* feat: reactive

* feat: only update when draft

* feat: improvement

* feat: sort mop

* fix: slight refactor, 'account' => 'Account'

---------

Co-authored-by: Tyler Matteson <[email protected]>
  • Loading branch information
fproldan and agritheory authored Sep 7, 2023
1 parent dadd084 commit a0471a3
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 6 deletions.
2 changes: 1 addition & 1 deletion check_run/check_run/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ def disallow_cancellation_if_in_check_run(doc, method=None):
frappe._(
f"""This document is currently selected for payment in draft {frappe.get_desk_link('Check Run', draft_check_run.name)} and cannot be cancelled."""
)
)
)
1 change: 1 addition & 0 deletions check_run/check_run/doctype/check_run/check_run.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ function get_entries(frm) {
frm.transactions = r.transactions
frm.modes_of_payment = r.modes_of_payment
check_run.mount_table(frm)
// TODO: make this role configurable
if (!frappe.user.has_role(['Accounts Manager'])) {
frm.disable_form()
cur_frm.$check_run.$children[0].state.status = frm.doc.status
Expand Down
18 changes: 13 additions & 5 deletions check_run/public/js/check_run/CheckRun.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<template>
<div>
<ModeOfPaymentSummary :transactions="transactions" :pay_to_account_currency="pay_to_account_currency" :status="state.status"/>
<table class="table table-compact table-hover check-run-table" style="text-align: center; margin: 0">
<thead>
<tr>
<th style="text-align: left" class="col col-sm-2" id="check-run-party-filter">
<span class="party-onclick party-display">Party</span>
<span class="filter-icon"
><svg class="icon icon-sm" style="" @click="toggleShowPartyFilter()">
<use class="" href="#icon-filter"></use></svg
></span>
<span class="filter-icon">
<svg class="icon icon-sm" style="" @click="toggleShowPartyFilter()">
<use class="" href="#icon-filter"></use>
</svg>
</span>
<div class="party-filter" v-if="state.show_party_filter">
<input type="text" class="form-control" v-model="state.party_filter" />
</div>
Expand Down Expand Up @@ -81,7 +83,7 @@

<span v-else>{{ transactions[i].mode_of_payment }}</span>
</td>
<td>{{ format_currency(item.amount, 'USD', 2) }}</td>
<td>{{ format_currency(item.amount, pay_to_account_currency, 2) }}</td>
<td>{{ moment(item.due_date).format('MM/DD/YY') }}</td>
<td v-if="state.status == 'Draft'" style="text-align: left">
<input
Expand All @@ -104,11 +106,13 @@
</template>
<script>
import ADropdown from './ADropdown.vue'
import ModeOfPaymentSummary from './ModeOfPaymentSummary.vue'
export default {
name: 'CheckRun',
components: {
ADropdown,
ModeOfPaymentSummary,
},
props: ['transactions', 'modes_of_payment', 'status', 'state'],
data() {
Expand All @@ -121,6 +125,7 @@ export default {
due_date: 1,
},
modeOfPaymentNames: this.modes_of_payment.map(mop => mop.name),
pay_to_account_currency: '',
}
},
watch: {
Expand Down Expand Up @@ -197,6 +202,9 @@ export default {
beforeMount() {
this.moment = moment
this.format_currency = format_currency
frappe.db.get_value('Account', cur_frm.doc.pay_to_account, 'account_currency').then(r => {
this.pay_to_account_currency = r.message.pay_to_account_currency
})
cur_frm.check_run_component = this
},
}
Expand Down
110 changes: 110 additions & 0 deletions check_run/public/js/check_run/ModeOfPaymentSummary.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<template>
<div>
<div id="modeOfPaymentSummary" class="row">
<div v-for="result in results" class="col">
{{ result.qty }} {{ result.mode_of_payment }}: {{ format_currency(result.amount, pay_to_account_currency, 2) }}
</div>
</div>
</div>
</template>

<script>
export default {
name: 'ModeOfPaymentSummary',
props: {
transactions: {
type: Array,
required: true,
default: () => [],
},
pay_to_account_currency: '',
status,
},
data() {
return {
results: [],
}
},
created() {
this.calculate_totals()
},
watch: {
transactions: {
handler: function(newValue) {
if (this.status == 'Draft') {
this.calculate_totals()
}
},
deep: true
},
},
methods: {
calculate_totals() {
let modes_of_payments = this.aggregate(this.transactions, 'mode_of_payment', 'amount', 'pay')
var results = []
if(!(cur_frm.doc.bank_account && cur_frm.doc.pay_to_account)){
return
}
frappe.xcall('check_run.check_run.doctype.check_run.check_run.get_check_run_settings', {doc: cur_frm.doc})
.then(r => {
if(!r){ return }
let number_of_invoices_per_voucher = r.number_of_invoices_per_voucher
$(modes_of_payments).each(function (index) {
var mode_of_payment = modes_of_payments[index]
var amounts = mode_of_payment.amount.filter(elements => { return elements !== null })
if (mode_of_payment.mode_of_payment == 'Check') {
var qty = `(${amounts.length}/${number_of_invoices_per_voucher})`
} else {
var qty = `(${amounts.length})`
}
results.push({
mode_of_payment: mode_of_payment.mode_of_payment,
qty: qty,
amount: amounts.reduce(function (acc, val) {
return acc + val
}, 0),
})
})
this.results = results.sort(function(a, b) {
var keyA = a.mode_of_payment, keyB = b.mode_of_payment;
if (keyA < keyB) return -1
if (keyA > keyB) return 1
return 0
})
})
},
aggregate(arr, on, who, filter) {
const agg = arr.reduce((a, b) => {
const onValue = b[on]
if (b[filter]) {
var whoValue = b[who]
} else {
var whoValue = null
}
if (a[onValue]) {
a[onValue] = {
[on]: onValue,
[who]: [...a[onValue][who], whoValue],
}
} else {
a[onValue] = {
[on]: onValue,
[who]: [whoValue],
}
}
return a
}, {})
return Object.values(agg)
},
},
beforeMount() {
this.format_currency = format_currency
},
}
</script>
<style scoped>
#modeOfPaymentSummary {
padding-bottom: 0.7rem;
}
</style>

0 comments on commit a0471a3

Please sign in to comment.