from rest_framework.viewsets import ModelViewSet
from .models import GoodsReception
from .serializers import GoodsReceptionSerializer
from warehouse.models import Stock

class GoodsReceptionViewSet(ModelViewSet):
    """
    API для работы с приемкой товаров.
    """
    queryset = GoodsReception.objects.all()
    serializer_class = GoodsReceptionSerializer

    def perform_create(self, serializer):
        reception = serializer.save()
        if reception.is_accepted:
            # Добавление на склад
            stock, created = Stock.objects.get_or_create(
                product=reception.product,
                location="Основной склад",
                defaults={'quantity': 0},
            )
            stock.quantity += reception.quantity
            stock.save()
