'modify patch to also adjust journal entries'

This commit is contained in:
tunde 2017-08-29 10:23:19 +01:00
parent 63c8cb2fd1
commit 3b327ed55f

View File

@ -3,35 +3,25 @@ import frappe
"""
This will update existing GL Entries by saving its linked Purchase/Sales Invoice's
due date as the due date for the GL Entry
and Journal Entry's due date as the due date for the GL Entry
"""
def execute():
pi_kwargs = dict(
voucher_type='Purchase Invoice', doctype='GL Entry', fields=['voucher_no'],
limit_start=0, limit_page_length=5, filters={
"ifnull(due_date, '')": ('=', ''), "ifnull(party, '')": ('!=', ''),
'voucher_type': 'Purchase Invoice', 'debit': ('!=', '0')
}
)
si_kwargs = dict(
voucher_type='Sales Invoice', doctype='GL Entry', fields=['voucher_no'],
limit_start=0, limit_page_length=5, filters={
"ifnull(due_date, '')": ('=', ''), "ifnull(party, '')": ('!=', ''),
'voucher_type': 'Sales Invoice', 'credit': ('!=', '0')
}
)
kwargs = [pi_kwargs, si_kwargs]
kwargs = get_query_kwargs()
for kwarg in kwargs:
for batch in get_result_in_batches(**kwarg):
conditions, names = build_conditions(batch, kwarg.get('voucher_type'))
voucher_num_col = kwarg.get('voucher_num_col', 'voucher_no')
voucher_type = kwarg.get('use_voucher_type') or kwarg.get('voucher_type')
conditions, names = build_conditions(batch, voucher_type, voucher_num_col)
if conditions and names:
start = 'UPDATE `tabGL Entry` SET `due_date` = CASE '
cond = ' '.join(conditions)
else_cond = ' ELSE `due_date` END WHERE '
frappe.db.sql(
start + cond + ' ELSE `due_date` END WHERE `voucher_no` IN %s',
start + cond + else_cond + voucher_num_col + ' IN %s',
values=(names,)
)
@ -53,16 +43,17 @@ def get_gle_batch(**kwargs):
limit_start = kwargs.get('limit_start')
limit_page_length = kwargs.get('limit_page_length')
filters = kwargs.get('filters')
or_filters = kwargs.get('or_filters')
results = frappe.get_list(
doctype, fields=fields, limit_start=limit_start, limit_page_length=limit_page_length,
filters=filters
filters=filters, or_filters=or_filters
)
return results
def build_conditions(query_results, voucher_type):
def build_conditions(query_results, voucher_type, voucher_num_col):
"""
builds the string to be used is sql CASE statement. Returns the a tuple of
the string for the CASE statement and a tuple of applicable voucher names
@ -70,9 +61,8 @@ def build_conditions(query_results, voucher_type):
conditions = []
invoice_names = []
# first extract the voucher names into two separate lists so it can be easy to query the db
for result in query_results:
voucher_no = result.get('voucher_no')
voucher_no = result.get(voucher_num_col)
if voucher_no:
invoice_names.append("%s" % (voucher_no,))
@ -83,6 +73,45 @@ def build_conditions(query_results, voucher_type):
if invoice_details:
for d in invoice_details:
conditions.append('WHEN `voucher_no`="{number}" THEN "{date}"'.format(number=d.name, date=d.due_date))
conditions.append('WHEN `{voucher_no}`="{number}" THEN "{date}"'.format(
number=d.name, date=d.due_date, voucher_no=voucher_num_col))
return conditions, invoice_names
def get_query_kwargs():
pi_kwargs = dict(
voucher_type='Purchase Invoice', doctype='GL Entry', fields=['voucher_no'],
limit_start=0, limit_page_length=5, filters={
"ifnull(due_date, '')": ('=', ''), "ifnull(party, '')": ('!=', ''),
'voucher_type': 'Purchase Invoice', 'debit': ('!=', '0')
}
)
si_kwargs = dict(
voucher_type='Sales Invoice', doctype='GL Entry', fields=['voucher_no'],
limit_start=0, limit_page_length=5, filters={
"ifnull(due_date, '')": ('=', ''), "ifnull(party, '')": ('!=', ''),
'voucher_type': 'Sales Invoice', 'credit': ('!=', '0')
}
)
journal_kwargs_si = dict(
voucher_type='Journal Entry', doctype='GL Entry', fields=['against_voucher'],
limit_start=0, limit_page_length=5, filters={
"ifnull(due_date, '')": ('=', ''), "ifnull(party, '')": ('!=', ''),
'voucher_type': 'Journal Entry', 'against_voucher_type': 'Sales Invoice'
},
voucher_num_col='against_voucher', use_voucher_type='Sales Invoice',
)
journal_kwargs_pi = dict(
voucher_type='Journal Entry', doctype='GL Entry', fields=['against_voucher'],
limit_start=0, limit_page_length=5, filters={
"ifnull(due_date, '')": ('=', ''), "ifnull(party, '')": ('!=', ''),
'voucher_type': 'Journal Entry', 'against_voucher_type': 'Purchase Invoice'
},
voucher_num_col='against_voucher', use_voucher_type='Purchase Invoice',
)
return [pi_kwargs, si_kwargs, journal_kwargs_pi, journal_kwargs_si]