From fa6d519836829c9f8750a983ae05fbb0ecac23f8 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Tue, 10 Dec 2019 01:50:32 +0530 Subject: [PATCH] refactor: Use knex for query building --- reports/Cashflow/Cashflow.js | 30 ++++++++++++++--------- src/pages/Dashboard/Expenses.vue | 34 ++++++++++++++------------ src/pages/Dashboard/UnpaidInvoices.vue | 26 ++++++++++---------- 3 files changed, 50 insertions(+), 40 deletions(-) diff --git a/reports/Cashflow/Cashflow.js b/reports/Cashflow/Cashflow.js index 1f5a5d10..6e60aa27 100644 --- a/reports/Cashflow/Cashflow.js +++ b/reports/Cashflow/Cashflow.js @@ -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); diff --git a/src/pages/Dashboard/Expenses.vue b/src/pages/Dashboard/Expenses.vue index 630444d9..9211a1cd 100644 --- a/src/pages/Dashboard/Expenses.vue +++ b/src/pages/Dashboard/Expenses.vue @@ -40,7 +40,10 @@ -
+
{{ _('No transactions yet') }} @@ -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'] }, diff --git a/src/pages/Dashboard/UnpaidInvoices.vue b/src/pages/Dashboard/UnpaidInvoices.vue index 80a5e5f7..832866c9 100644 --- a/src/pages/Dashboard/UnpaidInvoices.vue +++ b/src/pages/Dashboard/UnpaidInvoices.vue @@ -1,6 +1,10 @@