mirror of
https://github.com/frappe/books.git
synced 2024-11-08 23:00:56 +00:00
c1482b0d2f
- add callBespoke
68 lines
1.8 KiB
TypeScript
68 lines
1.8 KiB
TypeScript
import DatabaseCore from './core';
|
|
import { BespokeFunction } from './types';
|
|
|
|
export class BespokeQueries {
|
|
[key: string]: BespokeFunction;
|
|
|
|
static async getTopExpenses(
|
|
db: DatabaseCore,
|
|
fromDate: string,
|
|
toDate: string
|
|
) {
|
|
const expenseAccounts = db
|
|
.knex!.select('name')
|
|
.from('Account')
|
|
.where('rootType', 'Expense');
|
|
|
|
const topExpenses = await db
|
|
.knex!.select({
|
|
total: db.knex!.raw('sum(cast(?? as real)) - sum(cast(?? as real))', [
|
|
'debit',
|
|
'credit',
|
|
]),
|
|
})
|
|
.select('account')
|
|
.from('AccountingLedgerEntry')
|
|
.where('account', 'in', expenseAccounts)
|
|
.whereBetween('date', [fromDate, toDate])
|
|
.groupBy('account')
|
|
.orderBy('total', 'desc')
|
|
.limit(5);
|
|
return topExpenses;
|
|
}
|
|
|
|
static async getTotalOutstanding(
|
|
db: DatabaseCore,
|
|
schemaName: string,
|
|
fromDate: string,
|
|
toDate: string
|
|
) {
|
|
return await db.knex!(schemaName)
|
|
.sum({ total: 'baseGrandTotal' })
|
|
.sum({ outstanding: 'outstandingAmount' })
|
|
.where('submitted', true)
|
|
.whereBetween('date', [fromDate, toDate])
|
|
.first();
|
|
}
|
|
|
|
static async getCashflow(db: DatabaseCore, fromDate: string, toDate: string) {
|
|
const cashAndBankAccounts = db.knex!('Account')
|
|
.select('name')
|
|
.where('accountType', 'in', ['Cash', 'Bank'])
|
|
.andWhere('isGroup', false);
|
|
const dateAsMonthYear = db.knex!.raw('strftime("%m-%Y", ??)', 'date');
|
|
return await db.knex!('AccountingLedgerEntry')
|
|
.where('reverted', false)
|
|
.sum({
|
|
inflow: 'debit',
|
|
outflow: 'credit',
|
|
})
|
|
.select({
|
|
'month-year': dateAsMonthYear,
|
|
})
|
|
.where('account', 'in', cashAndBankAccounts)
|
|
.whereBetween('date', [fromDate, toDate])
|
|
.groupBy(dateAsMonthYear);
|
|
}
|
|
}
|