from apps.fim.models import PriceSet, FeeSet, Fee
from apps.utils.viewsets import CustomModelViewSet, CustomGenericViewSet
from rest_framework.mixins import ListModelMixin, UpdateModelMixin
from apps.fim.serializers import (PriceSetSerializer, FeeSetSerializer, FeeSerializer)
from rest_framework.views import APIView
from rest_framework.response import Response
from drf_yasg.utils import swagger_auto_schema
from apps.fim.serializers import CostStatSerializer
from apps.mtm.models import Material
import datetime
from apps.enm.models import EnStat, MpointStat
from django.db.models import Sum
from apps.fim.services import get_price_unit, get_cost_unit

# Create your views here.
class FeeViewSet(ListModelMixin, CustomGenericViewSet):
    """
    list:费用类型

    费用类型
    """
    perms_map = {'get': '*'}
    serializer_class = FeeSerializer
    queryset = Fee.objects.all()


class PriceSetViewSet(CustomModelViewSet):
    """
    list:物料价格配置

    物料价格配置
    """
    queryset = PriceSet.objects.all()
    serializer_class = PriceSetSerializer
    select_related_fields = ['material']
    filterset_fields = ['material', 'year', 'month']
    search_fields = ['material__name']


class FeeSetViewSet(CustomModelViewSet):
    """
    list:工段成本配置

    工段成本配置
    """
    queryset = FeeSet.objects.all()
    serializer_class = FeeSetSerializer
    select_related_fields = ['fee', 'mgroup']
    filterset_fields = {
            "fee": ["exact"],
            "mgroup__name": ["exact", "in"],
            "mgroup": ["exact"],
            "month": ["exact"],
            "year": ["exact"],
            "mgroup__cate": ["exact", "in"]
        }
    search_fields = ['fee__name', 'mgroup__name']
