Skip to content

Commit

Permalink
Add unique constraint and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
CaitBarnard committed Dec 10, 2024
1 parent 91ee1a8 commit 692d321
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
44 changes: 44 additions & 0 deletions core/migrations/0017_alter_attrition_cost_centre_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Generated by Django 5.1.3 on 2024-12-10 11:10

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("core", "0016_alter_attrition_options"),
("costcentre", "0008_alter_simplehistoryarchivedcostcentre_options_and_more"),
]

operations = [
migrations.AlterField(
model_name="attrition",
name="cost_centre",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.PROTECT,
to="costcentre.costcentre",
unique=True,
),
),
migrations.AlterField(
model_name="attrition",
name="financial_year",
field=models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
to="core.financialyear",
unique=True,
),
),
migrations.AlterField(
model_name="payuplift",
name="financial_year",
field=models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
to="core.financialyear",
unique=True,
),
),
]
9 changes: 6 additions & 3 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ def periods(self) -> list[float]:
return [getattr(self, month) for month in MONTHS]

financial_year = models.ForeignKey(
FinancialYear,
on_delete=models.PROTECT,
FinancialYear, on_delete=models.PROTECT, unique=True
)
apr = models.FloatField(default=1.0)
may = models.FloatField(default=1.0)
Expand All @@ -124,7 +123,11 @@ class Meta:
verbose_name_plural = "attrition"

cost_centre = models.ForeignKey(
"costcentre.CostCentre", on_delete=models.PROTECT, null=True, blank=True
"costcentre.CostCentre",
on_delete=models.PROTECT,
null=True,
blank=True,
unique=True,
)


Expand Down
14 changes: 6 additions & 8 deletions payroll/services/payroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,8 @@ def payroll_forecast_report(
pay_uplift_obj = PayUplift.objects.filter(financial_year=financial_year).first()
attrition_obj = get_attrition_instance(financial_year, cost_centre)

pay_uplift = (
np.array(pay_uplift_obj.periods) if pay_uplift_obj is not None else np.ones(12)
)
attrition = (
np.array(attrition_obj.periods) if attrition_obj is not None else np.ones(12)
)
pay_uplift = np.array(pay_uplift_obj.periods) if pay_uplift_obj else np.ones(12)
attrition = np.array(attrition_obj.periods) if attrition_obj else np.ones(12)
attrition_accumulate = np.array(list(accumulate(attrition, operator.mul)))

for employee in employee_qs.iterator():
Expand Down Expand Up @@ -124,12 +120,14 @@ def payroll_forecast_report(
)


def get_attrition_instance(financial_year, cost_centre):
def get_attrition_instance(
financial_year: FinancialYear, cost_centre: CostCentre
) -> Attrition | None:
instance = Attrition.objects.filter(
financial_year=financial_year, cost_centre=cost_centre
).first()

if instance is not None:
if instance:
return instance

return Attrition.objects.filter(
Expand Down

0 comments on commit 692d321

Please sign in to comment.