#coding=utf-8
import django_filters
from apps.base import clean_datetime_range
from models import ProductionPlan, SalePlan


class ProductionPlanFilter(django_filters.FilterSet):
    no = django_filters.CharFilter(name='no',lookup_expr='icontains')
    name = django_filters.CharFilter(name='name', lookup_expr='icontains')
    status = django_filters.CharFilter(name='status')
    create_user_text = django_filters.CharFilter(name='create_user__name', lookup_expr='icontains')

    class Meta:
        model = ProductionPlan
        fields = (
            'no', 'name', 'status', 'create_user_text'
        )

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



class SalePlanFilter(django_filters.FilterSet):
    no = django_filters.CharFilter(name='no',lookup_expr='icontains')
    customer_text = django_filters.CharFilter(name='customer__name', lookup_expr='icontains')
    create_time = django_filters.DateTimeFromToRangeFilter(field_name='create_time')

    class Meta:
        model = SalePlan
        fields = "__all__"

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