Skip to content

Commit

Permalink
Merge pull request #37032 from HarryPaulo/auth-rule-based-on-item-group
Browse files Browse the repository at this point in the history
feat: created "based on" Item Group to specify a different percentage…
  • Loading branch information
deepeshgarg007 authored Sep 13, 2023
2 parents 1db05c8 + da54ab5 commit 7711744
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


class AuthorizationControl(TransactionBase):
def get_appr_user_role(self, det, doctype_name, total, based_on, condition, item, company):
def get_appr_user_role(self, det, doctype_name, total, based_on, condition, master_name, company):
amt_list, appr_users, appr_roles = [], [], []
users, roles = "", ""
if det:
Expand Down Expand Up @@ -47,11 +47,11 @@ def get_appr_user_role(self, det, doctype_name, total, based_on, condition, item
frappe.msgprint(_("Not authroized since {0} exceeds limits").format(_(based_on)))
frappe.throw(_("Can be approved by {0}").format(comma_or(appr_roles + appr_users)))

def validate_auth_rule(self, doctype_name, total, based_on, cond, company, item=""):
def validate_auth_rule(self, doctype_name, total, based_on, cond, company, master_name=""):
chk = 1
add_cond1, add_cond2 = "", ""
if based_on == "Itemwise Discount":
add_cond1 += " and master_name = " + frappe.db.escape(cstr(item))
if based_on in ["Itemwise Discount", "Item Group wise Discount"]:
add_cond1 += " and master_name = " + frappe.db.escape(cstr(master_name))
itemwise_exists = frappe.db.sql(
"""select value from `tabAuthorization Rule`
where transaction = %s and value <= %s
Expand All @@ -71,11 +71,11 @@ def validate_auth_rule(self, doctype_name, total, based_on, cond, company, item=

if itemwise_exists:
self.get_appr_user_role(
itemwise_exists, doctype_name, total, based_on, cond + add_cond1, item, company
itemwise_exists, doctype_name, total, based_on, cond + add_cond1, master_name, company
)
chk = 0
if chk == 1:
if based_on == "Itemwise Discount":
if based_on in ["Itemwise Discount", "Item Group wise Discount"]:
add_cond2 += " and ifnull(master_name,'') = ''"

appr = frappe.db.sql(
Expand All @@ -95,7 +95,9 @@ def validate_auth_rule(self, doctype_name, total, based_on, cond, company, item=
(doctype_name, total, based_on),
)

self.get_appr_user_role(appr, doctype_name, total, based_on, cond + add_cond2, item, company)
self.get_appr_user_role(
appr, doctype_name, total, based_on, cond + add_cond2, master_name, company
)

def bifurcate_based_on_type(self, doctype_name, total, av_dis, based_on, doc_obj, val, company):
add_cond = ""
Expand Down Expand Up @@ -123,6 +125,12 @@ def bifurcate_based_on_type(self, doctype_name, total, av_dis, based_on, doc_obj
self.validate_auth_rule(
doctype_name, t.discount_percentage, based_on, add_cond, company, t.item_code
)
elif based_on == "Item Group wise Discount":
if doc_obj:
for t in doc_obj.get("items"):
self.validate_auth_rule(
doctype_name, t.discount_percentage, based_on, add_cond, company, t.item_group
)
else:
self.validate_auth_rule(doctype_name, auth_value, based_on, add_cond, company)

Expand All @@ -148,6 +156,7 @@ def validate_approving_authority(self, doctype_name, company, total, doc_obj="")
"Average Discount",
"Customerwise Discount",
"Itemwise Discount",
"Item Group wise Discount",
]

# Check for authorization set for individual user
Expand All @@ -166,7 +175,7 @@ def validate_approving_authority(self, doctype_name, company, total, doc_obj="")

# Remove user specific rules from global authorization rules
for r in based_on:
if r in final_based_on and r != "Itemwise Discount":
if r in final_based_on and not r in ["Itemwise Discount", "Item Group wise Discount"]:
final_based_on.remove(r)

# Check for authorization set on particular roles
Expand Down Expand Up @@ -194,7 +203,7 @@ def validate_approving_authority(self, doctype_name, company, total, doc_obj="")

# Remove role specific rules from global authorization rules
for r in based_on:
if r in final_based_on and r != "Itemwise Discount":
if r in final_based_on and not r in ["Itemwise Discount", "Item Group wise Discount"]:
final_based_on.remove(r)

# Check for global authorization
Expand Down
10 changes: 10 additions & 0 deletions erpnext/setup/doctype/authorization_rule/authorization_rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ frappe.ui.form.on("Authorization Rule", {
} else if(frm.doc.based_on==="Itemwise Discount") {
unhide_field("master_name");
frm.set_value("customer_or_item", "Item");
} else if(frm.doc.based_on==="Item Group wise Discount") {
unhide_field("master_name");
frm.set_value("customer_or_item", "Item Group");
} else {
frm.set_value("customer_or_item", "");
frm.set_value("master_name", "");
Expand Down Expand Up @@ -81,6 +84,13 @@ cur_frm.fields_dict['master_name'].get_query = function(doc) {
doctype: "Item",
query: "erpnext.controllers.queries.item_query"
}
else if (doc.based_on==="Item Group wise Discount")
return {
doctype: "Item Group",
filters: {
"is_group": 0
}
}
else
return {
filters: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,22 @@
"label": "Based On",
"oldfieldname": "based_on",
"oldfieldtype": "Select",
"options": "\nGrand Total\nAverage Discount\nCustomerwise Discount\nItemwise Discount\nNot Applicable",
"options": "\nGrand Total\nAverage Discount\nCustomerwise Discount\nItemwise Discount\nItem Group wise Discount\nNot Applicable",
"reqd": 1
},
{
"fieldname": "customer_or_item",
"fieldtype": "Select",
"hidden": 1,
"label": "Customer or Item",
"options": "Customer\nItem",
"options": "Customer\nItem\nItem Group",
"read_only": 1
},
{
"fieldname": "master_name",
"fieldtype": "Dynamic Link",
"in_list_view": 1,
"label": "Customer / Item Name",
"label": "Customer / Item / Item Group",
"oldfieldname": "master_name",
"oldfieldtype": "Link",
"options": "customer_or_item"
Expand Down Expand Up @@ -162,7 +162,7 @@
"icon": "fa fa-shield",
"idx": 1,
"links": [],
"modified": "2022-07-01 11:19:45.643991",
"modified": "2023-09-11 10:29:02.863193",
"modified_by": "Administrator",
"module": "Setup",
"name": "Authorization Rule",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def validate_rule(self):
"Average Discount",
"Customerwise Discount",
"Itemwise Discount",
"Item Group wise Discount",
]:
frappe.throw(
_("Cannot set authorization on basis of Discount for {0}").format(self.transaction)
Expand Down

0 comments on commit 7711744

Please sign in to comment.