# coding=utf-8
import django_filters
from apps.base import clean_datetime_range
from models import SaleOrder, GoodsDeliver


class SaleOrderFilter(django_filters.FilterSet):
    create_time = django_filters.DateTimeFromToRangeFilter(field_name='create_time')
    no = django_filters.CharFilter(name='no', lookup_expr='icontains')
    cleared = django_filters.CharFilter(name='cleared')
    create_user = django_filters.CharFilter(name='create_user__name', lookup_expr='icontains')
    customer_name = django_filters.CharFilter(name='customer__name', lookup_expr='icontains')
    customer_tel = django_filters.CharFilter(name='customer__mobile', lookup_expr='icontains')

    class Meta:
        model = SaleOrder
        fields = (
            'create_time', 'no', 'customer_name', 'customer_tel',
            'status', 'create_user', 'cleared'
        )

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


class GoodsDeliverFilter(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')
    create_time = django_filters.DateTimeFromToRangeFilter(field_name='create_time')

    class Meta:
        model = GoodsDeliver
        fields = "__all__"

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

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

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