@@ -301,25 +301,30 @@ def get_applicable_components(gratuity_rule):
301
301
302
302
303
303
def get_total_applicable_component_amount (employee , applicable_earnings_component , gratuity_rule ):
304
- sal_slip = get_last_salary_slip (employee )
305
- if not sal_slip :
306
- frappe .throw (_ ("No Salary Slip is found for Employee: {0}" ).format (bold (employee )))
307
- component_and_amounts = frappe .get_all (
308
- "Salary Detail" ,
309
- filters = {
310
- "docstatus" : 1 ,
311
- "parent" : sal_slip ,
312
- "parentfield" : "earnings" ,
313
- "salary_component" : ("in" , applicable_earnings_component ),
314
- },
315
- fields = ["amount" ],
316
- )
317
- total_applicable_components_amount = 0
318
- if not len (component_and_amounts ):
319
- frappe .throw (_ ("No Applicable Component is present in last month salary slip" ))
320
- for data in component_and_amounts :
321
- total_applicable_components_amount += data .amount
322
- return total_applicable_components_amount
304
+ salary_slip = get_last_salary_slip (employee )
305
+ if not salary_slip :
306
+ frappe .throw (_ ("No Salary Slip found for Employee: {0}" ).format (bold (employee )))
307
+
308
+ # consider full payment days for calculation as last month's salary slip
309
+ # might have less payment days as per attendance, making it non-deterministic
310
+ salary_slip .payment_days = salary_slip .total_working_days
311
+ salary_slip .process_salary_structure ()
312
+
313
+ total_amount = 0
314
+ component_found = False
315
+ for row in salary_slip .earnings :
316
+ if row .salary_component in applicable_earnings_component :
317
+ total_amount += flt (row .amount )
318
+ component_found = True
319
+
320
+ if not component_found :
321
+ frappe .throw (
322
+ _ ("No applicable Earning component found in last salary slip for Gratuity Rule: {0}" ).format (
323
+ bold (get_link_to_form ("Gratuity Rule" , gratuity_rule ))
324
+ )
325
+ )
326
+
327
+ return total_amount
323
328
324
329
325
330
def calculate_amount_based_on_current_slab (
@@ -354,10 +359,9 @@ def get_salary_structure(employee):
354
359
)[0 ].salary_structure
355
360
356
361
357
- def get_last_salary_slip (employee ) :
358
- salary_slips = frappe .get_list (
359
- "Salary Slip" , filters = {"employee" : employee , "docstatus" : 1 }, order_by = "start_date desc"
362
+ def get_last_salary_slip (employee : str ) -> dict | None :
363
+ salary_slip = frappe .db . get_value (
364
+ "Salary Slip" , {"employee" : employee , "docstatus" : 1 }, order_by = "start_date desc"
360
365
)
361
- if not salary_slips :
362
- return
363
- return salary_slips [0 ].name
366
+ if salary_slip :
367
+ return frappe .get_doc ("Salary Slip" , salary_slip )
0 commit comments