From b4ad5c01583356365866dc24bd0ad6c74d617e4b Mon Sep 17 00:00:00 2001 From: deepeshgarg007 Date: Sat, 4 May 2019 20:29:21 +0530 Subject: [PATCH] feat: Ability to add custom dimensions --- .../accounts/doctype/dimension/__init__.py | 0 .../accounts/doctype/dimension/dimension.js | 8 +++ .../accounts/doctype/dimension/dimension.json | 48 ++++++++++++++ .../accounts/doctype/dimension/dimension.py | 65 +++++++++++++++++++ .../doctype/dimension/test_dimension.py | 10 +++ 5 files changed, 131 insertions(+) create mode 100644 erpnext/accounts/doctype/dimension/__init__.py create mode 100644 erpnext/accounts/doctype/dimension/dimension.js create mode 100644 erpnext/accounts/doctype/dimension/dimension.json create mode 100644 erpnext/accounts/doctype/dimension/dimension.py create mode 100644 erpnext/accounts/doctype/dimension/test_dimension.py diff --git a/erpnext/accounts/doctype/dimension/__init__.py b/erpnext/accounts/doctype/dimension/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/erpnext/accounts/doctype/dimension/dimension.js b/erpnext/accounts/doctype/dimension/dimension.js new file mode 100644 index 0000000000..4926fff7da --- /dev/null +++ b/erpnext/accounts/doctype/dimension/dimension.js @@ -0,0 +1,8 @@ +// Copyright (c) 2019, Frappe Technologies Pvt. Ltd. and contributors +// For license information, please see license.txt + +frappe.ui.form.on('Dimension', { + // refresh: function(frm) { + + // } +}); diff --git a/erpnext/accounts/doctype/dimension/dimension.json b/erpnext/accounts/doctype/dimension/dimension.json new file mode 100644 index 0000000000..4937e4d7a2 --- /dev/null +++ b/erpnext/accounts/doctype/dimension/dimension.json @@ -0,0 +1,48 @@ +{ + "autoname": "field:label", + "creation": "2019-05-04 18:13:37.002352", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "label", + "fieldname" + ], + "fields": [ + { + "fieldname": "label", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Label", + "reqd": 1, + "unique": 1 + }, + { + "fieldname": "fieldname", + "fieldtype": "Data", + "label": "Fieldname" + } + ], + "modified": "2019-05-04 18:59:14.969008", + "modified_by": "Administrator", + "module": "Accounts", + "name": "Dimension", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "quick_entry": 1, + "sort_field": "modified", + "sort_order": "ASC", + "track_changes": 1 +} \ No newline at end of file diff --git a/erpnext/accounts/doctype/dimension/dimension.py b/erpnext/accounts/doctype/dimension/dimension.py new file mode 100644 index 0000000000..524b6acb00 --- /dev/null +++ b/erpnext/accounts/doctype/dimension/dimension.py @@ -0,0 +1,65 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2019, Frappe Technologies Pvt. Ltd. and contributors +# For license information, please see license.txt + +from __future__ import unicode_literals +import frappe +from frappe.model.document import Document +from frappe.custom.doctype.custom_field.custom_field import create_custom_fields +from frappe.custom.doctype.custom_field.custom_field import create_custom_field +from frappe import scrub + +class Dimension(Document): + + def before_insert(self): + self.set_fieldname() + self.make_dimension_in_accounting_doctypes() + + def on_trash(self): + self.delete_dimension() + + def set_fieldname(self): + if not self.fieldname: + self.fieldname = scrub(self.label) + + def make_dimension_in_accounting_doctypes(self): + last_created_dimension = get_last_created_dimension() + + doclist = ["GL Entry", "Sales Invoice", "Purchase Invoice", "Payment Entry", "BOM", "Sales Order", "Purchase Order", + "Stock Entry", "Budget", "Payroll Entry", "Delivery Note"] + + df = { + "fieldname": self.fieldname, + "label": self.label, + "fieldtype": "Data", + "insert_after": last_created_dimension if last_created_dimension else "project" + } + + for doctype in doclist: + create_custom_field(doctype, df) + + def delete_dimension(self): + doclist = ["GL Entry", "Sales Invoice", "Purchase Invoice", "Payment Entry", "BOM", "Sales Order", "Purchase Order", + "Stock Entry", "Budget", "Payroll Entry", "Delivery Note"] + + frappe.db.sql(""" + DELETE FROM `tabCustom Field` + WHERE fieldname = %s + AND dt IN (%s)""" % + ('%s', ', '.join(['%s']* len(doclist))), tuple([self.fieldname] + doclist)) + + frappe.db.sql(""" + DELETE FROM `tabProperty Setter` + WHERE field_name = %s + AND doc_type IN (%s)""" % + ('%s', ', '.join(['%s']* len(doclist))), tuple([self.fieldname] + doclist)) + + for doc in doclist: + frappe.clear_cache(doctype=doc) + + +def get_last_created_dimension(): + last_created_dimension = frappe.db.sql("select fieldname, max(creation) from `tabDimension`", as_dict=1) + + if last_created_dimension[0]: + return last_created_dimension[0].fieldname diff --git a/erpnext/accounts/doctype/dimension/test_dimension.py b/erpnext/accounts/doctype/dimension/test_dimension.py new file mode 100644 index 0000000000..f726e9da66 --- /dev/null +++ b/erpnext/accounts/doctype/dimension/test_dimension.py @@ -0,0 +1,10 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2019, Frappe Technologies Pvt. Ltd. and Contributors +# See license.txt +from __future__ import unicode_literals + +# import frappe +import unittest + +class TestDimension(unittest.TestCase): + pass