施肥散布実績機能を実装し運搬・作業記録・在庫連携を追加
This commit is contained in:
@@ -2,6 +2,7 @@ from django.contrib import admin
|
|||||||
from .models import (
|
from .models import (
|
||||||
Fertilizer, FertilizationPlan, FertilizationEntry,
|
Fertilizer, FertilizationPlan, FertilizationEntry,
|
||||||
DeliveryPlan, DeliveryGroup, DeliveryGroupField, DeliveryTrip, DeliveryTripItem,
|
DeliveryPlan, DeliveryGroup, DeliveryGroupField, DeliveryTrip, DeliveryTripItem,
|
||||||
|
SpreadingSession, SpreadingSessionItem,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -17,7 +18,7 @@ class FertilizationEntryInline(admin.TabularInline):
|
|||||||
|
|
||||||
@admin.register(FertilizationPlan)
|
@admin.register(FertilizationPlan)
|
||||||
class FertilizationPlanAdmin(admin.ModelAdmin):
|
class FertilizationPlanAdmin(admin.ModelAdmin):
|
||||||
list_display = ['name', 'year', 'variety']
|
list_display = ['name', 'year', 'variety', 'is_confirmed', 'confirmed_at']
|
||||||
list_filter = ['year']
|
list_filter = ['year']
|
||||||
inlines = [FertilizationEntryInline]
|
inlines = [FertilizationEntryInline]
|
||||||
|
|
||||||
@@ -60,3 +61,15 @@ class DeliveryGroupAdmin(admin.ModelAdmin):
|
|||||||
class DeliveryTripAdmin(admin.ModelAdmin):
|
class DeliveryTripAdmin(admin.ModelAdmin):
|
||||||
list_display = ['delivery_plan', 'order', 'name', 'date']
|
list_display = ['delivery_plan', 'order', 'name', 'date']
|
||||||
inlines = [DeliveryTripItemInline]
|
inlines = [DeliveryTripItemInline]
|
||||||
|
|
||||||
|
|
||||||
|
class SpreadingSessionItemInline(admin.TabularInline):
|
||||||
|
model = SpreadingSessionItem
|
||||||
|
extra = 0
|
||||||
|
|
||||||
|
|
||||||
|
@admin.register(SpreadingSession)
|
||||||
|
class SpreadingSessionAdmin(admin.ModelAdmin):
|
||||||
|
list_display = ['year', 'date', 'name']
|
||||||
|
list_filter = ['year', 'date']
|
||||||
|
inlines = [SpreadingSessionItemInline]
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
# Generated by Django 5.0 on 2026-03-17 08:49
|
||||||
|
|
||||||
|
import django.db.models.deletion
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('fertilizer', '0007_delivery_models'),
|
||||||
|
('fields', '0006_e1c_chusankan_17_fields'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='SpreadingSession',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('year', models.IntegerField(verbose_name='年度')),
|
||||||
|
('date', models.DateField(verbose_name='散布日')),
|
||||||
|
('name', models.CharField(blank=True, max_length=100, verbose_name='名前')),
|
||||||
|
('notes', models.TextField(blank=True, default='', verbose_name='備考')),
|
||||||
|
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||||
|
('updated_at', models.DateTimeField(auto_now=True)),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
'verbose_name': '散布実績',
|
||||||
|
'verbose_name_plural': '散布実績',
|
||||||
|
'ordering': ['-date', '-id'],
|
||||||
|
},
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='fertilizationentry',
|
||||||
|
name='actual_bags',
|
||||||
|
field=models.DecimalField(blank=True, decimal_places=4, max_digits=10, null=True, verbose_name='実績袋数'),
|
||||||
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='SpreadingSessionItem',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('actual_bags', models.DecimalField(decimal_places=4, max_digits=10, verbose_name='実散布袋数')),
|
||||||
|
('planned_bags_snapshot', models.DecimalField(decimal_places=4, max_digits=10, verbose_name='計画袋数スナップショット')),
|
||||||
|
('delivered_bags_snapshot', models.DecimalField(decimal_places=4, max_digits=10, verbose_name='運搬済み袋数スナップショット')),
|
||||||
|
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||||
|
('updated_at', models.DateTimeField(auto_now=True)),
|
||||||
|
('fertilizer', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='fertilizer.fertilizer', verbose_name='肥料')),
|
||||||
|
('field', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='fields.field', verbose_name='圃場')),
|
||||||
|
('session', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='items', to='fertilizer.spreadingsession', verbose_name='散布実績')),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
'verbose_name': '散布実績明細',
|
||||||
|
'verbose_name_plural': '散布実績明細',
|
||||||
|
'ordering': ['field__display_order', 'field__id', 'fertilizer__name'],
|
||||||
|
'unique_together': {('session', 'field', 'fertilizer')},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -69,6 +69,13 @@ class FertilizationEntry(models.Model):
|
|||||||
Fertilizer, on_delete=models.PROTECT, verbose_name='肥料'
|
Fertilizer, on_delete=models.PROTECT, verbose_name='肥料'
|
||||||
)
|
)
|
||||||
bags = models.DecimalField(max_digits=8, decimal_places=2, verbose_name='袋数')
|
bags = models.DecimalField(max_digits=8, decimal_places=2, verbose_name='袋数')
|
||||||
|
actual_bags = models.DecimalField(
|
||||||
|
max_digits=10,
|
||||||
|
decimal_places=4,
|
||||||
|
null=True,
|
||||||
|
blank=True,
|
||||||
|
verbose_name='実績袋数',
|
||||||
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = '施肥エントリ'
|
verbose_name = '施肥エントリ'
|
||||||
@@ -179,3 +186,63 @@ class DeliveryTripItem(models.Model):
|
|||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.trip} / {self.field.name} / {self.fertilizer.name}: {self.bags}袋"
|
return f"{self.trip} / {self.field.name} / {self.fertilizer.name}: {self.bags}袋"
|
||||||
|
|
||||||
|
|
||||||
|
class SpreadingSession(models.Model):
|
||||||
|
"""散布日単位の実績"""
|
||||||
|
year = models.IntegerField(verbose_name='年度')
|
||||||
|
date = models.DateField(verbose_name='散布日')
|
||||||
|
name = models.CharField(max_length=100, blank=True, verbose_name='名前')
|
||||||
|
notes = models.TextField(blank=True, default='', verbose_name='備考')
|
||||||
|
created_at = models.DateTimeField(auto_now_add=True)
|
||||||
|
updated_at = models.DateTimeField(auto_now=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
verbose_name = '散布実績'
|
||||||
|
verbose_name_plural = '散布実績'
|
||||||
|
ordering = ['-date', '-id']
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
label = self.name.strip() or f'{self.date}'
|
||||||
|
return f'{self.year} {label}'
|
||||||
|
|
||||||
|
|
||||||
|
class SpreadingSessionItem(models.Model):
|
||||||
|
"""散布実績明細:圃場×肥料ごとの実績"""
|
||||||
|
session = models.ForeignKey(
|
||||||
|
SpreadingSession,
|
||||||
|
on_delete=models.CASCADE,
|
||||||
|
related_name='items',
|
||||||
|
verbose_name='散布実績',
|
||||||
|
)
|
||||||
|
field = models.ForeignKey(
|
||||||
|
'fields.Field', on_delete=models.PROTECT, verbose_name='圃場'
|
||||||
|
)
|
||||||
|
fertilizer = models.ForeignKey(
|
||||||
|
Fertilizer, on_delete=models.PROTECT, verbose_name='肥料'
|
||||||
|
)
|
||||||
|
actual_bags = models.DecimalField(max_digits=10, decimal_places=4, verbose_name='実散布袋数')
|
||||||
|
planned_bags_snapshot = models.DecimalField(
|
||||||
|
max_digits=10,
|
||||||
|
decimal_places=4,
|
||||||
|
verbose_name='計画袋数スナップショット',
|
||||||
|
)
|
||||||
|
delivered_bags_snapshot = models.DecimalField(
|
||||||
|
max_digits=10,
|
||||||
|
decimal_places=4,
|
||||||
|
verbose_name='運搬済み袋数スナップショット',
|
||||||
|
)
|
||||||
|
created_at = models.DateTimeField(auto_now_add=True)
|
||||||
|
updated_at = models.DateTimeField(auto_now=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
verbose_name = '散布実績明細'
|
||||||
|
verbose_name_plural = '散布実績明細'
|
||||||
|
unique_together = [['session', 'field', 'fertilizer']]
|
||||||
|
ordering = ['field__display_order', 'field__id', 'fertilizer__name']
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return (
|
||||||
|
f'{self.session} / {self.field.name} / '
|
||||||
|
f'{self.fertilizer.name}: {self.actual_bags}袋'
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,8 +1,22 @@
|
|||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
|
from django.db.models import Sum
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
from apps.workrecords.services import sync_delivery_work_record
|
||||||
from .models import (
|
from .models import (
|
||||||
Fertilizer, FertilizationPlan, FertilizationEntry,
|
DeliveryGroup,
|
||||||
DeliveryPlan, DeliveryGroup, DeliveryGroupField, DeliveryTrip, DeliveryTripItem,
|
DeliveryGroupField,
|
||||||
|
DeliveryPlan,
|
||||||
|
DeliveryTrip,
|
||||||
|
DeliveryTripItem,
|
||||||
|
FertilizationEntry,
|
||||||
|
FertilizationPlan,
|
||||||
|
Fertilizer,
|
||||||
|
SpreadingSession,
|
||||||
|
SpreadingSessionItem,
|
||||||
)
|
)
|
||||||
|
from .services import sync_actual_bags_for_pairs, sync_spreading_session_side_effects
|
||||||
|
|
||||||
|
|
||||||
class FertilizerSerializer(serializers.ModelSerializer):
|
class FertilizerSerializer(serializers.ModelSerializer):
|
||||||
@@ -36,7 +50,16 @@ class FertilizationEntrySerializer(serializers.ModelSerializer):
|
|||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = FertilizationEntry
|
model = FertilizationEntry
|
||||||
fields = ['id', 'field', 'field_name', 'field_area_tan', 'fertilizer', 'fertilizer_name', 'bags']
|
fields = [
|
||||||
|
'id',
|
||||||
|
'field',
|
||||||
|
'field_name',
|
||||||
|
'field_area_tan',
|
||||||
|
'fertilizer',
|
||||||
|
'fertilizer_name',
|
||||||
|
'bags',
|
||||||
|
'actual_bags',
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class FertilizationPlanSerializer(serializers.ModelSerializer):
|
class FertilizationPlanSerializer(serializers.ModelSerializer):
|
||||||
@@ -45,15 +68,34 @@ class FertilizationPlanSerializer(serializers.ModelSerializer):
|
|||||||
entries = FertilizationEntrySerializer(many=True, read_only=True)
|
entries = FertilizationEntrySerializer(many=True, read_only=True)
|
||||||
field_count = serializers.SerializerMethodField()
|
field_count = serializers.SerializerMethodField()
|
||||||
fertilizer_count = serializers.SerializerMethodField()
|
fertilizer_count = serializers.SerializerMethodField()
|
||||||
|
planned_total_bags = serializers.SerializerMethodField()
|
||||||
|
spread_total_bags = serializers.SerializerMethodField()
|
||||||
|
remaining_total_bags = serializers.SerializerMethodField()
|
||||||
|
spread_status = serializers.SerializerMethodField()
|
||||||
is_confirmed = serializers.BooleanField(read_only=True)
|
is_confirmed = serializers.BooleanField(read_only=True)
|
||||||
confirmed_at = serializers.DateTimeField(read_only=True)
|
confirmed_at = serializers.DateTimeField(read_only=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = FertilizationPlan
|
model = FertilizationPlan
|
||||||
fields = [
|
fields = [
|
||||||
'id', 'name', 'year', 'variety', 'variety_name', 'crop_name',
|
'id',
|
||||||
'calc_settings', 'entries', 'field_count', 'fertilizer_count',
|
'name',
|
||||||
'is_confirmed', 'confirmed_at', 'created_at', 'updated_at'
|
'year',
|
||||||
|
'variety',
|
||||||
|
'variety_name',
|
||||||
|
'crop_name',
|
||||||
|
'calc_settings',
|
||||||
|
'entries',
|
||||||
|
'field_count',
|
||||||
|
'fertilizer_count',
|
||||||
|
'planned_total_bags',
|
||||||
|
'spread_total_bags',
|
||||||
|
'remaining_total_bags',
|
||||||
|
'spread_status',
|
||||||
|
'is_confirmed',
|
||||||
|
'confirmed_at',
|
||||||
|
'created_at',
|
||||||
|
'updated_at',
|
||||||
]
|
]
|
||||||
|
|
||||||
def get_variety_name(self, obj):
|
def get_variety_name(self, obj):
|
||||||
@@ -68,9 +110,32 @@ class FertilizationPlanSerializer(serializers.ModelSerializer):
|
|||||||
def get_fertilizer_count(self, obj):
|
def get_fertilizer_count(self, obj):
|
||||||
return obj.entries.values('fertilizer').distinct().count()
|
return obj.entries.values('fertilizer').distinct().count()
|
||||||
|
|
||||||
|
def get_planned_total_bags(self, obj):
|
||||||
|
total = sum((entry.bags or Decimal('0')) for entry in obj.entries.all())
|
||||||
|
return str(total)
|
||||||
|
|
||||||
|
def get_spread_total_bags(self, obj):
|
||||||
|
total = sum((entry.actual_bags or Decimal('0')) for entry in obj.entries.all())
|
||||||
|
return str(total)
|
||||||
|
|
||||||
|
def get_remaining_total_bags(self, obj):
|
||||||
|
planned = sum((entry.bags or Decimal('0')) for entry in obj.entries.all())
|
||||||
|
actual = sum((entry.actual_bags or Decimal('0')) for entry in obj.entries.all())
|
||||||
|
return str(planned - actual)
|
||||||
|
|
||||||
|
def get_spread_status(self, obj):
|
||||||
|
planned = sum((entry.bags or Decimal('0')) for entry in obj.entries.all())
|
||||||
|
actual = sum((entry.actual_bags or Decimal('0')) for entry in obj.entries.all())
|
||||||
|
if actual <= 0:
|
||||||
|
return 'unspread'
|
||||||
|
if actual > planned:
|
||||||
|
return 'over_applied'
|
||||||
|
if actual < planned:
|
||||||
|
return 'partial'
|
||||||
|
return 'completed'
|
||||||
|
|
||||||
|
|
||||||
class FertilizationPlanWriteSerializer(serializers.ModelSerializer):
|
class FertilizationPlanWriteSerializer(serializers.ModelSerializer):
|
||||||
"""保存用(entries を一括で受け取る)"""
|
|
||||||
entries = serializers.ListField(child=serializers.DictField(), write_only=True, required=False)
|
entries = serializers.ListField(child=serializers.DictField(), write_only=True, required=False)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
@@ -80,7 +145,8 @@ class FertilizationPlanWriteSerializer(serializers.ModelSerializer):
|
|||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
entries_data = validated_data.pop('entries', [])
|
entries_data = validated_data.pop('entries', [])
|
||||||
plan = FertilizationPlan.objects.create(**validated_data)
|
plan = FertilizationPlan.objects.create(**validated_data)
|
||||||
self._save_entries(plan, entries_data)
|
pairs = self._save_entries(plan, entries_data)
|
||||||
|
sync_actual_bags_for_pairs(plan.year, pairs)
|
||||||
return plan
|
return plan
|
||||||
|
|
||||||
def update(self, instance, validated_data):
|
def update(self, instance, validated_data):
|
||||||
@@ -90,21 +156,23 @@ class FertilizationPlanWriteSerializer(serializers.ModelSerializer):
|
|||||||
instance.save()
|
instance.save()
|
||||||
if entries_data is not None:
|
if entries_data is not None:
|
||||||
instance.entries.all().delete()
|
instance.entries.all().delete()
|
||||||
self._save_entries(instance, entries_data)
|
pairs = self._save_entries(instance, entries_data)
|
||||||
|
sync_actual_bags_for_pairs(instance.year, pairs)
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
def _save_entries(self, plan, entries_data):
|
def _save_entries(self, plan, entries_data):
|
||||||
|
pairs = set()
|
||||||
for entry in entries_data:
|
for entry in entries_data:
|
||||||
|
pairs.add((entry['field_id'], entry['fertilizer_id']))
|
||||||
FertilizationEntry.objects.create(
|
FertilizationEntry.objects.create(
|
||||||
plan=plan,
|
plan=plan,
|
||||||
field_id=entry['field_id'],
|
field_id=entry['field_id'],
|
||||||
fertilizer_id=entry['fertilizer_id'],
|
fertilizer_id=entry['fertilizer_id'],
|
||||||
bags=entry['bags'],
|
bags=entry['bags'],
|
||||||
)
|
)
|
||||||
|
return pairs
|
||||||
|
|
||||||
|
|
||||||
# ─── 運搬計画 ────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
class DeliveryGroupFieldSerializer(serializers.ModelSerializer):
|
class DeliveryGroupFieldSerializer(serializers.ModelSerializer):
|
||||||
id = serializers.IntegerField(source='field.id', read_only=True)
|
id = serializers.IntegerField(source='field.id', read_only=True)
|
||||||
name = serializers.CharField(source='field.name', read_only=True)
|
name = serializers.CharField(source='field.name', read_only=True)
|
||||||
@@ -128,18 +196,51 @@ class DeliveryGroupReadSerializer(serializers.ModelSerializer):
|
|||||||
class DeliveryTripItemSerializer(serializers.ModelSerializer):
|
class DeliveryTripItemSerializer(serializers.ModelSerializer):
|
||||||
field_name = serializers.CharField(source='field.name', read_only=True)
|
field_name = serializers.CharField(source='field.name', read_only=True)
|
||||||
fertilizer_name = serializers.CharField(source='fertilizer.name', read_only=True)
|
fertilizer_name = serializers.CharField(source='fertilizer.name', read_only=True)
|
||||||
|
spread_bags = serializers.SerializerMethodField()
|
||||||
|
remaining_bags = serializers.SerializerMethodField()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = DeliveryTripItem
|
model = DeliveryTripItem
|
||||||
fields = ['id', 'field', 'field_name', 'fertilizer', 'fertilizer_name', 'bags']
|
fields = [
|
||||||
|
'id',
|
||||||
|
'field',
|
||||||
|
'field_name',
|
||||||
|
'fertilizer',
|
||||||
|
'fertilizer_name',
|
||||||
|
'bags',
|
||||||
|
'spread_bags',
|
||||||
|
'remaining_bags',
|
||||||
|
]
|
||||||
|
|
||||||
|
def get_spread_bags(self, obj):
|
||||||
|
total = (
|
||||||
|
SpreadingSessionItem.objects.filter(
|
||||||
|
session__year=obj.trip.delivery_plan.year,
|
||||||
|
field_id=obj.field_id,
|
||||||
|
fertilizer_id=obj.fertilizer_id,
|
||||||
|
).aggregate(total=Sum('actual_bags'))['total']
|
||||||
|
)
|
||||||
|
return str(total or Decimal('0'))
|
||||||
|
|
||||||
|
def get_remaining_bags(self, obj):
|
||||||
|
total = (
|
||||||
|
SpreadingSessionItem.objects.filter(
|
||||||
|
session__year=obj.trip.delivery_plan.year,
|
||||||
|
field_id=obj.field_id,
|
||||||
|
fertilizer_id=obj.fertilizer_id,
|
||||||
|
).aggregate(total=Sum('actual_bags'))['total']
|
||||||
|
)
|
||||||
|
spread_total = total or Decimal('0')
|
||||||
|
return str(obj.bags - spread_total)
|
||||||
|
|
||||||
|
|
||||||
class DeliveryTripReadSerializer(serializers.ModelSerializer):
|
class DeliveryTripReadSerializer(serializers.ModelSerializer):
|
||||||
items = DeliveryTripItemSerializer(many=True, read_only=True)
|
items = DeliveryTripItemSerializer(many=True, read_only=True)
|
||||||
|
work_record_id = serializers.IntegerField(source='work_record.id', read_only=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = DeliveryTrip
|
model = DeliveryTrip
|
||||||
fields = ['id', 'order', 'name', 'date', 'items']
|
fields = ['id', 'order', 'name', 'date', 'work_record_id', 'items']
|
||||||
|
|
||||||
|
|
||||||
class DeliveryPlanListSerializer(serializers.ModelSerializer):
|
class DeliveryPlanListSerializer(serializers.ModelSerializer):
|
||||||
@@ -149,8 +250,13 @@ class DeliveryPlanListSerializer(serializers.ModelSerializer):
|
|||||||
class Meta:
|
class Meta:
|
||||||
model = DeliveryPlan
|
model = DeliveryPlan
|
||||||
fields = [
|
fields = [
|
||||||
'id', 'year', 'name', 'group_count', 'trip_count',
|
'id',
|
||||||
'created_at', 'updated_at',
|
'year',
|
||||||
|
'name',
|
||||||
|
'group_count',
|
||||||
|
'trip_count',
|
||||||
|
'created_at',
|
||||||
|
'updated_at',
|
||||||
]
|
]
|
||||||
|
|
||||||
def get_group_count(self, obj):
|
def get_group_count(self, obj):
|
||||||
@@ -170,20 +276,27 @@ class DeliveryPlanReadSerializer(serializers.ModelSerializer):
|
|||||||
class Meta:
|
class Meta:
|
||||||
model = DeliveryPlan
|
model = DeliveryPlan
|
||||||
fields = [
|
fields = [
|
||||||
'id', 'year', 'name', 'groups', 'trips',
|
'id',
|
||||||
'unassigned_fields', 'available_fertilizers', 'all_entries',
|
'year',
|
||||||
'created_at', 'updated_at',
|
'name',
|
||||||
|
'groups',
|
||||||
|
'trips',
|
||||||
|
'unassigned_fields',
|
||||||
|
'available_fertilizers',
|
||||||
|
'all_entries',
|
||||||
|
'created_at',
|
||||||
|
'updated_at',
|
||||||
]
|
]
|
||||||
|
|
||||||
def get_unassigned_fields(self, obj):
|
def get_unassigned_fields(self, obj):
|
||||||
assigned_ids = DeliveryGroupField.objects.filter(
|
assigned_ids = DeliveryGroupField.objects.filter(
|
||||||
delivery_plan=obj
|
delivery_plan=obj
|
||||||
).values_list('field_id', flat=True)
|
).values_list('field_id', flat=True)
|
||||||
# 年度の施肥計画に含まれる全圃場
|
|
||||||
plan_field_ids = FertilizationEntry.objects.filter(
|
plan_field_ids = FertilizationEntry.objects.filter(
|
||||||
plan__year=obj.year
|
plan__year=obj.year
|
||||||
).values_list('field_id', flat=True).distinct()
|
).values_list('field_id', flat=True).distinct()
|
||||||
from apps.fields.models import Field
|
from apps.fields.models import Field
|
||||||
|
|
||||||
unassigned = Field.objects.filter(
|
unassigned = Field.objects.filter(
|
||||||
id__in=plan_field_ids
|
id__in=plan_field_ids
|
||||||
).exclude(id__in=assigned_ids).order_by('display_order', 'id')
|
).exclude(id__in=assigned_ids).order_by('display_order', 'id')
|
||||||
@@ -197,20 +310,20 @@ class DeliveryPlanReadSerializer(serializers.ModelSerializer):
|
|||||||
return [{'id': f.id, 'name': f.name} for f in fertilizers]
|
return [{'id': f.id, 'name': f.name} for f in fertilizers]
|
||||||
|
|
||||||
def get_all_entries(self, obj):
|
def get_all_entries(self, obj):
|
||||||
"""年度の全施肥計画のエントリ(フロントで袋数計算に使用)"""
|
|
||||||
entries = FertilizationEntry.objects.filter(
|
entries = FertilizationEntry.objects.filter(
|
||||||
plan__year=obj.year
|
plan__year=obj.year
|
||||||
).select_related('field', 'fertilizer')
|
).select_related('field', 'fertilizer')
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
'field': e.field_id,
|
'field': entry.field_id,
|
||||||
'field_name': e.field.name,
|
'field_name': entry.field.name,
|
||||||
'field_area_tan': str(e.field.area_tan),
|
'field_area_tan': str(entry.field.area_tan),
|
||||||
'fertilizer': e.fertilizer_id,
|
'fertilizer': entry.fertilizer_id,
|
||||||
'fertilizer_name': e.fertilizer.name,
|
'fertilizer_name': entry.fertilizer.name,
|
||||||
'bags': str(e.bags),
|
'bags': str(entry.bags),
|
||||||
|
'actual_bags': str(entry.actual_bags) if entry.actual_bags is not None else None,
|
||||||
}
|
}
|
||||||
for e in entries
|
for entry in entries
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@@ -245,13 +358,13 @@ class DeliveryPlanWriteSerializer(serializers.ModelSerializer):
|
|||||||
return instance
|
return instance
|
||||||
|
|
||||||
def _save_groups(self, plan, groups_data):
|
def _save_groups(self, plan, groups_data):
|
||||||
for g_data in groups_data:
|
for group_data in groups_data:
|
||||||
group = DeliveryGroup.objects.create(
|
group = DeliveryGroup.objects.create(
|
||||||
delivery_plan=plan,
|
delivery_plan=plan,
|
||||||
name=g_data['name'],
|
name=group_data['name'],
|
||||||
order=g_data.get('order', 0),
|
order=group_data.get('order', 0),
|
||||||
)
|
)
|
||||||
for field_id in g_data.get('field_ids', []):
|
for field_id in group_data.get('field_ids', []):
|
||||||
DeliveryGroupField.objects.create(
|
DeliveryGroupField.objects.create(
|
||||||
delivery_plan=plan,
|
delivery_plan=plan,
|
||||||
group=group,
|
group=group,
|
||||||
@@ -259,17 +372,116 @@ class DeliveryPlanWriteSerializer(serializers.ModelSerializer):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def _save_trips(self, plan, trips_data):
|
def _save_trips(self, plan, trips_data):
|
||||||
for t_data in trips_data:
|
for trip_data in trips_data:
|
||||||
trip = DeliveryTrip.objects.create(
|
trip = DeliveryTrip.objects.create(
|
||||||
delivery_plan=plan,
|
delivery_plan=plan,
|
||||||
order=t_data.get('order', 0),
|
order=trip_data.get('order', 0),
|
||||||
name=t_data.get('name', ''),
|
name=trip_data.get('name', ''),
|
||||||
date=t_data.get('date'),
|
date=trip_data.get('date'),
|
||||||
)
|
)
|
||||||
for item in t_data.get('items', []):
|
for item in trip_data.get('items', []):
|
||||||
DeliveryTripItem.objects.create(
|
DeliveryTripItem.objects.create(
|
||||||
trip=trip,
|
trip=trip,
|
||||||
field_id=item['field_id'],
|
field_id=item['field_id'],
|
||||||
fertilizer_id=item['fertilizer_id'],
|
fertilizer_id=item['fertilizer_id'],
|
||||||
bags=item['bags'],
|
bags=item['bags'],
|
||||||
)
|
)
|
||||||
|
sync_delivery_work_record(trip)
|
||||||
|
|
||||||
|
|
||||||
|
class SpreadingSessionItemReadSerializer(serializers.ModelSerializer):
|
||||||
|
field_name = serializers.CharField(source='field.name', read_only=True)
|
||||||
|
fertilizer_name = serializers.CharField(source='fertilizer.name', read_only=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = SpreadingSessionItem
|
||||||
|
fields = [
|
||||||
|
'id',
|
||||||
|
'field',
|
||||||
|
'field_name',
|
||||||
|
'fertilizer',
|
||||||
|
'fertilizer_name',
|
||||||
|
'actual_bags',
|
||||||
|
'planned_bags_snapshot',
|
||||||
|
'delivered_bags_snapshot',
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class SpreadingSessionSerializer(serializers.ModelSerializer):
|
||||||
|
items = SpreadingSessionItemReadSerializer(many=True, read_only=True)
|
||||||
|
work_record_id = serializers.IntegerField(source='work_record.id', read_only=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = SpreadingSession
|
||||||
|
fields = [
|
||||||
|
'id',
|
||||||
|
'year',
|
||||||
|
'date',
|
||||||
|
'name',
|
||||||
|
'notes',
|
||||||
|
'work_record_id',
|
||||||
|
'items',
|
||||||
|
'created_at',
|
||||||
|
'updated_at',
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class SpreadingSessionItemWriteInputSerializer(serializers.Serializer):
|
||||||
|
field_id = serializers.IntegerField()
|
||||||
|
fertilizer_id = serializers.IntegerField()
|
||||||
|
actual_bags = serializers.DecimalField(max_digits=10, decimal_places=4)
|
||||||
|
planned_bags_snapshot = serializers.DecimalField(max_digits=10, decimal_places=4)
|
||||||
|
delivered_bags_snapshot = serializers.DecimalField(max_digits=10, decimal_places=4)
|
||||||
|
|
||||||
|
|
||||||
|
class SpreadingSessionWriteSerializer(serializers.ModelSerializer):
|
||||||
|
items = SpreadingSessionItemWriteInputSerializer(many=True, write_only=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = SpreadingSession
|
||||||
|
fields = ['id', 'year', 'date', 'name', 'notes', 'items']
|
||||||
|
|
||||||
|
def validate_items(self, value):
|
||||||
|
if not value:
|
||||||
|
raise serializers.ValidationError('items を1件以上指定してください。')
|
||||||
|
seen = set()
|
||||||
|
for item in value:
|
||||||
|
if item['actual_bags'] <= 0:
|
||||||
|
raise serializers.ValidationError('actual_bags は 0 より大きい値を指定してください。')
|
||||||
|
key = (item['field_id'], item['fertilizer_id'])
|
||||||
|
if key in seen:
|
||||||
|
raise serializers.ValidationError('同一 session 内で field + fertilizer を重複登録できません。')
|
||||||
|
seen.add(key)
|
||||||
|
return value
|
||||||
|
|
||||||
|
def create(self, validated_data):
|
||||||
|
items_data = validated_data.pop('items', [])
|
||||||
|
session = SpreadingSession.objects.create(**validated_data)
|
||||||
|
new_pairs = self._replace_items(session, items_data)
|
||||||
|
sync_spreading_session_side_effects(session, new_pairs)
|
||||||
|
return session
|
||||||
|
|
||||||
|
def update(self, instance, validated_data):
|
||||||
|
items_data = validated_data.pop('items', [])
|
||||||
|
old_pairs = {(item.field_id, item.fertilizer_id) for item in instance.items.all()}
|
||||||
|
for attr, value in validated_data.items():
|
||||||
|
setattr(instance, attr, value)
|
||||||
|
instance.save()
|
||||||
|
new_pairs = self._replace_items(instance, items_data)
|
||||||
|
sync_spreading_session_side_effects(instance, old_pairs | new_pairs)
|
||||||
|
return instance
|
||||||
|
|
||||||
|
def _replace_items(self, session, items_data):
|
||||||
|
session.items.all().delete()
|
||||||
|
new_pairs = set()
|
||||||
|
for item in items_data:
|
||||||
|
new_pairs.add((item['field_id'], item['fertilizer_id']))
|
||||||
|
SpreadingSessionItem.objects.create(
|
||||||
|
session=session,
|
||||||
|
field_id=item['field_id'],
|
||||||
|
fertilizer_id=item['fertilizer_id'],
|
||||||
|
actual_bags=item['actual_bags'],
|
||||||
|
planned_bags_snapshot=item['planned_bags_snapshot'],
|
||||||
|
delivered_bags_snapshot=item['delivered_bags_snapshot'],
|
||||||
|
)
|
||||||
|
return new_pairs
|
||||||
|
|||||||
65
backend/apps/fertilizer/services.py
Normal file
65
backend/apps/fertilizer/services.py
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
|
from django.db import transaction
|
||||||
|
from django.db.models import Sum
|
||||||
|
|
||||||
|
from apps.materials.models import StockTransaction
|
||||||
|
from apps.workrecords.services import sync_spreading_work_record
|
||||||
|
from .models import FertilizationEntry, SpreadingSessionItem
|
||||||
|
|
||||||
|
|
||||||
|
def sync_actual_bags_for_pairs(year, field_fertilizer_pairs):
|
||||||
|
pairs = {
|
||||||
|
(int(field_id), int(fertilizer_id))
|
||||||
|
for field_id, fertilizer_id in field_fertilizer_pairs
|
||||||
|
}
|
||||||
|
if not pairs:
|
||||||
|
return
|
||||||
|
|
||||||
|
for field_id, fertilizer_id in pairs:
|
||||||
|
total = (
|
||||||
|
SpreadingSessionItem.objects.filter(
|
||||||
|
session__year=year,
|
||||||
|
field_id=field_id,
|
||||||
|
fertilizer_id=fertilizer_id,
|
||||||
|
).aggregate(total=Sum('actual_bags'))['total']
|
||||||
|
)
|
||||||
|
FertilizationEntry.objects.filter(
|
||||||
|
plan__year=year,
|
||||||
|
field_id=field_id,
|
||||||
|
fertilizer_id=fertilizer_id,
|
||||||
|
).update(actual_bags=total)
|
||||||
|
|
||||||
|
|
||||||
|
@transaction.atomic
|
||||||
|
def sync_spreading_session_side_effects(session, field_fertilizer_pairs):
|
||||||
|
sync_actual_bags_for_pairs(session.year, field_fertilizer_pairs)
|
||||||
|
sync_stock_uses_for_spreading_session(session)
|
||||||
|
sync_spreading_work_record(session)
|
||||||
|
|
||||||
|
|
||||||
|
@transaction.atomic
|
||||||
|
def sync_stock_uses_for_spreading_session(session):
|
||||||
|
StockTransaction.objects.filter(spreading_item__session=session).delete()
|
||||||
|
|
||||||
|
session_items = session.items.select_related('fertilizer__material')
|
||||||
|
for item in session_items:
|
||||||
|
material = getattr(item.fertilizer, 'material', None)
|
||||||
|
if material is None:
|
||||||
|
continue
|
||||||
|
StockTransaction.objects.create(
|
||||||
|
material=material,
|
||||||
|
transaction_type=StockTransaction.TransactionType.USE,
|
||||||
|
quantity=item.actual_bags,
|
||||||
|
occurred_on=session.date,
|
||||||
|
note=f'散布実績「{session.name.strip() or session.date}」',
|
||||||
|
fertilization_plan=None,
|
||||||
|
spreading_item=item,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def to_decimal_or_zero(value):
|
||||||
|
try:
|
||||||
|
return Decimal(str(value))
|
||||||
|
except Exception:
|
||||||
|
return Decimal('0')
|
||||||
@@ -6,9 +6,11 @@ router = DefaultRouter()
|
|||||||
router.register(r'fertilizers', views.FertilizerViewSet, basename='fertilizer')
|
router.register(r'fertilizers', views.FertilizerViewSet, basename='fertilizer')
|
||||||
router.register(r'plans', views.FertilizationPlanViewSet, basename='fertilization-plan')
|
router.register(r'plans', views.FertilizationPlanViewSet, basename='fertilization-plan')
|
||||||
router.register(r'delivery', views.DeliveryPlanViewSet, basename='delivery-plan')
|
router.register(r'delivery', views.DeliveryPlanViewSet, basename='delivery-plan')
|
||||||
|
router.register(r'spreading', views.SpreadingSessionViewSet, basename='spreading-session')
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', include(router.urls)),
|
|
||||||
path('candidate_fields/', views.CandidateFieldsView.as_view(), name='candidate-fields'),
|
path('candidate_fields/', views.CandidateFieldsView.as_view(), name='candidate-fields'),
|
||||||
path('calculate/', views.CalculateView.as_view(), name='fertilizer-calculate'),
|
path('calculate/', views.CalculateView.as_view(), name='fertilizer-calculate'),
|
||||||
|
path('spreading/candidates/', views.SpreadingCandidatesView.as_view(), name='spreading-candidates'),
|
||||||
|
path('', include(router.urls)),
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
from decimal import Decimal, InvalidOperation
|
from decimal import Decimal, InvalidOperation
|
||||||
|
|
||||||
|
from django.db.models import Sum
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.template.loader import render_to_string
|
from django.template.loader import render_to_string
|
||||||
from rest_framework import viewsets, status
|
from rest_framework import viewsets, status
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
from rest_framework.exceptions import ValidationError
|
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
@@ -12,15 +12,14 @@ from weasyprint import HTML
|
|||||||
|
|
||||||
from apps.fields.models import Field
|
from apps.fields.models import Field
|
||||||
from apps.materials.stock_service import (
|
from apps.materials.stock_service import (
|
||||||
confirm_spreading as confirm_spreading_service,
|
|
||||||
create_reserves_for_plan,
|
create_reserves_for_plan,
|
||||||
delete_reserves_for_plan,
|
delete_reserves_for_plan,
|
||||||
unconfirm_spreading,
|
|
||||||
)
|
)
|
||||||
from apps.plans.models import Plan, Variety
|
from apps.plans.models import Plan
|
||||||
from .models import (
|
from .models import (
|
||||||
Fertilizer, FertilizationPlan, FertilizationEntry,
|
Fertilizer, FertilizationPlan, FertilizationEntry,
|
||||||
DeliveryPlan, DeliveryGroup, DeliveryGroupField, DeliveryTrip, DeliveryTripItem,
|
DeliveryPlan, DeliveryGroup, DeliveryGroupField, DeliveryTrip, DeliveryTripItem,
|
||||||
|
SpreadingSession, SpreadingSessionItem,
|
||||||
)
|
)
|
||||||
from .serializers import (
|
from .serializers import (
|
||||||
FertilizerSerializer,
|
FertilizerSerializer,
|
||||||
@@ -29,7 +28,10 @@ from .serializers import (
|
|||||||
DeliveryPlanListSerializer,
|
DeliveryPlanListSerializer,
|
||||||
DeliveryPlanReadSerializer,
|
DeliveryPlanReadSerializer,
|
||||||
DeliveryPlanWriteSerializer,
|
DeliveryPlanWriteSerializer,
|
||||||
|
SpreadingSessionSerializer,
|
||||||
|
SpreadingSessionWriteSerializer,
|
||||||
)
|
)
|
||||||
|
from .services import sync_actual_bags_for_pairs
|
||||||
|
|
||||||
|
|
||||||
class FertilizerViewSet(viewsets.ModelViewSet):
|
class FertilizerViewSet(viewsets.ModelViewSet):
|
||||||
@@ -60,8 +62,6 @@ class FertilizationPlanViewSet(viewsets.ModelViewSet):
|
|||||||
create_reserves_for_plan(instance)
|
create_reserves_for_plan(instance)
|
||||||
|
|
||||||
def perform_update(self, serializer):
|
def perform_update(self, serializer):
|
||||||
if serializer.instance.is_confirmed:
|
|
||||||
raise ValidationError({'detail': '確定済みの施肥計画は編集できません。'})
|
|
||||||
instance = serializer.save()
|
instance = serializer.save()
|
||||||
create_reserves_for_plan(instance)
|
create_reserves_for_plan(instance)
|
||||||
|
|
||||||
@@ -123,68 +123,6 @@ class FertilizationPlanViewSet(viewsets.ModelViewSet):
|
|||||||
response['Content-Disposition'] = f'attachment; filename="fertilization_{plan.year}_{plan.id}.pdf"'
|
response['Content-Disposition'] = f'attachment; filename="fertilization_{plan.year}_{plan.id}.pdf"'
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@action(detail=True, methods=['post'], url_path='confirm_spreading')
|
|
||||||
def confirm_spreading(self, request, pk=None):
|
|
||||||
plan = self.get_object()
|
|
||||||
|
|
||||||
if plan.is_confirmed:
|
|
||||||
return Response(
|
|
||||||
{'detail': 'この計画は既に散布確定済みです。'},
|
|
||||||
status=status.HTTP_400_BAD_REQUEST,
|
|
||||||
)
|
|
||||||
|
|
||||||
entries_data = request.data.get('entries', [])
|
|
||||||
if not entries_data:
|
|
||||||
return Response(
|
|
||||||
{'detail': '実績データが空です。'},
|
|
||||||
status=status.HTTP_400_BAD_REQUEST,
|
|
||||||
)
|
|
||||||
|
|
||||||
actual_entries = []
|
|
||||||
for entry in entries_data:
|
|
||||||
field_id = entry.get('field_id')
|
|
||||||
fertilizer_id = entry.get('fertilizer_id')
|
|
||||||
if not field_id or not fertilizer_id:
|
|
||||||
return Response(
|
|
||||||
{'detail': 'field_id と fertilizer_id が必要です。'},
|
|
||||||
status=status.HTTP_400_BAD_REQUEST,
|
|
||||||
)
|
|
||||||
try:
|
|
||||||
actual_bags = Decimal(str(entry.get('actual_bags', 0)))
|
|
||||||
except InvalidOperation:
|
|
||||||
return Response(
|
|
||||||
{'detail': 'actual_bags は数値で指定してください。'},
|
|
||||||
status=status.HTTP_400_BAD_REQUEST,
|
|
||||||
)
|
|
||||||
actual_entries.append(
|
|
||||||
{
|
|
||||||
'field_id': field_id,
|
|
||||||
'fertilizer_id': fertilizer_id,
|
|
||||||
'actual_bags': actual_bags,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
confirm_spreading_service(plan, actual_entries)
|
|
||||||
plan.refresh_from_db()
|
|
||||||
serializer = self.get_serializer(plan)
|
|
||||||
return Response(serializer.data)
|
|
||||||
|
|
||||||
@action(detail=True, methods=['post'], url_path='unconfirm')
|
|
||||||
def unconfirm(self, request, pk=None):
|
|
||||||
plan = self.get_object()
|
|
||||||
|
|
||||||
if not plan.is_confirmed:
|
|
||||||
return Response(
|
|
||||||
{'detail': 'この計画はまだ確定されていません。'},
|
|
||||||
status=status.HTTP_400_BAD_REQUEST,
|
|
||||||
)
|
|
||||||
|
|
||||||
unconfirm_spreading(plan)
|
|
||||||
plan.refresh_from_db()
|
|
||||||
serializer = self.get_serializer(plan)
|
|
||||||
return Response(serializer.data)
|
|
||||||
|
|
||||||
|
|
||||||
class CandidateFieldsView(APIView):
|
class CandidateFieldsView(APIView):
|
||||||
"""作付け計画から圃場候補を返す"""
|
"""作付け計画から圃場候補を返す"""
|
||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
@@ -421,3 +359,232 @@ class DeliveryPlanViewSet(viewsets.ModelViewSet):
|
|||||||
f'attachment; filename="delivery_{plan.year}_{plan.id}.pdf"'
|
f'attachment; filename="delivery_{plan.year}_{plan.id}.pdf"'
|
||||||
)
|
)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
class SpreadingSessionViewSet(viewsets.ModelViewSet):
|
||||||
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
queryset = SpreadingSession.objects.prefetch_related(
|
||||||
|
'items',
|
||||||
|
'items__field',
|
||||||
|
'items__fertilizer',
|
||||||
|
).select_related('work_record')
|
||||||
|
year = self.request.query_params.get('year')
|
||||||
|
if year:
|
||||||
|
queryset = queryset.filter(year=year)
|
||||||
|
return queryset
|
||||||
|
|
||||||
|
def get_serializer_class(self):
|
||||||
|
if self.action in ['create', 'update', 'partial_update']:
|
||||||
|
return SpreadingSessionWriteSerializer
|
||||||
|
return SpreadingSessionSerializer
|
||||||
|
|
||||||
|
def perform_destroy(self, instance):
|
||||||
|
year = instance.year
|
||||||
|
affected_pairs = {(item.field_id, item.fertilizer_id) for item in instance.items.all()}
|
||||||
|
instance.delete()
|
||||||
|
sync_actual_bags_for_pairs(year, affected_pairs)
|
||||||
|
|
||||||
|
|
||||||
|
class SpreadingCandidatesView(APIView):
|
||||||
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
|
def get(self, request):
|
||||||
|
year = request.query_params.get('year')
|
||||||
|
session_id = request.query_params.get('session_id')
|
||||||
|
delivery_plan_id = request.query_params.get('delivery_plan_id')
|
||||||
|
plan_id = request.query_params.get('plan_id')
|
||||||
|
if not year:
|
||||||
|
return Response(
|
||||||
|
{'detail': 'year が必要です。'},
|
||||||
|
status=status.HTTP_400_BAD_REQUEST,
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
year = int(year)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return Response(
|
||||||
|
{'detail': 'year は数値で指定してください。'},
|
||||||
|
status=status.HTTP_400_BAD_REQUEST,
|
||||||
|
)
|
||||||
|
|
||||||
|
if delivery_plan_id:
|
||||||
|
try:
|
||||||
|
delivery_plan_id = int(delivery_plan_id)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return Response(
|
||||||
|
{'detail': 'delivery_plan_id は数値で指定してください。'},
|
||||||
|
status=status.HTTP_400_BAD_REQUEST,
|
||||||
|
)
|
||||||
|
if plan_id:
|
||||||
|
try:
|
||||||
|
plan_id = int(plan_id)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return Response(
|
||||||
|
{'detail': 'plan_id は数値で指定してください。'},
|
||||||
|
status=status.HTTP_400_BAD_REQUEST,
|
||||||
|
)
|
||||||
|
|
||||||
|
current_session = None
|
||||||
|
current_map = {}
|
||||||
|
if session_id:
|
||||||
|
try:
|
||||||
|
current_session = SpreadingSession.objects.prefetch_related('items').get(
|
||||||
|
pk=session_id,
|
||||||
|
year=year,
|
||||||
|
)
|
||||||
|
except SpreadingSession.DoesNotExist:
|
||||||
|
return Response(
|
||||||
|
{'detail': '散布実績が見つかりません。'},
|
||||||
|
status=status.HTTP_404_NOT_FOUND,
|
||||||
|
)
|
||||||
|
current_map = {
|
||||||
|
(item.field_id, item.fertilizer_id): {
|
||||||
|
'actual_bags': item.actual_bags,
|
||||||
|
'field_name': item.field.name,
|
||||||
|
'field_area_tan': str(item.field.area_tan),
|
||||||
|
'fertilizer_name': item.fertilizer.name,
|
||||||
|
}
|
||||||
|
for item in current_session.items.all()
|
||||||
|
}
|
||||||
|
|
||||||
|
candidates = {}
|
||||||
|
|
||||||
|
plan_queryset = FertilizationEntry.objects.filter(plan__year=year)
|
||||||
|
if plan_id:
|
||||||
|
plan_queryset = plan_queryset.filter(plan_id=plan_id)
|
||||||
|
plan_rows = (
|
||||||
|
plan_queryset
|
||||||
|
.values(
|
||||||
|
'field_id',
|
||||||
|
'field__name',
|
||||||
|
'field__area_tan',
|
||||||
|
'fertilizer_id',
|
||||||
|
'fertilizer__name',
|
||||||
|
)
|
||||||
|
.annotate(planned_bags=Sum('bags'))
|
||||||
|
)
|
||||||
|
for row in plan_rows:
|
||||||
|
key = (row['field_id'], row['fertilizer_id'])
|
||||||
|
candidates.setdefault(
|
||||||
|
key,
|
||||||
|
{
|
||||||
|
'field': row['field_id'],
|
||||||
|
'field_name': row['field__name'],
|
||||||
|
'field_area_tan': str(row['field__area_tan']),
|
||||||
|
'fertilizer': row['fertilizer_id'],
|
||||||
|
'fertilizer_name': row['fertilizer__name'],
|
||||||
|
'planned_bags': Decimal('0'),
|
||||||
|
'delivered_bags': Decimal('0'),
|
||||||
|
'spread_bags': Decimal('0'),
|
||||||
|
'current_session_bags': Decimal('0'),
|
||||||
|
},
|
||||||
|
)['planned_bags'] = row['planned_bags'] or Decimal('0')
|
||||||
|
|
||||||
|
delivery_queryset = DeliveryTripItem.objects.filter(trip__delivery_plan__year=year)
|
||||||
|
if delivery_plan_id:
|
||||||
|
delivery_queryset = delivery_queryset.filter(trip__delivery_plan_id=delivery_plan_id)
|
||||||
|
delivery_rows = delivery_queryset.values(
|
||||||
|
'field_id',
|
||||||
|
'field__name',
|
||||||
|
'field__area_tan',
|
||||||
|
'fertilizer_id',
|
||||||
|
'fertilizer__name',
|
||||||
|
).annotate(delivered_bags=Sum('bags'))
|
||||||
|
for row in delivery_rows:
|
||||||
|
key = (row['field_id'], row['fertilizer_id'])
|
||||||
|
candidates.setdefault(
|
||||||
|
key,
|
||||||
|
{
|
||||||
|
'field': row['field_id'],
|
||||||
|
'field_name': row['field__name'],
|
||||||
|
'field_area_tan': str(row['field__area_tan']),
|
||||||
|
'fertilizer': row['fertilizer_id'],
|
||||||
|
'fertilizer_name': row['fertilizer__name'],
|
||||||
|
'planned_bags': Decimal('0'),
|
||||||
|
'delivered_bags': Decimal('0'),
|
||||||
|
'spread_bags': Decimal('0'),
|
||||||
|
'current_session_bags': Decimal('0'),
|
||||||
|
},
|
||||||
|
)['delivered_bags'] = row['delivered_bags'] or Decimal('0')
|
||||||
|
|
||||||
|
spread_queryset = SpreadingSessionItem.objects.filter(session__year=year)
|
||||||
|
if current_session is not None:
|
||||||
|
spread_queryset = spread_queryset.exclude(session=current_session)
|
||||||
|
spread_rows = (
|
||||||
|
spread_queryset
|
||||||
|
.values(
|
||||||
|
'field_id',
|
||||||
|
'field__name',
|
||||||
|
'field__area_tan',
|
||||||
|
'fertilizer_id',
|
||||||
|
'fertilizer__name',
|
||||||
|
)
|
||||||
|
.annotate(spread_bags=Sum('actual_bags'))
|
||||||
|
)
|
||||||
|
for row in spread_rows:
|
||||||
|
key = (row['field_id'], row['fertilizer_id'])
|
||||||
|
candidates.setdefault(
|
||||||
|
key,
|
||||||
|
{
|
||||||
|
'field': row['field_id'],
|
||||||
|
'field_name': row['field__name'],
|
||||||
|
'field_area_tan': str(row['field__area_tan']),
|
||||||
|
'fertilizer': row['fertilizer_id'],
|
||||||
|
'fertilizer_name': row['fertilizer__name'],
|
||||||
|
'planned_bags': Decimal('0'),
|
||||||
|
'delivered_bags': Decimal('0'),
|
||||||
|
'spread_bags': Decimal('0'),
|
||||||
|
'current_session_bags': Decimal('0'),
|
||||||
|
},
|
||||||
|
)['spread_bags'] = row['spread_bags'] or Decimal('0')
|
||||||
|
|
||||||
|
for key, current_data in current_map.items():
|
||||||
|
candidates.setdefault(
|
||||||
|
key,
|
||||||
|
{
|
||||||
|
'field': key[0],
|
||||||
|
'field_name': current_data['field_name'],
|
||||||
|
'field_area_tan': current_data['field_area_tan'],
|
||||||
|
'fertilizer': key[1],
|
||||||
|
'fertilizer_name': current_data['fertilizer_name'],
|
||||||
|
'planned_bags': Decimal('0'),
|
||||||
|
'delivered_bags': Decimal('0'),
|
||||||
|
'spread_bags': Decimal('0'),
|
||||||
|
'current_session_bags': Decimal('0'),
|
||||||
|
},
|
||||||
|
)['current_session_bags'] = current_data['actual_bags'] or Decimal('0')
|
||||||
|
|
||||||
|
rows = []
|
||||||
|
for candidate in candidates.values():
|
||||||
|
delivered = candidate['delivered_bags']
|
||||||
|
planned = candidate['planned_bags']
|
||||||
|
current_bags = candidate['current_session_bags']
|
||||||
|
if delivery_plan_id:
|
||||||
|
include_row = delivered > 0 or current_bags > 0
|
||||||
|
elif plan_id:
|
||||||
|
include_row = planned > 0 or current_bags > 0
|
||||||
|
else:
|
||||||
|
include_row = delivered > 0 or current_bags > 0
|
||||||
|
if not include_row:
|
||||||
|
continue
|
||||||
|
remaining = delivered - candidate['spread_bags']
|
||||||
|
rows.append(
|
||||||
|
{
|
||||||
|
'field': candidate['field'],
|
||||||
|
'field_name': candidate['field_name'],
|
||||||
|
'field_area_tan': candidate['field_area_tan'],
|
||||||
|
'fertilizer': candidate['fertilizer'],
|
||||||
|
'fertilizer_name': candidate['fertilizer_name'],
|
||||||
|
'planned_bags': str(planned),
|
||||||
|
'delivered_bags': str(delivered),
|
||||||
|
'spread_bags': str(candidate['spread_bags'] + current_bags),
|
||||||
|
'spread_bags_other': str(candidate['spread_bags']),
|
||||||
|
'current_session_bags': str(current_bags),
|
||||||
|
'remaining_bags': str(remaining),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
rows.sort(key=lambda row: (row['field_name'], row['fertilizer_name']))
|
||||||
|
return Response(rows)
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
# Generated by Django 5.0 on 2026-03-17 08:49
|
||||||
|
|
||||||
|
import django.db.models.deletion
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('fertilizer', '0008_spreadingsession_fertilizationentry_actual_bags_and_more'),
|
||||||
|
('materials', '0002_stocktransaction_fertilization_plan'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='stocktransaction',
|
||||||
|
name='spreading_item',
|
||||||
|
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='stock_transactions', to='fertilizer.spreadingsessionitem', verbose_name='散布実績明細'),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='stocktransaction',
|
||||||
|
name='transaction_type',
|
||||||
|
field=models.CharField(choices=[('purchase', '入庫'), ('use', '使用'), ('reserve', '引当'), ('adjustment_plus', '棚卸増'), ('adjustment_minus', '棚卸減'), ('discard', '廃棄')], max_length=30, verbose_name='取引種別'),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -205,6 +205,14 @@ class StockTransaction(models.Model):
|
|||||||
related_name='stock_reservations',
|
related_name='stock_reservations',
|
||||||
verbose_name='施肥計画',
|
verbose_name='施肥計画',
|
||||||
)
|
)
|
||||||
|
spreading_item = models.ForeignKey(
|
||||||
|
'fertilizer.SpreadingSessionItem',
|
||||||
|
on_delete=models.CASCADE,
|
||||||
|
null=True,
|
||||||
|
blank=True,
|
||||||
|
related_name='stock_transactions',
|
||||||
|
verbose_name='散布実績明細',
|
||||||
|
)
|
||||||
created_at = models.DateTimeField(auto_now_add=True)
|
created_at = models.DateTimeField(auto_now_add=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|||||||
@@ -14,9 +14,6 @@ def create_reserves_for_plan(plan):
|
|||||||
transaction_type=StockTransaction.TransactionType.RESERVE,
|
transaction_type=StockTransaction.TransactionType.RESERVE,
|
||||||
).delete()
|
).delete()
|
||||||
|
|
||||||
if plan.is_confirmed:
|
|
||||||
return
|
|
||||||
|
|
||||||
occurred_on = (
|
occurred_on = (
|
||||||
plan.updated_at.date() if getattr(plan, 'updated_at', None) else timezone.localdate()
|
plan.updated_at.date() if getattr(plan, 'updated_at', None) else timezone.localdate()
|
||||||
)
|
)
|
||||||
|
|||||||
1
backend/apps/workrecords/__init__.py
Normal file
1
backend/apps/workrecords/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
11
backend/apps/workrecords/admin.py
Normal file
11
backend/apps/workrecords/admin.py
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
|
from .models import WorkRecord
|
||||||
|
|
||||||
|
|
||||||
|
@admin.register(WorkRecord)
|
||||||
|
class WorkRecordAdmin(admin.ModelAdmin):
|
||||||
|
list_display = ['work_date', 'work_type', 'title', 'year', 'auto_created']
|
||||||
|
list_filter = ['work_type', 'year', 'auto_created']
|
||||||
|
search_fields = ['title']
|
||||||
|
|
||||||
8
backend/apps/workrecords/apps.py
Normal file
8
backend/apps/workrecords/apps.py
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class WorkrecordsConfig(AppConfig):
|
||||||
|
default_auto_field = 'django.db.models.BigAutoField'
|
||||||
|
name = 'apps.workrecords'
|
||||||
|
verbose_name = '作業記録'
|
||||||
|
|
||||||
36
backend/apps/workrecords/migrations/0001_initial.py
Normal file
36
backend/apps/workrecords/migrations/0001_initial.py
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
# Generated by Django 5.0 on 2026-03-17 08:49
|
||||||
|
|
||||||
|
import django.db.models.deletion
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
initial = True
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('fertilizer', '0008_spreadingsession_fertilizationentry_actual_bags_and_more'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='WorkRecord',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('work_date', models.DateField(verbose_name='作業日')),
|
||||||
|
('work_type', models.CharField(choices=[('fertilizer_delivery', '肥料運搬'), ('fertilizer_spreading', '肥料散布')], max_length=40, verbose_name='作業種別')),
|
||||||
|
('title', models.CharField(max_length=200, verbose_name='タイトル')),
|
||||||
|
('year', models.IntegerField(verbose_name='年度')),
|
||||||
|
('auto_created', models.BooleanField(default=True, verbose_name='自動生成')),
|
||||||
|
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||||
|
('updated_at', models.DateTimeField(auto_now=True)),
|
||||||
|
('delivery_trip', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='work_record', to='fertilizer.deliverytrip', verbose_name='運搬回')),
|
||||||
|
('spreading_session', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='work_record', to='fertilizer.spreadingsession', verbose_name='散布実績')),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
'verbose_name': '作業記録',
|
||||||
|
'verbose_name_plural': '作業記録',
|
||||||
|
'ordering': ['-work_date', '-updated_at', '-id'],
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
||||||
1
backend/apps/workrecords/migrations/__init__.py
Normal file
1
backend/apps/workrecords/migrations/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
44
backend/apps/workrecords/models.py
Normal file
44
backend/apps/workrecords/models.py
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
|
class WorkRecord(models.Model):
|
||||||
|
class WorkType(models.TextChoices):
|
||||||
|
FERTILIZER_DELIVERY = 'fertilizer_delivery', '肥料運搬'
|
||||||
|
FERTILIZER_SPREADING = 'fertilizer_spreading', '肥料散布'
|
||||||
|
|
||||||
|
work_date = models.DateField(verbose_name='作業日')
|
||||||
|
work_type = models.CharField(
|
||||||
|
max_length=40,
|
||||||
|
choices=WorkType.choices,
|
||||||
|
verbose_name='作業種別',
|
||||||
|
)
|
||||||
|
title = models.CharField(max_length=200, verbose_name='タイトル')
|
||||||
|
year = models.IntegerField(verbose_name='年度')
|
||||||
|
auto_created = models.BooleanField(default=True, verbose_name='自動生成')
|
||||||
|
delivery_trip = models.OneToOneField(
|
||||||
|
'fertilizer.DeliveryTrip',
|
||||||
|
on_delete=models.CASCADE,
|
||||||
|
null=True,
|
||||||
|
blank=True,
|
||||||
|
related_name='work_record',
|
||||||
|
verbose_name='運搬回',
|
||||||
|
)
|
||||||
|
spreading_session = models.OneToOneField(
|
||||||
|
'fertilizer.SpreadingSession',
|
||||||
|
on_delete=models.CASCADE,
|
||||||
|
null=True,
|
||||||
|
blank=True,
|
||||||
|
related_name='work_record',
|
||||||
|
verbose_name='散布実績',
|
||||||
|
)
|
||||||
|
created_at = models.DateTimeField(auto_now_add=True)
|
||||||
|
updated_at = models.DateTimeField(auto_now=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
ordering = ['-work_date', '-updated_at', '-id']
|
||||||
|
verbose_name = '作業記録'
|
||||||
|
verbose_name_plural = '作業記録'
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f'{self.work_date} {self.get_work_type_display()}'
|
||||||
|
|
||||||
38
backend/apps/workrecords/serializers.py
Normal file
38
backend/apps/workrecords/serializers.py
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
from .models import WorkRecord
|
||||||
|
|
||||||
|
|
||||||
|
class WorkRecordSerializer(serializers.ModelSerializer):
|
||||||
|
work_type_display = serializers.CharField(source='get_work_type_display', read_only=True)
|
||||||
|
delivery_plan_id = serializers.SerializerMethodField()
|
||||||
|
delivery_plan_name = serializers.SerializerMethodField()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = WorkRecord
|
||||||
|
fields = [
|
||||||
|
'id',
|
||||||
|
'work_date',
|
||||||
|
'work_type',
|
||||||
|
'work_type_display',
|
||||||
|
'title',
|
||||||
|
'year',
|
||||||
|
'auto_created',
|
||||||
|
'delivery_trip',
|
||||||
|
'delivery_plan_id',
|
||||||
|
'delivery_plan_name',
|
||||||
|
'spreading_session',
|
||||||
|
'created_at',
|
||||||
|
'updated_at',
|
||||||
|
]
|
||||||
|
|
||||||
|
def get_delivery_plan_id(self, obj):
|
||||||
|
if obj.delivery_trip_id:
|
||||||
|
return obj.delivery_trip.delivery_plan_id
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_delivery_plan_name(self, obj):
|
||||||
|
if obj.delivery_trip_id:
|
||||||
|
return obj.delivery_trip.delivery_plan.name
|
||||||
|
return None
|
||||||
|
|
||||||
33
backend/apps/workrecords/services.py
Normal file
33
backend/apps/workrecords/services.py
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
from .models import WorkRecord
|
||||||
|
|
||||||
|
|
||||||
|
def sync_delivery_work_record(trip):
|
||||||
|
if trip.date is None:
|
||||||
|
WorkRecord.objects.filter(delivery_trip=trip).delete()
|
||||||
|
return
|
||||||
|
|
||||||
|
WorkRecord.objects.update_or_create(
|
||||||
|
delivery_trip=trip,
|
||||||
|
defaults={
|
||||||
|
'work_date': trip.date,
|
||||||
|
'work_type': WorkRecord.WorkType.FERTILIZER_DELIVERY,
|
||||||
|
'title': f'肥料運搬: {trip.delivery_plan.name} {trip.order + 1}回目',
|
||||||
|
'year': trip.delivery_plan.year,
|
||||||
|
'auto_created': True,
|
||||||
|
'spreading_session': None,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def sync_spreading_work_record(session):
|
||||||
|
WorkRecord.objects.update_or_create(
|
||||||
|
spreading_session=session,
|
||||||
|
defaults={
|
||||||
|
'work_date': session.date,
|
||||||
|
'work_type': WorkRecord.WorkType.FERTILIZER_SPREADING,
|
||||||
|
'title': f'肥料散布: {session.name.strip() or session.date}',
|
||||||
|
'year': session.year,
|
||||||
|
'auto_created': True,
|
||||||
|
'delivery_trip': None,
|
||||||
|
},
|
||||||
|
)
|
||||||
12
backend/apps/workrecords/urls.py
Normal file
12
backend/apps/workrecords/urls.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
from django.urls import include, path
|
||||||
|
from rest_framework.routers import DefaultRouter
|
||||||
|
|
||||||
|
from .views import WorkRecordViewSet
|
||||||
|
|
||||||
|
router = DefaultRouter()
|
||||||
|
router.register(r'', WorkRecordViewSet, basename='workrecord')
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path('', include(router.urls)),
|
||||||
|
]
|
||||||
|
|
||||||
22
backend/apps/workrecords/views.py
Normal file
22
backend/apps/workrecords/views.py
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
from rest_framework import viewsets
|
||||||
|
from rest_framework.permissions import IsAuthenticated
|
||||||
|
|
||||||
|
from .models import WorkRecord
|
||||||
|
from .serializers import WorkRecordSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class WorkRecordViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
|
serializer_class = WorkRecordSerializer
|
||||||
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
queryset = WorkRecord.objects.select_related(
|
||||||
|
'delivery_trip',
|
||||||
|
'delivery_trip__delivery_plan',
|
||||||
|
'spreading_session',
|
||||||
|
)
|
||||||
|
year = self.request.query_params.get('year')
|
||||||
|
if year:
|
||||||
|
queryset = queryset.filter(year=year)
|
||||||
|
return queryset
|
||||||
|
|
||||||
@@ -45,6 +45,7 @@ INSTALLED_APPS = [
|
|||||||
'apps.weather',
|
'apps.weather',
|
||||||
'apps.fertilizer',
|
'apps.fertilizer',
|
||||||
'apps.materials',
|
'apps.materials',
|
||||||
|
'apps.workrecords',
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
|
|||||||
@@ -59,4 +59,5 @@ urlpatterns = [
|
|||||||
path('api/weather/', include('apps.weather.urls')),
|
path('api/weather/', include('apps.weather.urls')),
|
||||||
path('api/fertilizer/', include('apps.fertilizer.urls')),
|
path('api/fertilizer/', include('apps.fertilizer.urls')),
|
||||||
path('api/materials/', include('apps.materials.urls')),
|
path('api/materials/', include('apps.materials.urls')),
|
||||||
|
path('api/workrecords/', include('apps.workrecords.urls')),
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { useEffect, useState, useMemo, useCallback } from 'react';
|
import { useEffect, useState, useMemo, useCallback } from 'react';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { Plus, X, ChevronUp, ChevronDown, Pencil, Check, Truck, ArrowLeft } from 'lucide-react';
|
import { Plus, X, ChevronUp, ChevronDown, Pencil, Check, Truck, ArrowLeft, Sprout } from 'lucide-react';
|
||||||
import Navbar from '@/components/Navbar';
|
import Navbar from '@/components/Navbar';
|
||||||
import { DeliveryPlan, DeliveryAllEntry } from '@/types';
|
import { DeliveryPlan, DeliveryAllEntry } from '@/types';
|
||||||
import { api } from '@/lib/api';
|
import { api } from '@/lib/api';
|
||||||
@@ -676,9 +676,20 @@ export default function DeliveryEditPage({ planId }: Props) {
|
|||||||
運搬計画一覧
|
運搬計画一覧
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
<h1 className="text-xl font-bold text-gray-900 mb-6">
|
<div className="mb-6 flex items-center justify-between gap-4">
|
||||||
|
<h1 className="text-xl font-bold text-gray-900">
|
||||||
{isEdit ? '運搬計画を編集' : '運搬計画を新規作成'}
|
{isEdit ? '運搬計画を編集' : '運搬計画を新規作成'}
|
||||||
</h1>
|
</h1>
|
||||||
|
{isEdit && planId && (
|
||||||
|
<button
|
||||||
|
onClick={() => router.push(`/fertilizer/spreading?year=${year}&delivery_plan=${planId}`)}
|
||||||
|
className="flex items-center gap-2 rounded-md border border-emerald-300 px-4 py-2 text-sm text-emerald-700 hover:bg-emerald-50"
|
||||||
|
>
|
||||||
|
<Sprout className="h-4 w-4" />
|
||||||
|
この運搬計画から散布実績へ進む
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
{saveError && (
|
{saveError && (
|
||||||
<div className="flex items-start gap-2 bg-red-50 border border-red-300 text-red-700 rounded-md px-4 py-3 mb-4 text-sm">
|
<div className="flex items-start gap-2 bg-red-50 border border-red-300 text-red-700 rounded-md px-4 py-3 mb-4 text-sm">
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { Truck, Plus, FileDown, Pencil, Trash2, X } from 'lucide-react';
|
import { Truck, Plus, FileDown, Pencil, Trash2, X, Sprout } from 'lucide-react';
|
||||||
import Navbar from '@/components/Navbar';
|
import Navbar from '@/components/Navbar';
|
||||||
import { api } from '@/lib/api';
|
import { api } from '@/lib/api';
|
||||||
import { DeliveryPlanListItem } from '@/types';
|
import { DeliveryPlanListItem } from '@/types';
|
||||||
@@ -75,6 +75,14 @@ export default function DeliveryListPage() {
|
|||||||
<Truck className="h-7 w-7 text-green-700" />
|
<Truck className="h-7 w-7 text-green-700" />
|
||||||
<h1 className="text-2xl font-bold text-gray-900">運搬計画</h1>
|
<h1 className="text-2xl font-bold text-gray-900">運搬計画</h1>
|
||||||
</div>
|
</div>
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<button
|
||||||
|
onClick={() => router.push('/fertilizer/spreading')}
|
||||||
|
className="flex items-center gap-2 rounded-md border border-emerald-300 px-4 py-2 text-sm font-medium text-emerald-700 hover:bg-emerald-50 transition-colors"
|
||||||
|
>
|
||||||
|
<Sprout className="h-4 w-4" />
|
||||||
|
散布実績
|
||||||
|
</button>
|
||||||
<button
|
<button
|
||||||
onClick={() => router.push('/distribution/new')}
|
onClick={() => router.push('/distribution/new')}
|
||||||
className="flex items-center gap-2 px-4 py-2 bg-green-600 text-white rounded-md hover:bg-green-700 transition-colors text-sm font-medium"
|
className="flex items-center gap-2 px-4 py-2 bg-green-600 text-white rounded-md hover:bg-green-700 transition-colors text-sm font-medium"
|
||||||
@@ -83,6 +91,7 @@ export default function DeliveryListPage() {
|
|||||||
新規作成
|
新規作成
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
{/* 年度セレクタ */}
|
{/* 年度セレクタ */}
|
||||||
<div className="flex items-center gap-3 mb-6">
|
<div className="flex items-center gap-3 mb-6">
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { useState, useEffect, useCallback } from 'react';
|
import { useState, useEffect, useCallback } from 'react';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { ChevronLeft, Plus, X, Calculator, Save, FileDown, Undo2 } from 'lucide-react';
|
import { ChevronLeft, Plus, X, Calculator, Save, FileDown, Undo2, Sprout } from 'lucide-react';
|
||||||
import Navbar from '@/components/Navbar';
|
import Navbar from '@/components/Navbar';
|
||||||
import { api } from '@/lib/api';
|
import { api } from '@/lib/api';
|
||||||
import { Crop, FertilizationPlan, Fertilizer, Field, StockSummary } from '@/types';
|
import { Crop, FertilizationPlan, Fertilizer, Field, StockSummary } from '@/types';
|
||||||
@@ -62,6 +62,7 @@ export default function FertilizerEditPage({ planId }: { planId?: number }) {
|
|||||||
// roundedColumns: 四捨五入済みの肥料列ID(↩ トグル用)
|
// roundedColumns: 四捨五入済みの肥料列ID(↩ トグル用)
|
||||||
const [calcMatrix, setCalcMatrix] = useState<Matrix>({});
|
const [calcMatrix, setCalcMatrix] = useState<Matrix>({});
|
||||||
const [adjusted, setAdjusted] = useState<Matrix>({});
|
const [adjusted, setAdjusted] = useState<Matrix>({});
|
||||||
|
const [actualMatrix, setActualMatrix] = useState<Matrix>({});
|
||||||
const [roundedColumns, setRoundedColumns] = useState<Set<number>>(new Set());
|
const [roundedColumns, setRoundedColumns] = useState<Set<number>>(new Set());
|
||||||
const [stockByMaterialId, setStockByMaterialId] = useState<Record<number, StockSummary>>({});
|
const [stockByMaterialId, setStockByMaterialId] = useState<Record<number, StockSummary>>({});
|
||||||
const [initialPlanTotals, setInitialPlanTotals] = useState<Record<number, number>>({});
|
const [initialPlanTotals, setInitialPlanTotals] = useState<Record<number, number>>({});
|
||||||
@@ -102,8 +103,8 @@ export default function FertilizerEditPage({ planId }: { planId?: number }) {
|
|||||||
setName(plan.name);
|
setName(plan.name);
|
||||||
setYear(plan.year);
|
setYear(plan.year);
|
||||||
setVarietyId(plan.variety);
|
setVarietyId(plan.variety);
|
||||||
setIsConfirmed(plan.is_confirmed);
|
setIsConfirmed(false);
|
||||||
setConfirmedAt(plan.confirmed_at);
|
setConfirmedAt(null);
|
||||||
|
|
||||||
const fertIds = Array.from(new Set(plan.entries.map((e) => e.fertilizer)));
|
const fertIds = Array.from(new Set(plan.entries.map((e) => e.fertilizer)));
|
||||||
const ferts = fertsRes.data.filter((f: Fertilizer) => fertIds.includes(f.id));
|
const ferts = fertsRes.data.filter((f: Fertilizer) => fertIds.includes(f.id));
|
||||||
@@ -122,11 +123,17 @@ export default function FertilizerEditPage({ planId }: { planId?: number }) {
|
|||||||
|
|
||||||
// 保存済みの値は adjusted に復元
|
// 保存済みの値は adjusted に復元
|
||||||
const newAdjusted: Matrix = {};
|
const newAdjusted: Matrix = {};
|
||||||
|
const newActualMatrix: Matrix = {};
|
||||||
plan.entries.forEach((e) => {
|
plan.entries.forEach((e) => {
|
||||||
if (!newAdjusted[e.field]) newAdjusted[e.field] = {};
|
if (!newAdjusted[e.field]) newAdjusted[e.field] = {};
|
||||||
newAdjusted[e.field][e.fertilizer] = String(e.bags);
|
newAdjusted[e.field][e.fertilizer] = String(e.bags);
|
||||||
|
if (e.actual_bags !== null && e.actual_bags !== undefined) {
|
||||||
|
if (!newActualMatrix[e.field]) newActualMatrix[e.field] = {};
|
||||||
|
newActualMatrix[e.field][e.fertilizer] = String(e.actual_bags);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
setAdjusted(newAdjusted);
|
setAdjusted(newAdjusted);
|
||||||
|
setActualMatrix(newActualMatrix);
|
||||||
setInitialPlanTotals(
|
setInitialPlanTotals(
|
||||||
plan.entries.reduce((acc: Record<number, number>, entry) => {
|
plan.entries.reduce((acc: Record<number, number>, entry) => {
|
||||||
acc[entry.fertilizer] = (acc[entry.fertilizer] ?? 0) + Number(entry.bags);
|
acc[entry.fertilizer] = (acc[entry.fertilizer] ?? 0) + Number(entry.bags);
|
||||||
@@ -498,6 +505,15 @@ export default function FertilizerEditPage({ planId }: { planId?: number }) {
|
|||||||
</h1>
|
</h1>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
|
{!isNew && planId && (
|
||||||
|
<button
|
||||||
|
onClick={() => router.push(`/fertilizer/spreading?year=${year}&plan=${planId}`)}
|
||||||
|
className="flex items-center gap-2 px-4 py-2 border border-emerald-300 rounded-lg text-sm text-emerald-700 hover:bg-emerald-50"
|
||||||
|
>
|
||||||
|
<Sprout className="h-4 w-4" />
|
||||||
|
この施肥計画から散布実績へ進む
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
{!isNew && isConfirmed && (
|
{!isNew && isConfirmed && (
|
||||||
<button
|
<button
|
||||||
onClick={handleUnconfirm}
|
onClick={handleUnconfirm}
|
||||||
@@ -775,12 +791,14 @@ export default function FertilizerEditPage({ planId }: { planId?: number }) {
|
|||||||
{planFertilizers.map((fert) => {
|
{planFertilizers.map((fert) => {
|
||||||
const calcVal = calcMatrix[field.id]?.[fert.id];
|
const calcVal = calcMatrix[field.id]?.[fert.id];
|
||||||
const adjVal = adjusted[field.id]?.[fert.id];
|
const adjVal = adjusted[field.id]?.[fert.id];
|
||||||
|
const actualVal = actualMatrix[field.id]?.[fert.id];
|
||||||
// 計算結果があればラベルを表示(adjusted が上書きされた場合は参照値として)
|
// 計算結果があればラベルを表示(adjusted が上書きされた場合は参照値として)
|
||||||
const showRef = calcVal !== undefined;
|
const showRef = calcVal !== undefined;
|
||||||
// 入力欄: adjusted → calc値 → 空
|
// 入力欄: adjusted → calc値 → 空
|
||||||
const inputValue = adjVal !== undefined ? adjVal : (calcVal ?? '');
|
const inputValue = adjVal !== undefined ? adjVal : (calcVal ?? '');
|
||||||
return (
|
return (
|
||||||
<td key={fert.id} className="px-2 py-1 border border-gray-200">
|
<td key={fert.id} className="px-2 py-1 border border-gray-200">
|
||||||
|
<div className="space-y-1">
|
||||||
<div className="flex items-center justify-end gap-1.5">
|
<div className="flex items-center justify-end gap-1.5">
|
||||||
{showRef && (
|
{showRef && (
|
||||||
<span className="text-gray-300 text-xs tabular-nums">{calcVal}</span>
|
<span className="text-gray-300 text-xs tabular-nums">{calcVal}</span>
|
||||||
@@ -795,6 +813,12 @@ export default function FertilizerEditPage({ planId }: { planId?: number }) {
|
|||||||
disabled={isConfirmed}
|
disabled={isConfirmed}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
{actualVal !== undefined && (
|
||||||
|
<div className="text-right text-[11px] text-sky-700">
|
||||||
|
実績 {actualVal}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</td>
|
</td>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
|
|||||||
@@ -1,30 +1,41 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useState, useEffect } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { Plus, Pencil, Trash2, FileDown, Sprout, BadgeCheck, Undo2 } from 'lucide-react';
|
import { FileDown, NotebookText, Pencil, Plus, Sprout, Trash2, Truck } from 'lucide-react';
|
||||||
|
|
||||||
import ConfirmSpreadingModal from './_components/ConfirmSpreadingModal';
|
|
||||||
import Navbar from '@/components/Navbar';
|
import Navbar from '@/components/Navbar';
|
||||||
import { api } from '@/lib/api';
|
import { api } from '@/lib/api';
|
||||||
import { FertilizationPlan } from '@/types';
|
import { FertilizationPlan } from '@/types';
|
||||||
|
|
||||||
const currentYear = new Date().getFullYear();
|
const currentYear = new Date().getFullYear();
|
||||||
|
|
||||||
|
const STATUS_LABELS: Record<FertilizationPlan['spread_status'], string> = {
|
||||||
|
unspread: '未散布',
|
||||||
|
partial: '一部散布',
|
||||||
|
completed: '散布済み',
|
||||||
|
over_applied: '超過散布',
|
||||||
|
};
|
||||||
|
|
||||||
|
const STATUS_CLASSES: Record<FertilizationPlan['spread_status'], string> = {
|
||||||
|
unspread: 'bg-gray-100 text-gray-700',
|
||||||
|
partial: 'bg-amber-100 text-amber-800',
|
||||||
|
completed: 'bg-emerald-100 text-emerald-800',
|
||||||
|
over_applied: 'bg-rose-100 text-rose-800',
|
||||||
|
};
|
||||||
|
|
||||||
export default function FertilizerPage() {
|
export default function FertilizerPage() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [year, setYear] = useState<number>(() => {
|
const [year, setYear] = useState<number>(() => {
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
const saved = localStorage.getItem('fertilizerYear');
|
const saved = localStorage.getItem('fertilizerYear');
|
||||||
if (saved) return parseInt(saved);
|
if (saved) return parseInt(saved, 10);
|
||||||
}
|
}
|
||||||
return currentYear;
|
return currentYear;
|
||||||
});
|
});
|
||||||
const [plans, setPlans] = useState<FertilizationPlan[]>([]);
|
const [plans, setPlans] = useState<FertilizationPlan[]>([]);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [deleteError, setDeleteError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const [actionError, setActionError] = useState<string | null>(null);
|
|
||||||
const [confirmTarget, setConfirmTarget] = useState<FertilizationPlan | null>(null);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
localStorage.setItem('fertilizerYear', String(year));
|
localStorage.setItem('fertilizerYear', String(year));
|
||||||
@@ -33,41 +44,31 @@ export default function FertilizerPage() {
|
|||||||
|
|
||||||
const fetchPlans = async () => {
|
const fetchPlans = async () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
|
setError(null);
|
||||||
try {
|
try {
|
||||||
const res = await api.get(`/fertilizer/plans/?year=${year}`);
|
const res = await api.get(`/fertilizer/plans/?year=${year}`);
|
||||||
setPlans(res.data);
|
setPlans(res.data);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
|
setError('施肥計画の読み込みに失敗しました。');
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDelete = async (id: number, name: string) => {
|
const handleDelete = async (id: number, name: string) => {
|
||||||
setDeleteError(null);
|
setError(null);
|
||||||
setActionError(null);
|
|
||||||
try {
|
try {
|
||||||
await api.delete(`/fertilizer/plans/${id}/`);
|
await api.delete(`/fertilizer/plans/${id}/`);
|
||||||
await fetchPlans();
|
await fetchPlans();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
setDeleteError(`「${name}」の削除に失敗しました`);
|
setError(`「${name}」の削除に失敗しました。`);
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleUnconfirm = async (id: number, name: string) => {
|
|
||||||
setActionError(null);
|
|
||||||
try {
|
|
||||||
await api.post(`/fertilizer/plans/${id}/unconfirm/`);
|
|
||||||
await fetchPlans();
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
setActionError(`「${name}」の確定取消に失敗しました`);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handlePdf = async (id: number, name: string) => {
|
const handlePdf = async (id: number, name: string) => {
|
||||||
setActionError(null);
|
setError(null);
|
||||||
try {
|
try {
|
||||||
const res = await api.get(`/fertilizer/plans/${id}/pdf/`, { responseType: 'blob' });
|
const res = await api.get(`/fertilizer/plans/${id}/pdf/`, { responseType: 'blob' });
|
||||||
const url = URL.createObjectURL(new Blob([res.data], { type: 'application/pdf' }));
|
const url = URL.createObjectURL(new Blob([res.data], { type: 'application/pdf' }));
|
||||||
@@ -78,7 +79,7 @@ export default function FertilizerPage() {
|
|||||||
URL.revokeObjectURL(url);
|
URL.revokeObjectURL(url);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
setActionError('PDF出力に失敗しました');
|
setError('PDF出力に失敗しました。');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -87,22 +88,36 @@ export default function FertilizerPage() {
|
|||||||
return (
|
return (
|
||||||
<div className="min-h-screen bg-gray-50">
|
<div className="min-h-screen bg-gray-50">
|
||||||
<Navbar />
|
<Navbar />
|
||||||
<div className="max-w-5xl mx-auto px-4 py-8">
|
<div className="mx-auto max-w-6xl px-4 py-8">
|
||||||
<div className="flex items-center justify-between mb-6">
|
<div className="mb-6 flex items-center justify-between">
|
||||||
<div className="flex items-center gap-3">
|
<div className="flex items-center gap-3">
|
||||||
<Sprout className="h-6 w-6 text-green-600" />
|
<Sprout className="h-6 w-6 text-green-600" />
|
||||||
<h1 className="text-2xl font-bold text-gray-800">施肥計画</h1>
|
<h1 className="text-2xl font-bold text-gray-800">施肥計画</h1>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center gap-3">
|
<div className="flex items-center gap-3">
|
||||||
|
<button
|
||||||
|
onClick={() => router.push('/workrecords')}
|
||||||
|
className="flex items-center gap-2 rounded-lg border border-gray-300 px-4 py-2 text-sm text-gray-700 hover:bg-gray-100"
|
||||||
|
>
|
||||||
|
<NotebookText className="h-4 w-4" />
|
||||||
|
作業記録
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => router.push('/fertilizer/spreading')}
|
||||||
|
className="flex items-center gap-2 rounded-lg border border-emerald-300 px-4 py-2 text-sm text-emerald-700 hover:bg-emerald-50"
|
||||||
|
>
|
||||||
|
<Truck className="h-4 w-4" />
|
||||||
|
散布実績
|
||||||
|
</button>
|
||||||
<button
|
<button
|
||||||
onClick={() => router.push('/fertilizer/masters')}
|
onClick={() => router.push('/fertilizer/masters')}
|
||||||
className="px-4 py-2 text-sm border border-gray-300 rounded-lg text-gray-700 hover:bg-gray-100"
|
className="rounded-lg border border-gray-300 px-4 py-2 text-sm text-gray-700 hover:bg-gray-100"
|
||||||
>
|
>
|
||||||
肥料マスタ
|
肥料マスタ
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
onClick={() => router.push('/fertilizer/new')}
|
onClick={() => router.push('/fertilizer/new')}
|
||||||
className="flex items-center gap-2 px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700"
|
className="flex items-center gap-2 rounded-lg bg-green-600 px-4 py-2 text-white hover:bg-green-700"
|
||||||
>
|
>
|
||||||
<Plus className="h-4 w-4" />
|
<Plus className="h-4 w-4" />
|
||||||
新規作成
|
新規作成
|
||||||
@@ -110,113 +125,76 @@ export default function FertilizerPage() {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* 年度セレクタ */}
|
<div className="mb-6 flex items-center gap-3">
|
||||||
<div className="flex items-center gap-3 mb-6">
|
|
||||||
<label className="text-sm font-medium text-gray-700">年度:</label>
|
<label className="text-sm font-medium text-gray-700">年度:</label>
|
||||||
<select
|
<select
|
||||||
value={year}
|
value={year}
|
||||||
onChange={(e) => setYear(parseInt(e.target.value))}
|
onChange={(e) => setYear(parseInt(e.target.value, 10))}
|
||||||
className="border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-green-500"
|
className="rounded-lg border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-green-500"
|
||||||
>
|
>
|
||||||
{years.map((y) => (
|
{years.map((y) => (
|
||||||
<option key={y} value={y}>{y}年度</option>
|
<option key={y} value={y}>
|
||||||
|
{y}年度
|
||||||
|
</option>
|
||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{deleteError && (
|
{error && (
|
||||||
<div className="mb-4 flex items-start gap-2 bg-red-50 border border-red-300 text-red-700 rounded-lg px-4 py-3 text-sm">
|
<div className="mb-4 rounded-lg border border-red-300 bg-red-50 px-4 py-3 text-sm text-red-700">
|
||||||
<span className="font-bold shrink-0">⚠</span>
|
{error}
|
||||||
<span>{deleteError}</span>
|
|
||||||
<button onClick={() => setDeleteError(null)} className="ml-auto shrink-0 text-red-400 hover:text-red-600">✕</button>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{actionError && (
|
|
||||||
<div className="mb-4 flex items-start gap-2 bg-red-50 border border-red-300 text-red-700 rounded-lg px-4 py-3 text-sm">
|
|
||||||
<span className="font-bold shrink-0">⚠</span>
|
|
||||||
<span>{actionError}</span>
|
|
||||||
<button onClick={() => setActionError(null)} className="ml-auto shrink-0 text-red-400 hover:text-red-600">✕</button>
|
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{loading ? (
|
{loading ? (
|
||||||
<p className="text-gray-500">読み込み中...</p>
|
<p className="text-gray-500">読み込み中...</p>
|
||||||
) : plans.length === 0 ? (
|
) : plans.length === 0 ? (
|
||||||
<div className="bg-white rounded-lg shadow p-12 text-center text-gray-400">
|
<div className="rounded-lg bg-white p-12 text-center text-gray-400 shadow">
|
||||||
<Sprout className="h-12 w-12 mx-auto mb-3 opacity-30" />
|
<Sprout className="mx-auto mb-3 h-12 w-12 opacity-30" />
|
||||||
<p>{year}年度の施肥計画はありません</p>
|
<p>{year}年度の施肥計画はありません</p>
|
||||||
<button
|
<button
|
||||||
onClick={() => router.push('/fertilizer/new')}
|
onClick={() => router.push('/fertilizer/new')}
|
||||||
className="mt-4 px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 text-sm"
|
className="mt-4 rounded-lg bg-green-600 px-4 py-2 text-sm text-white hover:bg-green-700"
|
||||||
>
|
>
|
||||||
最初の計画を作成する
|
最初の計画を作成する
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<div className="bg-white rounded-lg shadow overflow-hidden">
|
<div className="overflow-hidden rounded-lg bg-white shadow">
|
||||||
<table className="w-full text-sm">
|
<table className="w-full text-sm">
|
||||||
<thead className="bg-gray-50 border-b">
|
<thead className="border-b bg-gray-50">
|
||||||
<tr>
|
<tr>
|
||||||
<th className="text-left px-4 py-3 font-medium text-gray-700">計画名</th>
|
<th className="px-4 py-3 text-left font-medium text-gray-700">計画名</th>
|
||||||
<th className="text-left px-4 py-3 font-medium text-gray-700">作物 / 品種</th>
|
<th className="px-4 py-3 text-left font-medium text-gray-700">作物 / 品種</th>
|
||||||
<th className="text-left px-4 py-3 font-medium text-gray-700">状態</th>
|
<th className="px-4 py-3 text-left font-medium text-gray-700">散布状況</th>
|
||||||
<th className="text-right px-4 py-3 font-medium text-gray-700">圃場数</th>
|
<th className="px-4 py-3 text-right font-medium text-gray-700">計画</th>
|
||||||
<th className="text-right px-4 py-3 font-medium text-gray-700">肥料種数</th>
|
<th className="px-4 py-3 text-right font-medium text-gray-700">実績</th>
|
||||||
<th className="px-4 py-3"></th>
|
<th className="px-4 py-3 text-right font-medium text-gray-700">残</th>
|
||||||
|
<th className="px-4 py-3 text-right font-medium text-gray-700">圃場</th>
|
||||||
|
<th className="px-4 py-3" />
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody className="divide-y divide-gray-100">
|
<tbody className="divide-y divide-gray-100">
|
||||||
{plans.map((plan) => (
|
{plans.map((plan) => (
|
||||||
<tr
|
<tr key={plan.id} className="hover:bg-gray-50">
|
||||||
key={plan.id}
|
<td className="px-4 py-3 font-medium text-gray-900">{plan.name}</td>
|
||||||
className={plan.is_confirmed ? 'bg-sky-50 hover:bg-sky-100/60' : 'hover:bg-gray-50'}
|
|
||||||
>
|
|
||||||
<td className="px-4 py-3 font-medium">
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<span>{plan.name}</span>
|
|
||||||
{plan.is_confirmed && (
|
|
||||||
<span className="inline-flex items-center gap-1 rounded-full bg-sky-100 px-2 py-0.5 text-xs text-sky-700">
|
|
||||||
<BadgeCheck className="h-3.5 w-3.5" />
|
|
||||||
確定済み
|
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</td>
|
|
||||||
<td className="px-4 py-3 text-gray-600">
|
<td className="px-4 py-3 text-gray-600">
|
||||||
{plan.crop_name} / {plan.variety_name}
|
{plan.crop_name} / {plan.variety_name}
|
||||||
</td>
|
</td>
|
||||||
<td className="px-4 py-3 text-gray-600">
|
|
||||||
{plan.is_confirmed
|
|
||||||
? `散布確定 ${plan.confirmed_at ? new Date(plan.confirmed_at).toLocaleString('ja-JP') : ''}`
|
|
||||||
: '未確定'}
|
|
||||||
</td>
|
|
||||||
<td className="px-4 py-3 text-right text-gray-600">{plan.field_count}筆</td>
|
|
||||||
<td className="px-4 py-3 text-right text-gray-600">{plan.fertilizer_count}種</td>
|
|
||||||
<td className="px-4 py-3">
|
<td className="px-4 py-3">
|
||||||
<div className="flex items-center gap-2 justify-end">
|
<span className={`inline-flex rounded-full px-2.5 py-1 text-xs font-medium ${STATUS_CLASSES[plan.spread_status]}`}>
|
||||||
{!plan.is_confirmed ? (
|
{STATUS_LABELS[plan.spread_status]}
|
||||||
<button
|
</span>
|
||||||
onClick={() => setConfirmTarget(plan)}
|
</td>
|
||||||
className="flex items-center gap-1 px-2.5 py-1.5 text-xs border border-emerald-300 rounded hover:bg-emerald-50 text-emerald-700"
|
<td className="px-4 py-3 text-right tabular-nums text-gray-600">{plan.planned_total_bags}</td>
|
||||||
title="散布確定"
|
<td className="px-4 py-3 text-right tabular-nums text-gray-600">{plan.spread_total_bags}</td>
|
||||||
>
|
<td className="px-4 py-3 text-right tabular-nums text-gray-600">{plan.remaining_total_bags}</td>
|
||||||
<BadgeCheck className="h-3.5 w-3.5" />
|
<td className="px-4 py-3 text-right text-gray-600">{plan.field_count}筆</td>
|
||||||
散布確定
|
<td className="px-4 py-3">
|
||||||
</button>
|
<div className="flex items-center justify-end gap-2">
|
||||||
) : (
|
|
||||||
<button
|
|
||||||
onClick={() => handleUnconfirm(plan.id, plan.name)}
|
|
||||||
className="flex items-center gap-1 px-2.5 py-1.5 text-xs border border-amber-300 rounded hover:bg-amber-50 text-amber-700"
|
|
||||||
title="確定取消"
|
|
||||||
>
|
|
||||||
<Undo2 className="h-3.5 w-3.5" />
|
|
||||||
確定取消
|
|
||||||
</button>
|
|
||||||
)}
|
|
||||||
<button
|
<button
|
||||||
onClick={() => handlePdf(plan.id, plan.name)}
|
onClick={() => handlePdf(plan.id, plan.name)}
|
||||||
className="flex items-center gap-1 px-2.5 py-1.5 text-xs border border-gray-300 rounded hover:bg-gray-100 text-gray-700"
|
className="flex items-center gap-1 rounded border border-gray-300 px-2.5 py-1.5 text-xs text-gray-700 hover:bg-gray-100"
|
||||||
title="PDF出力"
|
title="PDF出力"
|
||||||
>
|
>
|
||||||
<FileDown className="h-3.5 w-3.5" />
|
<FileDown className="h-3.5 w-3.5" />
|
||||||
@@ -224,7 +202,7 @@ export default function FertilizerPage() {
|
|||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
onClick={() => router.push(`/fertilizer/${plan.id}/edit`)}
|
onClick={() => router.push(`/fertilizer/${plan.id}/edit`)}
|
||||||
className="flex items-center gap-1 px-2.5 py-1.5 text-xs border border-blue-300 rounded hover:bg-blue-50 text-blue-700"
|
className="flex items-center gap-1 rounded border border-blue-300 px-2.5 py-1.5 text-xs text-blue-700 hover:bg-blue-50"
|
||||||
title="編集"
|
title="編集"
|
||||||
>
|
>
|
||||||
<Pencil className="h-3.5 w-3.5" />
|
<Pencil className="h-3.5 w-3.5" />
|
||||||
@@ -232,7 +210,7 @@ export default function FertilizerPage() {
|
|||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
onClick={() => handleDelete(plan.id, plan.name)}
|
onClick={() => handleDelete(plan.id, plan.name)}
|
||||||
className="flex items-center gap-1 px-2.5 py-1.5 text-xs border border-red-300 rounded hover:bg-red-50 text-red-600"
|
className="flex items-center gap-1 rounded border border-red-300 px-2.5 py-1.5 text-xs text-red-600 hover:bg-red-50"
|
||||||
title="削除"
|
title="削除"
|
||||||
>
|
>
|
||||||
<Trash2 className="h-3.5 w-3.5" />
|
<Trash2 className="h-3.5 w-3.5" />
|
||||||
@@ -247,13 +225,6 @@ export default function FertilizerPage() {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<ConfirmSpreadingModal
|
|
||||||
isOpen={confirmTarget !== null}
|
|
||||||
plan={confirmTarget}
|
|
||||||
onClose={() => setConfirmTarget(null)}
|
|
||||||
onConfirmed={fetchPlans}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
747
frontend/src/app/fertilizer/spreading/page.tsx
Normal file
747
frontend/src/app/fertilizer/spreading/page.tsx
Normal file
@@ -0,0 +1,747 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useEffect, useMemo, useState } from 'react';
|
||||||
|
import { useRouter, useSearchParams } from 'next/navigation';
|
||||||
|
import { ChevronLeft, Pencil, Plus, Save, Sprout, Trash2, X } from 'lucide-react';
|
||||||
|
|
||||||
|
import Navbar from '@/components/Navbar';
|
||||||
|
import { api } from '@/lib/api';
|
||||||
|
import {
|
||||||
|
DeliveryPlan,
|
||||||
|
FertilizationPlan,
|
||||||
|
SpreadingCandidate,
|
||||||
|
SpreadingSession,
|
||||||
|
} from '@/types';
|
||||||
|
|
||||||
|
const CURRENT_YEAR = new Date().getFullYear();
|
||||||
|
const YEAR_KEY = 'spreadingYear';
|
||||||
|
|
||||||
|
type SourceType = 'delivery' | 'plan' | 'year';
|
||||||
|
|
||||||
|
type FormState = {
|
||||||
|
date: string;
|
||||||
|
name: string;
|
||||||
|
notes: string;
|
||||||
|
itemValues: Record<string, string>;
|
||||||
|
};
|
||||||
|
|
||||||
|
type MatrixField = {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
area_tan: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type MatrixFertilizer = {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
const candidateKey = (fieldId: number, fertilizerId: number) => `${fieldId}:${fertilizerId}`;
|
||||||
|
|
||||||
|
const toNumber = (value: string | number | null | undefined) => {
|
||||||
|
const parsed = Number(value ?? 0);
|
||||||
|
return Number.isFinite(parsed) ? parsed : 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
const formatDisplay = (value: string | number | null | undefined) => {
|
||||||
|
const num = toNumber(value);
|
||||||
|
if (Number.isInteger(num)) {
|
||||||
|
return String(num);
|
||||||
|
}
|
||||||
|
return num.toFixed(4).replace(/\.?0+$/, '');
|
||||||
|
};
|
||||||
|
|
||||||
|
const formatInputValue = (value: number) => {
|
||||||
|
if (value <= 0) return '0';
|
||||||
|
return value.toFixed(2).replace(/\.?0+$/, '');
|
||||||
|
};
|
||||||
|
|
||||||
|
const getDefaultDate = (year: number) => {
|
||||||
|
const today = new Date();
|
||||||
|
if (today.getFullYear() !== year) {
|
||||||
|
return `${year}-01-01`;
|
||||||
|
}
|
||||||
|
const month = String(today.getMonth() + 1).padStart(2, '0');
|
||||||
|
const day = String(today.getDate()).padStart(2, '0');
|
||||||
|
return `${year}-${month}-${day}`;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getSourceType = (deliveryPlanId: number | null, fertilizationPlanId: number | null): SourceType => {
|
||||||
|
if (deliveryPlanId) return 'delivery';
|
||||||
|
if (fertilizationPlanId) return 'plan';
|
||||||
|
return 'year';
|
||||||
|
};
|
||||||
|
|
||||||
|
const buildCreateInitialValues = (rows: SpreadingCandidate[], sourceType: SourceType) => {
|
||||||
|
const values: Record<string, string> = {};
|
||||||
|
rows.forEach((candidate) => {
|
||||||
|
let base = 0;
|
||||||
|
if (sourceType === 'delivery') {
|
||||||
|
base = toNumber(candidate.delivered_bags) - toNumber(candidate.spread_bags_other);
|
||||||
|
} else if (sourceType === 'plan') {
|
||||||
|
base = toNumber(candidate.planned_bags) - toNumber(candidate.spread_bags_other);
|
||||||
|
} else {
|
||||||
|
base = toNumber(candidate.delivered_bags) - toNumber(candidate.spread_bags_other);
|
||||||
|
}
|
||||||
|
values[candidateKey(candidate.field, candidate.fertilizer)] = formatInputValue(Math.max(base, 0));
|
||||||
|
});
|
||||||
|
return values;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function SpreadingPage() {
|
||||||
|
const router = useRouter();
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
const queryYear = Number(searchParams.get('year') || '0') || null;
|
||||||
|
const deliveryPlanId = Number(searchParams.get('delivery_plan') || '0') || null;
|
||||||
|
const fertilizationPlanId = Number(searchParams.get('plan') || '0') || null;
|
||||||
|
const sourceType = getSourceType(deliveryPlanId, fertilizationPlanId);
|
||||||
|
|
||||||
|
const [year, setYear] = useState<number>(() => {
|
||||||
|
if (typeof window !== 'undefined') {
|
||||||
|
return parseInt(localStorage.getItem(YEAR_KEY) || String(CURRENT_YEAR), 10);
|
||||||
|
}
|
||||||
|
return CURRENT_YEAR;
|
||||||
|
});
|
||||||
|
const [sessions, setSessions] = useState<SpreadingSession[]>([]);
|
||||||
|
const [candidates, setCandidates] = useState<SpreadingCandidate[]>([]);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [formLoading, setFormLoading] = useState(false);
|
||||||
|
const [saving, setSaving] = useState(false);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
const [editingSessionId, setEditingSessionId] = useState<number | null>(null);
|
||||||
|
const [form, setForm] = useState<FormState | null>(null);
|
||||||
|
const [openedFromQuery, setOpenedFromQuery] = useState(false);
|
||||||
|
const [openedFromSource, setOpenedFromSource] = useState(false);
|
||||||
|
const [sourceName, setSourceName] = useState<string | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (queryYear && queryYear !== year) {
|
||||||
|
setYear(queryYear);
|
||||||
|
}
|
||||||
|
}, [queryYear, year]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
localStorage.setItem(YEAR_KEY, String(year));
|
||||||
|
void fetchSessions();
|
||||||
|
setForm(null);
|
||||||
|
setEditingSessionId(null);
|
||||||
|
setOpenedFromQuery(false);
|
||||||
|
setOpenedFromSource(false);
|
||||||
|
}, [year]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const loadSource = async () => {
|
||||||
|
if (deliveryPlanId) {
|
||||||
|
try {
|
||||||
|
const res = await api.get(`/fertilizer/delivery/${deliveryPlanId}/`);
|
||||||
|
const plan: DeliveryPlan = res.data;
|
||||||
|
setSourceName(plan.name);
|
||||||
|
return;
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
setSourceName(`運搬計画 #${deliveryPlanId}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (fertilizationPlanId) {
|
||||||
|
try {
|
||||||
|
const res = await api.get(`/fertilizer/plans/${fertilizationPlanId}/`);
|
||||||
|
const plan: FertilizationPlan = res.data;
|
||||||
|
setSourceName(plan.name);
|
||||||
|
return;
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
setSourceName(`施肥計画 #${fertilizationPlanId}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setSourceName(null);
|
||||||
|
};
|
||||||
|
void loadSource();
|
||||||
|
}, [deliveryPlanId, fertilizationPlanId]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const sessionParam = searchParams.get('session');
|
||||||
|
if (!sessionParam || openedFromQuery || sessions.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const targetId = Number(sessionParam);
|
||||||
|
if (!targetId) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const target = sessions.find((session) => session.id === targetId);
|
||||||
|
if (target) {
|
||||||
|
void openEditor(target);
|
||||||
|
setOpenedFromQuery(true);
|
||||||
|
}
|
||||||
|
}, [openedFromQuery, searchParams, sessions]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const sessionParam = searchParams.get('session');
|
||||||
|
if (sessionParam || sourceType === 'year' || openedFromSource || form || formLoading) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void startCreate();
|
||||||
|
setOpenedFromSource(true);
|
||||||
|
}, [form, formLoading, openedFromSource, searchParams, sourceType]);
|
||||||
|
|
||||||
|
const fetchSessions = async () => {
|
||||||
|
setLoading(true);
|
||||||
|
setError(null);
|
||||||
|
try {
|
||||||
|
const res = await api.get(`/fertilizer/spreading/?year=${year}`);
|
||||||
|
setSessions(res.data);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
setError('散布実績の読み込みに失敗しました。');
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const loadCandidates = async (sessionId?: number) => {
|
||||||
|
const params = new URLSearchParams({ year: String(year) });
|
||||||
|
if (sessionId) {
|
||||||
|
params.set('session_id', String(sessionId));
|
||||||
|
}
|
||||||
|
if (deliveryPlanId) {
|
||||||
|
params.set('delivery_plan_id', String(deliveryPlanId));
|
||||||
|
}
|
||||||
|
if (fertilizationPlanId) {
|
||||||
|
params.set('plan_id', String(fertilizationPlanId));
|
||||||
|
}
|
||||||
|
const res = await api.get(`/fertilizer/spreading/candidates/?${params.toString()}`);
|
||||||
|
setCandidates(res.data);
|
||||||
|
return res.data as SpreadingCandidate[];
|
||||||
|
};
|
||||||
|
|
||||||
|
const startCreate = async () => {
|
||||||
|
setFormLoading(true);
|
||||||
|
setError(null);
|
||||||
|
try {
|
||||||
|
const loaded = await loadCandidates();
|
||||||
|
setEditingSessionId(null);
|
||||||
|
setForm({
|
||||||
|
date: getDefaultDate(year),
|
||||||
|
name: '',
|
||||||
|
notes: '',
|
||||||
|
itemValues: buildCreateInitialValues(loaded, sourceType),
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
setError('散布候補の読み込みに失敗しました。');
|
||||||
|
} finally {
|
||||||
|
setFormLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const openEditor = async (session: SpreadingSession) => {
|
||||||
|
setFormLoading(true);
|
||||||
|
setError(null);
|
||||||
|
try {
|
||||||
|
await loadCandidates(session.id);
|
||||||
|
const itemValues = session.items.reduce<Record<string, string>>((acc, item) => {
|
||||||
|
acc[candidateKey(item.field, item.fertilizer)] = String(item.actual_bags);
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
setEditingSessionId(session.id);
|
||||||
|
setForm({
|
||||||
|
date: session.date,
|
||||||
|
name: session.name,
|
||||||
|
notes: session.notes,
|
||||||
|
itemValues,
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
setError('散布候補の読み込みに失敗しました。');
|
||||||
|
} finally {
|
||||||
|
setFormLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const closeEditor = () => {
|
||||||
|
setEditingSessionId(null);
|
||||||
|
setForm(null);
|
||||||
|
setCandidates([]);
|
||||||
|
};
|
||||||
|
|
||||||
|
const candidateMap = useMemo(() => {
|
||||||
|
const map = new Map<string, SpreadingCandidate>();
|
||||||
|
candidates.forEach((candidate) => {
|
||||||
|
map.set(candidateKey(candidate.field, candidate.fertilizer), candidate);
|
||||||
|
});
|
||||||
|
return map;
|
||||||
|
}, [candidates]);
|
||||||
|
|
||||||
|
const matrixFields = useMemo<MatrixField[]>(() => {
|
||||||
|
const map = new Map<number, MatrixField>();
|
||||||
|
candidates.forEach((candidate) => {
|
||||||
|
if (!map.has(candidate.field)) {
|
||||||
|
map.set(candidate.field, {
|
||||||
|
id: candidate.field,
|
||||||
|
name: candidate.field_name,
|
||||||
|
area_tan: candidate.field_area_tan,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return Array.from(map.values()).sort((a, b) => a.name.localeCompare(b.name, 'ja'));
|
||||||
|
}, [candidates]);
|
||||||
|
|
||||||
|
const matrixFertilizers = useMemo<MatrixFertilizer[]>(() => {
|
||||||
|
const map = new Map<number, MatrixFertilizer>();
|
||||||
|
candidates.forEach((candidate) => {
|
||||||
|
if (!map.has(candidate.fertilizer)) {
|
||||||
|
map.set(candidate.fertilizer, {
|
||||||
|
id: candidate.fertilizer,
|
||||||
|
name: candidate.fertilizer_name,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return Array.from(map.values()).sort((a, b) => a.name.localeCompare(b.name, 'ja'));
|
||||||
|
}, [candidates]);
|
||||||
|
|
||||||
|
const handleItemChange = (fieldId: number, fertilizerId: number, value: string) => {
|
||||||
|
if (!form) return;
|
||||||
|
const key = candidateKey(fieldId, fertilizerId);
|
||||||
|
setForm({
|
||||||
|
...form,
|
||||||
|
itemValues: {
|
||||||
|
...form.itemValues,
|
||||||
|
[key]: value,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const getCellValue = (fieldId: number, fertilizerId: number) => {
|
||||||
|
if (!form) return '';
|
||||||
|
return form.itemValues[candidateKey(fieldId, fertilizerId)] ?? '0';
|
||||||
|
};
|
||||||
|
|
||||||
|
const selectedRows = useMemo(() => {
|
||||||
|
if (!form) return [];
|
||||||
|
return candidates.filter((candidate) => {
|
||||||
|
const value = toNumber(form.itemValues[candidateKey(candidate.field, candidate.fertilizer)] || '0');
|
||||||
|
return value > 0;
|
||||||
|
});
|
||||||
|
}, [candidates, form]);
|
||||||
|
|
||||||
|
const getRowTotal = (fieldId: number) => {
|
||||||
|
if (!form) return 0;
|
||||||
|
return matrixFertilizers.reduce((sum, fertilizer) => {
|
||||||
|
const candidate = candidateMap.get(candidateKey(fieldId, fertilizer.id));
|
||||||
|
if (!candidate) return sum;
|
||||||
|
return sum + toNumber(getCellValue(fieldId, fertilizer.id));
|
||||||
|
}, 0);
|
||||||
|
};
|
||||||
|
|
||||||
|
const getColumnTotal = (fertilizerId: number) => {
|
||||||
|
if (!form) return 0;
|
||||||
|
return matrixFields.reduce((sum, field) => {
|
||||||
|
const candidate = candidateMap.get(candidateKey(field.id, fertilizerId));
|
||||||
|
if (!candidate) return sum;
|
||||||
|
return sum + toNumber(getCellValue(field.id, fertilizerId));
|
||||||
|
}, 0);
|
||||||
|
};
|
||||||
|
|
||||||
|
const totalInputBags = selectedRows.reduce((sum, candidate) => {
|
||||||
|
return sum + toNumber(form?.itemValues[candidateKey(candidate.field, candidate.fertilizer)] || '0');
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
const handleSave = async () => {
|
||||||
|
if (!form) return;
|
||||||
|
setError(null);
|
||||||
|
if (!form.date) {
|
||||||
|
setError('散布日を入力してください。');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const items = selectedRows.map((candidate) => ({
|
||||||
|
field_id: candidate.field,
|
||||||
|
fertilizer_id: candidate.fertilizer,
|
||||||
|
actual_bags: toNumber(form.itemValues[candidateKey(candidate.field, candidate.fertilizer)] || '0'),
|
||||||
|
planned_bags_snapshot: toNumber(candidate.planned_bags),
|
||||||
|
delivered_bags_snapshot: toNumber(candidate.delivered_bags),
|
||||||
|
}));
|
||||||
|
|
||||||
|
if (items.length === 0) {
|
||||||
|
setError('散布実績を1件以上入力してください。');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setSaving(true);
|
||||||
|
try {
|
||||||
|
const payload = {
|
||||||
|
year,
|
||||||
|
date: form.date,
|
||||||
|
name: form.name,
|
||||||
|
notes: form.notes,
|
||||||
|
items,
|
||||||
|
};
|
||||||
|
if (editingSessionId) {
|
||||||
|
await api.put(`/fertilizer/spreading/${editingSessionId}/`, payload);
|
||||||
|
} else {
|
||||||
|
await api.post('/fertilizer/spreading/', payload);
|
||||||
|
}
|
||||||
|
await fetchSessions();
|
||||||
|
closeEditor();
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
setError('散布実績の保存に失敗しました。');
|
||||||
|
} finally {
|
||||||
|
setSaving(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDelete = async (sessionId: number) => {
|
||||||
|
setError(null);
|
||||||
|
try {
|
||||||
|
await api.delete(`/fertilizer/spreading/${sessionId}/`);
|
||||||
|
await fetchSessions();
|
||||||
|
if (editingSessionId === sessionId) {
|
||||||
|
closeEditor();
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
setError('散布実績の削除に失敗しました。');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const years = Array.from({ length: 5 }, (_, i) => CURRENT_YEAR + 1 - i);
|
||||||
|
|
||||||
|
const sourceSummary =
|
||||||
|
sourceType === 'delivery'
|
||||||
|
? '初期値は運搬計画値から散布済を引いた値です。'
|
||||||
|
: sourceType === 'plan'
|
||||||
|
? '初期値は施肥計画値から散布済を引いた値です。'
|
||||||
|
: '初期値は運搬済みから散布済を引いた値です。';
|
||||||
|
|
||||||
|
const sourceLabel =
|
||||||
|
sourceType === 'delivery'
|
||||||
|
? '運搬計画を選択した状態です'
|
||||||
|
: sourceType === 'plan'
|
||||||
|
? '施肥計画を選択した状態です'
|
||||||
|
: null;
|
||||||
|
|
||||||
|
const clearFilterHref = `/fertilizer/spreading?year=${year}`;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="min-h-screen bg-gray-50">
|
||||||
|
<Navbar />
|
||||||
|
<main className="mx-auto max-w-7xl px-4 py-8">
|
||||||
|
<div className="mb-6 flex items-center justify-between">
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<button onClick={() => router.push('/fertilizer')} className="text-gray-500 hover:text-gray-700">
|
||||||
|
<ChevronLeft className="h-5 w-5" />
|
||||||
|
</button>
|
||||||
|
<Sprout className="h-6 w-6 text-green-700" />
|
||||||
|
<h1 className="text-2xl font-bold text-gray-900">散布実績</h1>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
onClick={() => void startCreate()}
|
||||||
|
className="flex items-center gap-2 rounded-lg bg-green-600 px-4 py-2 text-sm font-medium text-white hover:bg-green-700"
|
||||||
|
>
|
||||||
|
<Plus className="h-4 w-4" />
|
||||||
|
新規記録
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="mb-6 flex items-center gap-3">
|
||||||
|
<label className="text-sm font-medium text-gray-700">年度:</label>
|
||||||
|
<select
|
||||||
|
value={year}
|
||||||
|
onChange={(e) => setYear(Number(e.target.value))}
|
||||||
|
className="rounded-lg border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-green-500"
|
||||||
|
>
|
||||||
|
{years.map((y) => (
|
||||||
|
<option key={y} value={y}>
|
||||||
|
{y}年度
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{sourceLabel && (
|
||||||
|
<div className="mb-6 flex items-center justify-between rounded-lg border border-emerald-200 bg-emerald-50 px-4 py-3">
|
||||||
|
<div>
|
||||||
|
<div className="text-sm font-medium text-emerald-900">{sourceLabel}</div>
|
||||||
|
<div className="mt-1 text-sm text-emerald-700">
|
||||||
|
{sourceName ?? (sourceType === 'delivery' ? `運搬計画 #${deliveryPlanId}` : `施肥計画 #${fertilizationPlanId}`)}
|
||||||
|
{' '}を起点に散布候補を絞り込んでいます。
|
||||||
|
</div>
|
||||||
|
<div className="mt-1 text-xs text-emerald-700">{sourceSummary}</div>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
onClick={() => router.push(clearFilterHref)}
|
||||||
|
className="flex items-center gap-1 rounded border border-emerald-300 px-3 py-1.5 text-xs text-emerald-700 hover:bg-emerald-100"
|
||||||
|
>
|
||||||
|
<X className="h-3.5 w-3.5" />
|
||||||
|
絞り込み解除
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{error && (
|
||||||
|
<div className="mb-4 rounded-lg border border-red-300 bg-red-50 px-4 py-3 text-sm text-red-700">
|
||||||
|
{error}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{(form || formLoading) && (
|
||||||
|
<section className="mb-8 rounded-lg border border-emerald-200 bg-white shadow-sm">
|
||||||
|
<div className="border-b border-emerald-100 px-5 py-4">
|
||||||
|
<h2 className="text-lg font-semibold text-gray-900">
|
||||||
|
{editingSessionId ? '散布実績を編集' : '散布実績を登録'}
|
||||||
|
</h2>
|
||||||
|
<p className="mt-1 text-sm text-gray-500">
|
||||||
|
施肥計画と同じ感覚で、圃場 × 肥料のマトリックスで実績を入力します。
|
||||||
|
</p>
|
||||||
|
<p className="mt-1 text-xs text-gray-500">{sourceSummary}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{formLoading || !form ? (
|
||||||
|
<div className="px-5 py-8 text-sm text-gray-500">候補を読み込み中...</div>
|
||||||
|
) : (
|
||||||
|
<div className="space-y-5 px-5 py-5">
|
||||||
|
<div className="grid gap-4 md:grid-cols-3">
|
||||||
|
<div>
|
||||||
|
<label className="mb-1 block text-xs font-medium text-gray-600">散布日</label>
|
||||||
|
<input
|
||||||
|
type="date"
|
||||||
|
value={form.date}
|
||||||
|
onChange={(e) => setForm({ ...form, date: e.target.value })}
|
||||||
|
className="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-green-500"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label className="mb-1 block text-xs font-medium text-gray-600">名称</label>
|
||||||
|
<input
|
||||||
|
value={form.name}
|
||||||
|
onChange={(e) => setForm({ ...form, name: e.target.value })}
|
||||||
|
placeholder="例: 3/17 元肥散布"
|
||||||
|
className="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-green-500"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label className="mb-1 block text-xs font-medium text-gray-600">備考</label>
|
||||||
|
<input
|
||||||
|
value={form.notes}
|
||||||
|
onChange={(e) => setForm({ ...form, notes: e.target.value })}
|
||||||
|
placeholder="任意"
|
||||||
|
className="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-green-500"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="overflow-x-auto rounded-lg border border-gray-200">
|
||||||
|
<table className="min-w-full border-collapse text-sm">
|
||||||
|
<thead className="bg-gray-50">
|
||||||
|
<tr>
|
||||||
|
<th className="w-48 border border-gray-200 px-4 py-3 text-left font-medium text-gray-700">
|
||||||
|
圃場
|
||||||
|
</th>
|
||||||
|
{matrixFertilizers.map((fertilizer) => (
|
||||||
|
<th
|
||||||
|
key={fertilizer.id}
|
||||||
|
className="min-w-[220px] border border-gray-200 px-3 py-3 text-center font-medium text-gray-700"
|
||||||
|
>
|
||||||
|
<div>{fertilizer.name}</div>
|
||||||
|
<div className="mt-1 text-[11px] font-normal text-gray-400">
|
||||||
|
入力計 {formatDisplay(getColumnTotal(fertilizer.id))}袋
|
||||||
|
</div>
|
||||||
|
</th>
|
||||||
|
))}
|
||||||
|
<th className="w-28 border border-gray-200 px-3 py-3 text-right font-medium text-gray-700">
|
||||||
|
行合計
|
||||||
|
</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{matrixFields.length === 0 ? (
|
||||||
|
<tr>
|
||||||
|
<td
|
||||||
|
colSpan={matrixFertilizers.length + 2}
|
||||||
|
className="border border-gray-200 px-4 py-8 text-center text-gray-400"
|
||||||
|
>
|
||||||
|
散布対象の候補がありません。
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
) : (
|
||||||
|
matrixFields.map((field) => (
|
||||||
|
<tr key={field.id} className="hover:bg-gray-50">
|
||||||
|
<td className="border border-gray-200 px-4 py-3 align-top">
|
||||||
|
<div className="font-medium text-gray-900">{field.name}</div>
|
||||||
|
<div className="text-xs text-gray-400">{field.area_tan}反</div>
|
||||||
|
</td>
|
||||||
|
{matrixFertilizers.map((fertilizer) => {
|
||||||
|
const candidate = candidateMap.get(candidateKey(field.id, fertilizer.id));
|
||||||
|
if (!candidate) {
|
||||||
|
return (
|
||||||
|
<td
|
||||||
|
key={fertilizer.id}
|
||||||
|
className="border border-gray-200 bg-gray-50 px-3 py-3 text-center text-xs text-gray-300"
|
||||||
|
>
|
||||||
|
-
|
||||||
|
</td>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<td key={fertilizer.id} className="border border-gray-200 px-3 py-3 align-top">
|
||||||
|
<div className="flex items-center justify-between gap-3">
|
||||||
|
<div className="grid flex-1 grid-cols-2 gap-x-3 gap-y-1 text-[11px] leading-5 text-gray-500">
|
||||||
|
<div className="whitespace-nowrap">
|
||||||
|
<span className="mr-1 text-gray-400">計画</span>
|
||||||
|
<span>{formatDisplay(candidate.planned_bags)}</span>
|
||||||
|
</div>
|
||||||
|
<div className="whitespace-nowrap">
|
||||||
|
<span className="mr-1 text-gray-400">
|
||||||
|
{sourceType === 'plan' ? '計画残' : '未散布'}
|
||||||
|
</span>
|
||||||
|
<span>
|
||||||
|
{formatDisplay(
|
||||||
|
sourceType === 'plan'
|
||||||
|
? Math.max(toNumber(candidate.planned_bags) - toNumber(candidate.spread_bags_other), 0)
|
||||||
|
: Math.max(toNumber(candidate.delivered_bags) - toNumber(candidate.spread_bags_other), 0)
|
||||||
|
)}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="whitespace-nowrap">
|
||||||
|
<span className="mr-1 text-gray-400">運搬</span>
|
||||||
|
<span>{formatDisplay(candidate.delivered_bags)}</span>
|
||||||
|
</div>
|
||||||
|
<div className="whitespace-nowrap">
|
||||||
|
<span className="mr-1 text-gray-400">散布済</span>
|
||||||
|
<span>{formatDisplay(candidate.spread_bags_other)}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
step="0.1"
|
||||||
|
value={getCellValue(field.id, fertilizer.id)}
|
||||||
|
onChange={(e) => handleItemChange(field.id, fertilizer.id, e.target.value)}
|
||||||
|
className="w-20 shrink-0 rounded border border-gray-300 px-2 py-1.5 text-right text-sm focus:outline-none focus:ring-2 focus:ring-green-500"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
<td className="border border-gray-200 px-3 py-3 text-right font-medium text-gray-700">
|
||||||
|
{formatDisplay(getRowTotal(field.id))}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</tbody>
|
||||||
|
{matrixFields.length > 0 && (
|
||||||
|
<tfoot className="bg-gray-50">
|
||||||
|
<tr>
|
||||||
|
<td className="border border-gray-200 px-4 py-3 font-medium text-gray-700">合計</td>
|
||||||
|
{matrixFertilizers.map((fertilizer) => (
|
||||||
|
<td
|
||||||
|
key={fertilizer.id}
|
||||||
|
className="border border-gray-200 px-3 py-3 text-right font-medium text-gray-700"
|
||||||
|
>
|
||||||
|
{formatDisplay(getColumnTotal(fertilizer.id))}
|
||||||
|
</td>
|
||||||
|
))}
|
||||||
|
<td className="border border-gray-200 px-3 py-3 text-right font-bold text-green-700">
|
||||||
|
{formatDisplay(totalInputBags)}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</tfoot>
|
||||||
|
)}
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<p className="text-sm text-gray-500">
|
||||||
|
入力中 {selectedRows.length}件 / 合計 {formatDisplay(totalInputBags)}袋
|
||||||
|
</p>
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<button
|
||||||
|
onClick={closeEditor}
|
||||||
|
className="rounded-lg border border-gray-300 px-4 py-2 text-sm text-gray-700 hover:bg-gray-100"
|
||||||
|
>
|
||||||
|
キャンセル
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => void handleSave()}
|
||||||
|
disabled={saving}
|
||||||
|
className="flex items-center gap-2 rounded-lg bg-green-600 px-4 py-2 text-sm font-medium text-white hover:bg-green-700 disabled:cursor-not-allowed disabled:opacity-50"
|
||||||
|
>
|
||||||
|
<Save className="h-4 w-4" />
|
||||||
|
{saving ? '保存中...' : '保存'}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</section>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<section className="rounded-lg bg-white shadow-sm">
|
||||||
|
<div className="border-b px-5 py-4">
|
||||||
|
<h2 className="text-lg font-semibold text-gray-900">登録済み散布実績</h2>
|
||||||
|
</div>
|
||||||
|
{loading ? (
|
||||||
|
<div className="px-5 py-8 text-sm text-gray-500">読み込み中...</div>
|
||||||
|
) : sessions.length === 0 ? (
|
||||||
|
<div className="px-5 py-8 text-sm text-gray-400">この年度の散布実績はまだありません。</div>
|
||||||
|
) : (
|
||||||
|
<div className="overflow-x-auto">
|
||||||
|
<table className="w-full text-sm">
|
||||||
|
<thead className="bg-gray-50">
|
||||||
|
<tr>
|
||||||
|
<th className="px-4 py-3 text-left font-medium text-gray-700">散布日</th>
|
||||||
|
<th className="px-4 py-3 text-left font-medium text-gray-700">名称</th>
|
||||||
|
<th className="px-4 py-3 text-right font-medium text-gray-700">明細数</th>
|
||||||
|
<th className="px-4 py-3 text-right font-medium text-gray-700">合計袋数</th>
|
||||||
|
<th className="px-4 py-3 text-right font-medium text-gray-700">作業記録</th>
|
||||||
|
<th className="px-4 py-3" />
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody className="divide-y divide-gray-100">
|
||||||
|
{sessions.map((session) => {
|
||||||
|
const totalBags = session.items.reduce((sum, item) => sum + toNumber(item.actual_bags), 0);
|
||||||
|
return (
|
||||||
|
<tr key={session.id} className="hover:bg-gray-50">
|
||||||
|
<td className="px-4 py-3 text-gray-700">{session.date}</td>
|
||||||
|
<td className="px-4 py-3">
|
||||||
|
<div className="font-medium text-gray-900">{session.name || '名称なし'}</div>
|
||||||
|
{session.notes && <div className="text-xs text-gray-400">{session.notes}</div>}
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-3 text-right text-gray-600">{session.items.length}</td>
|
||||||
|
<td className="px-4 py-3 text-right tabular-nums text-gray-600">{formatDisplay(totalBags)}</td>
|
||||||
|
<td className="px-4 py-3 text-right text-gray-600">
|
||||||
|
{session.work_record_id ? `#${session.work_record_id}` : '-'}
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-3">
|
||||||
|
<div className="flex items-center justify-end gap-2">
|
||||||
|
<button
|
||||||
|
onClick={() => void openEditor(session)}
|
||||||
|
className="flex items-center gap-1 rounded border border-blue-300 px-2.5 py-1.5 text-xs text-blue-700 hover:bg-blue-50"
|
||||||
|
>
|
||||||
|
<Pencil className="h-3.5 w-3.5" />
|
||||||
|
編集
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => void handleDelete(session.id)}
|
||||||
|
className="flex items-center gap-1 rounded border border-red-300 px-2.5 py-1.5 text-xs text-red-600 hover:bg-red-50"
|
||||||
|
>
|
||||||
|
<Trash2 className="h-3.5 w-3.5" />
|
||||||
|
削除
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</section>
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
138
frontend/src/app/workrecords/page.tsx
Normal file
138
frontend/src/app/workrecords/page.tsx
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
import { useRouter } from 'next/navigation';
|
||||||
|
import { ChevronLeft, NotebookText } from 'lucide-react';
|
||||||
|
|
||||||
|
import Navbar from '@/components/Navbar';
|
||||||
|
import { api } from '@/lib/api';
|
||||||
|
import { WorkRecord } from '@/types';
|
||||||
|
|
||||||
|
const CURRENT_YEAR = new Date().getFullYear();
|
||||||
|
const YEAR_KEY = 'workRecordYear';
|
||||||
|
|
||||||
|
export default function WorkRecordsPage() {
|
||||||
|
const router = useRouter();
|
||||||
|
const [year, setYear] = useState<number>(() => {
|
||||||
|
if (typeof window !== 'undefined') {
|
||||||
|
return parseInt(localStorage.getItem(YEAR_KEY) || String(CURRENT_YEAR), 10);
|
||||||
|
}
|
||||||
|
return CURRENT_YEAR;
|
||||||
|
});
|
||||||
|
const [records, setRecords] = useState<WorkRecord[]>([]);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
localStorage.setItem(YEAR_KEY, String(year));
|
||||||
|
void fetchRecords();
|
||||||
|
}, [year]);
|
||||||
|
|
||||||
|
const fetchRecords = async () => {
|
||||||
|
setLoading(true);
|
||||||
|
setError(null);
|
||||||
|
try {
|
||||||
|
const res = await api.get(`/workrecords/?year=${year}`);
|
||||||
|
setRecords(res.data);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
setError('作業記録の読み込みに失敗しました。');
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const moveToSource = (record: WorkRecord) => {
|
||||||
|
if (record.spreading_session) {
|
||||||
|
router.push(`/fertilizer/spreading?session=${record.spreading_session}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (record.delivery_plan_id) {
|
||||||
|
router.push(`/distribution/${record.delivery_plan_id}/edit`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const years = Array.from({ length: 5 }, (_, i) => CURRENT_YEAR + 1 - i);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="min-h-screen bg-gray-50">
|
||||||
|
<Navbar />
|
||||||
|
<main className="mx-auto max-w-6xl px-4 py-8">
|
||||||
|
<div className="mb-6 flex items-center gap-3">
|
||||||
|
<button onClick={() => router.push('/fertilizer')} className="text-gray-500 hover:text-gray-700">
|
||||||
|
<ChevronLeft className="h-5 w-5" />
|
||||||
|
</button>
|
||||||
|
<NotebookText className="h-6 w-6 text-green-700" />
|
||||||
|
<h1 className="text-2xl font-bold text-gray-900">作業記録</h1>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="mb-6 flex items-center gap-3">
|
||||||
|
<label className="text-sm font-medium text-gray-700">年度:</label>
|
||||||
|
<select
|
||||||
|
value={year}
|
||||||
|
onChange={(e) => setYear(Number(e.target.value))}
|
||||||
|
className="rounded-lg border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-green-500"
|
||||||
|
>
|
||||||
|
{years.map((y) => (
|
||||||
|
<option key={y} value={y}>
|
||||||
|
{y}年度
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{error && (
|
||||||
|
<div className="mb-4 rounded-lg border border-red-300 bg-red-50 px-4 py-3 text-sm text-red-700">
|
||||||
|
{error}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="overflow-hidden rounded-lg bg-white shadow-sm">
|
||||||
|
{loading ? (
|
||||||
|
<div className="px-5 py-8 text-sm text-gray-500">読み込み中...</div>
|
||||||
|
) : records.length === 0 ? (
|
||||||
|
<div className="px-5 py-8 text-sm text-gray-400">この年度の作業記録はまだありません。</div>
|
||||||
|
) : (
|
||||||
|
<table className="w-full text-sm">
|
||||||
|
<thead className="border-b bg-gray-50">
|
||||||
|
<tr>
|
||||||
|
<th className="px-4 py-3 text-left font-medium text-gray-700">作業日</th>
|
||||||
|
<th className="px-4 py-3 text-left font-medium text-gray-700">種別</th>
|
||||||
|
<th className="px-4 py-3 text-left font-medium text-gray-700">タイトル</th>
|
||||||
|
<th className="px-4 py-3 text-left font-medium text-gray-700">参照先</th>
|
||||||
|
<th className="px-4 py-3" />
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody className="divide-y divide-gray-100">
|
||||||
|
{records.map((record) => (
|
||||||
|
<tr key={record.id} className="hover:bg-gray-50">
|
||||||
|
<td className="px-4 py-3 text-gray-700">{record.work_date}</td>
|
||||||
|
<td className="px-4 py-3 text-gray-700">{record.work_type_display}</td>
|
||||||
|
<td className="px-4 py-3 font-medium text-gray-900">{record.title}</td>
|
||||||
|
<td className="px-4 py-3 text-gray-600">
|
||||||
|
{record.spreading_session
|
||||||
|
? `散布実績 #${record.spreading_session}`
|
||||||
|
: record.delivery_plan_name
|
||||||
|
? `${record.delivery_plan_name}`
|
||||||
|
: '-'}
|
||||||
|
</td>
|
||||||
|
<td className="px-4 py-3 text-right">
|
||||||
|
{(record.spreading_session || record.delivery_plan_id) && (
|
||||||
|
<button
|
||||||
|
onClick={() => moveToSource(record)}
|
||||||
|
className="rounded border border-gray-300 px-2.5 py-1.5 text-xs text-gray-700 hover:bg-gray-100"
|
||||||
|
>
|
||||||
|
開く
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
))}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useRouter, usePathname } from 'next/navigation';
|
import { useRouter, usePathname } from 'next/navigation';
|
||||||
import { LogOut, Wheat, MapPin, FileText, Upload, LayoutDashboard, Mail, History, Shield, KeyRound, Cloud, Sprout, FlaskConical, Package } from 'lucide-react';
|
import { LogOut, Wheat, MapPin, FileText, Upload, LayoutDashboard, History, Shield, KeyRound, Cloud, Sprout, FlaskConical, Package, NotebookText, PencilLine } from 'lucide-react';
|
||||||
import { logout } from '@/lib/api';
|
import { logout } from '@/lib/api';
|
||||||
|
|
||||||
export default function Navbar() {
|
export default function Navbar() {
|
||||||
@@ -114,7 +114,7 @@ export default function Navbar() {
|
|||||||
<button
|
<button
|
||||||
onClick={() => router.push('/fertilizer')}
|
onClick={() => router.push('/fertilizer')}
|
||||||
className={`flex items-center px-3 py-2 text-sm rounded-md transition-colors ${
|
className={`flex items-center px-3 py-2 text-sm rounded-md transition-colors ${
|
||||||
pathname?.startsWith('/fertilizer')
|
pathname?.startsWith('/fertilizer') && !pathname?.startsWith('/fertilizer/spreading')
|
||||||
? 'text-green-700 bg-green-50'
|
? 'text-green-700 bg-green-50'
|
||||||
: 'text-gray-700 hover:text-gray-900 hover:bg-gray-100'
|
: 'text-gray-700 hover:text-gray-900 hover:bg-gray-100'
|
||||||
}`}
|
}`}
|
||||||
@@ -122,6 +122,17 @@ export default function Navbar() {
|
|||||||
<Sprout className="h-4 w-4 mr-2" />
|
<Sprout className="h-4 w-4 mr-2" />
|
||||||
施肥計画
|
施肥計画
|
||||||
</button>
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => router.push('/fertilizer/spreading')}
|
||||||
|
className={`flex items-center px-3 py-2 text-sm rounded-md transition-colors ${
|
||||||
|
pathname?.startsWith('/fertilizer/spreading')
|
||||||
|
? 'text-green-700 bg-green-50'
|
||||||
|
: 'text-gray-700 hover:text-gray-900 hover:bg-gray-100'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<PencilLine className="h-4 w-4 mr-2" />
|
||||||
|
散布実績
|
||||||
|
</button>
|
||||||
<button
|
<button
|
||||||
onClick={() => router.push('/distribution')}
|
onClick={() => router.push('/distribution')}
|
||||||
className={`flex items-center px-3 py-2 text-sm rounded-md transition-colors ${
|
className={`flex items-center px-3 py-2 text-sm rounded-md transition-colors ${
|
||||||
@@ -144,6 +155,17 @@ export default function Navbar() {
|
|||||||
<Package className="h-4 w-4 mr-2" />
|
<Package className="h-4 w-4 mr-2" />
|
||||||
在庫管理
|
在庫管理
|
||||||
</button>
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => router.push('/workrecords')}
|
||||||
|
className={`flex items-center px-3 py-2 text-sm rounded-md transition-colors ${
|
||||||
|
pathname?.startsWith('/workrecords')
|
||||||
|
? 'text-green-700 bg-green-50'
|
||||||
|
: 'text-gray-700 hover:text-gray-900 hover:bg-gray-100'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<NotebookText className="h-4 w-4 mr-2" />
|
||||||
|
作業記録
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center space-x-1">
|
<div className="flex items-center space-x-1">
|
||||||
|
|||||||
@@ -140,7 +140,8 @@ export interface FertilizationEntry {
|
|||||||
field_area_tan?: string;
|
field_area_tan?: string;
|
||||||
fertilizer: number;
|
fertilizer: number;
|
||||||
fertilizer_name?: string;
|
fertilizer_name?: string;
|
||||||
bags: number;
|
bags: number | string;
|
||||||
|
actual_bags?: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface FertilizationPlan {
|
export interface FertilizationPlan {
|
||||||
@@ -154,6 +155,10 @@ export interface FertilizationPlan {
|
|||||||
entries: FertilizationEntry[];
|
entries: FertilizationEntry[];
|
||||||
field_count: number;
|
field_count: number;
|
||||||
fertilizer_count: number;
|
fertilizer_count: number;
|
||||||
|
planned_total_bags: string;
|
||||||
|
spread_total_bags: string;
|
||||||
|
remaining_total_bags: string;
|
||||||
|
spread_status: 'unspread' | 'partial' | 'completed' | 'over_applied';
|
||||||
is_confirmed: boolean;
|
is_confirmed: boolean;
|
||||||
confirmed_at: string | null;
|
confirmed_at: string | null;
|
||||||
created_at: string;
|
created_at: string;
|
||||||
@@ -180,6 +185,8 @@ export interface DeliveryTripItem {
|
|||||||
fertilizer: number;
|
fertilizer: number;
|
||||||
fertilizer_name: string;
|
fertilizer_name: string;
|
||||||
bags: string;
|
bags: string;
|
||||||
|
spread_bags: string;
|
||||||
|
remaining_bags: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DeliveryTrip {
|
export interface DeliveryTrip {
|
||||||
@@ -187,6 +194,7 @@ export interface DeliveryTrip {
|
|||||||
order: number;
|
order: number;
|
||||||
name: string;
|
name: string;
|
||||||
date: string | null;
|
date: string | null;
|
||||||
|
work_record_id: number | null;
|
||||||
items: DeliveryTripItem[];
|
items: DeliveryTripItem[];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,6 +205,7 @@ export interface DeliveryAllEntry {
|
|||||||
fertilizer: number;
|
fertilizer: number;
|
||||||
fertilizer_name: string;
|
fertilizer_name: string;
|
||||||
bags: string;
|
bags: string;
|
||||||
|
actual_bags?: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DeliveryPlan {
|
export interface DeliveryPlan {
|
||||||
@@ -222,6 +231,59 @@ export interface DeliveryPlanListItem {
|
|||||||
updated_at: string;
|
updated_at: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface SpreadingCandidate {
|
||||||
|
field: number;
|
||||||
|
field_name: string;
|
||||||
|
field_area_tan: string;
|
||||||
|
fertilizer: number;
|
||||||
|
fertilizer_name: string;
|
||||||
|
planned_bags: string;
|
||||||
|
delivered_bags: string;
|
||||||
|
spread_bags: string;
|
||||||
|
spread_bags_other: string;
|
||||||
|
current_session_bags: string;
|
||||||
|
remaining_bags: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SpreadingSessionItem {
|
||||||
|
id: number;
|
||||||
|
field: number;
|
||||||
|
field_name: string;
|
||||||
|
fertilizer: number;
|
||||||
|
fertilizer_name: string;
|
||||||
|
actual_bags: string;
|
||||||
|
planned_bags_snapshot: string;
|
||||||
|
delivered_bags_snapshot: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SpreadingSession {
|
||||||
|
id: number;
|
||||||
|
year: number;
|
||||||
|
date: string;
|
||||||
|
name: string;
|
||||||
|
notes: string;
|
||||||
|
work_record_id: number | null;
|
||||||
|
items: SpreadingSessionItem[];
|
||||||
|
created_at: string;
|
||||||
|
updated_at: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface WorkRecord {
|
||||||
|
id: number;
|
||||||
|
work_date: string;
|
||||||
|
work_type: 'fertilizer_delivery' | 'fertilizer_spreading';
|
||||||
|
work_type_display: string;
|
||||||
|
title: string;
|
||||||
|
year: number;
|
||||||
|
auto_created: boolean;
|
||||||
|
delivery_trip: number | null;
|
||||||
|
delivery_plan_id: number | null;
|
||||||
|
delivery_plan_name: string | null;
|
||||||
|
spreading_session: number | null;
|
||||||
|
created_at: string;
|
||||||
|
updated_at: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface MailSender {
|
export interface MailSender {
|
||||||
id: number;
|
id: number;
|
||||||
type: 'address' | 'domain';
|
type: 'address' | 'domain';
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user