Calculate Available Leaves in Python or JS

Hello All

I created a new Custom Doctype, and I’m trying to calculate the current Leave Balance (Available Leaves) of an Employee.
I tried fetching the data from the “Leave Allocation” Doctype but it hadn’t updated the “Total Allocated Leaves” of the Employee.
My goal is to fetch the Available Leaves of the “Leave Type” in a Specific “Leave Period”, as shown in the image below…

There are submitted Leave Applications on the same Employee before but not updated in the “Leave Allocation” so the Leave Balance should be less…

Can anyone please help me on this issue?
P.S: I’m a novice in Python coding :smiley:

Thank you

I believe I solved my own issue after digging in Frappe Framework ( Database API (frappeframework.com)) and a few trial runs in Python coding.

Here’s what I wrote:

type or paste code here
if doc.docstatus == 0:
    lle_list = frappe.db.get_list('Leave Ledger Entry', filters={
        'employee': doc.employee,
        'leave_type': doc.leave_type
    },
    fields=['leaves']
    )

    sum = 0
    for a in lle_list:
        var = a.leaves
        sum = sum + var

    doc.db_set('docfield_name', sum)
    # doc.save()

Hopefully this code will help someone who needs it.