#coding=utf-8
import django_filters
from django.conf import settings
from django.db.models import Q
from models import PurchasePlan, PurchaseOrder, PurchaseUser, GodownEntry, PurchasePayment, PurchaseOrderDetail, \
    GodownEntryReturn, GodownEntryReturnDetail, PurchasePrice
from apps.base import clean_datetime_range



class PurchasePlanFilter(django_filters.FilterSet):
    no = django_filters.CharFilter(name='no', lookup_expr='icontains')
    name = django_filters.CharFilter(name='name', lookup_expr='icontains')
    notes = django_filters.CharFilter(name='notes', lookup_expr='icontains')

    class Meta:
        model = PurchasePlan
        fields = ('no', 'name', 'status', 'notes')

    # def filter_status(self, queryset, *args):
    #     if args[1]:
    #         value = int(args[1])
    #         if value != 3:
    #             queryset = queryset.filter(status=value)
    #         else:
    #             queryset = queryset.filter(~Q(status=settings.PASS))
    #     return queryset


class PurchasePriceFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(name='purchase_detail__product__name', lookup_expr='icontains')
    model = django_filters.CharFilter(name='purchase_detail__product__model', lookup_expr='icontains')

    class Meta:
        model = PurchaseUser
        fields = "__all__"


class PurchaseOrderFilter(django_filters.FilterSet):
    create_time = django_filters.DateTimeFromToRangeFilter(field_name='create_time')
    no = django_filters.CharFilter(name='no', lookup_expr='icontains')
    supplier_name = django_filters.CharFilter(name='supplier__name', lookup_expr='icontains')
    status = django_filters.CharFilter(field_name='status')
    notes = django_filters.CharFilter(name='notes', lookup_expr='icontains')

    class Meta:
        model = PurchaseOrder
        fields = (
            'create_time', 'no', 'supplier_name', 'status', 'arrval', 'notes', )

    def __init__(self, data=None, *args, **kwargs):
        data = clean_datetime_range(data, 'create_time','source')
        super(PurchaseOrderFilter, self).__init__(data, *args, **kwargs)


class PurchaseOrderDetailFilter(django_filters.FilterSet):
    supplier_name = django_filters.CharFilter(name='main__supplier__name', lookup_expr='icontains')
    order_no = django_filters.CharFilter(name='main__no', lookup_expr='icontains')

    class Meta:
        model = PurchaseOrderDetail
        fields = ('order_no', 'supplier_name', 'check_status',)

    def __init__(self, data=None, *args, **kwargs):
        data = clean_datetime_range(data, 'create_time')
        super(PurchaseOrderDetailFilter, self).__init__(data, *args, **kwargs)


class GodownEntryFilter(django_filters.FilterSet):
    no = django_filters.CharFilter(name='no', lookup_expr='icontains')
    supplier_text = django_filters.CharFilter(name='supplier__name', lookup_expr='icontains')
    purchase_order_no = django_filters.CharFilter(name='purchase_order__no', lookup_expr='icontains')
    create_user_text = django_filters.CharFilter(name='create_user__name', lookup_expr='icontains')
    create_time = django_filters.DateTimeFromToRangeFilter(field_name='create_time')
    check_time = django_filters.DateTimeFromToRangeFilter(field_name='check_time')
    class Meta:
        model = GodownEntry
        fields = "__all__"
    
    def __init__(self, data=None, *args, **kwargs):
        data = clean_datetime_range(data, 'create_time', 'source')
        data = clean_datetime_range(data, 'check_time')
        super(GodownEntryFilter, self).__init__(data, *args, **kwargs)


class GodownEntryReturnFilter(django_filters.FilterSet):
    no = django_filters.CharFilter(name='no', lookup_expr='icontains')
    supplier_text = django_filters.CharFilter(name='supplier__name', lookup_expr='icontains')
    create_user_text = django_filters.CharFilter(name='create_user__name', lookup_expr='icontains')
    create_time = django_filters.DateTimeFromToRangeFilter(field_name='create_time')
    check_time = django_filters.DateTimeFromToRangeFilter(field_name='check_time')

    class Meta:
        model = GodownEntryReturn
        fields = "__all__"

    def __init__(self, data=None, *args, **kwargs):
        data = clean_datetime_range(data, 'create_time', 'source')
        data = clean_datetime_range(data, 'check_time')
        super(GodownEntryReturnFilter, self).__init__(data, *args, **kwargs)


class GodownEntryReturnDetailFilter(django_filters.FilterSet):
    main_no = django_filters.CharFilter(name='main__no', lookup_expr='icontains')
    godownentry_no = django_filters.CharFilter(name='godownentry_detail__main__no', lookup_expr='icontains')
    main_supplier = django_filters.CharFilter(name='main__supplier__name', lookup_expr='icontains')
    main_create_user = django_filters.CharFilter(name='main__create_user__name', lookup_expr='icontains')
    main_check_user = django_filters.CharFilter(name='main__check_user__name', lookup_expr='icontains')
    main_create_time = django_filters.DateTimeFromToRangeFilter(field_name='main__create_time')
    main_check_time = django_filters.DateTimeFromToRangeFilter(field_name='main__check_time')
    notes = django_filters.CharFilter(name='notes', lookup_expr='icontains')

    class Meta:
        model = GodownEntryReturnDetail
        fields = "__all__"

    def __init__(self, data=None, *args, **kwargs):
        data = clean_datetime_range(data, 'main__create_time')
        data = clean_datetime_range(data, 'main__check_time')
        super(GodownEntryReturnDetailFilter, self).__init__(data, *args, **kwargs)


class PurchasePaymentFilter(django_filters.FilterSet):
    create_time = django_filters.DateTimeFromToRangeFilter(field_name='create_time')
    no = django_filters.CharFilter(name='no', lookup_expr='icontains')
    order_no = django_filters.CharFilter(name='order__no', lookup_expr='icontains')
    supplier_name = django_filters.CharFilter(name='order__supplier__name', lookup_expr='icontains')
    notes = django_filters.CharFilter(name='notes', lookup_expr='icontains')

    class Meta:
        model = PurchasePayment
        fields = (
            'create_time', 'no', 'supplier_name', 'status', 'order_no', 'notes', )

    def __init__(self, data=None, *args, **kwargs):
        data = clean_datetime_range(data, 'create_time','source')
        super(PurchasePaymentFilter, self).__init__(data, *args, **kwargs)


class PurchasePriceExportFilter(django_filters.FilterSet):
    supplier_name = django_filters.CharFilter(name='supplier__name', lookup_expr='icontains')

    class Meta:
        model = PurchasePrice
        fields = "__all__"