from rest_framework import serializers
from apps.pm.models import ProductionPlan, SubProductionPlan
from apps.mtm.serializers import MaterialSimpleSerializer, ProcessSimpleSerializer

class SubplanGanttSerializer(serializers.ModelSerializer):
    process_ = ProcessSimpleSerializer(source='process', read_only=True)
    product_ = MaterialSimpleSerializer(source='product', read_only=True)
    class Meta:
        model = SubProductionPlan
        fields = ['id', 'number', 'start_date', 'end_date', 'count', 'count_real', 'count_ok', 'start_date_real', 'end_date_real', 'process_', 'product_']

class PlanGanttSerializer(serializers.ModelSerializer):
    children = serializers.SerializerMethodField()
    product_ = MaterialSimpleSerializer(source='product', read_only=True)
    class Meta:
        model = ProductionPlan
        fields = ['id', 'number', 'start_date', 'end_date', 'children', 'count', 'count_real', 
                'count_ok', 'product', 'product_']

    def get_children(self, obj):
        subplans = SubProductionPlan.objects.filter(production_plan=obj).order_by('process__number')
        return SubplanGanttSerializer(instance=subplans, many=True).data