Backend: - apps/materials 新規作成(Material, FertilizerProfile, PesticideProfile, StockTransaction) - 資材マスタ CRUD API(/api/materials/materials/) - 入出庫履歴 API(/api/materials/stock-transactions/) - 在庫集計 API(/api/materials/stock-summary/) - 既存 Fertilizer に material OneToOneField 追加(0005マイグレーション、データ移行込み) Frontend: - /materials: 在庫一覧画面(タブフィルタ、履歴展開、入出庫モーダル) - /materials/masters: 資材マスタ管理(肥料/農薬/その他タブ、インライン編集) - Navbar に「在庫管理」メニュー追加 - Material/StockTransaction/StockSummary 型定義追加 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
135 lines
4.7 KiB
Python
135 lines
4.7 KiB
Python
from decimal import Decimal
|
|
|
|
from rest_framework import generics, status, viewsets
|
|
from rest_framework.permissions import IsAuthenticated
|
|
from rest_framework.response import Response
|
|
|
|
from .models import Material, StockTransaction
|
|
from .serializers import (
|
|
MaterialReadSerializer,
|
|
MaterialWriteSerializer,
|
|
StockSummarySerializer,
|
|
StockTransactionSerializer,
|
|
)
|
|
|
|
|
|
class MaterialViewSet(viewsets.ModelViewSet):
|
|
"""資材マスタ CRUD"""
|
|
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
def get_queryset(self):
|
|
queryset = Material.objects.select_related(
|
|
'fertilizer_profile',
|
|
'pesticide_profile',
|
|
).prefetch_related('stock_transactions')
|
|
|
|
material_type = self.request.query_params.get('material_type')
|
|
if material_type:
|
|
queryset = queryset.filter(material_type=material_type)
|
|
|
|
active = self.request.query_params.get('active')
|
|
if active is not None:
|
|
queryset = queryset.filter(is_active=active.lower() == 'true')
|
|
|
|
return queryset
|
|
|
|
def get_serializer_class(self):
|
|
if self.action in ['create', 'update', 'partial_update']:
|
|
return MaterialWriteSerializer
|
|
return MaterialReadSerializer
|
|
|
|
def destroy(self, request, *args, **kwargs):
|
|
instance = self.get_object()
|
|
if instance.stock_transactions.exists():
|
|
return Response(
|
|
{'detail': 'この資材には入出庫履歴があるため削除できません。無効化してください。'},
|
|
status=status.HTTP_400_BAD_REQUEST,
|
|
)
|
|
return super().destroy(request, *args, **kwargs)
|
|
|
|
|
|
class StockTransactionViewSet(viewsets.ModelViewSet):
|
|
"""入出庫履歴 CRUD"""
|
|
|
|
serializer_class = StockTransactionSerializer
|
|
permission_classes = [IsAuthenticated]
|
|
http_method_names = ['get', 'post', 'delete', 'head', 'options']
|
|
|
|
def get_queryset(self):
|
|
queryset = StockTransaction.objects.select_related('material')
|
|
|
|
material_id = self.request.query_params.get('material_id')
|
|
if material_id:
|
|
queryset = queryset.filter(material_id=material_id)
|
|
|
|
material_type = self.request.query_params.get('material_type')
|
|
if material_type:
|
|
queryset = queryset.filter(material__material_type=material_type)
|
|
|
|
date_from = self.request.query_params.get('date_from')
|
|
if date_from:
|
|
queryset = queryset.filter(occurred_on__gte=date_from)
|
|
|
|
date_to = self.request.query_params.get('date_to')
|
|
if date_to:
|
|
queryset = queryset.filter(occurred_on__lte=date_to)
|
|
|
|
return queryset
|
|
|
|
|
|
class StockSummaryView(generics.ListAPIView):
|
|
"""在庫集計一覧"""
|
|
|
|
serializer_class = StockSummarySerializer
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
def get_queryset(self):
|
|
return Material.objects.none()
|
|
|
|
def list(self, request, *args, **kwargs):
|
|
queryset = Material.objects.prefetch_related('stock_transactions').order_by(
|
|
'material_type',
|
|
'name',
|
|
)
|
|
|
|
material_type = request.query_params.get('material_type')
|
|
if material_type:
|
|
queryset = queryset.filter(material_type=material_type)
|
|
|
|
active = request.query_params.get('active')
|
|
if active is not None:
|
|
queryset = queryset.filter(is_active=active.lower() == 'true')
|
|
|
|
results = []
|
|
for material in queryset:
|
|
transactions = list(material.stock_transactions.all())
|
|
increase = sum(
|
|
txn.quantity
|
|
for txn in transactions
|
|
if txn.transaction_type in StockTransaction.INCREASE_TYPES
|
|
)
|
|
decrease = sum(
|
|
txn.quantity
|
|
for txn in transactions
|
|
if txn.transaction_type in StockTransaction.DECREASE_TYPES
|
|
)
|
|
last_date = max((txn.occurred_on for txn in transactions), default=None)
|
|
results.append(
|
|
{
|
|
'material_id': material.id,
|
|
'name': material.name,
|
|
'material_type': material.material_type,
|
|
'material_type_display': material.get_material_type_display(),
|
|
'maker': material.maker,
|
|
'stock_unit': material.stock_unit,
|
|
'stock_unit_display': material.get_stock_unit_display(),
|
|
'is_active': material.is_active,
|
|
'current_stock': increase - decrease if transactions else Decimal('0'),
|
|
'last_transaction_date': last_date,
|
|
}
|
|
)
|
|
|
|
serializer = self.get_serializer(results, many=True)
|
|
return Response(serializer.data)
|