How can we restrict users from making backdate entry but after every 10th date? For example, there is a fee for students for July so users can only make entries till 10 Aug after the 10th it restricts users.
I have tried this code.
frappe.ui.form.on("Fees", "validate", function(frm) {
var today = get_today();
var current_month = frappe.datetime.str_to_obj(today).getMonth();
var posting_date_month = frappe.datetime.str_to_obj(frm.doc.posting_date).getMonth();
if (posting_date_month < current_month) {
msgprint(__("Cannot select a date in the previous month."));
validated = false;
}
});
I recently encountered a similar situation in my project. My requirement was to allow adding one record per day and to prevent updating past records. I’ve shared the code, and I hope it will be helpful.
import frappe
from frappe.model.document import Document
from datetime import datetime
class ChefClosingChecklist(Document):
def before_insert(self):
branch_id = self.branch_id
user_name = self.user_name
current_date = datetime.today().date()
rec_count = self.get_the_record_count(branch_id, user_name, current_date)
if (rec_count > 0):
frappe.throw("You are limited to adding just one record per day.")
def on_update(self):
current_date = datetime.today().date()
doc_save_date = datetime.strptime(self.chef_close_date, '%Y-%m-%d').date()
if (current_date == doc_save_date):
pass
else:
frappe.throw("Editing records from the past is not permitted")
def get_the_record_count(self, branch_id, user_name, date_obj):
rec_count = frappe.db.count('Chef Closing Checklist', filters={
'user_name': user_name,
'branch_id': branch_id,
'chef_close_date': date_obj
})
return rec_count
1 Like
Thanks but I want to add in the client script the code I have mentioned above is working but just for the month’s validation I want to modify the same code but with both month and day validation.
Can you help in that case?
Understood. I hope the code below is useful to you.
frappe.ui.form.on("teststandard", "validate", function(frm) {
var current_date = frappe.datetime.now_date();
var current_month = frappe.datetime.str_to_obj(current_date).getMonth()+1; // JavaScript months are 0-11
var current_date_part = frappe.datetime.str_to_obj(current_date).getDate();
var posting_date = frappe.datetime.str_to_obj(frm.doc.posting_date);
var posting_date_month = posting_date.getMonth()+1;// JavaScript months are 0-11
var posting_date_date_part = new Date(posting_date).getDate();
console.log('current_date ',current_date);
console.log('current_month',current_month);
console.log('current_date_part',current_date_part);
console.log('posting_date',posting_date);
console.log('posting_date_month',posting_date_month);
console.log('posting_date_date_part',posting_date_date_part);
if (posting_date_month < current_month) {
msgprint("Cannot select a date in the previous month.");
validated = false;
}
});
1 Like