Set Permissions Based on the Start and End Date of the Payroll Period

Hello everyone,

I am using the project doctype and I have added a field called “Payroll Period” which is linked to the Payroll Period list. I want to automatically be able to update the permissions for each Payroll Period so users can only view Payroll Periods spanning 30 days.

Let’s say I have several Payroll Periods. There are several that ended one year ago, and several that are fairly new. How do I program it so that users can only view the payroll periods spanning 30 days?

I know I can use the User Permissions Manager, but to update that every time I want to change the permissions would be very tedious.

Here is the custom script that I tried to use, but didn’t work.

frappe.ui.form.on('Timesheet',  {
    validate: function(frm) {
        if(doc.payroll_period >= + 30 | doc.payroll_period <= - 30) {
            msgprint('You are not allowed to access those dates');
            validated = false;

Any help is appreciated