Refactored queries to be more clean
This commit is contained in:
parent
7057cbf9e5
commit
f10ad98ebf
@ -32,37 +32,41 @@ class DocType:
|
|||||||
query_dict = {
|
query_dict = {
|
||||||
|
|
||||||
'invoiced_amount': self.generate_gle_query({
|
'invoiced_amount': self.generate_gle_query({
|
||||||
|
'type': 'invoiced_amount',
|
||||||
'field': 'debit',
|
'field': 'debit',
|
||||||
'type': 'Customer',
|
'master_type': 'Customer',
|
||||||
}),
|
}),
|
||||||
|
|
||||||
'payables': self.generate_gle_query({
|
'payables': self.generate_gle_query({
|
||||||
|
'type': 'payables',
|
||||||
'field': 'credit',
|
'field': 'credit',
|
||||||
'type': 'Supplier',
|
'master_type': 'Supplier',
|
||||||
}),
|
}),
|
||||||
|
|
||||||
'collections': self.generate_gle_query({
|
'collections': self.generate_gle_query({
|
||||||
|
'type': 'collections',
|
||||||
'field': 'credit',
|
'field': 'credit',
|
||||||
'type': 'Customer',
|
'master_type': 'Customer',
|
||||||
'against': 'Bank or Cash'
|
|
||||||
}),
|
}),
|
||||||
|
|
||||||
'payments': self.generate_gle_query({
|
'payments': self.generate_gle_query({
|
||||||
|
'type': 'payments',
|
||||||
'field': 'debit',
|
'field': 'debit',
|
||||||
'type': 'Supplier',
|
'master_type': 'Supplier',
|
||||||
'against': 'Bank or Cash'
|
|
||||||
}),
|
}),
|
||||||
|
|
||||||
'income': self.generate_gle_query({
|
'income': self.generate_gle_query({
|
||||||
|
'type': 'income',
|
||||||
'debit_or_credit': 'Credit'
|
'debit_or_credit': 'Credit'
|
||||||
}),
|
}),
|
||||||
|
|
||||||
'expenses_booked': self.generate_gle_query({
|
'expenses_booked': self.generate_gle_query({
|
||||||
|
'type': 'expenses_booked',
|
||||||
'debit_or_credit': 'Debit'
|
'debit_or_credit': 'Debit'
|
||||||
}),
|
}),
|
||||||
|
|
||||||
'bank_balance': self.generate_gle_query({
|
'bank_balance': self.generate_gle_query({
|
||||||
'bank_balance': None
|
'type': 'bank_balance'
|
||||||
}),
|
}),
|
||||||
|
|
||||||
'new_leads': """""",
|
'new_leads': """""",
|
||||||
@ -103,81 +107,106 @@ class DocType:
|
|||||||
"""
|
"""
|
||||||
Returns generated query string
|
Returns generated query string
|
||||||
"""
|
"""
|
||||||
start_date = '2011-11-01'
|
self.process_args(args)
|
||||||
end_date = '2011-11-30'
|
|
||||||
args.update({
|
|
||||||
'start_date': start_date,
|
|
||||||
'end_date': end_date,
|
|
||||||
'company': self.doc.company,
|
|
||||||
'select': None,
|
|
||||||
'where': None
|
|
||||||
})
|
|
||||||
|
|
||||||
|
query = None
|
||||||
|
|
||||||
self.evaluate_query_conditions(args)
|
if args['type'] in ['invoiced_amount', 'payables']:
|
||||||
|
query = """
|
||||||
|
SELECT
|
||||||
|
SUM(IFNULL(gle.%(field)s, 0)) AS '%(field)s',
|
||||||
|
%(common_select)s
|
||||||
|
FROM
|
||||||
|
%(common_from)s
|
||||||
|
WHERE
|
||||||
|
%(common_where)s AND
|
||||||
|
ac.master_type = '%(master_type)s' AND
|
||||||
|
%(start_date_condition)s AND
|
||||||
|
%(end_date_condition)s""" % args
|
||||||
|
|
||||||
query = """
|
elif args['type'] in ['collections', 'payments']:
|
||||||
SELECT
|
args['bc_accounts_regex'] = self.get_bc_accounts_regex()
|
||||||
%(select)s,
|
query = """
|
||||||
COUNT(*) AS 'count'
|
SELECT
|
||||||
FROM
|
SUM(IFNULL(gle.%(field)s, 0)) AS '%(field)s',
|
||||||
`tabGL Entry` gle,
|
%(common_select)s
|
||||||
`tabAccount` ac
|
FROM
|
||||||
WHERE
|
%(common_from)s
|
||||||
gle.company = '%(company)s' AND
|
WHERE
|
||||||
gle.account = ac.name AND
|
%(common_where)s AND
|
||||||
ac.docstatus < 2 AND
|
ac.master_type = '%(master_type)s' AND
|
||||||
IFNULL(gle.is_cancelled, 'No') = 'No' AND
|
gle.against REGEXP '%(bc_accounts_regex)s' AND
|
||||||
%(where)s AND
|
%(start_date_condition)s AND
|
||||||
gle.posting_date <= '%(end_date)s'""" % args
|
%(end_date_condition)s""" % args
|
||||||
|
|
||||||
if 'group_by' in args.keys():
|
elif args['type'] in ['income', 'expenses_booked']:
|
||||||
query = query + args['group_by']
|
query = """
|
||||||
|
SELECT
|
||||||
|
SUM(IFNULL(gle.debit, 0)) AS 'debit',
|
||||||
|
SUM(IFNULL(gle.credit, 0)) AS 'credit',
|
||||||
|
%(common_select)s
|
||||||
|
FROM
|
||||||
|
%(common_from)s
|
||||||
|
WHERE
|
||||||
|
%(common_where)s AND
|
||||||
|
ac.is_pl_account = 'Yes' AND
|
||||||
|
ac.debit_or_credit = '%(debit_or_credit)s' AND
|
||||||
|
%(start_date_condition)s AND
|
||||||
|
%(end_date_condition)s""" % args
|
||||||
|
|
||||||
|
elif args['type'] == 'bank_balance':
|
||||||
|
query = """
|
||||||
|
SELECT
|
||||||
|
ac.name AS 'name',
|
||||||
|
SUM(IFNULL(gle.debit, 0)) AS 'debit',
|
||||||
|
SUM(IFNULL(gle.credit, 0)) AS 'credit',
|
||||||
|
%(common_select)s
|
||||||
|
FROM
|
||||||
|
%(common_from)s
|
||||||
|
WHERE
|
||||||
|
%(common_where)s AND
|
||||||
|
ac.account_type = 'Bank or Cash' AND
|
||||||
|
%(end_date_condition)s
|
||||||
|
GROUP BY
|
||||||
|
ac.name""" % args
|
||||||
|
|
||||||
return query
|
return query
|
||||||
|
|
||||||
|
|
||||||
def evaluate_query_conditions(self, args):
|
def process_args(self, args):
|
||||||
"""
|
"""
|
||||||
Modify query according to type of information required based on args passed
|
Adds common conditions in dictionary "args"
|
||||||
"""
|
"""
|
||||||
# If collections or payments
|
start_date = '2011-11-01'
|
||||||
if 'against' in args.keys():
|
end_date = '2011-11-30'
|
||||||
if args['against'] == 'Bank or Cash':
|
|
||||||
bc_account_list = webnotes.conn.sql("""
|
|
||||||
SELECT name
|
|
||||||
FROM `tabAccount`
|
|
||||||
WHERE account_type = 'Bank or Cash'""", as_list=1)
|
|
||||||
args['reg'] = '(' + '|'.join([ac[0] for ac in bc_account_list]) + ')'
|
|
||||||
args['where'] = """
|
|
||||||
ac.master_type = '%(type)s' AND
|
|
||||||
gle.against REGEXP '%(reg)s' AND
|
|
||||||
gle.posting_date >= '%(start_date)s'""" % args
|
|
||||||
|
|
||||||
# If income or expenses_booked
|
args.update({
|
||||||
elif 'debit_or_credit' in args.keys():
|
'common_select': "COUNT(*) AS 'count'",
|
||||||
args['select'] = """
|
|
||||||
SUM(IFNULL(gle.debit, 0)) AS 'debit',
|
|
||||||
SUM(IFNULL(gle.credit, 0)) AS 'credit'"""
|
|
||||||
|
|
||||||
args['where'] = """
|
'common_from': "`tabGL Entry` gle, `tabAccount` ac",
|
||||||
ac.is_pl_account = 'Yes' AND
|
|
||||||
ac.debit_or_credit = '%(debit_or_credit)s' AND
|
|
||||||
gle.posting_date >= '%(start_date)s'""" % args
|
|
||||||
|
|
||||||
elif 'bank_balance' in args.keys():
|
'common_where': """
|
||||||
args['select'] = "ac.name AS 'name', SUM(IFNULL(debit, 0)) AS 'debit', SUM(IFNULL(credit, 0)) AS 'credit'"
|
gle.company = '%s' AND
|
||||||
args['where'] = "ac.account_type = 'Bank or Cash'"
|
gle.account = ac.name AND
|
||||||
args['group_by'] = "GROUP BY ac.name"
|
ac.docstatus < 2 AND
|
||||||
|
IFNULL(gle.is_cancelled, 'No') = 'No'""" % self.doc.company,
|
||||||
|
|
||||||
# For everything else
|
'start_date_condition': "gle.posting_date >= '%s'" % start_date,
|
||||||
else:
|
|
||||||
args['where'] = """
|
|
||||||
ac.master_type = '%(type)s' AND
|
|
||||||
gle.posting_date >= '%(start_date)s'""" % args
|
|
||||||
|
|
||||||
if not args['select']:
|
'end_date_condition': "gle.posting_date <= '%s'" % end_date
|
||||||
args['select'] = "SUM(IFNULL(gle.%(field)s, 0)) AS '%(field)s'" % args
|
})
|
||||||
|
|
||||||
|
|
||||||
|
def get_bc_accounts_regex(self):
|
||||||
|
"""
|
||||||
|
Returns a regular expression of 'Bank or Cash' type account list
|
||||||
|
"""
|
||||||
|
bc_account_list = webnotes.conn.sql("""
|
||||||
|
SELECT name
|
||||||
|
FROM `tabAccount`
|
||||||
|
WHERE account_type = 'Bank or Cash'""", as_list=1)
|
||||||
|
|
||||||
|
return '(' + '|'.join([ac[0] for ac in bc_account_list]) + ')'
|
||||||
|
|
||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user