2
0
mirror of https://github.com/frappe/books.git synced 2024-12-23 03:19:01 +00:00

refactor: Use knex for query building

This commit is contained in:
Faris Ansari 2019-12-10 01:50:32 +05:30
parent 0b1dbca734
commit fa6d519836
3 changed files with 50 additions and 40 deletions

View File

@ -3,18 +3,24 @@ const { DateTime } = require('luxon');
class Cashflow {
async run({ fromDate, toDate, periodicity }) {
let res = await frappe.db.sql(
`
select sum(debit) as inflow, sum(credit) as outflow, strftime("%m-%Y", date) as "month-year"
from AccountingLedgerEntry
where account in (
select name from Account where accountType in ('Cash', 'Bank') and isGroup = 0
)
and date >= $fromDate and date <= $toDate
group by strftime("%m-%Y", date)
`,
{ $fromDate: fromDate, $toDate: toDate }
);
let cashAndBankAccounts = frappe.db
.knex('Account')
.select('name')
.where('accountType', 'in', ['Cash', 'Bank'])
.andWhere('isGroup', 0);
let dateAsMonthYear = frappe.db.knex.raw('strftime("%m-%Y", ??)', 'date');
let res = await frappe.db
.knex('AccountingLedgerEntry')
.sum({
inflow: 'debit',
outflow: 'credit'
})
.select({
'month-year': dateAsMonthYear
})
.where('account', 'in', cashAndBankAccounts)
.whereBetween('date', [fromDate, toDate])
.groupBy(dateAsMonthYear);
let periodList = getPeriodList(fromDate, toDate, periodicity);

View File

@ -40,7 +40,10 @@
</div>
</div>
</div>
<div v-if="totalExpense === 0" class="absolute inset-0 flex justify-center items-center">
<div
v-if="totalExpense === 0"
class="absolute inset-0 flex justify-center items-center"
>
<span class="text-base text-gray-600">
{{ _('No transactions yet') }}
</span>
@ -84,20 +87,21 @@ export default {
methods: {
async render() {
let { fromDate, toDate } = await getDatesAndPeriodicity(this.period);
let topExpenses = await frappe.db.sql(
`
select sum(debit) - sum(credit) as total, account from AccountingLedgerEntry
where account in (
select name from Account where rootType = "Expense"
)
and date >= $fromDate and date <= $toDate
group by account
order by total desc
limit 5
`,
{ $fromDate: fromDate, $toDate: toDate }
);
let expenseAccounts = frappe.db.knex
.select('name')
.from('Account')
.where('rootType', 'Expense');
let topExpenses = await frappe.db.knex
.select({
total: frappe.db.knex.raw('sum(??) - sum(??)', ['debit', 'credit'])
})
.select('account')
.from('AccountingLedgerEntry')
.where('account', 'in', expenseAccounts)
.whereBetween('date', [fromDate, toDate])
.groupBy('account')
.orderBy('total', 'desc')
.limit(5);
let shades = [
{ class: 'bg-gray-800', hex: theme.backgroundColor.gray['800'] },

View File

@ -1,6 +1,10 @@
<template>
<div class="flex -mx-4">
<div class="w-1/2 px-4 flex flex-col justify-between" v-for="invoice in invoices" :key="invoice.title">
<div
class="w-1/2 px-4 flex flex-col justify-between"
v-for="invoice in invoices"
:key="invoice.title"
>
<SectionHeader>
<template slot="title">{{ invoice.title }}</template>
<PeriodSelector
@ -119,18 +123,14 @@ export default {
this.$data[d.periodKey]
);
let res = await frappe.db.sql(
`
select
sum(baseGrandTotal) as total,
sum(outstandingAmount) as outstanding
from ${d.doctype}
where date >= $fromDate and date <= $toDate
and submitted = 1
`,
{ $fromDate: fromDate, $toDate: toDate }
);
let { total, outstanding } = res[0];
let result = await frappe.db
.knex(d.doctype)
.sum({ total: 'baseGrandTotal' })
.sum({ outstanding: 'outstandingAmount' })
.whereBetween('date', [fromDate, toDate])
.first();
let { total, outstanding } = result;
d.total = total;
d.unpaid = outstanding;
d.paid = total - outstanding;