From 3b327ed55f76bd6ce9401693976ef8b546fc27c2 Mon Sep 17 00:00:00 2001 From: tunde Date: Tue, 29 Aug 2017 10:23:19 +0100 Subject: [PATCH] 'modify patch to also adjust journal entries' --- .../v8_8/update_gl_due_date_for_pi_and_si.py | 75 +++++++++++++------ 1 file changed, 52 insertions(+), 23 deletions(-) diff --git a/erpnext/patches/v8_8/update_gl_due_date_for_pi_and_si.py b/erpnext/patches/v8_8/update_gl_due_date_for_pi_and_si.py index 48da3ea95e..636da6974e 100644 --- a/erpnext/patches/v8_8/update_gl_due_date_for_pi_and_si.py +++ b/erpnext/patches/v8_8/update_gl_due_date_for_pi_and_si.py @@ -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]