From 6c92cff22657908498a6ee208965a75593fadf44 Mon Sep 17 00:00:00 2001 From: Gaurav Naik Date: Fri, 11 May 2018 07:52:50 +0530 Subject: [PATCH 1/7] Accounting Period autoname --- .../doctype/accounting_period/accounting_period.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/accounting_period/accounting_period.py b/erpnext/accounts/doctype/accounting_period/accounting_period.py index 31f18490a35..030189d88fa 100644 --- a/erpnext/accounts/doctype/accounting_period/accounting_period.py +++ b/erpnext/accounts/doctype/accounting_period/accounting_period.py @@ -7,4 +7,9 @@ import frappe from frappe.model.document import Document class AccountingPeriod(Document): - pass + def validate(self): + self.validate_overlap() + + def autoname(self): + company_abbr = frappe.db.get_value("Company", self.company, "abbr") + self.name = " - ".join([self.period_name, company_abbr]) From 9502476c6753cb8c628d48e2b534d48e4ec2625b Mon Sep 17 00:00:00 2001 From: Gaurav Naik Date: Fri, 11 May 2018 07:53:12 +0530 Subject: [PATCH 2/7] Accounting Period overlap validation --- .../accounting_period/accounting_period.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/erpnext/accounts/doctype/accounting_period/accounting_period.py b/erpnext/accounts/doctype/accounting_period/accounting_period.py index 030189d88fa..306bf9177e9 100644 --- a/erpnext/accounts/doctype/accounting_period/accounting_period.py +++ b/erpnext/accounts/doctype/accounting_period/accounting_period.py @@ -13,3 +13,22 @@ class AccountingPeriod(Document): def autoname(self): company_abbr = frappe.db.get_value("Company", self.company, "abbr") self.name = " - ".join([self.period_name, company_abbr]) + + def validate_overlap(self): + existing_accounting_period = frappe.db.sql("""select name from `tabAccounting Period` + where ( + (%(start_date)s between start_date and end_date) + or (%(end_date)s between start_date and end_date) + or (start_date between %(start_date)s and %(end_date)s) + or (end_date between %(start_date)s and %(end_date)s) + ) and name!=%(name)s and company=%(company)s""", + { + "start_date": self.start_date, + "end_date": self.end_date, + "name": self.name, + "company": self.company + }, as_dict=True) + + if len(existing_accounting_period) > 0: + frappe.throw("Accounting Period overlaps with {0}".format(existing_accounting_period[0].get("name"))) + From 32b3aa257c65ac0182b07e79bdc987a258e99564 Mon Sep 17 00:00:00 2001 From: Gaurav Naik Date: Mon, 14 May 2018 18:02:24 +0530 Subject: [PATCH 3/7] [Fix] Indentation for accounting_period overlap --- .../accounting_period/accounting_period.py | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/erpnext/accounts/doctype/accounting_period/accounting_period.py b/erpnext/accounts/doctype/accounting_period/accounting_period.py index 306bf9177e9..8760d8f35e8 100644 --- a/erpnext/accounts/doctype/accounting_period/accounting_period.py +++ b/erpnext/accounts/doctype/accounting_period/accounting_period.py @@ -15,20 +15,20 @@ class AccountingPeriod(Document): self.name = " - ".join([self.period_name, company_abbr]) def validate_overlap(self): - existing_accounting_period = frappe.db.sql("""select name from `tabAccounting Period` - where ( - (%(start_date)s between start_date and end_date) - or (%(end_date)s between start_date and end_date) - or (start_date between %(start_date)s and %(end_date)s) - or (end_date between %(start_date)s and %(end_date)s) - ) and name!=%(name)s and company=%(company)s""", - { - "start_date": self.start_date, - "end_date": self.end_date, - "name": self.name, - "company": self.company - }, as_dict=True) + existing_accounting_period = frappe.db.sql("""select name from `tabAccounting Period` + where ( + (%(start_date)s between start_date and end_date) + or (%(end_date)s between start_date and end_date) + or (start_date between %(start_date)s and %(end_date)s) + or (end_date between %(start_date)s and %(end_date)s) + ) and name!=%(name)s and company=%(company)s""", + { + "start_date": self.start_date, + "end_date": self.end_date, + "name": self.name, + "company": self.company + }, as_dict=True) - if len(existing_accounting_period) > 0: - frappe.throw("Accounting Period overlaps with {0}".format(existing_accounting_period[0].get("name"))) + if len(existing_accounting_period) > 0: + frappe.throw("Accounting Period overlaps with {0}".format(existing_accounting_period[0].get("name"))) From 2fe38c3ff5b89c72e5a15f365e6a005370bc0c8f Mon Sep 17 00:00:00 2001 From: Gaurav Naik Date: Mon, 14 May 2018 18:06:34 +0530 Subject: [PATCH 4/7] Bootstrap documents for closing --- .../accounting_period/accounting_period.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/erpnext/accounts/doctype/accounting_period/accounting_period.py b/erpnext/accounts/doctype/accounting_period/accounting_period.py index 8760d8f35e8..f2ba5789653 100644 --- a/erpnext/accounts/doctype/accounting_period/accounting_period.py +++ b/erpnext/accounts/doctype/accounting_period/accounting_period.py @@ -10,6 +10,9 @@ class AccountingPeriod(Document): def validate(self): self.validate_overlap() + def before_insert(self): + self.bootstrap_doctypes_for_closing() + def autoname(self): company_abbr = frappe.db.get_value("Company", self.company, "abbr") self.name = " - ".join([self.period_name, company_abbr]) @@ -32,3 +35,20 @@ class AccountingPeriod(Document): if len(existing_accounting_period) > 0: frappe.throw("Accounting Period overlaps with {0}".format(existing_accounting_period[0].get("name"))) + def get_doctypes_for_closing(self): + docs_for_closing = [] + #if not self.closed_documents or len(self.closed_documents) == 0: + doctypes = ["Sales Invoice", "Purchase Invoice", "Journal Entry", "Payroll Entry", "Bank Reconciliation", "Asset", "Purchase Order", "Sales Order", "Leave Application", "Leave Allocation", "Stock Entry"] + closed_doctypes = [{"document_type": doctype, "closed": 1} for doctype in doctypes] + for closed_doctype in closed_doctypes: + docs_for_closing.append(closed_doctype) + + return docs_for_closing + + def bootstrap_doctypes_for_closing(self): + if self.closed_documents.length == 0: + for doctype_for_closing in self.get_doctypes_for_closing(): + self.append('closed_documents', { + "document_type": doctype_for_closing.document_type, + "closed": doctype_for_closing.closed + }) From 52571a80daef6bba3ffeb10712183347bd69ad41 Mon Sep 17 00:00:00 2001 From: Gaurav Naik Date: Mon, 14 May 2018 18:06:50 +0530 Subject: [PATCH 5/7] Bootstrap documents for closing (js) --- .../accounting_period/accounting_period.js | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/doctype/accounting_period/accounting_period.js b/erpnext/accounts/doctype/accounting_period/accounting_period.js index 1fb57eabcfd..e3d805a1681 100644 --- a/erpnext/accounts/doctype/accounting_period/accounting_period.js +++ b/erpnext/accounts/doctype/accounting_period/accounting_period.js @@ -2,7 +2,23 @@ // For license information, please see license.txt frappe.ui.form.on('Accounting Period', { - refresh: function(frm) { - + onload: function(frm) { + if(frm.doc.closed_documents.length === 0 || (frm.doc.closed_documents.length === 1 && frm.doc.closed_documents[0].document_type == undefined)) { + frappe.call({ + method: "get_doctypes_for_closing", + doc:frm.doc, + callback: function(r) { + if(r.message) { + cur_frm.clear_table("closed_documents"); + r.message.forEach(function(element) { + var c = frm.add_child("closed_documents"); + c.document_type = element.document_type; + c.closed = element.closed; + }); + refresh_field("closed_documents"); + } + } + }); + } } }); From caaebb9d2ac1c3b6cd421a23619e799f80fe7116 Mon Sep 17 00:00:00 2001 From: Gaurav Naik Date: Mon, 14 May 2018 18:11:09 +0530 Subject: [PATCH 6/7] [WIP] Accounting Period Test cases --- .../test_accounting_period.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/accounting_period/test_accounting_period.py b/erpnext/accounts/doctype/accounting_period/test_accounting_period.py index 99694d2136e..cc2e6a9fc6f 100644 --- a/erpnext/accounts/doctype/accounting_period/test_accounting_period.py +++ b/erpnext/accounts/doctype/accounting_period/test_accounting_period.py @@ -7,4 +7,21 @@ import frappe import unittest class TestAccountingPeriod(unittest.TestCase): - pass + def test_overlap(self): + ap1 = create_accounting_period({"start_date":"2018-04-01", "end_date":"2018-06-30", "company":"Wind Power LLC"}) + ap1.save() + ap2 = create_accounting_period({"start_date":"2018-06-30", "end_date":"2018-07-10", "company":"Wind Power LLC"}) + self.assertRaises(frappe.OverlapError, accounting_period_2.save()) + + def tearDown(self): + pass + + +def create_accounting_period(**args): + accounting_period = frappe.new_doc("Accounting Period") + accounting_period.start_date = args.start_date or frappe.utils.datetime.date(2018, 4, 1) + accounting_period.end_date = args.end_date or frappe.utils.datetime.date(2018, 6, 30) + accounting_period.company = args.company + accounting_period.period_name = "_Test_Period_Name_1" + + return accounting_period From 21b2df41accea57653f39891ab0bd30b801ed6dc Mon Sep 17 00:00:00 2001 From: Gaurav Naik Date: Mon, 14 May 2018 18:15:47 +0530 Subject: [PATCH 7/7] [Fix] len() instead of .length --- erpnext/accounts/doctype/accounting_period/accounting_period.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/accounting_period/accounting_period.py b/erpnext/accounts/doctype/accounting_period/accounting_period.py index f2ba5789653..32441db2c1b 100644 --- a/erpnext/accounts/doctype/accounting_period/accounting_period.py +++ b/erpnext/accounts/doctype/accounting_period/accounting_period.py @@ -46,7 +46,7 @@ class AccountingPeriod(Document): return docs_for_closing def bootstrap_doctypes_for_closing(self): - if self.closed_documents.length == 0: + if len(self.closed_documents) == 0: for doctype_for_closing in self.get_doctypes_for_closing(): self.append('closed_documents', { "document_type": doctype_for_closing.document_type,