#coding=utf-8
import django_filters
from apps.base import clean_datetime_range
from models import Material, Consumable, Deliver
from apps.warehouse.models import Inventory


class MaterialFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(name='product_base__name',lookup_expr='icontains')
    model = django_filters.CharFilter(name='product_base__model', lookup_expr='icontains')
    enabled = django_filters.CharFilter(name='product_base__enabled')

    class Meta:
        model = Material
        fields = (
            'name', 'model', 'enabled'
        )

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


class ConsumableFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(name='product_base__name',lookup_expr='icontains')
    model = django_filters.CharFilter(name='product_base__model', lookup_expr='icontains')
    enabled = django_filters.CharFilter(name='product_base__enabled')

    class Meta:
        model = Consumable
        fields = (
            'name', 'model', 'enabled'
        )

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


class DeliverFilter(django_filters.FilterSet):
    no = django_filters.CharFilter(name='no', lookup_expr='icontains')
    create_user_text = django_filters.CharFilter(name='create_user__name', lookup_expr='icontains')
    receiver_text = django_filters.CharFilter(name='receiver__name', lookup_expr='icontains')
    create_time = django_filters.DateTimeFromToRangeFilter(field_name='create_time')

    class Meta:
        model = Deliver
        fields = "__all__"

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

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

    class Meta:
        model = Inventory
        fields = "__all__"

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


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

    class Meta:
        model = Deliver
        fields = (
            'no',
        )
