Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Leave Encashment calculations #31062

Merged
merged 2 commits into from
May 23, 2022

Conversation

ruchamahabal
Copy link
Member

@ruchamahabal ruchamahabal commented May 18, 2022

Problem:

The Leave Balance calculation in Leave Encashment is incorrect:

get_leave_allocation doesn't fetch allocation.from_date:

def get_leave_allocation(self):
leave_allocation = frappe.db.sql(
"""select name, to_date, total_leaves_allocated, carry_forwarded_leaves_count from `tabLeave Allocation` where '{0}'
between from_date and to_date and docstatus=1 and leave_type='{1}'
and employee= '{2}'""".format(
self.encashment_date or getdate(nowdate()), self.leave_type, self.employee
),
as_dict=1,
) # nosec

but from_date is being accessed in get_unused_leaves for getting encashable leave balance which is None:

self.leave_balance = (
allocation.total_leaves_allocated
- allocation.carry_forwarded_leaves_count
- get_unused_leaves(self.employee, self.leave_type, allocation.from_date, self.encashment_date)
)

Also, the above formula is not correct for calculating leave balance. It's subtracting unused leaves from allocated leaves. Instead, it should remove used leaves from leaves allocated to get the encashable balance. So if 10 leaves are allocated 6 leaves are used, balance should be 4 but this does 10 - 4 = 6. 🤷🏻‍♀️

This feature seems to be broken since the time it was introduced. It also has a test but the test was incorrect causing no failures.

Fix:

Fetch from_date in the query

Change the leave balance calculation formula to:

self.leave_balance = (
	allocation.total_leaves_allocated
	- allocation.carry_forwarded_leaves_count
	# adding this because the function returns a -ve number
	+ get_leaves_for_period(
		self.employee, self.leave_type, allocation.from_date, self.encashment_date
	)
)

Subtract the leaves taken from leaves allocated to get the balance leaves that can be encashed.

Closes #28020, Closes #23448, Closes #24706

@ruchamahabal ruchamahabal requested a review from pateljannat as a code owner May 18, 2022 08:49
@ruchamahabal
Copy link
Member Author

ruchamahabal commented May 18, 2022

Also, I don't know if this is how it should be. The Encashment threshold days setting seems to be adding some functionality that should work exactly opposite to what it's doing right now maybe?

As per docs and functionality:

image

Which organization has a threshold above which you can encash? Like if you have 15 leaves, you cannot encash 5 but you can encash 10?

image

Expected (Maybe):

Shouldn't it be like; you can only encash up to 5 leaves because 5 is the threshold. Currently, it's very confusing for the user.
Maybe we should have minimum and maximum thresholds as separate fields. I am not sure about how HRs use this.

cc: @nabinhait

@codecov
Copy link

codecov bot commented May 18, 2022

Codecov Report

Merging #31062 (5fe1616) into develop (3019f93) will decrease coverage by 0.09%.
The diff coverage is 100.00%.

@@             Coverage Diff             @@
##           develop   #31062      +/-   ##
===========================================
- Coverage    63.04%   62.94%   -0.10%     
===========================================
  Files          986      986              
  Lines        67343    67345       +2     
===========================================
- Hits         42457    42392      -65     
- Misses       24886    24953      +67     
Impacted Files Coverage Δ
...xt/hr/doctype/leave_encashment/leave_encashment.py 78.82% <100.00%> (+0.51%) ⬆️
...unts/report/purchase_register/purchase_register.py 34.21% <0.00%> (-44.08%) ⬇️
...ext/accounts/report/balance_sheet/balance_sheet.py 36.36% <0.00%> (-21.82%) ⬇️
...e_purchase_register/item_wise_purchase_register.py 57.42% <0.00%> (-20.80%) ⬇️
...em_wise_sales_register/item_wise_sales_register.py 51.13% <0.00%> (-12.22%) ⬇️
...saction/incorrect_balance_qty_after_transaction.py 88.37% <0.00%> (-9.31%) ⬇️
...t/accounts/report/sales_register/sales_register.py 70.76% <0.00%> (-7.02%) ⬇️
...urity_shortfall/process_loan_security_shortfall.py 93.75% <0.00%> (-6.25%) ⬇️
erpnext/hr/doctype/job_offer/job_offer.py 82.92% <0.00%> (-4.88%) ⬇️
...it_and_loss_statement/profit_and_loss_statement.py 71.42% <0.00%> (-3.90%) ⬇️
... and 29 more

- incorrect leave balance and encashable days in encashment
@ruchamahabal ruchamahabal force-pushed the fix-leave-encashment branch from 3c707fb to 3c3b26f Compare May 18, 2022 09:10
@ruchamahabal
Copy link
Member Author

Expected (Maybe):

Shouldn't it be like; you can only encash up to 5 leaves because 5 is the threshold. Currently, it's very confusing for the user.
Maybe we should have minimum and maximum thresholds as separate fields. I am not sure about how HRs use this.

Will look into this in a separate PR.

@ruchamahabal ruchamahabal merged commit 34e238c into frappe:develop May 23, 2022
@ruchamahabal ruchamahabal deleted the fix-leave-encashment branch May 23, 2022 04:43
mergify bot pushed a commit that referenced this pull request May 23, 2022
ruchamahabal added a commit that referenced this pull request May 23, 2022
Co-authored-by: Rucha Mahabal <ruchamahabal2@gmail.com>
frappe-pr-bot pushed a commit that referenced this pull request May 26, 2022
# [13.31.0](v13.30.0...v13.31.0) (2022-05-26)

### Bug Fixes

* Account currency validation for first transaction ([228f10b](228f10b))
* Add party account validation for journal entry ([7f853b1](7f853b1))
* always update item_name for stock entry (backport [#31068](#31068)) ([#31071](#31071)) ([f519dc6](f519dc6))
* change project's actual_start_date fieldtype from Data to Date (backport [#31085](#31085)) ([#31135](#31135)) ([8c2f203](8c2f203))
* Chart data for monthly periodicity in Cash Flow report ([#31039](#31039)) ([f14e9b7](f14e9b7))
* corrective job card creation (backport [#31083](#31083)) ([#31084](#31084)) ([c17c260](c17c260))
* don't capture payments with invoice_id as donations ([168a9d4](168a9d4))
* employee advance status update on return via additional salary ([d59c3d2](d59c3d2))
* Handle missing HSN Codes ([ce3a21e](ce3a21e))
* Healthcare module accounting test cases ([09a42a1](09a42a1))
* **India:** Async issue in company address trigger ([2ea3318](2ea3318))
* **india:** error while parsing e-invoice ([#31061](#31061)) ([1461d66](1461d66))
* **india:** eway bill cancel api is disabled ([#31060](#31060)) ([95491e1](95491e1))
* Job Card excess transfer behaviour (backport [#31054](#31054)) ([#31096](#31096)) ([3984f04](3984f04))
* Leave Encashment calculations (backport [#31062](#31062)) ([#31091](#31091)) ([ba76b64](ba76b64))
* Loan Doc query in Bank Reconciliation Statement ([611d1af](611d1af))
* Loan repayment entries for payroll payable account ([ea6d754](ea6d754))
* multiple entries for same payment term ([90b1147](90b1147))
* Party account validation in JV ([d10c2e5](d10c2e5))
* payments duplicate on pos closing entry (backport [#30976](#30976)) ([#31005](#31005)) ([0efbabe](0efbabe))
* **pos:** paid amount calculation for multicurrency invoice ([#31122](#31122)) ([98eb7da](98eb7da))
* remove bad default for Membership From Date ([34928d2](34928d2))
* Remove validation from Journal Entry ([4ca6cdc](4ca6cdc))
* replace document states with list settings ([78e9e66](78e9e66))
* timesheet fetching in sales invoice ([216c32f](216c32f))
* Use directly <a> and style it as button instead of using button ([0ab9fc0](0ab9fc0))

### Features

* **Employee Advance:** add 'Returned' and 'Partly Claimed and Returned' status ([42e7a86](42e7a86))

### Reverts

* Revert "fix: Add party account validation for journal entry" ([9d43a90](9d43a90))
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
1 participant