From b629356b7c9f9ef1c846ab6e4fdc53c81173a64d Mon Sep 17 00:00:00 2001 From: Ejaaz Khan Date: Tue, 7 Jan 2025 11:54:10 +0530 Subject: [PATCH 1/3] refactor: change message of date comparision and refactor code --- erpnext/accounts/party.py | 51 +++++++++++--------- erpnext/controllers/accounts_controller.py | 16 +++--- erpnext/public/js/controllers/transaction.js | 8 +++ 3 files changed, 46 insertions(+), 29 deletions(-) diff --git a/erpnext/accounts/party.py b/erpnext/accounts/party.py index 22fa4b3b8233..4ac16d57b210 100644 --- a/erpnext/accounts/party.py +++ b/erpnext/accounts/party.py @@ -621,34 +621,41 @@ def get_due_date_from_template(template_name, posting_date, bill_date): return due_date -def validate_due_date(posting_date, due_date, bill_date=None, template_name=None): +def validate_due_date(posting_date, due_date, bill_date=None, template_name=None, doctype=None): if getdate(due_date) < getdate(posting_date): - frappe.throw(_("Due Date cannot be before Posting / Supplier Invoice Date")) + doctype_date = "Date" + if doctype == "Purchase Invoice": + doctype_date = "Supplier Invoice Date" + + if doctype == "Sales Invoice": + doctype_date = "Posting Date" + + frappe.throw(_("Due Date cannot be before {0}").format(doctype_date)) else: - if not template_name: - return + validate_due_date_with_template(posting_date, due_date, bill_date, template_name) - default_due_date = get_due_date_from_template(template_name, posting_date, bill_date).strftime( - "%Y-%m-%d" - ) - if not default_due_date: - return +def validate_due_date_with_template(posting_date, due_date, bill_date, template_name): + if not template_name: + return - if default_due_date != posting_date and getdate(due_date) > getdate(default_due_date): - is_credit_controller = ( - frappe.db.get_single_value("Accounts Settings", "credit_controller") in frappe.get_roles() - ) - if is_credit_controller: - msgprint( - _("Note: Due / Reference Date exceeds allowed customer credit days by {0} day(s)").format( - date_diff(due_date, default_due_date) - ) - ) - else: - frappe.throw( - _("Due / Reference Date cannot be after {0}").format(formatdate(default_due_date)) + default_due_date = format(get_due_date_from_template(template_name, posting_date, bill_date)) + + if not default_due_date: + return + + if default_due_date != posting_date and getdate(due_date) > getdate(default_due_date): + is_credit_controller = ( + frappe.db.get_single_value("Accounts Settings", "credit_controller") in frappe.get_roles() + ) + if is_credit_controller: + msgprint( + _("Note: Due Date exceeds allowed customer credit days by {0} day(s)").format( + date_diff(due_date, default_due_date) ) + ) + else: + frappe.throw(_("Due Date cannot be after {0}").format(formatdate(default_due_date))) @frappe.whitelist() diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 7d65e5daa650..44625b540f09 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -676,16 +676,18 @@ def validate_due_date(self): frappe.throw(_("Due Date is mandatory")) validate_due_date( - posting_date, - self.due_date, - self.payment_terms_template, + posting_date=posting_date, + due_date=self.due_date, + template_name=self.payment_terms_template, + doctype=self.doctype, ) elif self.doctype == "Purchase Invoice": validate_due_date( - posting_date, - self.due_date, - self.bill_date, - self.payment_terms_template, + posting_date=posting_date, + due_date=self.due_date, + bill_date=self.bill_date, + template_name=self.payment_terms_template, + doctype=self.doctype, ) def set_price_list_currency(self, buying_or_selling): diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js index fb28919d24c7..a04a8c9f7769 100644 --- a/erpnext/public/js/controllers/transaction.js +++ b/erpnext/public/js/controllers/transaction.js @@ -1040,6 +1040,14 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe due_date() { // due_date is to be changed, payment terms template and/or payment schedule must // be removed as due_date is automatically changed based on payment terms + + // if there is only one row in payment schedule child table, set its due date as the due date + if (this.frm.doc.payment_schedule.length == 1){ + this.frm.doc.payment_schedule[0].due_date = this.frm.doc.due_date; + this.frm.refresh_field("payment_schedule"); + return + } + if ( this.frm.doc.due_date && !this.frm.updating_party_details && From 985da0e117017ffbc1be38422572f99b4ab70b88 Mon Sep 17 00:00:00 2001 From: Ejaaz Khan Date: Tue, 7 Jan 2025 17:50:21 +0530 Subject: [PATCH 2/3] refactor: commonify function call for sales and purchase invoice --- erpnext/controllers/accounts_controller.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 44625b540f09..76324f103431 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -671,21 +671,16 @@ def validate_due_date(self): if frappe.flags.in_import and getdate(self.due_date) < getdate(posting_date): self.due_date = posting_date - elif self.doctype == "Sales Invoice": - if not self.due_date: + elif self.doctype in ["Sales Invoice", "Purchase Invoice"]: + if self.doctype == "Sales Invoice" and not self.due_date: frappe.throw(_("Due Date is mandatory")) + bill_date = self.bill_date if self.doctype == "Purchase Invoice" else None + validate_due_date( posting_date=posting_date, due_date=self.due_date, - template_name=self.payment_terms_template, - doctype=self.doctype, - ) - elif self.doctype == "Purchase Invoice": - validate_due_date( - posting_date=posting_date, - due_date=self.due_date, - bill_date=self.bill_date, + bill_date=bill_date, template_name=self.payment_terms_template, doctype=self.doctype, ) From 7a01975086872ba5defa6f34a3e585afb112a714 Mon Sep 17 00:00:00 2001 From: Ejaaz Khan Date: Wed, 8 Jan 2025 10:34:04 +0530 Subject: [PATCH 3/3] refactor: remove redundant mandatory error validation --- erpnext/controllers/accounts_controller.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index 76324f103431..0b23f1bcfa61 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -672,9 +672,6 @@ def validate_due_date(self): self.due_date = posting_date elif self.doctype in ["Sales Invoice", "Purchase Invoice"]: - if self.doctype == "Sales Invoice" and not self.due_date: - frappe.throw(_("Due Date is mandatory")) - bill_date = self.bill_date if self.doctype == "Purchase Invoice" else None validate_due_date(