From 215c901a0d856d659c742bd5571daa32c55ad49d Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Mon, 2 Sep 2013 15:38:53 +0530 Subject: [PATCH 1/9] [fix] [minor] recreate gl entries when using auto inventory accounting to fix bug introduced due to commit - 5dd6b1d082d180133813c1c661d5e72076a19491 --- controllers/selling_controller.py | 9 ++++-- patches/patch_list.py | 1 + patches/september_2013/__init__.py | 0 .../p01_fix_buying_amount_gl_entries.py | 31 +++++++++++++++++++ 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 patches/september_2013/__init__.py create mode 100644 patches/september_2013/p01_fix_buying_amount_gl_entries.py diff --git a/controllers/selling_controller.py b/controllers/selling_controller.py index a13d747279..5605ccf3a2 100644 --- a/controllers/selling_controller.py +++ b/controllers/selling_controller.py @@ -99,6 +99,8 @@ class SellingController(StockController): for item in self.doclist.get({"parentfield": self.fname}): if item.item_code in self.stock_items or \ (item_sales_bom and item_sales_bom.get(item.item_code)): + + buying_amount = 0 if item.item_code in self.stock_items: buying_amount = get_buying_amount(self.doc.doctype, self.doc.name, item.name, stock_ledger_entries.get((item.item_code, @@ -108,9 +110,10 @@ class SellingController(StockController): self.doc.doctype, self.doc.name, item.name, stock_ledger_entries, item_sales_bom) - item.buying_amount = buying_amount >= 0.01 and buying_amount or 0 - webnotes.conn.set_value(item.doctype, item.name, "buying_amount", - item.buying_amount) + # buying_amount >= 0.01 so that gl entry doesn't get created for such small amounts + item.buying_amount = buying_amount >= 0.01 and buying_amount or 0 + webnotes.conn.set_value(item.doctype, item.name, "buying_amount", + item.buying_amount) def check_expense_account(self, item): if item.buying_amount and not item.expense_account: diff --git a/patches/patch_list.py b/patches/patch_list.py index 1d382a56bd..2db62cf3a8 100644 --- a/patches/patch_list.py +++ b/patches/patch_list.py @@ -257,4 +257,5 @@ patch_list = [ "execute:webnotes.reload_doc('accounts', 'Print Format', 'POS Invoice') # 2013-08-16", "patches.august_2013.p06_fix_sle_against_stock_entry", "execute:webnotes.bean('Style Settings').save() #2013-08-20", + "patches.september_2013.p01_fix_buying_amount_gl_entries", ] \ No newline at end of file diff --git a/patches/september_2013/__init__.py b/patches/september_2013/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/patches/september_2013/p01_fix_buying_amount_gl_entries.py b/patches/september_2013/p01_fix_buying_amount_gl_entries.py new file mode 100644 index 0000000000..b32317469d --- /dev/null +++ b/patches/september_2013/p01_fix_buying_amount_gl_entries.py @@ -0,0 +1,31 @@ +# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. +# License: GNU General Public License v3. See license.txt + +from __future__ import unicode_literals +import webnotes +import webnotes.defaults +from webnotes.utils import cint + +def execute(): + if not cint(webnotes.defaults.get_global_default("auto_inventory_accounting")): + return + + # fix delivery note + for dn in webnotes.conn.sql_list("""select name from `tabDelivery Note` where docstatus=1 + and posting_date >= "2013-08-06" """): + recreate_gl_entries("Delivery Note", dn) + + # fix sales invoice + for si in webnotes.conn.sql_list("""select name from `tabSales Invoice` where docstatus=1 + and update_stock=1 and posting_date >= "2013-08-06" """): + recreate_gl_entries("Sales Invoice", si) + +def recreate_gl_entries(doctype, name): + # remove gl entries + webnotes.conn.sql("""delete from `tabGL Entry` where voucher_type=%s + and voucher_no=%s""", (doctype, name)) + + # calculate buying amount and make gl entries + bean = webnotes.bean(doctype, name) + bean.run_method("set_buying_amount") + bean.run_method("make_gl_entries") \ No newline at end of file From d22a9cae90fb7c815197789f9813bb1b38d655cc Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Mon, 2 Sep 2013 15:54:52 +0530 Subject: [PATCH 2/9] [fix] [minor] recreate gl entries when using auto inventory accounting to fix bug introduced due to commit - 5dd6b1d082d180133813c1c661d5e72076a19491 --- .../p01_fix_buying_amount_gl_entries.py | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/patches/september_2013/p01_fix_buying_amount_gl_entries.py b/patches/september_2013/p01_fix_buying_amount_gl_entries.py index b32317469d..1fe3b4623d 100644 --- a/patches/september_2013/p01_fix_buying_amount_gl_entries.py +++ b/patches/september_2013/p01_fix_buying_amount_gl_entries.py @@ -13,14 +13,14 @@ def execute(): # fix delivery note for dn in webnotes.conn.sql_list("""select name from `tabDelivery Note` where docstatus=1 and posting_date >= "2013-08-06" """): - recreate_gl_entries("Delivery Note", dn) + recreate_gl_entries("Delivery Note", dn, "delivery_note_details") # fix sales invoice for si in webnotes.conn.sql_list("""select name from `tabSales Invoice` where docstatus=1 and update_stock=1 and posting_date >= "2013-08-06" """): - recreate_gl_entries("Sales Invoice", si) + recreate_gl_entries("Sales Invoice", si, "entries") -def recreate_gl_entries(doctype, name): +def recreate_gl_entries(doctype, name, parentfield): # remove gl entries webnotes.conn.sql("""delete from `tabGL Entry` where voucher_type=%s and voucher_no=%s""", (doctype, name)) @@ -28,4 +28,20 @@ def recreate_gl_entries(doctype, name): # calculate buying amount and make gl entries bean = webnotes.bean(doctype, name) bean.run_method("set_buying_amount") + + # update missing expense account and cost center + for item in bean.doclist.get({"parentfield": parentfield}): + if item.buying_amount and not (item.expense_account and item.cost_center): + item_values = webnotes.conn.get_value("Item", item.item_code, + ["purchase_account", "default_sales_cost_center"]) + company_values = webnotes.conn.get_value("Company", bean.doc.company, + ["default_expense_account", "cost_center"]) + if not item.expense_account: + item.expense_account = (item_values and item_values[0]) or (company_values and company_values[0]) + if not item.cost_center: + item.cost_center = (item_values and item_values[1]) or (company_values and company_values[1]) + + webnotes.conn.set_value(item.doctype, item.name, "expense_account", item.expense_account) + webnotes.conn.set_value(item.doctype, item.name, "cost_center", item.cost_center) + bean.run_method("make_gl_entries") \ No newline at end of file From c81eabe07b431a61cfcafceedb0cef17d49ce424 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Mon, 2 Sep 2013 16:27:34 +0530 Subject: [PATCH 3/9] [fix] [minor] recreate gl entries when using auto inventory accounting to fix bug introduced due to commit - 5dd6b1d082d180133813c1c661d5e72076a19491 --- .../p01_fix_buying_amount_gl_entries.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/patches/september_2013/p01_fix_buying_amount_gl_entries.py b/patches/september_2013/p01_fix_buying_amount_gl_entries.py index 1fe3b4623d..160aa15e54 100644 --- a/patches/september_2013/p01_fix_buying_amount_gl_entries.py +++ b/patches/september_2013/p01_fix_buying_amount_gl_entries.py @@ -35,11 +35,26 @@ def recreate_gl_entries(doctype, name, parentfield): item_values = webnotes.conn.get_value("Item", item.item_code, ["purchase_account", "default_sales_cost_center"]) company_values = webnotes.conn.get_value("Company", bean.doc.company, - ["default_expense_account", "cost_center"]) + ["default_expense_account", "cost_center", "stock_adjustment_cost_center"]) if not item.expense_account: item.expense_account = (item_values and item_values[0]) or (company_values and company_values[0]) + if not item.cost_center: - item.cost_center = (item_values and item_values[1]) or (company_values and company_values[1]) + item.cost_center = (item_values and item_values[1]) or \ + (company_values and (company_values[1] or company_values[2])) + + if not (item.expense_account and item.cost_center): + res = webnotes.conn.sql("""select expense_account, cost_center + from `tab%s` child where docstatus=1 and item_code=%s + ifnull(expense_account, '')!='' and ifnull(cost_center, '')!='' + and (select company from `tab%s` parent where parent.name=child.parent)=%s + order by creation desc limit 1""" % (item.doctype, "%s", doctype, "%s"), + (item.item_code, bean.doc.company)) + if res: + if not item.expense_account: + item.expense_account = res[0][0] + if not item.cost_center: + item.cost_center = res[0][1] webnotes.conn.set_value(item.doctype, item.name, "expense_account", item.expense_account) webnotes.conn.set_value(item.doctype, item.name, "cost_center", item.cost_center) From d09039ae43d8915ad59f5570511ecb3ec05a2ddb Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Mon, 2 Sep 2013 16:29:52 +0530 Subject: [PATCH 4/9] [fix] [minor] recreate gl entries when using auto inventory accounting to fix bug introduced due to commit - 5dd6b1d082d180133813c1c661d5e72076a19491 --- patches/september_2013/p01_fix_buying_amount_gl_entries.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/patches/september_2013/p01_fix_buying_amount_gl_entries.py b/patches/september_2013/p01_fix_buying_amount_gl_entries.py index 160aa15e54..22017b4c44 100644 --- a/patches/september_2013/p01_fix_buying_amount_gl_entries.py +++ b/patches/september_2013/p01_fix_buying_amount_gl_entries.py @@ -45,9 +45,9 @@ def recreate_gl_entries(doctype, name, parentfield): if not (item.expense_account and item.cost_center): res = webnotes.conn.sql("""select expense_account, cost_center - from `tab%s` child where docstatus=1 and item_code=%s - ifnull(expense_account, '')!='' and ifnull(cost_center, '')!='' - and (select company from `tab%s` parent where parent.name=child.parent)=%s + from `tab%s` child where docstatus=1 and item_code=%s and + ifnull(expense_account, '')!='' and ifnull(cost_center, '')!='' and + (select company from `tab%s` parent where parent.name=child.parent)=%s order by creation desc limit 1""" % (item.doctype, "%s", doctype, "%s"), (item.item_code, bean.doc.company)) if res: From fe73c38867849b52c4380c921396eee8c668bf30 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Mon, 2 Sep 2013 16:34:49 +0530 Subject: [PATCH 5/9] [fix] [minor] recreate gl entries when using auto inventory accounting to fix bug introduced due to commit - 5dd6b1d082d180133813c1c661d5e72076a19491 --- .../p01_fix_buying_amount_gl_entries.py | 34 ++++++------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/patches/september_2013/p01_fix_buying_amount_gl_entries.py b/patches/september_2013/p01_fix_buying_amount_gl_entries.py index 22017b4c44..7379c302a9 100644 --- a/patches/september_2013/p01_fix_buying_amount_gl_entries.py +++ b/patches/september_2013/p01_fix_buying_amount_gl_entries.py @@ -32,29 +32,17 @@ def recreate_gl_entries(doctype, name, parentfield): # update missing expense account and cost center for item in bean.doclist.get({"parentfield": parentfield}): if item.buying_amount and not (item.expense_account and item.cost_center): - item_values = webnotes.conn.get_value("Item", item.item_code, - ["purchase_account", "default_sales_cost_center"]) - company_values = webnotes.conn.get_value("Company", bean.doc.company, - ["default_expense_account", "cost_center", "stock_adjustment_cost_center"]) - if not item.expense_account: - item.expense_account = (item_values and item_values[0]) or (company_values and company_values[0]) - - if not item.cost_center: - item.cost_center = (item_values and item_values[1]) or \ - (company_values and (company_values[1] or company_values[2])) - - if not (item.expense_account and item.cost_center): - res = webnotes.conn.sql("""select expense_account, cost_center - from `tab%s` child where docstatus=1 and item_code=%s and - ifnull(expense_account, '')!='' and ifnull(cost_center, '')!='' and - (select company from `tab%s` parent where parent.name=child.parent)=%s - order by creation desc limit 1""" % (item.doctype, "%s", doctype, "%s"), - (item.item_code, bean.doc.company)) - if res: - if not item.expense_account: - item.expense_account = res[0][0] - if not item.cost_center: - item.cost_center = res[0][1] + res = webnotes.conn.sql("""select expense_account, cost_center + from `tab%s` child where docstatus=1 and item_code=%s and + ifnull(expense_account, '')!='' and ifnull(cost_center, '')!='' and + (select company from `tab%s` parent where parent.name=child.parent)=%s + order by creation desc limit 1""" % (item.doctype, "%s", doctype, "%s"), + (item.item_code, bean.doc.company)) + if res: + if not item.expense_account: + item.expense_account = res[0][0] + if not item.cost_center: + item.cost_center = res[0][1] webnotes.conn.set_value(item.doctype, item.name, "expense_account", item.expense_account) webnotes.conn.set_value(item.doctype, item.name, "cost_center", item.cost_center) From 79401717e0e0527ba90446e015b604cbb058be62 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Mon, 2 Sep 2013 16:44:09 +0530 Subject: [PATCH 6/9] [fix] [minor] recreate gl entries when using auto inventory accounting to fix bug introduced due to commit - 5dd6b1d082d180133813c1c661d5e72076a19491 --- .../p01_fix_buying_amount_gl_entries.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/patches/september_2013/p01_fix_buying_amount_gl_entries.py b/patches/september_2013/p01_fix_buying_amount_gl_entries.py index 7379c302a9..7812a2aaaa 100644 --- a/patches/september_2013/p01_fix_buying_amount_gl_entries.py +++ b/patches/september_2013/p01_fix_buying_amount_gl_entries.py @@ -21,10 +21,6 @@ def execute(): recreate_gl_entries("Sales Invoice", si, "entries") def recreate_gl_entries(doctype, name, parentfield): - # remove gl entries - webnotes.conn.sql("""delete from `tabGL Entry` where voucher_type=%s - and voucher_no=%s""", (doctype, name)) - # calculate buying amount and make gl entries bean = webnotes.bean(doctype, name) bean.run_method("set_buying_amount") @@ -35,16 +31,20 @@ def recreate_gl_entries(doctype, name, parentfield): res = webnotes.conn.sql("""select expense_account, cost_center from `tab%s` child where docstatus=1 and item_code=%s and ifnull(expense_account, '')!='' and ifnull(cost_center, '')!='' and - (select company from `tab%s` parent where parent.name=child.parent)=%s - order by creation desc limit 1""" % (item.doctype, "%s", doctype, "%s"), - (item.item_code, bean.doc.company)) + (select company from `tabAccount` ac where ac.name=child.expense_account)=%s and + (select company from `tabCost Center` cc where cc.name=child.cost_center)=%s + order by creation desc limit 1""" % (item.doctype, "%s", "%s", "%s"), + (item.item_code, bean.doc.company, bean.doc.company)) if res: if not item.expense_account: item.expense_account = res[0][0] if not item.cost_center: item.cost_center = res[0][1] - webnotes.conn.set_value(item.doctype, item.name, "expense_account", item.expense_account) - webnotes.conn.set_value(item.doctype, item.name, "cost_center", item.cost_center) - + webnotes.conn.set_value(item.doctype, item.name, "expense_account", item.expense_account) + webnotes.conn.set_value(item.doctype, item.name, "cost_center", item.cost_center) + + # remove gl entries + webnotes.conn.sql("""delete from `tabGL Entry` where voucher_type=%s + and voucher_no=%s""", (doctype, name)) bean.run_method("make_gl_entries") \ No newline at end of file From 9bb928f595aa54baa28366ff93c57d6126ce5c01 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Mon, 2 Sep 2013 17:02:00 +0530 Subject: [PATCH 7/9] [fix] [minor] recreate gl entries when using auto inventory accounting to fix bug introduced due to commit - 5dd6b1d082d180133813c1c661d5e72076a19491 --- .../p01_fix_buying_amount_gl_entries.py | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/patches/september_2013/p01_fix_buying_amount_gl_entries.py b/patches/september_2013/p01_fix_buying_amount_gl_entries.py index 7812a2aaaa..0a25cd4faf 100644 --- a/patches/september_2013/p01_fix_buying_amount_gl_entries.py +++ b/patches/september_2013/p01_fix_buying_amount_gl_entries.py @@ -12,12 +12,12 @@ def execute(): # fix delivery note for dn in webnotes.conn.sql_list("""select name from `tabDelivery Note` where docstatus=1 - and posting_date >= "2013-08-06" """): + and posting_date >= "2013-08-06" order by posting_date"""): recreate_gl_entries("Delivery Note", dn, "delivery_note_details") # fix sales invoice for si in webnotes.conn.sql_list("""select name from `tabSales Invoice` where docstatus=1 - and update_stock=1 and posting_date >= "2013-08-06" """): + and update_stock=1 and posting_date >= "2013-08-06" order by posting_date"""): recreate_gl_entries("Sales Invoice", si, "entries") def recreate_gl_entries(doctype, name, parentfield): @@ -27,24 +27,34 @@ def recreate_gl_entries(doctype, name, parentfield): # update missing expense account and cost center for item in bean.doclist.get({"parentfield": parentfield}): - if item.buying_amount and not (item.expense_account and item.cost_center): + if item.buying_amount and not validate_item_values(item, bean.doc.company): res = webnotes.conn.sql("""select expense_account, cost_center from `tab%s` child where docstatus=1 and item_code=%s and ifnull(expense_account, '')!='' and ifnull(cost_center, '')!='' and (select company from `tabAccount` ac where ac.name=child.expense_account)=%s and - (select company from `tabCost Center` cc where cc.name=child.cost_center)=%s + (select company from `tabCost Center` cc where cc.name=child.cost_center)=%s and order by creation desc limit 1""" % (item.doctype, "%s", "%s", "%s"), (item.item_code, bean.doc.company, bean.doc.company)) if res: - if not item.expense_account: - item.expense_account = res[0][0] - if not item.cost_center: - item.cost_center = res[0][1] - + item.expense_account = res[0][0] + item.cost_center = res[0][1] + webnotes.conn.set_value(item.doctype, item.name, "expense_account", item.expense_account) webnotes.conn.set_value(item.doctype, item.name, "cost_center", item.cost_center) # remove gl entries webnotes.conn.sql("""delete from `tabGL Entry` where voucher_type=%s and voucher_no=%s""", (doctype, name)) - bean.run_method("make_gl_entries") \ No newline at end of file + bean.run_method("make_gl_entries") + +def validate_item_values(item, company): + if item.expense_account and \ + webnotes.conn.get_value("Account", item.expense_account, "company")!=company: + return False + elif item.cost_center and \ + webnotes.conn.get_value("Cost Center", item.cost_center, "company")!=company: + return False + elif not (item.expense_account and item.cost_center): + return False + + return True \ No newline at end of file From dc30890b790c6fb7b8a6200d754258252c8ec595 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Mon, 2 Sep 2013 17:03:38 +0530 Subject: [PATCH 8/9] [fix] [minor] recreate gl entries when using auto inventory accounting to fix bug introduced due to commit - 5dd6b1d082d180133813c1c661d5e72076a19491 --- patches/september_2013/p01_fix_buying_amount_gl_entries.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/patches/september_2013/p01_fix_buying_amount_gl_entries.py b/patches/september_2013/p01_fix_buying_amount_gl_entries.py index 0a25cd4faf..011180a58a 100644 --- a/patches/september_2013/p01_fix_buying_amount_gl_entries.py +++ b/patches/september_2013/p01_fix_buying_amount_gl_entries.py @@ -32,7 +32,7 @@ def recreate_gl_entries(doctype, name, parentfield): from `tab%s` child where docstatus=1 and item_code=%s and ifnull(expense_account, '')!='' and ifnull(cost_center, '')!='' and (select company from `tabAccount` ac where ac.name=child.expense_account)=%s and - (select company from `tabCost Center` cc where cc.name=child.cost_center)=%s and + (select company from `tabCost Center` cc where cc.name=child.cost_center)=%s order by creation desc limit 1""" % (item.doctype, "%s", "%s", "%s"), (item.item_code, bean.doc.company, bean.doc.company)) if res: From 0b8254e69b9ce90391b56f774c3fa8a6f1c10ccf Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Mon, 2 Sep 2013 17:17:48 +0530 Subject: [PATCH 9/9] [fix] [minor] recreate gl entries when using auto inventory accounting to fix bug introduced due to commit - 5dd6b1d082d180133813c1c661d5e72076a19491 --- .../september_2013/p01_fix_buying_amount_gl_entries.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/patches/september_2013/p01_fix_buying_amount_gl_entries.py b/patches/september_2013/p01_fix_buying_amount_gl_entries.py index 011180a58a..369a2914f1 100644 --- a/patches/september_2013/p01_fix_buying_amount_gl_entries.py +++ b/patches/september_2013/p01_fix_buying_amount_gl_entries.py @@ -25,6 +25,9 @@ def recreate_gl_entries(doctype, name, parentfield): bean = webnotes.bean(doctype, name) bean.run_method("set_buying_amount") + company_values = webnotes.conn.get_value("Company", bean.doc.company, ["default_expense_account", + "stock_adjustment_account", "cost_center", "stock_adjustment_cost_center"]) + # update missing expense account and cost center for item in bean.doclist.get({"parentfield": parentfield}): if item.buying_amount and not validate_item_values(item, bean.doc.company): @@ -38,9 +41,12 @@ def recreate_gl_entries(doctype, name, parentfield): if res: item.expense_account = res[0][0] item.cost_center = res[0][1] + elif company_values: + item.expense_account = company_values[0] or company_values[1] + item.cost_center = company_values[2] or company_values[3] - webnotes.conn.set_value(item.doctype, item.name, "expense_account", item.expense_account) - webnotes.conn.set_value(item.doctype, item.name, "cost_center", item.cost_center) + webnotes.conn.set_value(item.doctype, item.name, "expense_account", item.expense_account) + webnotes.conn.set_value(item.doctype, item.name, "cost_center", item.cost_center) # remove gl entries webnotes.conn.sql("""delete from `tabGL Entry` where voucher_type=%s