diff --git a/backend/apps/farms/migrations/0021_cycle_feed_initial_balance_iot.py b/backend/apps/farms/migrations/0021_cycle_feed_initial_balance_iot.py
new file mode 100644
index 0000000..46de760
--- /dev/null
+++ b/backend/apps/farms/migrations/0021_cycle_feed_initial_balance_iot.py
@@ -0,0 +1,16 @@
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("farms", "0020_flock_iot_api_url"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="cycle",
+ name="feed_initial_balance_iot",
+ field=models.IntegerField(blank=True, null=True),
+ ),
+ ]
diff --git a/backend/apps/farms/models.py b/backend/apps/farms/models.py
index 3acec71..3a819af 100644
--- a/backend/apps/farms/models.py
+++ b/backend/apps/farms/models.py
@@ -115,6 +115,7 @@ class Cycle(models.Model):
doc_in_count = models.IntegerField()
feed_initial_balance = models.IntegerField(default=0)
feed_initial_balance_date = models.DateField(null=True, blank=True)
+ feed_initial_balance_iot = models.IntegerField(null=True, blank=True)
status = models.CharField(
max_length=30, choices=STATUS_CHOICES, default=STATUS_ACTIVE, db_index=True
)
diff --git a/backend/apps/farms/serializers.py b/backend/apps/farms/serializers.py
index e943de4..36cefb1 100644
--- a/backend/apps/farms/serializers.py
+++ b/backend/apps/farms/serializers.py
@@ -113,6 +113,7 @@ class CycleSerializer(PkAsIdMixin, serializers.ModelSerializer):
"doc_in_count",
"feed_initial_balance",
"feed_initial_balance_date",
+ "feed_initial_balance_iot",
"status",
"proposed_end_date",
"close_requested_at",
@@ -125,6 +126,7 @@ class CycleSerializer(PkAsIdMixin, serializers.ModelSerializer):
"kandang_name",
"feed_initial_balance",
"feed_initial_balance_date",
+ "feed_initial_balance_iot",
"end_date",
"status",
"proposed_end_date",
diff --git a/backend/apps/farms/services/pusat_export.py b/backend/apps/farms/services/pusat_export.py
index be4e832..bc97102 100644
--- a/backend/apps/farms/services/pusat_export.py
+++ b/backend/apps/farms/services/pusat_export.py
@@ -186,7 +186,15 @@ def _enrich_cycle_initial_balance(cycle: Cycle, row: dict[str, Any]) -> dict[str
row["feed_initial_balance_sync_error"] = None
return row
- iot_in, sync_error = pull_initial_balance_iot(cycle, balance_date)
+ if cycle.feed_initial_balance_iot is not None:
+ iot_in = cycle.feed_initial_balance_iot
+ sync_error = None
+ else:
+ iot_in, sync_error = pull_initial_balance_iot(cycle, balance_date)
+ if iot_in is not None:
+ cycle.feed_initial_balance_iot = iot_in
+ cycle.save(update_fields=["feed_initial_balance_iot", "updated_at"])
+
manual = cycle.feed_initial_balance
row["feed_initial_balance_iot"] = iot_in
row["feed_initial_balance_accuracy"] = initial_balance_accuracy(manual, iot_in)
diff --git a/backend/apps/farms/tests.py b/backend/apps/farms/tests.py
index d3ec34e..60bc6c5 100644
--- a/backend/apps/farms/tests.py
+++ b/backend/apps/farms/tests.py
@@ -394,9 +394,13 @@ class FarmAPITests(TestCase):
)
self.assertEqual(response.status_code, 200, response.data)
self.assertEqual(response.data["iot"], 115)
+ self.assertEqual(response.data["feed_initial_balance_iot"], 115)
self.assertEqual(response.data["accuracy"], 95.8)
mock_pull.assert_called_once_with(cycle, start)
+ cycle.refresh_from_db()
+ self.assertEqual(cycle.feed_initial_balance_iot, 115)
+
@patch("apps.farms.views.pull_initial_balance_iot", return_value=(112, None))
def test_compare_initial_balance_returns_iot_and_accuracy(self, mock_pull):
start = date(2026, 8, 27)
@@ -420,3 +424,31 @@ class FarmAPITests(TestCase):
self.assertEqual(response.data["iot"], 112)
self.assertEqual(response.data["accuracy"], 93.3)
mock_pull.assert_called_once_with(cycle, start)
+
+ @patch("apps.farms.views.pull_initial_balance_iot", return_value=(None, None))
+ def test_compare_initial_balance_falls_back_to_stored_iot_when_history_expired(self, mock_pull):
+ start = date(2026, 8, 27)
+ 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,
+ feed_initial_balance=120,
+ feed_initial_balance_date=start,
+ feed_initial_balance_iot=115,
+ status=Cycle.STATUS_ACTIVE,
+ )
+
+ self.client.force_authenticate(self.user)
+ response = self.client.get(
+ f"/api/v1/cycles/{cycle.pk}/initial-balance-compare/",
+ {"date": start.isoformat(), "manual": 120},
+ )
+ self.assertEqual(response.status_code, 200, response.data)
+ self.assertEqual(response.data["iot"], 115)
+ self.assertEqual(response.data["feed_initial_balance_iot"], 115)
+ self.assertEqual(response.data["accuracy"], 95.8)
+ self.assertIsNone(response.data["sync_error"])
+
diff --git a/backend/apps/farms/tests_pusat_export.py b/backend/apps/farms/tests_pusat_export.py
index d5cec59..f17819e 100644
--- a/backend/apps/farms/tests_pusat_export.py
+++ b/backend/apps/farms/tests_pusat_export.py
@@ -176,6 +176,37 @@ class PusatExportTests(TestCase):
self.assertIsNone(cycle_row["feed_initial_balance_sync_error"])
mock_pull.assert_called_once_with(self.cycle, today)
+ def test_ops_export_falls_back_to_cycle_iot_when_history_unavailable(self):
+ self._auth()
+ today = timezone.localdate()
+ self.cycle.feed_initial_balance = 120
+ self.cycle.feed_initial_balance_date = today
+ self.cycle.feed_initial_balance_iot = 115
+ self.cycle.save(
+ update_fields=[
+ "feed_initial_balance",
+ "feed_initial_balance_date",
+ "feed_initial_balance_iot",
+ "updated_at",
+ ]
+ )
+ with patch(
+ "apps.farms.services.pusat_export.pull_initial_balance_iot",
+ return_value=(None, None),
+ ) as mock_pull:
+ response = self.client.get(
+ "/api/v1/pusat/export/",
+ {"site_id": self.site.pk, "cycle_id": self.cycle.pk},
+ )
+ self.assertEqual(response.status_code, 200, response.data)
+ cycle_row = response.data["sites"][0]["kandangs"][0]["cycles"][0]
+ self.assertEqual(cycle_row["feed_initial_balance"], 120)
+ self.assertEqual(cycle_row["feed_initial_balance_date"], today.isoformat())
+ self.assertEqual(cycle_row["feed_initial_balance_iot"], 115)
+ self.assertEqual(cycle_row["feed_initial_balance_accuracy"], 95.8)
+ self.assertIsNone(cycle_row["feed_initial_balance_sync_error"])
+ mock_pull.assert_not_called()
+
def test_ops_export_skips_iot_pull_without_balance_date(self):
self._auth()
with patch(
diff --git a/backend/apps/farms/views.py b/backend/apps/farms/views.py
index a3df76c..48d7144 100644
--- a/backend/apps/farms/views.py
+++ b/backend/apps/farms/views.py
@@ -343,6 +343,23 @@ class CycleViewSet(viewsets.ModelViewSet):
)
iot_in, sync_error = pull_initial_balance_iot(cycle, balance_date)
+ if (
+ iot_in is None
+ and cycle.feed_initial_balance_iot is not None
+ and cycle.feed_initial_balance_date
+ and balance_date == cycle.feed_initial_balance_date
+ ):
+ iot_in = cycle.feed_initial_balance_iot
+ sync_error = None
+ elif (
+ iot_in is not None
+ and cycle.feed_initial_balance_iot is None
+ and cycle.feed_initial_balance_date
+ and balance_date == cycle.feed_initial_balance_date
+ ):
+ cycle.feed_initial_balance_iot = iot_in
+ cycle.save(update_fields=["feed_initial_balance_iot", "updated_at"])
+
return Response(
{
"cycle": cycle.pk,
@@ -353,6 +370,7 @@ class CycleViewSet(viewsets.ModelViewSet):
if cycle.feed_initial_balance_date
else None
),
+ "feed_initial_balance_iot": cycle.feed_initial_balance_iot,
"manual": feed_in_manual,
"manual_total": feed_in_manual,
"iot": iot_in,
@@ -399,15 +417,30 @@ class CycleViewSet(viewsets.ModelViewSet):
previous_date = cycle.feed_initial_balance_date
previous_balance = cycle.feed_initial_balance
+ # Pull IoT "in" at the balance date from karung-web-admin /api/combined.
+ iot_in, sync_error = pull_initial_balance_iot(cycle, balance_date)
+ if iot_in is not None:
+ cycle.feed_initial_balance_iot = iot_in
+ elif "feed_initial_balance_iot" in request.data:
+ custom_iot = request.data.get("feed_initial_balance_iot")
+ cycle.feed_initial_balance_iot = int(custom_iot) if custom_iot is not None else None
+ elif previous_date == balance_date and cycle.feed_initial_balance_iot is not None:
+ iot_in = cycle.feed_initial_balance_iot
+ sync_error = None
+ else:
+ cycle.feed_initial_balance_iot = None
+
cycle.feed_initial_balance = feed_in_manual
cycle.feed_initial_balance_date = balance_date
cycle.save(
- update_fields=["feed_initial_balance", "feed_initial_balance_date", "updated_at"]
+ update_fields=[
+ "feed_initial_balance",
+ "feed_initial_balance_date",
+ "feed_initial_balance_iot",
+ "updated_at",
+ ]
)
- # Pull IoT "in" at the balance date from karung-web-admin /api/combined.
- iot_in, sync_error = pull_initial_balance_iot(cycle, balance_date)
-
cleanup_opening_balance_feed_rows(
cycle,
previous_date=previous_date,
@@ -429,6 +462,7 @@ class CycleViewSet(viewsets.ModelViewSet):
if cycle.feed_initial_balance_date
else None
),
+ "feed_initial_balance_iot": cycle.feed_initial_balance_iot,
"manual": feed_in_manual,
"manual_total": feed_in_manual,
"iot": iot_in,
diff --git a/backend/database/ERD_dashboard.json b/backend/database/ERD_dashboard.json
index 5cc5cbf..e93899e 100644
--- a/backend/database/ERD_dashboard.json
+++ b/backend/database/ERD_dashboard.json
@@ -391,6 +391,22 @@
"parent": "-jXtr_My4sWfuMSxO9_i-1",
"label": "feed_initial_balance_date date NOT NULL"
},
+ {
+ "id": "-jXtr_My4sWfuMSxO9_i-4",
+ "type": "node",
+ "parent": "IRJI-bqvK2qqk487lMMM-10"
+ },
+ {
+ "id": "-jXtr_My4sWfuMSxO9_i-5",
+ "type": "node",
+ "parent": "-jXtr_My4sWfuMSxO9_i-4"
+ },
+ {
+ "id": "-jXtr_My4sWfuMSxO9_i-6",
+ "type": "node",
+ "parent": "-jXtr_My4sWfuMSxO9_i-4",
+ "label": "feed_initial_balance_iot int"
+ },
{
"id": "IRJI-bqvK2qqk487lMMM-51",
"type": "node",
diff --git a/backend/database/ERD_dashboard.png b/backend/database/ERD_dashboard.png
index e3f6589..142138f 100644
Binary files a/backend/database/ERD_dashboard.png and b/backend/database/ERD_dashboard.png differ
diff --git a/backend/database/erd_schema.sql b/backend/database/erd_schema.sql
index 9a9ca33..41afada 100644
--- a/backend/database/erd_schema.sql
+++ b/backend/database/erd_schema.sql
@@ -124,6 +124,7 @@ CREATE TABLE IF NOT EXISTS cycles (
doc_in_count INTEGER NOT NULL,
feed_initial_balance INTEGER NOT NULL DEFAULT 0,
feed_initial_balance_date DATE,
+ feed_initial_balance_iot INTEGER,
status VARCHAR(30) NOT NULL DEFAULT 'active', -- active | pending_close | closed
proposed_end_date DATE, -- staff proposed close date (cleared after decision)
close_requested_at TIMESTAMP WITH TIME ZONE,
diff --git a/components/Dashboard.tsx b/components/Dashboard.tsx
index 3849406..cc89bd6 100644
--- a/components/Dashboard.tsx
+++ b/components/Dashboard.tsx
@@ -486,6 +486,11 @@ const Dashboard: React.FC
karung
+ {selectedCycle?.feed_initial_balance_iot != null && ( ++ IoT: {formatNumber(selectedCycle.feed_initial_balance_iot)} karung +
+ )} diff --git a/components/FeedSackCountingPage.tsx b/components/FeedSackCountingPage.tsx index d684bff..31b6665 100644 --- a/components/FeedSackCountingPage.tsx +++ b/components/FeedSackCountingPage.tsx @@ -398,6 +398,7 @@ const FeedSackCountingPage: React.FC<{ onSettings: () => void }> = ({ onSettings updateCycle(saved.cycle, { feed_initial_balance: saved.feed_initial_balance, feed_initial_balance_date: saved.feed_initial_balance_date, + feed_initial_balance_iot: saved.feed_initial_balance_iot, }); void reload(selectedCycle.id); }} diff --git a/components/feedSack/InitialBalanceCard.tsx b/components/feedSack/InitialBalanceCard.tsx index cefeeb0..ee6e81e 100644 --- a/components/feedSack/InitialBalanceCard.tsx +++ b/components/feedSack/InitialBalanceCard.tsx @@ -25,7 +25,13 @@ const InitialBalanceCard: React.FC- Saldo awal tersimpan:{' '} - - {formatNumber(cycle.feed_initial_balance)} karung - -
+