From 0dfba375266516830557d12d476834f6ef8f25a2 Mon Sep 17 00:00:00 2001 From: Felipe Orellana Date: Tue, 8 Jun 2021 15:09:50 +0000 Subject: [PATCH 1/3] fix: Added missing date and enabled validations --- .../doctype/coupon_code/coupon_code.json | 10 +++++++++- erpnext/controllers/selling_controller.py | 20 ++++++++++++++++--- erpnext/patches.txt | 3 ++- .../patches/v13_0/set_coupon_code_enabled.py | 5 +++++ 4 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 erpnext/patches/v13_0/set_coupon_code_enabled.py diff --git a/erpnext/accounts/doctype/coupon_code/coupon_code.json b/erpnext/accounts/doctype/coupon_code/coupon_code.json index 82e68c01cb9d..aec11c58afea 100644 --- a/erpnext/accounts/doctype/coupon_code/coupon_code.json +++ b/erpnext/accounts/doctype/coupon_code/coupon_code.json @@ -7,6 +7,7 @@ "editable_grid": 1, "engine": "InnoDB", "field_order": [ + "enabled", "coupon_name", "coupon_type", "customer", @@ -136,9 +137,16 @@ "fieldtype": "Link", "label": "Customer Group", "options": "Customer Group" + }, + { + "default": "1", + "fieldname": "enabled", + "fieldtype": "Check", + "in_list_view": 1, + "label": "Enabled" } ], - "modified": "2020-12-17 00:02:05.148366", + "modified": "2021-06-08 09:44:21.974315", "modified_by": "forellana@digithinkit.com", "module": "Accounts", "name": "Coupon Code", diff --git a/erpnext/controllers/selling_controller.py b/erpnext/controllers/selling_controller.py index 221d915bd943..274614b6d986 100644 --- a/erpnext/controllers/selling_controller.py +++ b/erpnext/controllers/selling_controller.py @@ -3,7 +3,7 @@ from __future__ import unicode_literals import frappe -from frappe.utils import cint, flt, cstr, comma_or +from frappe.utils import cint, flt, cstr, comma_or, getdate, today from frappe import _, throw from erpnext.stock.get_item_details import get_bin_details from erpnext.stock.utils import get_incoming_rate @@ -55,11 +55,25 @@ def validate_coupon(self): if self.meta.get_field("coupon_code"): if self.coupon_code: - coupon_customer = frappe.get_value("Coupon Code", self.coupon_code, "allowed_customer") - coupon_customer_group = frappe.get_value("Coupon Code", self.coupon_code, "allowed_customer_group") + coupon = frappe.get_doc("Coupon Code", self.coupon_code) + + coupon_customer = coupon.allowed_customer + coupon_customer_group = coupon.allowed_customer_group + + if coupon.valid_from: + if coupon.valid_from > getdate(today()): + frappe.throw(_("Sorry, this coupon code's validity has not started"), title=_("Coupon Error")) + elif coupon.valid_upto: + if coupon.valid_upto < getdate(today()): + frappe.throw(_("Sorry, this coupon code's validity has expired"), title=_("Coupon Error")) + elif coupon.used >= coupon.maximum_use: + frappe.throw(_("Sorry, this coupon code is no longer valid"), title=_("Coupon Error")) party_name = self.customer_name if self.doctype != "Quotation" else self.party_name + if not coupon.enabled: + frappe.throw(_("Coupon \"{}\" is not a valid coupon").format(self.coupon_code)) + if self.doctype == "Quotation" and self.quotation_to != "Customer": if coupon_customer or coupon_customer_group: frappe.throw(_("Coupon \"{}\" can only be used with \"Customer\" party types").format(self.coupon_code)) diff --git a/erpnext/patches.txt b/erpnext/patches.txt index b859067d1ca3..b8c4e216c87d 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -720,4 +720,5 @@ erpnext.patches.v13_0.make_mobile_application_access_roles #2021-04-08 erpnext.patches.v13_0.delete_company_custom_fields execute:frappe.reload_doc('setup', 'doctype', 'company', force=True) erpnext.patches.v13_0.set_is_used #01-04-2021 -erpnext.patches.v13_0.set_coa_batch_in_batch \ No newline at end of file +erpnext.patches.v13_0.set_coa_batch_in_batch +erpnext.patches.v13_0.set_coupon_code_enabled \ No newline at end of file diff --git a/erpnext/patches/v13_0/set_coupon_code_enabled.py b/erpnext/patches/v13_0/set_coupon_code_enabled.py new file mode 100644 index 000000000000..6d734bf3b45a --- /dev/null +++ b/erpnext/patches/v13_0/set_coupon_code_enabled.py @@ -0,0 +1,5 @@ +import frappe + +def execute(): + # Default existing coupons to enabled + frappe.db.sql("UPDATE `tabCoupon Code` SET enabled = 1") From f3647fa4774c45e9958f098aad0c3dbaeb99f6b7 Mon Sep 17 00:00:00 2001 From: Felipe Orellana Date: Tue, 8 Jun 2021 15:27:25 +0000 Subject: [PATCH 2/3] feat: change enabled validation order --- erpnext/controllers/selling_controller.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/erpnext/controllers/selling_controller.py b/erpnext/controllers/selling_controller.py index 274614b6d986..8f9c032ea72e 100644 --- a/erpnext/controllers/selling_controller.py +++ b/erpnext/controllers/selling_controller.py @@ -60,6 +60,9 @@ def validate_coupon(self): coupon_customer = coupon.allowed_customer coupon_customer_group = coupon.allowed_customer_group + if not coupon.enabled: + frappe.throw(_("Coupon \"{}\" is not a valid coupon").format(self.coupon_code)) + if coupon.valid_from: if coupon.valid_from > getdate(today()): frappe.throw(_("Sorry, this coupon code's validity has not started"), title=_("Coupon Error")) @@ -71,9 +74,6 @@ def validate_coupon(self): party_name = self.customer_name if self.doctype != "Quotation" else self.party_name - if not coupon.enabled: - frappe.throw(_("Coupon \"{}\" is not a valid coupon").format(self.coupon_code)) - if self.doctype == "Quotation" and self.quotation_to != "Customer": if coupon_customer or coupon_customer_group: frappe.throw(_("Coupon \"{}\" can only be used with \"Customer\" party types").format(self.coupon_code)) From 61d1fba8e83d996873bc7f42fa6cf58db711e682 Mon Sep 17 00:00:00 2001 From: Felipe Orellana <1656249+DeveloperMetal@users.noreply.github.com> Date: Tue, 8 Jun 2021 11:31:16 -0400 Subject: [PATCH 3/3] Update erpnext/patches/v13_0/set_coupon_code_enabled.py Co-authored-by: sahil28297 <37302950+sahil28297@users.noreply.github.com> --- erpnext/patches/v13_0/set_coupon_code_enabled.py | 1 + 1 file changed, 1 insertion(+) diff --git a/erpnext/patches/v13_0/set_coupon_code_enabled.py b/erpnext/patches/v13_0/set_coupon_code_enabled.py index 6d734bf3b45a..9e54b3884ed6 100644 --- a/erpnext/patches/v13_0/set_coupon_code_enabled.py +++ b/erpnext/patches/v13_0/set_coupon_code_enabled.py @@ -2,4 +2,5 @@ def execute(): # Default existing coupons to enabled + frappe.reload_doc("accounts", "doctype", "coupon_code", force=1) frappe.db.sql("UPDATE `tabCoupon Code` SET enabled = 1")