refactor: validate due date code and message according to doctype (#45126)
* refactor: change message of date comparision and refactor code * refactor: commonify function call for sales and purchase invoice * refactor: remove redundant mandatory error validation
This commit is contained in:
@@ -621,16 +621,25 @@ def get_due_date_from_template(template_name, posting_date, bill_date):
|
|||||||
return due_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):
|
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:
|
else:
|
||||||
|
validate_due_date_with_template(posting_date, due_date, bill_date, template_name)
|
||||||
|
|
||||||
|
|
||||||
|
def validate_due_date_with_template(posting_date, due_date, bill_date, template_name):
|
||||||
if not template_name:
|
if not template_name:
|
||||||
return
|
return
|
||||||
|
|
||||||
default_due_date = get_due_date_from_template(template_name, posting_date, bill_date).strftime(
|
default_due_date = format(get_due_date_from_template(template_name, posting_date, bill_date))
|
||||||
"%Y-%m-%d"
|
|
||||||
)
|
|
||||||
|
|
||||||
if not default_due_date:
|
if not default_due_date:
|
||||||
return
|
return
|
||||||
@@ -641,14 +650,12 @@ def validate_due_date(posting_date, due_date, bill_date=None, template_name=None
|
|||||||
)
|
)
|
||||||
if is_credit_controller:
|
if is_credit_controller:
|
||||||
msgprint(
|
msgprint(
|
||||||
_("Note: Due / Reference Date exceeds allowed customer credit days by {0} day(s)").format(
|
_("Note: Due Date exceeds allowed customer credit days by {0} day(s)").format(
|
||||||
date_diff(due_date, default_due_date)
|
date_diff(due_date, default_due_date)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
frappe.throw(
|
frappe.throw(_("Due Date cannot be after {0}").format(formatdate(default_due_date)))
|
||||||
_("Due / Reference Date cannot be after {0}").format(formatdate(default_due_date))
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
|
|||||||
@@ -671,21 +671,15 @@ class AccountsController(TransactionBase):
|
|||||||
if frappe.flags.in_import and getdate(self.due_date) < getdate(posting_date):
|
if frappe.flags.in_import and getdate(self.due_date) < getdate(posting_date):
|
||||||
self.due_date = posting_date
|
self.due_date = posting_date
|
||||||
|
|
||||||
elif self.doctype == "Sales Invoice":
|
elif self.doctype in ["Sales Invoice", "Purchase Invoice"]:
|
||||||
if not self.due_date:
|
bill_date = self.bill_date if self.doctype == "Purchase Invoice" else None
|
||||||
frappe.throw(_("Due Date is mandatory"))
|
|
||||||
|
|
||||||
validate_due_date(
|
validate_due_date(
|
||||||
posting_date,
|
posting_date=posting_date,
|
||||||
self.due_date,
|
due_date=self.due_date,
|
||||||
self.payment_terms_template,
|
bill_date=bill_date,
|
||||||
)
|
template_name=self.payment_terms_template,
|
||||||
elif self.doctype == "Purchase Invoice":
|
doctype=self.doctype,
|
||||||
validate_due_date(
|
|
||||||
posting_date,
|
|
||||||
self.due_date,
|
|
||||||
self.bill_date,
|
|
||||||
self.payment_terms_template,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def set_price_list_currency(self, buying_or_selling):
|
def set_price_list_currency(self, buying_or_selling):
|
||||||
|
|||||||
@@ -1037,6 +1037,14 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe
|
|||||||
due_date() {
|
due_date() {
|
||||||
// due_date is to be changed, payment terms template and/or payment schedule must
|
// 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
|
// 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 (
|
if (
|
||||||
this.frm.doc.due_date &&
|
this.frm.doc.due_date &&
|
||||||
!this.frm.updating_party_details &&
|
!this.frm.updating_party_details &&
|
||||||
|
|||||||
Reference in New Issue
Block a user