<commit_before># Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt

from __future__ import unicode_literals
import webnotes
from webnotes.utils import flt, add_days
from webnotes import _
from accounts.utils import get_balance_on

def execute(filters=None):
	validate_filters(filters)
	columns = get_columns()
	data = []
	if filters.get("group_by"):
		data += get_grouped_gle(filters)
	else:
		data += get_gl_entries(filters)
		if data:
			data.append(get_total_row(data))

	if filters.get("account"):
		data = [get_opening_balance_row(filters)] + data + [get_closing_balance_row(filters)]

	return columns, data
	
def validate_filters(filters):
	if filters.get("account") and filters.get("group_by") == "Group by Account":
		webnotes.throw(_("Can not filter based on Account, if grouped by Account"))
		
	if filters.get("voucher_no") and filters.get("group_by") == "Group by Voucher":
		webnotes.throw(_("Can not filter based on Voucher No, if grouped by Voucher"))
	
def get_columns():
	return ["Posting Date:Date:100", "Account:Link/Account:200", "Debit:Float:100", 
		"Credit:Float:100", "Voucher Type::120", "Voucher No::160", "Link::20", 
		"Cost Center:Link/Cost Center:100", "Remarks::200"]
		
def get_opening_balance_row(filters):
	opening_balance = get_balance_on(filters["account"], add_days(filters["from_date"], -1))
	return ["", "Opening Balance", opening_balance, 0.0, "", "", ""]
	
def get_closing_balance_row(filters):
	closing_balance = get_balance_on(filters["account"], filters["to_date"])
	return ["", "Closing Balance", closing_balance, 0.0, "", "", ""]
		
def get_gl_entries(filters):
	gl_entries = webnotes.conn.sql("""select 
			posting_date, account, debit, credit, voucher_type, voucher_no, cost_center, remarks 
		from `tabGL Entry`
		where company=%(company)s 
			and posting_date between %(from_date)s and %(to_date)s
			{conditions}
		order by posting_date, account"""\
		.format(conditions=get_conditions(filters)), filters, as_list=1)
		
	for d in gl_entries:
		icon = """<a href="%s"><i class="icon icon-share" style="cursor: pointer;"></i></a>""" \
			% ("/".join(["#Form", d[4], d[5]]),)
		d.insert(6, icon)
		
	return gl_entries
			
def get_conditions(filters):
	conditions = []
	if filters.get("account"):
		conditions.append("account=%(account)s")
	if filters.get("voucher_no"):
		conditions.append("voucher_no=%(voucher_no)s")
	
	return "and {}".format(" and ".join(conditions)) if conditions else ""
		
def get_grouped_gle(filters):
	gle_map = {}
	gle = get_gl_entries(filters)
	for d in gle:
		gle_map.setdefault(d[1 if filters["group_by"]=="Group by Account" else 5], []).append(d)
		
	data = []
	for entries in gle_map.values():<commit_msg>General ledger filter by account group<commit_after># Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt

from __future__ import unicode_literals
import webnotes
from webnotes.utils import flt, add_days
from webnotes import _
from accounts.utils import get_balance_on

def execute(filters=None):
	account_details = webnotes.conn.get_value("Account", filters["account"], 
		["debit_or_credit", "group_or_ledger"], as_dict=True)
	validate_filters(filters, account_details.group_or_ledger)
	columns = get_columns()
	data = []
	if filters.get("group_by"):
		data += get_grouped_gle(filters)
	else:
		data += get_gl_entries(filters)
		if data:
			data.append(get_total_row(data))

	if filters.get("account"):
		data = [get_opening_balance_row(filters, account_details.debit_or_credit)] + data + \
			[get_closing_balance_row(filters, account_details.debit_or_credit)]

	return columns, data
	
def validate_filters(filters, group_or_ledger):
	if group_or_ledger == "Ledger" and filters.get("group_by") == "Group by Account":
		webnotes.throw(_("Can not filter based on Account, if grouped by Account"))
		
	if filters.get("voucher_no") and filters.get("group_by") == "Group by Voucher":
		webnotes.throw(_("Can not filter based on Voucher No, if grouped by Voucher"))
	
def get_columns():
	return ["Posting Date:Date:100", "Account:Link/Account:200", "Debit:Float:100", 
		"Credit:Float:100", "Voucher Type::120", "Voucher No::160", "Link::20", 
		"Cost Center:Link/Cost Center:100", "Remarks::200"]
		
def get_opening_balance_row(filters, debit_or_credit):
	opening_balance = get_balance_on(filters["account"], add_days(filters["from_date"], -1))
	return get_balance_row(opening_balance, debit_or_credit, "Opening Balance")
	
def get_closing_balance_row(filters, debit_or_credit):
	closing_balance = get_balance_on(filters["account"], filters["to_date"])
	return get_balance_row(closing_balance, debit_or_credit, "Closing Balance")
	
def get_balance_row(balance, debit_or_credit, balance_label):
	if debit_or_credit == "Debit":
		return ["", balance_label, balance, 0.0, "", "", ""]
	else:
		return ["", balance_label, 0.0, balance, "", "", ""]
		
def get_gl_entries(filters):
	gl_entries = webnotes.conn.sql("""select 
			posting_date, account, debit, credit, voucher_type, voucher_no, cost_center, remarks 
		from `tabGL Entry`
		where company=%(company)s 
			and posting_date between %(from_date)s and %(to_date)s
			{conditions}
		order by posting_date, account"""\
		.format(conditions=get_conditions(filters)), filters, as_list=1)
		
	for d in gl_entries:
		icon = """<a href="%s"><i class="icon icon-share" style="cursor: pointer;"></i></a>""" \
			% ("/".join(["#Form", d[4], d[5]]),)
		d.insert(6, icon)
		
	return gl_entries
			
def get_conditions(filters):
	conditions = []
	if filters.get("account"):
		lft, rgt = webnotes.conn.get_value("Account", filters["account"], ["lft", "rgt"])
		conditions.append("""account in (select name from tabAccount 
			where lft>=%s and rgt<=%s and docstatus<2)""" % (lft, rgt))
	if filters.get("voucher_no"):
		conditions.append("voucher_no=%(voucher_no)s")
	
	return "and {}".format(" and ".join(conditions)) if conditions else ""
		
def get_grouped_gle(filters):
	gle_map = {}
	gle = get_gl_entries(filters)
	for d in gle:
		gle_map.setdefault(d[1 if filters["group_by"]=="Group by Account" else 5], []).append(d)
		
	data = []
	for entries in gle_map.values():
{'fraction_non_alphanumeric': 0.1505686368732981, 'fraction_numerical': 0.013615249078968444, 'mean_word_length': 5.801742919389978, 'pattern_counts': {'":': 5, '<': 13, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '668135', 'n_tokens_mistral': 2326, 'n_tokens_neox': 2115, 'n_words': 502}