fix: Ability to disable dimensions
This commit is contained in:
parent
705c03c093
commit
d0a1ed9017
@ -6,5 +6,11 @@ frappe.ui.form.on('Accounting Dimension', {
|
||||
document_type: function(frm){
|
||||
frm.set_value('label', frm.doc.document_type);
|
||||
frm.set_value('fieldname', frappe.scrub(frm.doc.document_type))
|
||||
}
|
||||
|
||||
frappe.db.get_value('Accounting Dimension', {'document_type': frm.doc.document_type}, 'document_type', (r) => {
|
||||
if (r.document_type) {
|
||||
frm.set_df_property('document_type', 'description', "Document type is already set as dimension");
|
||||
}
|
||||
});
|
||||
},
|
||||
});
|
||||
|
@ -7,7 +7,8 @@
|
||||
"document_type",
|
||||
"label",
|
||||
"fieldname",
|
||||
"is_mandatory"
|
||||
"is_mandatory",
|
||||
"disable"
|
||||
],
|
||||
"fields": [
|
||||
{
|
||||
@ -30,12 +31,19 @@
|
||||
"reqd": 1
|
||||
},
|
||||
{
|
||||
"default": "0",
|
||||
"fieldname": "is_mandatory",
|
||||
"fieldtype": "Check",
|
||||
"label": "Is Mandatory"
|
||||
},
|
||||
{
|
||||
"default": "0",
|
||||
"fieldname": "disable",
|
||||
"fieldtype": "Check",
|
||||
"label": "Disable"
|
||||
}
|
||||
],
|
||||
"modified": "2019-05-16 13:37:04.240148",
|
||||
"modified": "2019-05-17 20:35:31.014495",
|
||||
"modified_by": "Administrator",
|
||||
"module": "Accounts",
|
||||
"name": "Accounting Dimension",
|
||||
|
@ -4,19 +4,23 @@
|
||||
|
||||
from __future__ import unicode_literals
|
||||
import frappe
|
||||
import json
|
||||
from frappe.model.document import Document
|
||||
from frappe.custom.doctype.custom_field.custom_field import create_custom_field
|
||||
from frappe import scrub
|
||||
from frappe.utils import cstr
|
||||
from frappe.utils.background_jobs import enqueue
|
||||
|
||||
class AccountingDimension(Document):
|
||||
def on_update(self):
|
||||
frappe.enqueue(disable_dimension, doc=self)
|
||||
|
||||
def before_insert(self):
|
||||
self.set_fieldname_and_label()
|
||||
self.make_accounting_dimension_in_accounting_doctypes()
|
||||
frappe.enqueue(make_accounting_dimension_in_accounting_doctypes, doc=self)
|
||||
|
||||
def on_trash(self):
|
||||
self.delete_accounting_dimension()
|
||||
frappe.enqueue(delete_accounting_dimension, doc=self)
|
||||
|
||||
def set_fieldname_and_label(self):
|
||||
if not self.label:
|
||||
@ -25,90 +29,109 @@ class AccountingDimension(Document):
|
||||
if not self.fieldname:
|
||||
self.fieldname = scrub(self.label)
|
||||
|
||||
def make_accounting_dimension_in_accounting_doctypes(self):
|
||||
def make_accounting_dimension_in_accounting_doctypes(doc):
|
||||
doclist = get_doclist()
|
||||
|
||||
doclist = ["GL Entry", "Sales Invoice", "Purchase Invoice", "Payment Entry", "BOM", "Sales Order", "Purchase Order", "Asset",
|
||||
"Stock Entry", "Budget", "Payroll Entry", "Delivery Note", "Sales Invoice Item", "Purchase Invoice Item", "Sales Order Item",
|
||||
"Purchase Order Item", "Journal Entry Account", "Material Request Item", "Delivery Note Item", "Purchase Receipt Item",
|
||||
"Purchase Order Item"]
|
||||
if doc.is_mandatory:
|
||||
df.update({
|
||||
"reqd": 1
|
||||
})
|
||||
|
||||
if self.is_mandatory:
|
||||
for doctype in doclist:
|
||||
|
||||
df = {
|
||||
"fieldname": doc.fieldname,
|
||||
"label": doc.label,
|
||||
"fieldtype": "Link",
|
||||
"options": doc.document_type,
|
||||
"insert_after": "cost_center"
|
||||
}
|
||||
|
||||
if doctype == "Budget":
|
||||
df.update({
|
||||
"reqd": 1
|
||||
"depends_on": "eval:doc.budget_against == '{0}'".format(doc.document_type)
|
||||
})
|
||||
|
||||
for doctype in doclist:
|
||||
create_custom_field(doctype, df)
|
||||
|
||||
df = {
|
||||
"fieldname": self.fieldname,
|
||||
"label": self.label,
|
||||
"fieldtype": "Link",
|
||||
"options": self.document_type,
|
||||
"insert_after": "cost_center"
|
||||
}
|
||||
property_setter = frappe.db.exists("Property Setter", "Budget-budget_against-options")
|
||||
|
||||
if doctype == "Budget":
|
||||
df.update({
|
||||
"depends_on": "eval:doc.budget_against == '{0}'".format(self.document_type)
|
||||
})
|
||||
if property_setter:
|
||||
property_setter_doc = frappe.get_doc("Property Setter", "Budget-budget_against-options")
|
||||
property_setter_doc.doc_type = 'Budget'
|
||||
property_setter_doc.doctype_or_field = "DocField"
|
||||
property_setter_doc.fiel_dname = "budget_against"
|
||||
property_setter_doc.property = "options"
|
||||
property_setter_doc.property_type = "Text"
|
||||
property_setter_doc.value = property_setter_doc.value + "\n" + doc.document_type
|
||||
property_setter_doc.save()
|
||||
|
||||
create_custom_field(doctype, df)
|
||||
|
||||
property_setter = frappe.db.exists("Property Setter", "Budget-budget_against-options")
|
||||
|
||||
if property_setter:
|
||||
property_setter_doc = frappe.get_doc("Property Setter", "Budget-budget_against-options")
|
||||
property_setter_doc.doc_type = 'Budget'
|
||||
property_setter_doc.doctype_or_field = "DocField"
|
||||
property_setter_doc.fiel_dname = "budget_against"
|
||||
property_setter_doc.property = "options"
|
||||
property_setter_doc.property_type = "Text"
|
||||
property_setter_doc.value = property_setter_doc.value + "\n" + self.document_type
|
||||
property_setter_doc.save()
|
||||
|
||||
frappe.clear_cache(doctype='Budget')
|
||||
else:
|
||||
frappe.get_doc({
|
||||
"doctype": "Property Setter",
|
||||
"doctype_or_field": "DocField",
|
||||
"doc_type": "Budget",
|
||||
"field_name": "budget_against",
|
||||
"property": "options",
|
||||
"property_type": "Text",
|
||||
"value": "\nCost Center\nProject\n" + self.document_type
|
||||
}).insert(ignore_permissions=True)
|
||||
frappe.clear_cache(doctype=doctype)
|
||||
frappe.clear_cache(doctype='Budget')
|
||||
else:
|
||||
create_custom_field(doctype, df)
|
||||
frappe.clear_cache(doctype=doctype)
|
||||
frappe.get_doc({
|
||||
"doctype": "Property Setter",
|
||||
"doctype_or_field": "DocField",
|
||||
"doc_type": "Budget",
|
||||
"field_name": "budget_against",
|
||||
"property": "options",
|
||||
"property_type": "Text",
|
||||
"value": "\nCost Center\nProject\n" + doc.document_type
|
||||
}).insert(ignore_permissions=True)
|
||||
frappe.clear_cache(doctype=doctype)
|
||||
else:
|
||||
create_custom_field(doctype, df)
|
||||
frappe.clear_cache(doctype=doctype)
|
||||
|
||||
def delete_accounting_dimension(self):
|
||||
doclist = ["GL Entry", "Sales Invoice", "Purchase Invoice", "Payment Entry", "BOM", "Sales Order", "Purchase Order", "Asset",
|
||||
"Stock Entry", "Budget", "Payroll Entry", "Delivery Note", "Sales Invoice Item", "Purchase Invoice Item", "Sales Order Item",
|
||||
"Purchase Order Item", "Journal Entry Account", "Material Request Item", "Delivery Note Item", "Purchase Receipt Item",
|
||||
"Purchase Order Item"]
|
||||
def delete_accounting_dimension(doc):
|
||||
doclist = get_doclist()
|
||||
|
||||
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 `tabCustom Field`
|
||||
WHERE fieldname = %s
|
||||
AND dt IN (%s)""" %
|
||||
('%s', ', '.join(['%s']* len(doclist))), tuple([doc.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))
|
||||
frappe.db.sql("""
|
||||
DELETE FROM `tabProperty Setter`
|
||||
WHERE field_name = %s
|
||||
AND doc_type IN (%s)""" %
|
||||
('%s', ', '.join(['%s']* len(doclist))), tuple([doc.fieldname] + doclist))
|
||||
|
||||
budget_against_property = frappe.get_doc("Property Setter", "Budget-budget_against-options")
|
||||
value_list = budget_against_property.value.split('\n')[3:]
|
||||
value_list.remove(self.document_type)
|
||||
budget_against_property = frappe.get_doc("Property Setter", "Budget-budget_against-options")
|
||||
value_list = budget_against_property.value.split('\n')[3:]
|
||||
value_list.remove(doc.document_type)
|
||||
|
||||
budget_against_property.value = "\nCost Center\nProject\n" + "\n".join(value_list)
|
||||
budget_against_property.save()
|
||||
budget_against_property.value = "\nCost Center\nProject\n" + "\n".join(value_list)
|
||||
budget_against_property.save()
|
||||
|
||||
for doctype in doclist:
|
||||
frappe.clear_cache(doctype=doctype)
|
||||
|
||||
def disable_dimension(doc):
|
||||
if doc.disable:
|
||||
df = {"read_only": 1}
|
||||
else:
|
||||
df = {"read_only": 0}
|
||||
|
||||
doclist = get_doclist()
|
||||
|
||||
for doctype in doclist:
|
||||
field = frappe.db.get_value("Custom Field", {"dt": doctype, "fieldname": doc.fieldname})
|
||||
if field:
|
||||
custom_field = frappe.get_doc("Custom Field", field)
|
||||
custom_field.update(df)
|
||||
custom_field.save()
|
||||
|
||||
frappe.clear_cache(doctype=doctype)
|
||||
|
||||
def get_doclist():
|
||||
doclist = ["GL Entry", "Sales Invoice", "Purchase Invoice", "Payment Entry", "Asset",
|
||||
"Expense Claim", "Stock Entry", "Budget", "Payroll Entry", "Delivery Note", "Sales Invoice Item", "Purchase Invoice Item",
|
||||
"Purchase Order Item", "Journal Entry Account", "Material Request Item", "Delivery Note Item", "Purchase Receipt Item",
|
||||
"Stock Entry Detail", "Payment Entry Deduction"]
|
||||
|
||||
return doclist
|
||||
|
||||
for doc in doclist:
|
||||
frappe.clear_cache(doctype=doc)
|
||||
|
||||
def get_accounting_dimensions():
|
||||
accounting_dimensions = frappe.get_all("Accounting Dimension", fields=["fieldname"])
|
||||
|
@ -1,12 +0,0 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from frappe import _
|
||||
|
||||
def get_data():
|
||||
return {
|
||||
'transactions': [
|
||||
{
|
||||
'label': _('Accounting Doctypes')
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue
Block a user