How to Manage and Remove Specific Fee Categories in Fee Schedule?

How can I remove certain fee categories from the fee schedule when creating a fee schedule from the monthly fee structure?

For example, some fees are charged once a year and others are only charged in specific months. For instance, ID card fees are charged yearly, and catering charges are only applied when the school is in session, such as for 9 or 10 months.

Currently, there is no option to delete fee components in the fee schedule, as the table is read-only. How can I work around this limitation to manage fees that are charged annually or only in specific months?

you can adopt a few approaches to work around the limitations of the read-only table. Here are some possible solutions:

Approach 1: Use Conditional Scripting

You can use custom scripts to dynamically adjust the fee schedule based on specific conditions such as the month or whether the fee is an annual charge. This involves writing client-side scripts (JavaScript) or server-side scripts (Python) to modify the fee schedule.

Client-Side Script (JavaScript)

You can create a custom script that runs when you load the fee schedule form. This script can hide or disable fee components based on the current month or other conditions.

  1. Create a Custom Script for Fee Schedule Doctype:

    • Go to Custom Script in your ERPNext/Frappe.
    • Create a new custom script for the Fee Schedule doctype.
  2. Add the Following Script:

    frappe.ui.form.on('Fee Schedule', {
        refresh: function(frm) {
            // Get the current month
            var current_month = new Date().getMonth() + 1; // JavaScript months are 0-11
    
            // Iterate through each fee component
            frm.doc.fee_structure.forEach(function(row) {
                // Example condition: remove or hide annual fees if not the start of the year
                if (row.fee_category == 'ID Card' && current_month != 1) {
                    // Hide the fee component
                    frappe.model.set_value(row.doctype, row.name, 'hidden', true);
                }
    
                // Example condition: only show catering fees during session months (e.g., Jan to Oct)
                if (row.fee_category == 'Catering' && (current_month < 1 || current_month > 10)) {
                    // Hide the fee component
                    frappe.model.set_value(row.doctype, row.name, 'hidden', true);
                }
            });
    
            frm.refresh_fields('fee_structure');
        }
    });
    

Server-Side Script (Python)

If more complex logic is required, you might need a server-side script to adjust the fee schedule before saving.

  1. Create a Server-Side Script:

    • Create a new server-side script (or add to an existing custom app).
  2. Modify the Fee Schedule Before Saving:

    import frappe
    from frappe.model.document import Document
    from datetime import datetime
    
    class CustomFeeSchedule(Document):
        def before_save(self):
            current_month = datetime.now().month
            
            fee_components = self.get('fee_structure')
            updated_components = []
    
            for component in fee_components:
                # Example condition: exclude annual fees if not the start of the year
                if component.fee_category == 'ID Card' and current_month != 1:
                    continue
    
                # Example condition: only include catering fees during session months (e.g., Jan to Oct)
                if component.fee_category == 'Catering' and (current_month < 1 or current_month > 10):
                    continue
    
                updated_components.append(component)
            
            self.set('fee_structure', updated_components)
    
  3. Override the Fee Schedule Doctype with Custom Logic:

    • You might need to override the existing Fee Schedule logic in ERPNext to incorporate your custom logic.

Approach 2: Create Custom Fields and Scripts

Create custom fields in the Fee Schedule doctype to mark specific fee components as annual or monthly, then use these fields in scripts to determine visibility.

  1. Add Custom Fields:

    • Add a checkbox field is_annual and specific_months (a table of months) in the Fee Schedule doctype.
  2. Modify Fee Schedule Based on Custom Fields:

    • Use the same scripting approach to hide/show components based on these custom fields.

Approach 3: Use Multiple Fee Schedules

Create separate fee schedules for annual and monthly fees. This way, you can manually manage when each fee schedule is applied without modifying the table directly.

  1. Create Separate Fee Schedules:

    • One for annual fees and another for monthly fees.
  2. Apply Fees Programmatically:

    • Write a script to automatically apply the correct fee schedule based on the current month or specific conditions.