from django.db.models import Sum
from rest_framework import serializers

from LiveStock.Cooperative.serializers import CooperativeSerializer
from LiveStock.Rancher.serializers import RancherSerializer
from LiveStock.models import LiveStock, Rancher
import datetime

class LiveStockSerializer(serializers.ModelSerializer):
    rancher = serializers.SerializerMethodField('get_rancher')
    age = serializers.SerializerMethodField('get_age')

    class Meta:
        model = LiveStock
        fields = '__all__'



    def get_rancher(self,instance):
        rancher=Rancher.objects.filter(herd_code=instance.herd_code).first()
        ser_data=RancherSerializer(rancher)
        return ser_data.data

    def get_age(self,instance):
        if instance.birth_day_gh:
            now=datetime.datetime.now().date()
            age=(now - instance.birth_day_gh.date()).days
            return age
        else:
            return None

class PosLiveStockSerializer(serializers.ModelSerializer):
    cooperative=CooperativeSerializer(read_only=True)
    class Meta:
        model = Rancher
        fields = ['key','fullname','cooperative','mobile','city','herd_code','national_id','allow_buy','weight_allocation_heavy','weight_allocation_light']
