from dataclasses import fields
from distutils.command.build_py import build_py
from pyexpat import model
from django.forms import FileField
from rest_framework import serializers
from base.models import Country, RentForCountry, SubPublisherUser
import magazine

from magazineapp.models import Author, BlogsImages, MagMapAuthor, MagazineCommissionInfo, MagazineInfo, MagazinePurchaseInfo, WithdrawalDetails
from web.utils import get_mgn_commision
from .models import *
from magazine import settings

from django.contrib.auth import get_user_model
User = get_user_model()

### Magazine Purchase info serializer.

class MagazinePurchaseInfoListSerializer(serializers.ModelSerializer):
    purchase_name = serializers.SerializerMethodField()
    magazineInfos = serializers.SerializerMethodField()
    publisher_name = serializers.SerializerMethodField()
    rent_price = serializers.SerializerMethodField()
    buy_price = serializers.SerializerMethodField()
    subscription_price = serializers.SerializerMethodField()
    commission = serializers.SerializerMethodField()
    ammount = serializers.SerializerMethodField()

    class Meta:
        model = MagazinePurchaseInfo
        fields = ('id','rent_date','buy_date','is_buyed','is_rented','rent_expiry_date','magazineInfos','users','purchase_name', 'publisher_name','rent_price','buy_price','subscription_price','commission','ammount') 

    def get_purchase_name(self,obj):
        return f'{obj.users.first_name} {obj.users.last_name}'

    def get_magazineInfos(self,obj):
        return f'{obj.magazineInfos.name}'

    def get_publisher_name(self, obj):
        return f'{obj.magazineInfos.users.first_name} {obj.magazineInfos.users.last_name}'

    def get_rent_price(self, obj):
        if obj.is_rented == 1:
            self.rent_price = obj.rent_price
        else:
            self.rent_price = 0
        return self.rent_price

    def get_buy_price(self, obj):
        if obj.is_buyed == 1:
            self.buy_price = obj.buy_price
        else:
            self.buy_price = 0
        return self.buy_price

    def get_subscription_price(self, obj):
        if obj.is_subscription == 1:
            self.subscription_price = obj.subscription_price
        else:
            self.subscription_price = 0
        return self.subscription_price


    def get_commission(self, obj):
        request = self.context.get("request")
        active_user = request.user.id

        userid = obj.magazineInfos.users_id
        return get_mgn_commision(active_user,userid)
       
    def get_ammount(self, obj,):
        userid = obj.magazineInfos.users_id

        request = self.context.get("request")
        active_user = request.user.id

        userrecord = User.objects.filter(id=active_user).last()
        ammount = 0
        if userrecord.roles_id in [1,2]:
            commission = get_mgn_commision(active_user,userid)
            commission = float(commission)
            # print(commission)
            if obj.is_buyed == 1:
                if obj.buy_price is None:
                    buy_price = 0
                    percent = buy_price * (commission/100)
                    # print(percent, "Percent:----------")
                    ammount = percent
                    ammount = round(ammount, 2)
                    # print(ammount, " :---------------")
                else:
                    buy_price = obj.buy_price
                    percent = buy_price * (commission/100)
                    # print(percent, "Percent:----------")
                    ammount = percent
                    ammount = round(ammount, 2)
                    # print(ammount, " :---------------")

            elif obj.is_rented == 1:
                if obj.rent_price is None:
                    rent_price = 0
                    percent = rent_price * (commission/100)
                    # print(percent, "Percent:----------")
                    ammount = percent
                    ammount = round(ammount, 2)
                    # print(ammount, " :---------------")
                else:
                    rent_price = obj.rent_price
                    percent = rent_price * (commission/100)
                    # print(percent, "Percent:----------")
                    ammount = percent
                    ammount = round(ammount, 2)
                    # print(ammount, " :---------------")
                    
            return ammount

        elif userrecord.roles_id == 3:
            commission = get_mgn_commision(active_user,userid)
            commission = float(commission)
            # print(commission)
            if obj.is_buyed == 1:
                if obj.buy_price is None:
                    buy_price = 0
                    percent = buy_price * (commission/100)
                    # print(percent, "Percent:----------")
                    ammount = percent
                    ammount = round(ammount, 2)
                    # print(ammount, " :---------------")
                else:
                    buy_price = obj.buy_price
                    percent = buy_price * (commission/100)
                    # print(percent, "Percent:----------")
                    ammount = percent
                    ammount = round(ammount, 2)
                    # print(ammount, " :---------------")

            elif obj.is_rented == 1:
                if obj.rent_price is None:
                    rent_price = 0
                    percent = rent_price * (commission/100)
                    # print(percent, "Percent:----------")
                    ammount = percent
                    ammount = round(ammount, 2)
                    # print(ammount, " :---------------")
                else:
                    rent_price = obj.rent_price
                    percent = rent_price * (commission/100)
                    # print(percent, "Percent:----------")
                    ammount = percent
                    ammount = round(ammount, 2)
                    # print(ammount, " :---------------")
                    
            return ammount

        elif userrecord.roles_id == 4:
            commission = get_mgn_commision(active_user,userid)
            commission = float(commission)
            # print(commission)
            if obj.is_buyed == 1:
                if obj.buy_price is None:
                    buy_price = 0
                    percent = buy_price * (commission/100)
                    # print(percent, "Percent:----------")
                    ammount = percent
                    ammount = round(ammount, 2)
                    # print(ammount, " :---------------")
                else:
                    buy_price = obj.buy_price
                    percent = buy_price * (commission/100)
                    # print(percent, "Percent:----------")
                    ammount = percent
                    ammount = round(ammount, 2)
                    # print(ammount, " :---------------")

            elif obj.is_rented == 1:
                if obj.rent_price is None:
                    rent_price = 0
                    percent = rent_price * (commission/100)
                    # print(percent, "Percent:----------")
                    ammount = percent
                    ammount = round(ammount, 2)
                    # print(ammount, " :---------------")
                else:
                    rent_price = obj.rent_price
                    percent = rent_price * (commission/100)
                    # print(percent, "Percent:----------")
                    ammount = percent
                    ammount = round(ammount, 2)
                    # print(ammount, " :---------------")
           
                    
            return ammount
        else:
            pass
        
#########################################
# Admin amount Get.
class MagazinePurchaseInfoSumAmountSerializer(serializers.ModelSerializer):
    ammount = serializers.SerializerMethodField()
    class Meta:
        model = MagazinePurchaseInfo
        fields = ['ammount',]

    def get_ammount(self, obj,):
        ammount = 0
        if obj.magazineInfos:
            userid = obj.magazineInfos.users_id

            request = self.context.get("request")
            active_user = request.user.id

            userrecord = User.objects.filter(id=active_user).last()
            if userrecord.roles_id in [1,2]:
                commission = get_mgn_commision(active_user,userid)
                commission = float(commission)
                # print(commission)
                if obj.is_buyed == 1:
                    if obj.buy_price is None:
                        buy_price = 0
                        percent = buy_price * (commission/100)
                        # print(percent, "Percent:----------")
                        ammount = percent
                        ammount = round(ammount, 2)
                        # print(ammount, " :---------------")
                    else:
                        buy_price = obj.buy_price
                        if buy_price and commission:
                            # print(type(buy_price), type(commission))
                            commission = int(commission)
                            # print(type(buy_price), type(commission))
                            percent = buy_price * (commission/100)
                            # print(percent, "Percent:----------")
                            ammount = percent
                            ammount = round(ammount, 2)
                            # print(ammount, " :---------------")
                        else:
                            percent = 0 * (commission/100)
                            # print(percent, "Percent:----------")
                            ammount = percent
                            ammount = round(ammount, 2)
                            # print(ammount, " :---------------")


                elif obj.is_rented == 1:
                    if obj.rent_price is None:
                        rent_price = 0
                        percent = rent_price * (commission/100)
                        # print(percent, "Percent:----------")
                        ammount = percent
                        ammount = round(ammount, 2)
                        # print(ammount, " :---------------")
                    else:
                        rent_price = obj.rent_price
                        if rent_price:
                            commission = int(commission)
                            percent = rent_price * (commission/100)
                            # print(percent, "Percent:----------")
                            ammount = percent
                            ammount = round(ammount, 2)
                            # print(ammount, " :---------------")
                        else:
                            percent = 0 * (commission/100)
                            # print(percent, "Percent:----------")
                            ammount = percent
                            ammount = round(ammount, 2)
                            # print(ammount, " :---------------")

                return ammount

            elif userrecord.roles_id == 3:
                commission = get_mgn_commision(active_user,userid)
                commission = float(commission)
                
                # print(commission)
                if obj.is_buyed == 1:
                    if obj.buy_price is None:
                        buy_price = 0
                        percent = buy_price * (commission/100)
                        # print(percent, "Percent:----------")
                        ammount = percent
                        ammount = round(ammount, 2)
                        # print(ammount, " :---------------")
                    else:
                        buy_price = obj.buy_price
                        percent = buy_price * (commission/100)
                        # print(percent, "Percent:----------")
                        ammount = percent
                        ammount = round(ammount, 2)
                        # print(ammount, " :---------------")


                elif obj.is_rented == 1:
                    if obj.rent_price is None:
                        rent_price = 0
                        percent = rent_price * (commission/100)
                        # print(percent, "Percent:----------")
                        ammount = percent
                        ammount = round(ammount, 2)
                        # print(ammount, " :---------------")
                    else:
                        rent_price = obj.rent_price
                        percent = rent_price * (commission/100)
                        # print(percent, "Percent:----------")
                        ammount = percent
                        ammount = round(ammount, 2)
                        # print(ammount, " :---------------")
                    
                return ammount

                # print('commission', commission)
                # print('ammount>>>>', ammount)
                    

            elif userrecord.roles_id == 4:
                commission = get_mgn_commision(active_user,userid)
                commission = float(commission)
                # print(commission)
                if obj.is_buyed == 1:
                    if obj.buy_price is None:
                        buy_price = 0
                        percent = buy_price * (commission/100)
                        # print(percent, "Percent:----------")
                        ammount = percent
                        ammount = round(ammount, 2)
                        # print(ammount, " :---------------")
                    else:
                        buy_price = obj.buy_price
                        percent = buy_price * (commission/100)
                        # print(percent, "Percent:----------")
                        ammount = percent
                        ammount = round(ammount, 2)
                        # print(ammount, " :---------------")


                elif obj.is_rented == 1:
                    if obj.rent_price is None:
                        rent_price = 0
                        percent = rent_price * (commission/100)
                        # print(percent, "Percent:----------")
                        ammount = percent
                        ammount = round(ammount, 2)
                        # print(ammount, " :---------------")
                    else:
                        rent_price = obj.rent_price
                        percent = rent_price * (commission/100)
                        # print(percent, "Percent:----------")
                        ammount = percent
                        ammount = round(ammount, 2)
                        # print(ammount, " :---------------")
                    
                return ammount

            else:
                return 0
        else:
            return 0

from django.db.models import Count
class MagazineSellDetailsSerializer(serializers.ModelSerializer):
    rents = serializers.SerializerMethodField()
    buys = serializers.SerializerMethodField()
    magazineInfos = serializers.SerializerMethodField()
    total = serializers.SerializerMethodField()

    class Meta:
        model = MagazineInfo
        fields = ['rents', 'buys', 'magazineInfos','total']

    def get_rents(self, obj,):
        return MagazinePurchaseInfo.objects.filter(magazineInfos_id=obj.id, is_rented=True).count()
        

    def get_buys(self, obj):
        buys = MagazinePurchaseInfo.objects.filter(magazineInfos_id=obj.id, is_buyed=True).count()
        return buys

    def get_magazineInfos(self, obj):
        return obj.name

    def get_total(self, obj):
        total = MagazinePurchaseInfo.objects.filter(magazineInfos_id=obj.id).count()
        return total
    


class GetAuhtorSerializer(serializers.ModelSerializer):
    status = serializers.SerializerMethodField()
     
    class Meta:
        model = Author
        fields = ['id', 'author_name', 'status']

    def get_status(self, obj):
        mag_id = self.context.get("magzine_id")
        magazine_author = MagMapAuthor.objects.filter(magazine_id= mag_id,author_id=obj.id).last()

        if magazine_author:
            return True
        else:
            return False

    def get_author_name(self, obj):
        return obj.author_name


# Magazine Report Module
class ReportListSerializer(serializers.ModelSerializer):
    user_name = serializers.SerializerMethodField()
    magazine = serializers.SerializerMethodField()
    publisher_name = serializers.SerializerMethodField()
    rent_price = serializers.SerializerMethodField()
    buy_price = serializers.SerializerMethodField()
    hardcopy_price = serializers.SerializerMethodField()
    subscription_price = serializers.SerializerMethodField()
    commission = serializers.SerializerMethodField()
    offers = serializers.SerializerMethodField()
    discount = serializers.SerializerMethodField()
    ammount = serializers.SerializerMethodField()
    after_comission_ammount = serializers.SerializerMethodField()


    class Meta:
        model = MagazinePurchaseInfo
        fields = ('id','rent_date','hardcopy_price','buy_date','is_buyed','discount','after_comission_ammount','offers','is_rented','rent_expiry_date','magazine','user_name', 'publisher_name','rent_price','buy_price','subscription_price','commission','ammount') 

    def get_user_name(self,obj):
        return f'{obj.users.first_name} {obj.users.last_name}'
        
    def get_magazine(self,obj):
        return f'{obj.magazineInfos.name}'

    def get_publisher_name(self, obj):
        return f'{obj.magazineInfos.users.first_name} {obj.magazineInfos.users.last_name}'

    def get_rent_price(self, obj):
        if obj.is_rented == 1:
            self.rent_price = obj.rent_price
        else:
            self.rent_price = 0
        return self.rent_price

    def get_buy_price(self, obj):
        if obj.is_buyed == 1:
            self.buy_price = obj.buy_price
        else:
            self.buy_price = 0
        return self.buy_price

    def get_hardcopy_price(self, obj):
        if obj.is_hard_copy == 1:
            self.hard_copy_price = obj.hard_copy_price
        else:
            self.hard_copy_price = 0
        return self.hard_copy_price

    def get_offers(self, obj):
        if obj.magazineOffer:
            self.offers = obj.magazineOffer.code
        else:
            self.offers = 'Not Applied'
            
        return self.offers
    
    def get_discount(self, obj):
        if obj.magazineOffer:
            self.discount = obj.magazineOffer.discount
        else:
            self.discount = 0
        
        return self.discount

    def get_subscription_price(self, obj):
        if obj.is_subscription == 1:
            self.subscription_price = obj.subscription_price
        else:
            self.subscription_price = 0
        return self.subscription_price


    def get_commission(self, obj):
        request = self.context.get("request")
        active_user = request.user.id

        userid = obj.magazineInfos.users_id
        return get_mgn_commision(active_user,userid)
    
    def get_ammount(self, obj,):
        if obj.is_buyed == 1:
            if obj.buy_price is None:
                buy_price = 0
                ammount = buy_price
                ammount = round(ammount, 2)
            else:
                if obj.is_offered:
                    buy_price = obj.offer_price
                    ammount = buy_price
                    ammount = round(ammount, 2)
                else:
                    buy_price = obj.buy_price
                    ammount = buy_price
                    ammount = round(ammount, 2)
            return ammount 

        elif obj.is_rented == 1:
            if obj.rent_price is None:
                rent_price = 0
                ammount = rent_price
                ammount = round(ammount, 2)
            else:
                rent_price = obj.rent_price
                ammount = rent_price
                ammount = round(ammount, 2)
            
            return ammount 

        elif obj.is_hard_copy == 1:
            if obj.hard_copy_price is None:
                hard_copy_price = 0
                ammount = hard_copy_price
                ammount = round(ammount, 2)
            else:
                hard_copy_price = obj.hard_copy_price
                ammount = hard_copy_price
                ammount = round(ammount, 2)

                    
        
            return ammount 

       
    def get_after_comission_ammount(self, obj,):
        userid = obj.magazineInfos.users_id

        request = self.context.get("request")
        active_user = request.user.id

        userrecord = User.objects.filter(id=active_user).last()
        ammount = 0
        
        if userrecord.roles_id in [1,2]:
            commission = get_mgn_commision(active_user,userid)
            commission = float(commission)
            # print(commission)
            if obj.is_buyed == 1:
                if obj.buy_price is None:
                    buy_price = 0
                    percent = buy_price * (commission/100)
                    ammount = percent
                    ammount = round(ammount, 2)
                else:
                    if obj.is_offered:
                        buy_price = obj.offer_price
                        percent = buy_price * (commission/100)
                        ammount = percent
                        ammount = round(ammount, 2)
                    else:
                        buy_price = obj.buy_price
                        percent = buy_price * (commission/100)
                        ammount = percent
                        ammount = round(ammount, 2)

            elif obj.is_rented == 1:
                if obj.rent_price is None:
                    rent_price = 0
                    percent = rent_price * (commission/100)
                    # print(percent, "Percent:----------")
                    ammount = percent
                    ammount = round(ammount, 2)
                    # print(ammount, " :---------------")
                else:
                    rent_price = obj.rent_price
                    percent = rent_price * (commission/100)
                    # print(percent, "Percent:----------")
                    ammount = percent
                    ammount = round(ammount, 2)
                    # print(ammount, " :---------------")

            elif obj.is_hard_copy == 1:
                if obj.hard_copy_price is None:
                    hard_copy_price = 0
                    percent = hard_copy_price * (commission/100)
                    # print(percent, "Percent:----------")
                    ammount = percent
                    ammount = round(ammount, 2)
                    # print(ammount, " :---------------")
                else:
                    if obj.is_offered:
                        hard_copy_price = obj.hard_copy_price
                        percent = hard_copy_price * (commission/100)
                        ammount = percent
                        ammount = round(ammount, 2)
                    else:
                        hard_copy_price = obj.hard_copy_price
                        percent = hard_copy_price * (commission/100)
                        ammount = percent
                        ammount = round(ammount, 2)
                    
            return ammount

        elif userrecord.roles_id == 3:
            commission = get_mgn_commision(active_user,userid)
            commission = float(commission)

            # print(commission)
            if obj.is_buyed == 1:
                if obj.buy_price is None:
                    buy_price = 0
                    percent = buy_price * (commission/100)
                    # print(percent, "Percent:----------")
                    ammount = percent
                    ammount = round(ammount, 2)
                    # print(ammount, "Would be Zero :---------------")
                else:
                    if obj.is_offered:
                        buy_price = obj.offer_price
                        percent = buy_price * (commission/100)
                        ammount = buy_price - percent
                        ammount = round(ammount, 2)
                    else:
                        buy_price = obj.buy_price
                        percent = buy_price * (commission/100)
                        ammount = buy_price - percent
                        ammount = round(ammount, 2)

            elif obj.is_rented == 1:
                if obj.rent_price is None:
                    rent_price = 0
                    percent = rent_price * (commission/100)
                    # print(percent, "Percent:----------")
                    ammount = percent
                    ammount = round(ammount, 2)
                    # print(ammount, " :---------------")
                else:
                    rent_price = obj.rent_price
                    percent = rent_price * (commission/100)
                    # print(percent, "Percent:----------")
                    ammount = rent_price - percent
                    ammount = round(ammount, 2)
                    # print(ammount, " :---------------")

            elif obj.is_hard_copy == 1:
                if obj.hard_copy_price is None:
                    hard_copy_price = 0
                    percent = hard_copy_price * (commission/100)
                    # print(percent, "Percent:----------")
                    ammount = percent
                    ammount = round(ammount, 2)
                    # print(ammount, " :---------------")
                else:
                    if obj.is_offered:
                        hard_copy_price = obj.hard_copy_price
                        percent = hard_copy_price * (commission/100)
                        ammount = percent
                        ammount = round(ammount, 2)
                    else:
                        hard_copy_price = obj.hard_copy_price
                        percent = hard_copy_price * (commission/100)
                        ammount = percent
                        ammount = round(ammount, 2)
                    
            return ammount

        elif userrecord.roles_id == 4:
            commission = get_mgn_commision(active_user,userid)
            commission = float(commission)
            # print(commission)
            if obj.is_buyed == 1:
                if obj.buy_price is None:
                    buy_price = 0
                    percent = buy_price * (commission/100)
                    # print(percent, "Percent:----------")
                    ammount = percent
                    ammount = round(ammount, 2)
                    # print(ammount, " :---------------")
                else:
                    if obj.is_offered:
                        buy_price = obj.offer_price
                        percent = buy_price * (commission/100)
                        ammount = percent
                        ammount = round(ammount, 2)
                    else:
                        buy_price = obj.buy_price
                        percent = buy_price * (commission/100)
                        ammount = percent
                        ammount = round(ammount, 2)

            elif obj.is_rented == 1:
                if obj.rent_price is None:
                    rent_price = 0
                    percent = rent_price * (commission/100)
                    # print(percent, "Percent:----------")
                    ammount = percent
                    ammount = round(ammount, 2)
                    # print(ammount, " :---------------")
                else:
                    rent_price = obj.rent_price
                    percent = rent_price * (commission/100)
                    # print(percent, "Percent:----------")
                    ammount = percent
                    ammount = round(ammount, 2)
                    # print(ammount, " :---------------")

            elif obj.is_hard_copy == 1:
                if obj.hard_copy_price is None:
                    hard_copy_price = 0
                    percent = hard_copy_price * (commission/100)
                    # print(percent, "Percent:----------")
                    ammount = percent
                    ammount = round(ammount, 2)
                    # print(ammount, " :---------------")
                else:
                    if obj.is_offered:
                        hard_copy_price = obj.hard_copy_price
                        percent = hard_copy_price * (commission/100)
                        ammount = percent
                        ammount = round(ammount, 2)
                    else:
                        buy_price = obj.hard_copy_price
                        percent = hard_copy_price * (commission/100)
                        ammount = percent
                        ammount = round(ammount, 2)
           
                    
            return ammount
        else:
            pass

### Buy user magazine pages.
class MagazinePurchaseInfoByUserSerializer(serializers.ModelSerializer):
    class Meta:
        model = MagazinePurchaseInfo
        fields = '__all__'
        
#########################################

class MagazineGroupwiseReportSerializer(serializers.ModelSerializer):
    magazine = serializers.SerializerMethodField()
    publisher_fn = serializers.SerializerMethodField()
    publisher_ln = serializers.SerializerMethodField()
    total_amount = serializers.SerializerMethodField()
    commission = serializers.SerializerMethodField()
    admin_amount = serializers.SerializerMethodField()
    publisher_amount = serializers.SerializerMethodField()

    class Meta:
        model = MagazineInfo
        fields = ('id','magazine','publisher_fn','publisher_ln','total_amount','commission','admin_amount','publisher_amount') #'publisher','total_amount','commission','earned_amount',

    def get_magazine(slef, obj):
        magazine_sell_status = MagazinePurchaseInfo.objects.filter(magazineInfos_id=obj.id,status="success").exists()
        if magazine_sell_status:
            magazine = obj.name
            return magazine

    # def get_mgn_purchase_by_user(self, obj):
    #     purchage_mgn = MagazinePurchaseInfo.objects.filter(magazineInfos_id=obj.id).all()
    #     return MagazinePurchaseInfoByUserSerializer(purchage_mgn, many=True).data
    
    def get_publisher_fn(self, obj):
        magazine_sell_status = MagazinePurchaseInfo.objects.filter(magazineInfos_id=obj.id,status="success").exists()
        if magazine_sell_status:
            publisher = obj.users.first_name
            return publisher

    def get_publisher_ln(self, obj):
        magazine_sell_status = MagazinePurchaseInfo.objects.filter(magazineInfos_id=obj.id,status="success").exists()
        if magazine_sell_status:
            publisher = obj.users.last_name
            return publisher
    
    def get_total_amount(slef, obj):
        rent = 0
        buy = 0
        magazine_sell_status = MagazinePurchaseInfo.objects.filter(magazineInfos_id=obj.id,status="success").exists()
        if magazine_sell_status:
            magazine_data = MagazinePurchaseInfo.objects.filter(magazineInfos_id=obj.id,status="success").all()
            for data in magazine_data:
                if data.is_rented == 1:
                    if data.rent_price:
                        rent = rent + data.rent_price 
                    else:
                        rent = rent + 0
                elif data.is_buyed == 1:
                    if data.is_offered == 1:
                        buy = buy + data.offer_price
                    elif data.buy_price is None:
                        buy = buy + 0
                    else:
                        buy = buy + data.buy_price
            
            # print(buy)
            # print(rent)
            # print(buy + rent)
            return buy + rent
    
    def get_commission(self, obj):
        magazine_sell_status = MagazinePurchaseInfo.objects.filter(magazineInfos_id=obj.id,status="success").exists()
        if magazine_sell_status:
            request = self.context.get("request")
            active_user = request.user.id

            userid = obj.users_id
            return get_mgn_commision(active_user,userid)

    def get_admin_amount(self,obj):
        rent = 0
        buy = 0
        magazine_sell_status = MagazinePurchaseInfo.objects.filter(magazineInfos_id=obj.id,status="success").exists()
        if magazine_sell_status:
            magazine_data = MagazinePurchaseInfo.objects.filter(magazineInfos_id=obj.id,status="success").all()
            for data in magazine_data:
                if data.is_rented == 1:
                    if data.rent_price:
                        rent = rent + data.rent_price 
                    else:
                        rent = rent + 0 
                elif data.is_buyed == 1:
                    if data.is_offered == 1:
                        buy = buy + data.offer_price
                    elif data.buy_price is None:
                        buy = buy + 0
                    else:
                        buy = buy + data.buy_price
            
            # print(buy)
            # print(rent)
            # print(buy + rent)
            amount = buy + rent
            request = self.context.get("request")
            active_user = request.user.id
            userid = obj.users_id

            commission = get_mgn_commision(active_user,userid)
            commission = float(commission)

            total_amount = amount
            percent = total_amount * (commission/100)
            ammount = percent
            ammount = round(ammount, 2)
            return ammount

    
    def get_publisher_amount(self,obj):
        rent = 0
        buy = 0
        magazine_sell_status = MagazinePurchaseInfo.objects.filter(magazineInfos_id=obj.id,status="success").exists()
        if magazine_sell_status:
            magazine_data = MagazinePurchaseInfo.objects.filter(magazineInfos_id=obj.id,status="success").all()
            for data in magazine_data:
                if data.is_rented == 1:
                    if data.is_rented == 1:
                        if data.rent_price:
                            rent = rent + data.rent_price 
                    else:
                        rent = rent + 0  
                elif data.is_buyed == 1:
                    if data.is_offered == 1:
                        buy = buy + data.offer_price
                    elif data.buy_price is None:
                        buy = buy + 0
                    else:
                        buy = buy + data.buy_price
            
            # print(buy)
            # print(rent)
            # print(buy + rent)
            amount = buy + rent
            request = self.context.get("request")
            active_user = request.user.id
            userid = obj.users_id

            commission = get_mgn_commision(active_user,userid)
            commission = float(commission)

            total_amount = amount
            percent = total_amount * (commission/100)
            ammount = percent
            ammount = round(ammount, 2)
            total_amount = total_amount - ammount
            total_amount = round(total_amount, 2)
            return total_amount
            

class ReportTotalAmountSerializer(serializers.ModelSerializer):
    publisher_amount = serializers.SerializerMethodField()

    class Meta:
        model = MagazineInfo
        fields = ('publisher_amount') #'publisher','total_amount','commission','earned_amount',
    
    def get_publisher_amount(self,obj):
        rent = 0
        buy = 0
        magazine_sell_status = MagazinePurchaseInfo.objects.filter(magazineInfos_id=obj.id).exists()
        if magazine_sell_status:
            magazine_data = MagazinePurchaseInfo.objects.filter(magazineInfos_id=obj.id).all()
            for data in magazine_data:
                if data.is_rented == 1:
                    rent = rent + data.rent_price 
                elif data.is_buyed == 1:
                    if data.is_offered == 1:
                        buy = buy + data.offer_price
                    elif data.buy_price is None:
                        buy = buy + 0
                    else:
                        buy = buy + data.buy_price
            
            # print(buy)
            # print(rent)
            # print(buy + rent)
            amount = buy + rent
            request = self.context.get("request")
            active_user = request.user.id
            userid = obj.users_id

            commission = get_mgn_commision(active_user,userid)
            commission = float(commission)
            total_amount = amount
            percent = total_amount * (commission/100)
            ammount = percent
            ammount = round(ammount, 2)
            total_amount = total_amount - ammount
            total_amount = round(total_amount, 2)
            return total_amount
            
class GetCountrySerializer(serializers.ModelSerializer):
    status = serializers.SerializerMethodField()
     
    class Meta:
        model = Country
        fields = ['id', 'name', 'status']

    def get_status(self, obj):
        rent_id = self.context.get("rent_id")
        rent_country = RentForCountry.objects.filter(rent_id= rent_id,country_id=obj.id).last()

        if rent_country:
            return True
        else:
            return False

    def get_name(self, obj):
        return obj.name

    


class BlogImageURLSerializer(serializers.ModelSerializer):
    url = serializers.SerializerMethodField()
    class Meta:
        model = BlogsImages
        fields = ['url']
    
    def get_url(self, obj):
        if obj.files:
            return obj.files.url
        

    
class WithdrawalDetailsSerializer(serializers.ModelSerializer):
    # name = serializers.SerializerMethodField()
    class Meta:
        model = MagazinePurchaseInfo
        fields = '__all__'

    # def get_name(self, obj):
    #     if obj.magazineInfos:
    #         return obj.magazineInfos.name


class DetectModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = BlogsImages
        fields = ["files"]