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

Duplicated Deductions in Payroll Calculation #541

Open
unreal0901 opened this issue Feb 28, 2025 · 1 comment
Open

Duplicated Deductions in Payroll Calculation #541

unreal0901 opened this issue Feb 28, 2025 · 1 comment

Comments

@unreal0901
Copy link
Contributor

Environment

  • Python Version: 3.11.9
  • Operating System: Windows 11 Pro

Issue Description

When calculating payroll deductions, duplicate entries appear in the results for employees with specific deduction assignments.
I'm not sure if this behavior is intended, but I believe each deduction should be applied only once.

Additionally, there is another issue: when creating a deduction and specifying it for a particular employee, the Include_all_active_employees boolean is automatically set to True in the database by default.

Image

Files

payroll -> methods -> payslip_calc.py

Url

    path("create-payslip", component_views.create_payslip, name="create-payslip"),

Steps to Reproduce

  1. Create an employee.
  2. Create a contract.
  3. Attach a filing to the contract.
  4. Create a deduction.
  5. Apply the deduction to specific employees.
  6. Generate a payslip – duplicate deductions appear for each employee.
  7. When installments are present, a TypeError occurs.

Debug Output

Specific Deductions:
ID: 8
Conditional Deductions:
Active Employee Deductions:
ID: 8
All Deductions:
ID: 8
ID: 8

The output shows the same deduction (ID: 8) appearing multiple times in the final result.

Proposed Solution

  1. Fix duplicate deductions

    • Add .distinct() when combining querysets:
    deductions = (specific_deductions | conditional_deduction | active_employee_deduction).distinct()
  2. Fix queryset merge errors

    • Add .distinct() to both installment querysets:
    installments = deductions.filter(is_installment=True).distinct()
    • Use IDs instead of directly merging querysets:
    pretax_ids = list(pretax_deductions["installments"].values_list("id", flat=True))
    posttax_ids = list(post_tax_deductions["installments"].values_list("id", flat=True))
    all_ids = list(set(pretax_ids + posttax_ids))
    installments = models.Deduction.objects.filter(id__in=all_ids)
  3. Ensure consistent use of .distinct()

    • Apply .distinct() across all relevant querysets.
    • Use distinct querysets in both calculate_pre_tax_deduction and calculate_post_tax_deduction to prevent TypeErrors when merging non-unique querysets with unique querysets.
@unreal0901 unreal0901 changed the title Duplicated Deductions in Payroll Calculation and QuerySet Merge Error Duplicated Deductions in Payroll Calculation Mar 1, 2025
@horilla-opensource
Copy link
Owner

Hi @unreal0901 ,

Thank you for reporting this issue.

We acknowledge the problem of duplicate deduction entries and the unexpected behavior of the Include_all_active_employees field. Your detailed explanation and proposed solution are greatly appreciated.

We'll review the suggested fixes, including the use of .distinct() across querysets and the approach to merge deductions by IDs. This will help ensure accurate payroll calculations and prevent TypeErrors.

We'll update you once the fix is implemented. Thank you for contributing to improving Horilla!

Best Regards,
Team Horilla

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants