update add feed initial balance iot to database
This commit is contained in:
1 parent
3e1b38697d
commit
535a34266b
14 files changed
+175
-12
No files matched your search
@@ -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),
|
||||
),
|
||||
]
|
||||
@@ -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
|
||||
)
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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"])
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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",
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 337 KiB After Width: | Height: | Size: 339 KiB |
@@ -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,
|
||||
|
||||
@@ -486,6 +486,11 @@ const Dashboard: React.FC<Props> = ({ setView }) => {
|
||||
{formatNumber(feedInitialBalance)}
|
||||
</p>
|
||||
<p className="mt-1 text-xs text-gray-400">karung</p>
|
||||
{selectedCycle?.feed_initial_balance_iot != null && (
|
||||
<p className="mt-1 text-xs font-semibold text-purple-600">
|
||||
IoT: {formatNumber(selectedCycle.feed_initial_balance_iot)} karung
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -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);
|
||||
}}
|
||||
|
||||
@@ -25,7 +25,13 @@ const InitialBalanceCard: React.FC<InitialBalanceCardProps> = ({ cycle, onSaved
|
||||
setManualEntry(cycle.feed_initial_balance > 0 ? String(cycle.feed_initial_balance) : '');
|
||||
setComparison(null);
|
||||
setError(null);
|
||||
}, [cycle.id, cycle.feed_initial_balance, cycle.feed_initial_balance_date, cycle.start_date]);
|
||||
}, [
|
||||
cycle.id,
|
||||
cycle.feed_initial_balance,
|
||||
cycle.feed_initial_balance_date,
|
||||
cycle.feed_initial_balance_iot,
|
||||
cycle.start_date,
|
||||
]);
|
||||
|
||||
const manualValue = manualEntry === '' ? null : Number(manualEntry);
|
||||
|
||||
@@ -160,12 +166,19 @@ const InitialBalanceCard: React.FC<InitialBalanceCardProps> = ({ cycle, onSaved
|
||||
</button>
|
||||
</div>
|
||||
{cycle.feed_initial_balance > 0 && comparison == null && (
|
||||
<p className="text-sm text-gray-600">
|
||||
Saldo awal tersimpan:{' '}
|
||||
<strong className="text-blue-700">
|
||||
{formatNumber(cycle.feed_initial_balance)} karung
|
||||
</strong>
|
||||
</p>
|
||||
<div className="text-sm text-gray-600 flex flex-wrap items-center gap-2">
|
||||
<span>
|
||||
Saldo awal tersimpan:{' '}
|
||||
<strong className="text-blue-700">
|
||||
{formatNumber(cycle.feed_initial_balance)} karung
|
||||
</strong>
|
||||
</span>
|
||||
{cycle.feed_initial_balance_iot != null && (
|
||||
<span className="text-purple-700 font-semibold">
|
||||
(IoT: {formatNumber(cycle.feed_initial_balance_iot)} karung)
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
|
||||
@@ -71,6 +71,7 @@ export type Cycle = {
|
||||
doc_in_count: number;
|
||||
feed_initial_balance: number;
|
||||
feed_initial_balance_date?: string | null;
|
||||
feed_initial_balance_iot?: number | null;
|
||||
status: CycleStatus;
|
||||
proposed_end_date?: string | null;
|
||||
close_requested_at?: string | null;
|
||||
@@ -381,6 +382,7 @@ export type CycleWrite = {
|
||||
doc_in_count: number;
|
||||
feed_initial_balance?: number;
|
||||
feed_initial_balance_date?: string | null;
|
||||
feed_initial_balance_iot?: number | null;
|
||||
status?: CycleStatus;
|
||||
};
|
||||
|
||||
@@ -389,6 +391,7 @@ export type InitialBalanceResult = {
|
||||
date: string;
|
||||
feed_initial_balance: number;
|
||||
feed_initial_balance_date: string | null;
|
||||
feed_initial_balance_iot?: number | null;
|
||||
manual: number;
|
||||
manual_total: number;
|
||||
iot: number | null;
|
||||
|
||||
Reference in new issue
Block a user