fix age to start at 0 on start date of cycle instead of 1

This commit is contained in:
Alberto-Audrix committed 2026-09-02 09:58:59 +07:00
1 parent 3411ed4391
commit 54eb277c16
7 files changed
+33 -10

No files matched your search

+4 -2
View File
@@ -116,13 +116,15 @@ class Cycle(models.Model):
@property
def current_day(self) -> int:
"""0-based cycle age: start_date is day 0."""
from apps.operations.services.visibility import visible_through_date
effective = visible_through_date()
if effective < self.start_date:
return 0
elapsed = (effective - self.start_date).days + 1
return min(max(elapsed, 1), self.total_days)
elapsed = (effective - self.start_date).days
max_day = max(self.total_days - 1, 0)
return min(max(elapsed, 0), max_day)
@property
def visible_through_date(self) -> date:
+24
View File
@@ -128,6 +128,30 @@ class FarmAPITests(TestCase):
self.assertEqual(Flock.objects.filter(kandang=self.kandang).count(), 1)
self.assertEqual(flock.kandang_id, self.kandang.pk)
def test_current_day_is_zero_based_from_start_date(self):
start = date(2026, 9, 2)
cycle = Cycle.objects.create(
kandang=self.kandang,
total_days=35,
start_date=start,
end_date=start + timedelta(days=34),
doc_in_weight=40,
doc_in_count=9000,
status=Cycle.STATUS_ACTIVE,
)
with patch(
"apps.operations.services.visibility.visible_through_date",
return_value=start,
):
self.assertEqual(cycle.current_day, 0)
with patch(
"apps.operations.services.visibility.visible_through_date",
return_value=start + timedelta(days=1),
):
self.assertEqual(cycle.current_day, 1)
def test_list_flocks_by_kandang(self):
self.client.force_authenticate(self.user)
Flock.objects.create(flock_name="Lantai 1", kandang=self.kandang)