from rest_framework import serializers
from rm_gallery.models import *
from django.db.models import *

#Start Parameters in request URL
def getLanguage(self):
    if(self.context.__contains__('request')):
        languageId = self.context['request'].query_params.get('languageId', None)
    elif (self.context.__contains__('languageId')):
        languageId = self.context['languageId']
    else:
        languageId = None

    if languageId is not None:
        selectedLanguage = Language.objects.get(id=languageId)
        if selectedLanguage.isEnabled:
            return languageId
    
    return None

def getIsDashboard(self):
    if(self.context.__contains__('request')):
        isDashboard = self.context['request'].query_params.get('isDashboard', None)
    elif (self.context.__contains__('isDashboard')):
        isDashboard = self.context['isDashboard']
    else:
        isDashboard = None
    return isDashboard

def getClientId(self):
    clientId = None
    if(self.context.__contains__('request')):
        clientId = self.context['request'].query_params.get('clientId', None)
    elif (self.context.__contains__('clientId')):
        clientId = self.context['clientId']
    return clientId

def getIsShowFeatureBullet(self):
    if(self.context.__contains__('request')):
        isShowFeatureBullet = self.context['request'].query_params.get('isShowFeatureBullet', None)
    elif (self.context.__contains__('isShowFeatureBullet')):
        isShowFeatureBullet = self.context['isShowFeatureBullet']
    else:
        isShowFeatureBullet = None
    return isShowFeatureBullet

def getIsMultipleSKU(self):
    if(self.context.__contains__('request')):
        isMultipleSKU = self.context['request'].query_params.get('isMultipleSKU', None)
    elif (self.context.__contains__('isMultipleSKU')):
        isMultipleSKU = self.context['isMultipleSKU']
    else:
        isMultipleSKU = None
    return isMultipleSKU

def getIsShowItem(self):
    if(self.context.__contains__('request')):
        isSKUDetail = self.context['request'].query_params.get('isShowItem', None)
    elif (self.context.__contains__('isShowItem')):
        isSKUDetail = self.context['isShowItem']
    else:
        isSKUDetail = None
    return isSKUDetail

def getIsShowRequestReturnDetail(self):
    if(self.context.__contains__('request')):
        isShowRequestReturnDetail = self.context['request'].query_params.get('isShowRequestReturnDetail', None)
    elif (self.context.__contains__('isShowRequestReturnDetail')):
        isShowRequestReturnDetail = self.context['isShowRequestReturnDetail']
    else:
        isShowRequestReturnDetail = None
    return isShowRequestReturnDetail

def getIsShowShippingItemDetail(self):
    if(self.context.__contains__('request')):
        isShowShippingItemDetail = self.context['request'].query_params.get('isShowShippingItemDetail', None)
    elif (self.context.__contains__('isShowShippingItemDetail')):
        isShowShippingItemDetail = self.context['isShowShippingItemDetail']
    else:
        isShowShippingItemDetail = None
    return isShowShippingItemDetail

def getIsFavoriteWithCurrentClient(self,instance):
    clientId = getClientId(self)
    val = 0
    if clientId is not None:
        val =Favorite.objects.filter(itemId=str(instance.id)).filter(clientId=str(clientId)).count()
    if val > 0:
        return True
    return False

#End Parameters in request URL

def getCountry(self,instance):
    languageId = getLanguage(self)
    country = Country.objects.get(id=str(instance.countryId.id))
    return CountrySerializer(country,context={'languageId':languageId}).data
    
def getCity(self,instance):
    languageId = getLanguage(self)
    city = City.objects.get(id=str(instance.cityId.id))
    return CitySerializer(city,context={'languageId':languageId}).data

def getZone(self,instance):
    languageId = getLanguage(self)
    zone = Zone.objects.get(id=str(instance.zoneId.id))
    return ZoneSerializer(zone,context={'languageId':languageId}).data

def getDistrict(self,instance):
    languageId = getLanguage(self)
    district = District.objects.get(id=str(instance.districtId.id))
    return DistrictSerializer(district,context={'languageId':languageId}).data

def getDepartment(self,instance):
    languageId = getLanguage(self)
    department = Department.objects.get(id=str(instance.departmentId.id))
    return DepartmentDisplaySerializer(department,context={'languageId':languageId}).data

def getTransactionType(self,instance):
    languageId = getLanguage(self)
    transactionType = TransactionType.objects.get(id=str(instance.transactionTypeId.id))
    return TransactionTypeSerializer(transactionType,context={'languageId':languageId}).data

def getTaxes(self, instance):
    languageId = getLanguage(self)
    taxes = Taxes.objects.get(id=str(instance.taxesId.id))
    return TaxesSerializer(taxes,context={'languageId':languageId}).data

def getPaymentMethod(self, instance):
    languageId = getLanguage(self)
    paymentMethod = PaymentMethod.objects.get(id=str(instance.paymentMethodId.id))
    return PaymentMethodSerializer(paymentMethod,context={'languageId':languageId}).data

def getSubscriptionStatus(self, instance):
    languageId = getLanguage(self)
    subscriptionStatus = SubscriptionStatus.objects.get(id=str(instance.subscriptionStatusId.id))
    return SubscriptionStatusSerializer(subscriptionStatus,context={'languageId':languageId}).data

def getSubscriptionType(self, instance):
    languageId = getLanguage(self)
    subscriptionType = SubscriptionType.objects.get(id=str(instance.subscriptionTypeId.id))
    return SubscriptionTypeSerializer(subscriptionType,context={'languageId':languageId}).data

def getReturnCost(self, instance):
    languageId = getLanguage(self)
    returnCost = ReturnCost.objects.get(id=str(instance.returnCostId.id))
    return ReturnCostSerializer(returnCost,context={'languageId':languageId}).data

def getReturnStatus(self, instance):
    languageId = getLanguage(self)
    returnStatus = ReturnStatus.objects.get(id=str(instance.returnStatusId.id))
    return ReturnStatusSerializer(returnStatus,context={'languageId':languageId}).data

def getSKU(self,instance):
    clientId = getClientId(self)
    languageId = getLanguage(self)
    isDashboard = getIsDashboard(self)
    isShowItem = getIsShowItem(self)
    sku = SKU.objects.get(id=instance.skuId.id)
    return SKUSerializer(sku,context={'clientId':clientId,'languageId':languageId,'isDashboard':isDashboard,'isShowItem':isShowItem}).data
    
def getClient(self, instance):
    languageId = getLanguage(self)
    client = Client.objects.get(id=str(instance.clientId.id))
    return ClientDisplaySerializer(client,context={'languageId':languageId}).data

def getSupplierInfo(self, instance):
    supplier = Supplier.objects.get(id=str(instance.supplierId.id))
    return SupplierDisplaySerializer(supplier).data

class DashboardDataSerializer(serializers.Serializer):
    countItems = serializers.IntegerField()
    sumPaymentTotal = serializers.FloatField()
    sumPaymentPaid = serializers.FloatField()
    sumPaymentChange = serializers.FloatField()
    countTransactionIn = serializers.IntegerField()
    sumTransactionInTotal = serializers.FloatField()
    sumTransactionInPaid = serializers.FloatField()
    countTransactionOut = serializers.IntegerField()
    sumTransactionOutTotal = serializers.FloatField()
    sumTransactionOutPaid = serializers.FloatField()
    currentBalanceTotal = serializers.FloatField()
    currentBalancePaid = serializers.FloatField()
    currentSumTaxes = serializers.FloatField()

#Settings

class SettingsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Settings
        fields = [
            'id',
            'isMaintenanceClient',
            'isMaintenanceSupplier',
            'isEnableFreeSubscriptionForSupplier',
            'itemSKULimit',
            'itemSKUOptionLimit',
            'itemFeatureBulletLimit',
            'maxCartQuantity',
            'isUseStandardShipping',
            'isUseZoneShipping',
            'isUseAddressSelection',
            'isUseAddressManually',
            'isEnableStoreBankCards',
            'isUseTaxes',
            'isInitialSettingsDone',
            'createdDate',
            'updatedDate'
        ]

#Language

class LanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = Language
        fields = [
            'id',
            'name',
            'countryCode',
            'isEnabled',
            'languageCode',
            'createdDate',
            'updatedDate'
        ]

#Social Link

class SocialLinkSerializer(serializers.ModelSerializer):
    class Meta:
        model = SocialLink
        fields = [
            'id',
            'keyName',
            'url',
            'createdDate',
            'updatedDate'
        ]

#Provider

class ProviderSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    class Meta:
        model = Provider
        fields = [
            'id',
            'keyName',
            'name',
            'languageList',
            'createdDate',
            'updatedDate',
        ]
    def get_name(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            providerLanguageList = ProviderLanguage.objects.filter(providerId=str(instance.id)).filter(languageId=languageId)
            if providerLanguageList.__len__() > 0:
                return providerLanguageList[0].name
    
    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                providerLanguageList = ProviderLanguage.objects.filter(providerId=str(instance.id))
                if providerLanguageList.__len__() > 0:
                    return ProviderLanguageSerializer(providerLanguageList,many=True).data

class ProviderLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ProviderLanguage
        fields = [
            'id',
            'name',
            'providerId',
            'languageId'
        ]

#Item Cost

class ItemCostSerializer(serializers.ModelSerializer):
    class Meta:
        model = ItemCost
        fields = [
            'id',
            'priceInPercent',
            'minPrice',
            'maxPrice',
            'isEnabled',
            'createdDate',
            'updatedDate'
        ]

#Fees

class FeesSerializer(serializers.ModelSerializer):
    class Meta:
        model = Fees
        fields = [
            'id',
            'handlingFees',
            'packingFees',
            'isEnabled',
            'createdDate',
            'updatedDate'
        ]

#Taxes

class TaxesSerializer(serializers.ModelSerializer):
    class Meta:
        model = Taxes
        fields = [
            'id',
            'price',
            'createdDate',
            'updatedDate'
        ]

#Order From

class OrderFromSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    class Meta:
        model = OrderFrom
        fields = [
            'id',
            'keyName',
            'name',
            'languageList',
            'createdDate',
            'updatedDate',
        ]
    def get_name(self, instance):
        languageId = getLanguage(self)
        orderFromLanguageList = OrderFromLanguage.objects.filter(orderFromId=str(instance.id)).filter(languageId=languageId)
        if orderFromLanguageList.__len__() > 0:
            return orderFromLanguageList[0].name
    
    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                orderFromLanguageList = OrderFromLanguage.objects.filter(orderFromId=str(instance.id))
                if orderFromLanguageList.__len__() > 0:
                    return OrderFromLanguageSerializer(orderFromLanguageList,many=True).data

class OrderFromLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = OrderFromLanguage
        fields = [
            'id',
            'name',
            'orderFromId',
            'languageId'
        ]

#Cancel Type

class CancelTypeSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    class Meta:
        model = CancelType
        fields = [
            'id',
            'keyName',
            'price',
            'isPercentage',
            'name' ,
            'languageList',
            'createdDate',
            'updatedDate',          
        ]
    def get_name(self, instance):
        languageId = getLanguage(self)
        cancelTypeLanguageList = CancelTypeLanguage.objects.filter(cancelTypeId=str(instance.id)).filter(languageId=languageId)
        if cancelTypeLanguageList.__len__() > 0:
            return cancelTypeLanguageList[0].name
    
    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                cancelTypeLanguageList = CancelTypeLanguage.objects.filter(cancelTypeId=str(instance.id))
                if cancelTypeLanguageList.__len__() > 0:
                    return CancelTypeLanguageSerializer(cancelTypeLanguageList,many=True).data

class CancelTypeLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = CancelTypeLanguage
        fields = [
            'id',
            'name',
            'cancelTypeId',
            'languageId'
        ]

#Return Cost

class ReturnCostSerializer(serializers.ModelSerializer):
    class Meta:
        model = ReturnCost
        fields = [
            'id',
            'cost',
            'createdDate',
            'updatedDate',          
        ]

#Return Status

class ReturnStatusSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    class Meta:
        model = ReturnStatus
        fields = [
            'id',
            'keyName',
            'name' ,
            'languageList',
            'createdDate',
            'updatedDate',          
        ]
    def get_name(self, instance):
        languageId = getLanguage(self)
        returnStatusLanguageList = ReturnStatusLanguage.objects.filter(returnStatusId=str(instance.id)).filter(languageId=languageId)
        if returnStatusLanguageList.__len__() > 0:
            return returnStatusLanguageList[0].name
    
    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                returnStatusLanguageList = ReturnStatusLanguage.objects.filter(returnStatusId=str(instance.id))
                if returnStatusLanguageList.__len__() > 0:
                    return ReturnStatusLanguageSerializer(returnStatusLanguageList,many=True).data

class ReturnStatusLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ReturnStatusLanguage
        fields = [
            'id',
            'name',
            'returnStatusId',
            'languageId'
        ]

#Track Status

class TrackStatusSerializer(serializers.ModelSerializer):
    description = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    class Meta:
        model = TrackStatus
        fields = [
            'id',
            'keyName',
            'description',
            'languageList',
            'createdDate',
            'updatedDate',
        ]
    
    def get_description(self, instance):
        languageId = getLanguage(self)
        trackStatusLanguageList = TrackStatusLanguage.objects.filter(trackStatusId=str(instance.id)).filter(languageId=languageId)
        if trackStatusLanguageList.__len__() > 0:
            return trackStatusLanguageList[0].description

    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                trackStatusLanguageList = TrackStatusLanguage.objects.filter(trackStatusId=str(instance.id))
                if trackStatusLanguageList.__len__() > 0:
                    return TrackStatusLanguageSerializer(trackStatusLanguageList,many=True).data

class TrackStatusLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = TrackStatusLanguage
        fields = [
            'id',
            'description',
            'trackStatusId',
            'languageId'
        ]

#Payment Method

class PaymentMethodSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    class Meta:
        model = PaymentMethod
        fields = [
            'id',
            'keyName',
            'name',
            'fixedPrice',
            'price',
            'isPercentage',
            'isEnabled',
            'languageList',
            'createdDate',
            'updatedDate',
        ]
    def get_name(self, instance):
        languageId = getLanguage(self)
        paymentMethodLanguageList = PaymentMethodLanguage.objects.filter(paymentMethodId=str(instance.id)).filter(languageId=languageId)
        if paymentMethodLanguageList.__len__() > 0:
            return paymentMethodLanguageList[0].name

    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                paymentMethodLanguageList = PaymentMethodLanguage.objects.filter(paymentMethodId=str(instance.id))
                if paymentMethodLanguageList.__len__() > 0:
                    return PaymentMethodLanguageSerializer(paymentMethodLanguageList,many=True).data

class PaymentMethodLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = PaymentMethodLanguage
        fields = [
            'id',
            'name',
            'paymentMethodId',
            'languageId'
        ]

#EligableType

class EligibleTypeSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    class Meta:
        model = EligibleType
        fields = [
            'id',
            'keyName',
            'keyOption',
            'name',
            'languageList',
            'createdDate',
            'updatedDate',
        ]
    
    def get_name(self, instance):
        languageId = getLanguage(self)
        eligibleTypeLanguageList = EligibleTypeLanguage.objects.filter(eligibleTypeId=str(instance.id)).filter(languageId=languageId)
        if eligibleTypeLanguageList.__len__() > 0:
            return eligibleTypeLanguageList[0].name

    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                eligibleTypeLanguageList = EligibleTypeLanguage.objects.filter(eligibleTypeId=str(instance.id))
                if eligibleTypeLanguageList.__len__() > 0:
                    return EligibleTypeLanguageSerializer(eligibleTypeLanguageList,many=True).data
    
class EligibleTypeLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = EligibleTypeLanguage
        fields = [
            'id',
            'name',
            'eligibleTypeId',
            'languageId'
        ]

#Country

class CountrySerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    class Meta:
        model = Country
        fields = [
            'id',
            'isActive',
            'isMainBranch',
            'name',
            'languageList',
            'createdDate',
            'updatedDate',
        ]
    
    def get_name(self, instance):
        languageId = getLanguage(self)
        countryLanguageList = CountryLanguage.objects.filter(countryId=str(instance.id)).filter(languageId=languageId)
        if countryLanguageList.__len__() > 0:
            return countryLanguageList[0].name

    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                countryLanguageList = CountryLanguage.objects.filter(countryId=str(instance.id))
                if countryLanguageList.__len__() > 0:
                    return CountryLanguageSerializer(countryLanguageList,many=True).data
    
class CountryLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = CountryLanguage
        fields = [
            'id',
            'name',
            'countryId',
            'languageId'
        ]

#City

class CitySerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    class Meta:
        model = City
        fields = [
            'id',
            'isActive',
            'name',
            'languageList',
            'createdDate',
            'updatedDate',
            'countryId',
        ]

    def get_name(self, instance):
        languageId = getLanguage(self)
        cityLanguageList = CityLanguage.objects.filter(cityId=str(instance.id)).filter(languageId=languageId)
        if cityLanguageList.__len__() > 0:
            return cityLanguageList[0].name

    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                cityLanguageList = CityLanguage.objects.filter(cityId=str(instance.id))
                if cityLanguageList.__len__() > 0:
                    return CityLanguageSerializer(cityLanguageList,many=True).data

class CityLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = CityLanguage
        fields = [
            'id',
            'name',
            'cityId',
            'languageId'
        ]

class ZoneSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    class Meta:
        model = Zone
        fields = [
            'id',
            'isActive',
            'name',
            'languageList',
            'createdDate',
            'updatedDate',
            'cityId',
        ]

    def get_name(self, instance):
        languageId = getLanguage(self)
        zoneLanguageList = ZoneLanguage.objects.filter(zoneId=str(instance.id)).filter(languageId=languageId)
        if zoneLanguageList.__len__() > 0:
            return zoneLanguageList[0].name

    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                zoneLanguageList = ZoneLanguage.objects.filter(zoneId=str(instance.id))
                if zoneLanguageList.__len__() > 0:
                    return ZoneLanguageSerializer(zoneLanguageList,many=True).data

class ZoneLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ZoneLanguage
        fields = [
            'id',
            'name',
            'zoneId',
            'languageId'
        ]

#District

class DistrictSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    class Meta:
        model = District
        fields = [
            'id',
            'isActive',
            'name',
            'languageList',
            'createdDate',
            'updatedDate',
            'zoneId',
        ]

    def get_name(self, instance):
        languageId = getLanguage(self)
        districtLanguageList = DistrictLanguage.objects.filter(districtId=str(instance.id)).filter(languageId=languageId)
        if districtLanguageList.__len__() > 0:
            return districtLanguageList[0].name

    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                districtLanguageList = DistrictLanguage.objects.filter(districtId=str(instance.id))
                if districtLanguageList.__len__() > 0:
                    return DistrictLanguageSerializer(districtLanguageList,many=True).data

class DistrictLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = DistrictLanguage
        fields = [
            'id',
            'name',
            'districtId',
            'languageId'
        ]

class DistrictPriceSerializer(serializers.Serializer):
    countryId = serializers.IntegerField()
    country = serializers.CharField()
    cityId = serializers.IntegerField()
    city = serializers.CharField()
    zoneId = serializers.IntegerField()
    zone = serializers.CharField()
    districtId = serializers.IntegerField()
    district = serializers.CharField()
    deliveryPriceId = serializers.IntegerField()
    price = serializers.FloatField()

class AllDistrictSerializer(serializers.Serializer):
    countryId = serializers.IntegerField()
    country = serializers.CharField()
    cityId = serializers.IntegerField()
    city = serializers.CharField()
    zoneId = serializers.IntegerField()
    zone = serializers.CharField()
    districtId = serializers.IntegerField()
    district = serializers.CharField()

class DeliveryPriceSerializer(serializers.ModelSerializer):
    class Meta:
        model = DeliveryPrice
        fields = [
            'id',
            'name',
            'upToWeight',
            'price',
            'additionalWeight',
            'additionalWeightPrice',
            'isInternal',
            'isStandard',
            'isEnabled',
            'createdDate',
            'updatedDate',
        ]

class DeliveryPriceCityZoneSerializer(serializers.ModelSerializer):
    class Meta:
        model = DeliveryPriceCityZone
        fields = [
            'id',
            'createdDate',
            'updatedDate',
            'deliveryPriceId',
            'cityId',
            'zoneId'
        ]

#Department

class DepartmentSerializer(serializers.ModelSerializer):
    mainDepartment = serializers.SerializerMethodField()
    itemCount = serializers.SerializerMethodField()
    itemImageLink = serializers.SerializerMethodField()
    name = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    imageList = serializers.SerializerMethodField()
    #To Be used in Future for different department with different brand
    #brandList = serializers.SerializerMethodField()
    class Meta:
        model = Department
        fields = [
            'id',
            'name',
            'languageList',
            'keyName',
            'isSelected',
            'isPopular',
            'isVisible',
            'isUseItemImageLink',
            'totalViews',
            'image',
            'createdDate',
            'updatedDate',
            'departmentId',
            'mainDepartment',
            'itemCount',
            'itemImageLink',
            'imageList',
            #'brandList'
        ]
    def get_mainDepartment(self, instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if instance.departmentId is not None:
                return getDepartment(self,instance)
    
    def get_itemImageLink(self,instance):
        if instance.isUseItemImageLink == True:
            deparmentTemp = None
            id = None
            selectedSKU = None
            
            if instance.departmentId is None:
                deparmentTemp = Department.objects.filter(departmentId = instance.id).first()
                if deparmentTemp is not None:
                    id = deparmentTemp.id
            else:
                id = instance.id
            
            if id is not None:
                selectedItem = Item.objects.filter(departmentId = id).first()
                if selectedItem is not None:
                    selectedSKU = SKU.objects.filter(itemId = selectedItem.id).first()

            if selectedSKU is not None:
                if selectedSKU.image is not None:
                    if selectedSKU.image and hasattr(selectedSKU.image,'url'):
                        return selectedSKU.image.url
                    
            else:
                return None
        else:
            return None

    def get_itemCount(self, instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            return Item.objects.filter(departmentId=instance.id).count()
    
    def get_name(self, instance):
        languageId = getLanguage(self)
        departmentLanguageList = DepartmentLanguage.objects.filter(departmentId=str(instance.id)).filter(languageId=languageId)
        if departmentLanguageList.__len__()>0:
            return departmentLanguageList[0].name

    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                departmentLanguageList = DepartmentLanguage.objects.filter(departmentId=str(instance.id))
                if departmentLanguageList.__len__() > 0:
                    return DepartmentLanguageSerializer(departmentLanguageList,many=True).data

    def get_imageList(self,instance):
        itemList = []
        skuList = []
        itemList = Item.objects.annotate(item_sku_count = Count('item_sku')).filter(departmentId = instance.id).filter(isApproved=True).filter(item_sku_count__gt=0).order_by('-createdDate')[0:4]
            
        for item in itemList:
            selectedSKUList = SKU.objects.filter(itemId = item.id)
            if selectedSKUList.__len__() > 0:
                skuList.append(selectedSKUList[0])

        return SKUBasicSerializer(skuList,many=True).data

    # def get_brandList(self,instance):
    #     languageId = getLanguage(self)
    #     tempList = Item.objects.filter(departmentId__departmentId = instance.id).values('brandId').distinct()
    #     brandList = Brand.objects.filter(id__in = [temp['brandId'] for temp in tempList])
    #     return BrandDisplaySerializer(brandList,context={'languageId':languageId},many=True).data

class DepartmentLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = DepartmentLanguage
        fields = [
            'id',
            'name',
            'departmentId',
            'languageId'
        ]

class DepartmentDisplaySerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    class Meta:
        model = Department
        fields = [
            'id',
            'keyName',
            'name',
            'departmentId'
        ]
    def get_name(self, instance):
        languageId = getLanguage(self)
        departmentLanguageList = DepartmentLanguage.objects.filter(departmentId=str(instance.id)).filter(languageId=languageId)
        if departmentLanguageList.__len__() > 0:
            return departmentLanguageList[0].name

#Brand

class BrandSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    itemImageLink = serializers.SerializerMethodField()
    class Meta:
        model = Brand
        fields = [
            'id',
            'keyName',
            'isVisible',
            'name',
            'languageList',
            'isUseItemImageLink',
            'image',
            'createdDate',
            'updatedDate',
            'itemImageLink'
        ]
    
    def get_itemImageLink(self,instance):
        if instance.isUseItemImageLink == True:
            id = instance.id
            selectedSKU = None
            
            if id is not None:
                selectedItem = Item.objects.filter(brandId = id).first()
                if selectedItem is not None:
                    selectedSKU = SKU.objects.filter(itemId = selectedItem.id).first()
            
            if selectedSKU is not None:
                if selectedSKU.image is not None:
                    if selectedSKU.image and hasattr(selectedSKU.image,'url'):
                        return selectedSKU.image.url
            else:
                return None
        else:
            return None
        
    def get_name(self, instance):
        languageId = getLanguage(self)
        brandLanguageList = BrandLanguage.objects.filter(brandId=str(instance.id)).filter(languageId=languageId)
        if brandLanguageList.__len__() > 0:
            return brandLanguageList[0].name
            
    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                brandLanguageList = BrandLanguage.objects.filter(brandId=str(instance.id))
                if brandLanguageList.__len__() > 0:
                    return BrandLanguageSerializer(brandLanguageList,many=True).data

class BrandLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = BrandLanguage
        fields = [
            'id',
            'name',
            'brandId',
            'languageId'
        ]

class BrandDisplaySerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()

    class Meta:
        model = Brand
        fields = [
            'id',
            'keyName',
            'name',
        ]
        
    def get_name(self, instance):
        languageId = getLanguage(self)
        brandLanguageList = BrandLanguage.objects.filter(brandId=str(instance.id)).filter(languageId=languageId)
        if brandLanguageList.__len__() > 0:
            return brandLanguageList[0].name

#Subscription Status

class SubscriptionStatusSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    class Meta:
        model = SubscriptionStatus
        fields = [
            'id',
            'keyName',
            'name',
            'languageList',
            'createdDate',
            'updatedDate',
        ]
    def get_name(self, instance):
        languageId = getLanguage(self)
        subscriptionStatusLanguageList = SubscriptionStatusLanguage.objects.filter(subscriptionStatusId=str(instance.id)).filter(languageId=languageId)
        if subscriptionStatusLanguageList.__len__() > 0:
            return subscriptionStatusLanguageList[0].name

    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                subscriptionStatusLanguageList = SubscriptionStatusLanguage.objects.filter(subscriptionStatusId=str(instance.id))
                if subscriptionStatusLanguageList.__len__() > 0:
                    return SubscriptionStatusLanguageSerializer(subscriptionStatusLanguageList,many=True).data

class SubscriptionStatusLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = SubscriptionStatusLanguage
        fields = [
            'id',
            'name',
            'subscriptionStatusId',
            'languageId'
        ]

#Subscription Option

class SubscriptionOptionSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    class Meta:
        model = SubscriptionOption
        fields = [
            'id',
            'keyName',
            'name',
            'languageList',
            'createdDate',
            'updatedDate',
        ]
    def get_name(self, instance):
        languageId = getLanguage(self)
        subscriptionOptionLanguageList = SubscriptionOptionLanguage.objects.filter(subscriptionOptionId=str(instance.id)).filter(languageId=languageId)
        if subscriptionOptionLanguageList.__len__() > 0:
            return subscriptionOptionLanguageList[0].name

    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                subscriptionOptionLanguageList = SubscriptionOptionLanguage.objects.filter(subscriptionOptionId=str(instance.id))
                if subscriptionOptionLanguageList.__len__() > 0:
                    return SubscriptionOptionLanguageSerializer(subscriptionOptionLanguageList,many=True).data

class SubscriptionOptionLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = SubscriptionOptionLanguage
        fields = [
            'id',
            'name',
            'subscriptionOptionId',
            'languageId'
        ]

#Subscription Type

class SubscriptionTypeSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    class Meta:
        model = SubscriptionType
        fields = [
            'id',
            'name',
            'languageList',
            'costPerMonth',
            'price',
            'discount',
            'serviceCost',
            'durationInMonth',
            'isEnabled',
            'createdDate',
            'updatedDate',
            'subscriptionOptionId',
        ]
    def get_name(self, instance):
        languageId = getLanguage(self)
        subscriptionTypeLanguageList = SubscriptionTypeLanguage.objects.filter(subscriptionTypeId=str(instance.id)).filter(languageId=languageId)
        if subscriptionTypeLanguageList.__len__() > 0:
            return subscriptionTypeLanguageList[0].name

    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                subscriptionTypeLanguageList = SubscriptionTypeLanguage.objects.filter(subscriptionTypeId=str(instance.id))
                if subscriptionTypeLanguageList.__len__() > 0:
                    return SubscriptionTypeLanguageSerializer(subscriptionTypeLanguageList,many=True).data

class SubscriptionTypeLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = SubscriptionTypeLanguage
        fields = [
            'id',
            'name',
            'subscriptionTypeId',
            'languageId'
        ]

#Transaction Type

class TransactionTypeSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    class Meta:
        model = TransactionType
        fields = [
            'id',
            'keyName',
            'name',
            'languageList',
            'createdDate',
            'updatedDate',
        ]
    def get_name(self, instance):
        languageId = getLanguage(self)
        transactionTypeLanguageList = TransactionTypeLanguage.objects.filter(transactionTypeId=str(instance.id)).filter(languageId=languageId)
        if transactionTypeLanguageList.__len__() > 0:
            return transactionTypeLanguageList[0].name

    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                transactionTypeLanguageList = TransactionTypeLanguage.objects.filter(transactionTypeId=str(instance.id))
                if transactionTypeLanguageList.__len__() > 0:
                    return TransactionTypeLanguageSerializer(transactionTypeLanguageList,many=True).data

class TransactionTypeLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = TransactionTypeLanguage
        fields = [
            'id',
            'name',
            'transactionTypeId',
            'languageId'
        ]

#New Arrival

class NewArrivalSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    description = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    imageList = serializers.SerializerMethodField()
    class Meta:
        model = NewArrival
        fields = [
            'id',
            'name',
            'description',
            'image',
            'createdDate',
            'updatedDate',
            'languageList',
            'imageList',
        ]
    def get_name(self, instance):
        languageId = getLanguage(self)
        newArrivalLanguageList = NewArrivalLanguage.objects.filter(newArrivalId=str(instance.id)).filter(languageId=languageId)
        if newArrivalLanguageList.__len__() > 0:
            return newArrivalLanguageList[0].name

    def get_description(self, instance):
        languageId = getLanguage(self)
        newArrivalLanguageList = NewArrivalLanguage.objects.filter(newArrivalId=str(instance.id)).filter(languageId=languageId)
        if newArrivalLanguageList.__len__() > 0:
            return newArrivalLanguageList[0].description
    
    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                newArrivalLanguageList = NewArrivalLanguage.objects.filter(newArrivalId=str(instance.id))
                if newArrivalLanguageList.__len__() > 0:
                    return NewArrivalLanguageSerializer(newArrivalLanguageList,many=True).data

    def get_imageList(self,instance):
        itemList = []
        skuList = []
        itemList = Item.objects.annotate(item_sku_count = Count('item_sku')).filter(newArrivalId = instance.id).filter(isApproved=True).filter(item_sku_count__gt=0).order_by('-createdDate')[0:4]

        for item in itemList:
            selectedSKUList = SKU.objects.filter(itemId = item.id)
            if selectedSKUList.__len__() > 0:
                skuList.append(selectedSKUList[0])

        return SKUBasicSerializer(skuList,many=True).data    

class NewArrivalLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = NewArrivalLanguage
        fields = [
            'id',
            'name',
            'description',
            'newArrivalId',
            'languageId'
        ]

class NewArrivalDisplaySerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    class Meta:
        model = NewArrival
        fields = [
            'id',
            'name',
        ]
    def get_name(self, instance):
        languageId = getLanguage(self)
        newArrivalLanguageList = NewArrivalLanguage.objects.filter(newArrivalId=str(instance.id)).filter(languageId=languageId)
        if newArrivalLanguageList.__len__() > 0:
            return newArrivalLanguageList[0].name

#App Content

class AppContentSerializer(serializers.ModelSerializer):
    itemImageLink = serializers.SerializerMethodField()
    imageList = serializers.SerializerMethodField()
    class Meta:
        model = AppContent
        fields = [
            'id',
            'keyName',
            'description',
            'isSliderFlag',
            'navigateTo',
            'isUseItemImageLink',
            'isUrl',
            'image',
            'createdDate',
            'updatedDate',
            'itemImageLink',
            'imageList'
        ]
    def get_itemImageLink(self,instance):
        if instance.isUseItemImageLink == True:
            selectedItem = None
            selectedSKU = None
            if instance.keyName == 'ban':
                selectedItem = Item.objects.filter(totalViews__gte = 0).first()
            elif instance.keyName == 'tp':
                selectedItem = Item.objects.filter(isTop = True).first()
            elif instance.keyName == 'fp':
                selectedItem = Item.objects.filter(isFavorite = True).first()
            elif instance.keyName == 'mdp':
                selectedItem = Item.objects.filter(item_sku__discount__gt = 0).first()
            elif instance.keyName == 'sd':
                selectedItem = None
                selectedDepartment = Department.objects.filter(isSelected = True).first()
                if selectedDepartment is not None:
                    selectedItemList = Item.objects.filter(departmentId = selectedDepartment.id)
                    if selectedItemList.__len__() > 0:
                        selectedItem = selectedItemList[0]

            if selectedItem is not None:
                selectedSKU = SKU.objects.filter(itemId = selectedItem.id).first()
                
            if selectedSKU is not None:
                if selectedSKU.image is not None:
                    if selectedSKU.image and hasattr(selectedSKU.image,'url'):
                        return selectedSKU.image.url
            else:
                return None
        else:
            return None
    
    def get_imageList(self,instance):
        maxNumOfRecords = 4
        minNumOfRecords = 0
        departmentList = []
        itemList = []
        skuList = []
        if instance.keyName == 'tp':
            departmentList = Item.objects.filter(isTop = True).values('departmentId').distinct()[minNumOfRecords:maxNumOfRecords]
        
        elif instance.keyName == 'fp':
            departmentList = Item.objects.filter(isFavorite = True).values('departmentId').distinct()[minNumOfRecords:maxNumOfRecords]
        
        elif instance.keyName == 'mdp':
            departmentList = Item.objects.filter(item_sku__discount__gt = 0).values('departmentId').distinct()[minNumOfRecords:maxNumOfRecords]
        
        elif instance.keyName == 'sd':
            selectedDepartment = Department.objects.filter(isSelected = True).first()
            if selectedDepartment is not None:
                itemList = Item.objects.filter(departmentId = selectedDepartment.id).order_by('-createdDate')[minNumOfRecords:maxNumOfRecords]
        
        # elif instance.keyName == 'ed':
        #     print('explore department')
        
        elif instance.keyName == 'nwap':
            selectedNewArrival = NewArrival.objects.all().order_by('-createdDate').first()
            if selectedNewArrival is not None:
                departmentList = Item.objects.filter(newArrivalId = selectedNewArrival.id).values('departmentId').distinct()[minNumOfRecords:maxNumOfRecords]
        
        elif instance.keyName == 'ban':
            departmentList = Item.objects.filter(totalViews__gte = 0).values('departmentId').distinct()[minNumOfRecords:maxNumOfRecords]
        
        if departmentList.__len__() > 0:
            for department in departmentList:
                selectedItem = Item.objects.annotate(item_sku_count = Count('item_sku')).filter(departmentId = department['departmentId']).filter(isApproved=True).filter(item_sku_count__gt=0).order_by('-createdDate').first()
                if selectedItem is not None:
                    itemList.append(selectedItem)
            
        for item in itemList:
            selectedSKUList = SKU.objects.filter(itemId = item.id)
            if selectedSKUList.__len__() > 0:
                skuList.append(selectedSKUList[0])

        return SKUBasicSerializer(skuList,many=True).data    

#Copoun

class CouponSerializer(serializers.ModelSerializer):
    class Meta:
        model = Coupon
        fields = [
            'id',
            'code',
            'discount',
            'startDate',
            'endDate',
            'description',
            'createdDate',
            'updatedDate',
        ]

#Client

class ClientSerializer(serializers.ModelSerializer):
    provider =  serializers.SerializerMethodField()
    phoneList =  serializers.SerializerMethodField()
    creditCardList =  serializers.SerializerMethodField()
    addressBookList =  serializers.SerializerMethodField()
    selectedLanguage = serializers.SerializerMethodField()
    wallet = serializers.SerializerMethodField()
    class Meta:
        model = Client
        fields = [
            'id',
            'firstName',
            'lastName',
            'email',
            'password',
            'createdDate',
            'updatedDate',
            'providerId',
            'selectedLanguageId',          
            'provider',
            'selectedLanguage',
            'phoneList',
            'creditCardList',
            'addressBookList',
            'wallet',
        ]
    def get_provider(self,instance):
        languageId = getLanguage(self)
        provider = Provider.objects.get(id=str(instance.providerId.id))
        return ProviderSerializer(provider,context={'languageId':languageId}).data

    def get_phoneList(self, instance):
        phoneList = ClientPhone.objects.filter(clientId=str(instance.id))
        return ClientPhoneSerializer(phoneList,many=True).data

    def get_creditCardList(self, instance):
        creditCardList = ClientCreditCard.objects.filter(clientId=str(instance.id))
        return ClientCreditCardSerializer(creditCardList,many=True).data

    def get_addressBookList(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            addressBookList = ClientAddressBook.objects.filter(clientId=str(instance.id))
            return ClientAddressBookSerializer(addressBookList,many=True,context={'languageId':languageId}).data
        else:
            return []
        
    def get_selectedLanguage(self, instance):
        selectedLanguage = Language.objects.get(id=str(instance.selectedLanguageId.id))
        return LanguageSerializer(selectedLanguage).data

    def get_wallet(self, instance):
        if Wallet.objects.filter(clientId=str(instance.id)).exists():
            wallet = Wallet.objects.get(clientId=str(instance.id))
            return WalletSerializer(wallet).data

class ClientDisplaySerializer(serializers.ModelSerializer):
    class Meta:
        model = Client
        fields = [
            'id',
            'firstName',
            'lastName',
        ]

#Client Phone

class ClientPhoneSerializer(serializers.ModelSerializer):
    class Meta:
        model = ClientPhone
        fields = [
            'id',
            'phone',
            'createdDate',
            'updatedDate',
            'clientId',
        ]

#Client Credit Card

class ClientCreditCardSerializer(serializers.ModelSerializer):
    class Meta:
        model = ClientCreditCard
        fields = [
            'id',
            'holder',
            'number',
            'expireYear',
            'expireMonth',
            'createdDate',
            'updatedDate',
            'clientId',
        ]

#Address Book

class ClientAddressBookSerializer(serializers.ModelSerializer):
    class Meta:
        model = ClientAddressBook
        fields = [
            'id',
            'streetName',
            'buildingNumber',
            'floorNumber',
            'apartmentNumber',
            'notes',
            'districtId',
            'isManually',
            'addressLine1',
            'addressLine2',
            'city',
            'county',
            'postCode',
            'createdDate',
            'updatedDate',
            'clientId',
        ]

#Wallet

class WalletSerializer(serializers.ModelSerializer):
    inCount = serializers.SerializerMethodField()
    inSum = serializers.SerializerMethodField()
    outCount = serializers.SerializerMethodField()
    outSum = serializers.SerializerMethodField()
    transCount = serializers.SerializerMethodField()
    transSum = serializers.SerializerMethodField()
    class Meta:
        model = Wallet
        fields = [
            'createdDate',
            'updatedDate',
            'clientId',
            'inCount',
            'inSum',
            'outCount',
            'outSum',
            'transCount',
            'transSum'
        ]
    def get_inCount(self,instance):
        return Transaction.objects.filter(transactionTypeId=1).filter(walletId=instance.clientId.id).order_by('-id').count()
    
    def get_outCount(self,instance):
        return Transaction.objects.filter(transactionTypeId=2).filter(walletId=instance.clientId.id).order_by('-id').count()
    
    def get_inSum(self,instance):
        inValDict = Transaction.objects.filter(transactionTypeId=1).filter(walletId=instance.clientId.id).order_by('-id').aggregate(Sum('amount'))
        if inValDict is not None:
            inVal = inValDict['amount__sum']
            if inVal is not None:
                return inVal
            else:
                return 0
    
    def get_outSum(self,instance):
        outValDict = Transaction.objects.filter(transactionTypeId=2).filter(walletId=instance.clientId.id).order_by('-id').aggregate(Sum('amount'))
        if outValDict is not None:
            outVal = outValDict['amount__sum']
            if outVal is not None:
                return outVal
            else:
                return 0
    
    def get_transCount(self,instance):
        inValDict = Transaction.objects.filter(transactionTypeId=1).filter(walletId=instance.clientId.id).order_by('-id').count()
        outValDict = Transaction.objects.filter(transactionTypeId=2).filter(walletId=instance.clientId.id).order_by('-id').count()
        if inValDict is not None:
            if outValDict is not None:
                inVal = inValDict
                outVal = outValDict
                total =  inVal + outVal
                return total

    def get_transSum(self,instance):
        inValDict = Transaction.objects.filter(transactionTypeId=1).filter(walletId=instance.clientId.id).order_by('-id').aggregate(Sum('amount'))
        outValDict = Transaction.objects.filter(transactionTypeId=2).filter(walletId=instance.clientId.id).order_by('-id').aggregate(Sum('amount'))
        if inValDict is not None:
            if outValDict is not None:
                inVal = inValDict['amount__sum']
                outVal = outValDict['amount__sum']
                if inVal is not None:
                    if outVal is not None:
                        total =  inVal - outVal
                    else:
                        total = 0
                else:
                    total = 0
            else:
                total = 0
        else:
            total = 0
        return total

#Transaction

class TransactionSerializer(serializers.ModelSerializer):
    transactionType = serializers.SerializerMethodField()
    class Meta:
        model = Transaction
        fields = [
            'id',
            'amount',
            'createdDate',
            'updatedDate',
            'transactionTypeId',
            'walletId',
            'transactionType'
        ]
    def get_transactionType(self, instance):
        return getTransactionType(self,instance)

#Purchase Order

class PurchaseOrderSerializer(serializers.ModelSerializer):
    paymentMethod = serializers.SerializerMethodField()
    orderFrom = serializers.SerializerMethodField()
    cancelType = serializers.SerializerMethodField()
    coupon = serializers.SerializerMethodField()
    client = serializers.SerializerMethodField()
    countSalesOrder = serializers.SerializerMethodField()

    subTotal = 0
    delivery = 0
    service = 0
    taxes = 0
    transactionFees = 0
    total = 0

    class Meta:
        model = PurchaseOrder
        fields = [
            'id',
            'createdDate',
            'updatedDate',
            'sessionId',
            'paymentIntentId',
            'isCanceled',
            'isRefunded',
            'paymentMethodId',
            'taxesId',
            'orderFromId',
            'cancelTypeId',
            'couponId',
            'clientId',
            'paymentMethod',
            'orderFrom',
            'coupon',
            'cancelType',
            'client',
            'countSalesOrder',
        ]

    def get_countSalesOrder(self,instance):
        salesOrderList = SalesOrder.objects.filter(shippingItemId__purchaseOrderId=str(instance.id))
        return salesOrderList.__len__()
        
    def get_paymentMethod(self, instance):
        return getPaymentMethod(self,instance)

    def get_orderFrom(self, instance):
        languageId = getLanguage(self)
        orderFrom = OrderFrom.objects.get(id=str(instance.orderFromId.id))
        return OrderFromSerializer(orderFrom,context={'languageId':languageId}).data
        
    def get_cancelType(self,instance):
        languageId = getLanguage(self)
        if instance.cancelTypeId is not None:
            cancelType = CancelType.objects.get(id=str(instance.cancelTypeId.id))
            return CancelTypeSerializer(cancelType,context={'languageId':languageId}).data
        
    def get_coupon(self, instance):
        if instance.couponId is not None:
            coupon = Coupon.objects.get(id=str(instance.couponId.id))
            return CouponSerializer(coupon).data
        
    def get_client(self,instance):
        if instance.clientId is not None:
            client = Client.objects.get(id=str(instance.clientId.id))
            return ClientDisplaySerializer(client).data

    def calculate(self,instance):
        shippingItemList = ShippingItem.objects.filter(purchaseOrderId = instance.id)
    
        for shippingItem in shippingItemList:
            
            purchaseShippingItemList = PurchaseShippingItem.objects.filter(shippingItemId = shippingItem.id)
            for purchaseShippingItem in purchaseShippingItemList:
                self.subTotal = self.subTotal + (purchaseShippingItem.offeredPrice * (purchaseShippingItem.deliveredQuantity + purchaseShippingItem.canceledQuantity + purchaseShippingItem.returnedQuantity))
                selectedItemCost = ItemCost.objects.get(id = str(purchaseShippingItem.itemCostId.id))
                if selectedItemCost is not None:
                    actualPrice = purchaseShippingItem.skuPrice / (1 - (purchaseShippingItem.skuDiscount / 100))
                    currentItemCost = (actualPrice * selectedItemCost.priceInPercent) / 100
                    self.service = self.service + currentItemCost
            
            if shippingItem.feesId is not None:
                selectedFees = Fees.objects.get(id = str(shippingItem.feesId.id))
                if selectedFees is not None:
                    self.service = self.service + selectedFees.handlingFees + selectedFees.packingFees

            if shippingItem.deliveryPriceId is not None:
                selectedDeliveryPrice = DeliveryPrice.objects.get(id = str(shippingItem.deliveryPriceId.id))
                if selectedDeliveryPrice is not None:
                    self.delivery = self.delivery + selectedDeliveryPrice.price

        if instance.taxesId is not None:
            selectedTaxes = Taxes.objects.get(id=str(instance.taxesId.id))
            if selectedTaxes is not None:
                self.taxes = (self.service * selectedTaxes.price) / 100
            else:
                self.taxes = 0
        else:
            self.taxes = 0
        
        totalAfterTaxes = self.subTotal + self.delivery + self.service + self.taxes

        self.sumPaymentMethod = 0
        paymentMethod = PaymentMethod.objects.get(id=str(instance.paymentMethodId.id))
        if paymentMethod is not None:
            if paymentMethod.isPercentage == True:
                preTotal = totalAfterTaxes + paymentMethod.fixedPrice
                preTotal = preTotal / (1 - (paymentMethod.price / 100))
                self.transactionFees = preTotal - totalAfterTaxes
                self.transactionFees = self.transactionFees * shippingItemList.__len__()
            else:
                self.transactionFees = paymentMethod.price * shippingItemList.__len__()

        self.total = self.subTotal + self.delivery + self.service + self.taxes + self.transactionFees

        if instance.couponId is not None:
            self.total = self.total - instance.couponId.discount

    def to_representation(self, instance):
        ret = super().to_representation(instance)
        self.subTotal = 0
        self.delivery = 0
        self.service = 0
        self.taxes = 0
        self.total = 0
        self.sumPaymentMethod = 0
        self.calculate(instance)
        ret['subTotal'] = self.subTotal
        ret['delivery'] = self.delivery
        ret['service'] = self.service
        ret['taxes'] = self.taxes
        ret['transactionFees'] = self.transactionFees
        ret['total'] = self.total
        return ret
    
#Supplier

class SupplierSerializer(serializers.ModelSerializer):
    phoneList = serializers.SerializerMethodField()
    creditCardList = serializers.SerializerMethodField()
    documentList = serializers.SerializerMethodField()
    selectedLanguage = serializers.SerializerMethodField()
    currentSubscription = serializers.SerializerMethodField()
    class Meta:
        model = Supplier
        fields = [
            'id',
            'name',
            'email',
            'password',
            'isVerified',
            'createdDate',
            'updatedDate',
            'districtId',
            'selectedLanguageId',
            'isFreeSubscriptionUsed',
            'selectedLanguage',
            'phoneList',
            'creditCardList',
            'documentList',
            'currentSubscription',
        ]

    def get_selectedLanguage(self, instance):
        selectedLanguage = Language.objects.get(id=str(instance.selectedLanguageId.id))
        return LanguageSerializer(selectedLanguage).data

    def get_phoneList(self, instance):
        phoneList = SupplierPhone.objects.filter(supplierId=str(instance.id))
        return SupplierPhoneSerializer(phoneList,many=True).data
    
    def get_creditCardList(self, instance):
        creditCardList = SupplierCreditCard.objects.filter(supplierId=str(instance.id))
        return SupplierCreditCardSerializer(creditCardList,many=True).data

    def get_documentList(self, instance):
        documentList = SupplierDocument.objects.filter(supplierId=str(instance.id))
        return SupplierDocumentSerializer(documentList,many=True).data
    
    def get_currentSubscription(self,instance):
        statusList = SubscriptionStatus.objects.filter(keyName = 'active')
        if statusList.__len__() > 0:
            currentSubscriptionList = Subscription.objects.filter(subscriptionStatusId = statusList[0].id).filter(supplierId=instance.id)
            if currentSubscriptionList.__len__() > 0:
                languageId = getLanguage(self)
                isDashboard = getIsDashboard(self)
                return SubscriptionSerializer(currentSubscriptionList[0],context={'languageId':languageId,'isDashboard':isDashboard}).data

class SupplierDisplaySerializer(serializers.ModelSerializer):
    class Meta:
        model = Supplier
        fields = [
            'id',
            'name',
            'isVerified',
            'districtId',
        ]

#Supplier Phone

class SupplierPhoneSerializer(serializers.ModelSerializer):
    class Meta:
        model = SupplierPhone
        fields = [
            'id',
            'phone',
            'createdDate',
            'updatedDate',
            'supplierId',
        ]

#Supplier Credit Card

class SupplierCreditCardSerializer(serializers.ModelSerializer):
    class Meta:
        model = SupplierCreditCard
        fields = [
            'id',
            'holder',
            'number',
            'expireYear',
            'expireMonth',
            'createdDate',
            'updatedDate',
            'supplierId',
        ]

#Supplier Document

class SupplierDocumentSerializer(serializers.ModelSerializer):
    class Meta:
        model = SupplierDocument
        fields = [
            'id',
            'fileName',
            'contentType',
            'fileDocument',
            'createdDate',
            'updatedDate',
            'supplierId',
        ]

#Subscription

class SubscriptionSerializer(serializers.ModelSerializer):
    subscriptionType = serializers.SerializerMethodField()
    subscriptionStatus = serializers.SerializerMethodField()
    class Meta:
        model = Subscription
        fields = [
            'id',
            'price',
            'discount',
            'serviceCost',
            'startDate',
            'endDate',
            'createdDate',
            'updatedDate',
            'supplierId',
            'subscriptionTypeId',
            'subscriptionStatusId',
            'subscriptionType',
            'subscriptionStatus'
        ]
    def get_subscriptionType(self, instance):
        return getSubscriptionType(self,instance)
    
    def get_subscriptionStatus(self, instance):
        return getSubscriptionStatus(self,instance)

#Delivery Man

class DeliveryManSerializer(serializers.ModelSerializer):
    selectedLanguage = serializers.SerializerMethodField()
    phoneList = serializers.SerializerMethodField()
    class Meta:
        model = DeliveryMan
        fields = [
            'id',
            'name',
            'email',
            'password',
            'createdDate',
            'updatedDate',
            'districtId',
            'selectedLanguageId',
            'selectedLanguage',
            'phoneList'
        ]

    def get_selectedLanguage(self, instance):
        selectedLanguage = Language.objects.get(id=str(instance.selectedLanguageId.id))
        return LanguageSerializer(selectedLanguage).data

    def get_phoneList(self, instance):
        phoneList = DeliveryManPhone.objects.filter(deliveryManId=str(instance.id))
        return DeliveryManPhoneSerializer(phoneList,many=True).data

class DeliveryManDisplaySerializer(serializers.ModelSerializer):
    class Meta:
        model = DeliveryMan
        fields = [
            'id',
            'name',
        ]

#Delivery Man Phone

class DeliveryManPhoneSerializer(serializers.ModelSerializer):
    class Meta:
        model = DeliveryManPhone
        fields = [
            'id',
            'phone',
            'createdDate',
            'updatedDate',
            'deliveryManId',
        ]

#Delivery Man Price List

class DeliveryManPriceSerializer(serializers.ModelSerializer):
    class Meta:
        model = DeliveryManPrice
        fields = [
            'id',
            'price',
            'createdDate',
            'updatedDate',
            'deliveryPriceId',
            'deliveryManId'
        ]

#Shipping Item

class ShippingItemSerializer(serializers.ModelSerializer):
    isSalesOrderCreated = serializers.SerializerMethodField()
    isCanceled = serializers.SerializerMethodField()
    deliveryPrice = serializers.SerializerMethodField()
    fees = serializers.SerializerMethodField()
    shippingAddress = serializers.SerializerMethodField()
    purchaseShippingItemList = serializers.SerializerMethodField()
    class Meta:
        model = ShippingItem
        fields = [
            'id',
            'createdDate',
            'updatedDate',
            'deliveryPriceId',
            'feesId',
            'purchaseOrderId',
            'isSalesOrderCreated',
            'isCanceled',
            'isValidAndPaid',
            'deliveryPrice',
            'fees',
            'shippingAddress',
            'purchaseShippingItemList',
        ]

    def get_isSalesOrderCreated(self,instance):
        if SalesOrder.objects.filter(shippingItemId=str(instance.id)).exists():
            return True
        else:
            return False
    
    def get_isCanceled(self,instance):
        return instance.purchaseOrderId.isCanceled

    def get_deliveryPrice(self, instance):
        if instance.deliveryPriceId is not None:
            if DeliveryPrice.objects.filter(id=str(instance.deliveryPriceId.id)).exists():
                languageId = getLanguage(self)
                deliveryPrice = DeliveryPrice.objects.get(id = str(instance.deliveryPriceId.id))
                return DeliveryPriceSerializer(deliveryPrice,context={'languageId':languageId}).data

    def get_fees(self, instance):
        if instance.feesId is not None:
            if Fees.objects.filter(id=str(instance.feesId.id)).exists():
                languageId = getLanguage(self)
                fees = Fees.objects.get(id = str(instance.feesId.id))
                return FeesSerializer(fees,context={'languageId':languageId}).data

    def get_shippingAddress(self, instance):
        if ShippingAddress.objects.filter(shippingItemId=str(instance.id)).exists():
            languageId = getLanguage(self)
            shippingAddress = ShippingAddress.objects.get(shippingItemId=str(instance.id))
            return ShippingAddressSerializer(shippingAddress,context={'languageId':languageId}).data

    def get_purchaseShippingItemList(self,instance):
        isShowShippingItemDetail = getIsShowShippingItemDetail(self)
        if isShowShippingItemDetail is not None:
            if int(isShowShippingItemDetail) == 1:
                clientId = getClientId(self)
                languageId = getLanguage(self)
                isDashboard = getIsDashboard(self)
                isShowItem = getIsShowItem(self)
                isShowRequestReturnDetail = getIsShowRequestReturnDetail(self)
                purchaseShippingItem = PurchaseShippingItem.objects.filter(shippingItemId = instance.id)
                return PurchaseShippingItemSerializer(purchaseShippingItem,many=True,context={'clientId':clientId,'languageId':languageId,'isDashboard':isDashboard,'isShowItem':isShowItem,'isShowRequestReturnDetail':isShowRequestReturnDetail}).data

#Shipping Address

class ShippingAddressSerializer(serializers.ModelSerializer):
    deliveryMan = serializers.SerializerMethodField()
    trackList = serializers.SerializerMethodField()
    class Meta:
        model = ShippingAddress
        fields = [
            'shippingItemId',
            'trackNumber',
            'phone',
            'streetName',
            'buildingNumber',
            'floorNumber',
            'apartmentNumber',
            'notes',
            'districtId',
            'isManually',
            'addressLine1',
            'addressLine2',
            'city',
            'county',
            'postCode',
            'createdDate',
            'updatedDate',
            'deliveryManId',
            'deliveryMan',
            'trackList',
        ]

    def get_deliveryMan(self, instance):
        if instance.deliveryManId is not None:
            languageId = getLanguage(self)
            deliveryMan = DeliveryMan.objects.get(id=str(instance.deliveryManId.id))
            return DeliveryManDisplaySerializer(deliveryMan,context={'languageId':languageId}).data

    def get_trackList(self,instance):
        languageId = getLanguage(self)
        trackList = Track.objects.filter(shippingAddressId = instance.shippingItemId.id).order_by('-id')
        return TrackSerializer(trackList,many=True,context={'languageId':languageId}).data

#Track

class TrackSerializer(serializers.ModelSerializer):
    trackStatus = serializers.SerializerMethodField()
    class Meta:
        model = Track
        fields = [
            'id',
            'createdDate',
            'updatedDate',
            'shippingAddressId',
            'trackStatusId',
            'trackStatus',
        ]
    def get_trackStatus(self, instance):
        languageId = getLanguage(self)
        trackStatus = TrackStatus.objects.get(id=str(instance.trackStatusId.id))
        return TrackStatusSerializer(trackStatus,context={'languageId':languageId}).data

#Sales Order

class SalesOrderSerializer(serializers.ModelSerializer):
    shippingItem = serializers.SerializerMethodField()
    supplier = serializers.SerializerMethodField()
    salesOrderItemList = serializers.SerializerMethodField()
    class Meta:
        model = SalesOrder
        fields = [
            'shippingItemId',
            'createdDate',
            'updatedDate',
            'isConfirmed',
            'supplierId',
            'shippingItem',
            'supplier',
            'salesOrderItemList'
        ]

    def get_shippingItem(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if int(isDashboard) == 1:
                languageId = getLanguage(self)
                isShowShippingItemDetail = getIsShowShippingItemDetail(self)
                shippingItem = ShippingItem.objects.get(id=str(instance.shippingItemId.id))
                return ShippingItemSerializer(shippingItem,context={'languageId':languageId,'isDashboard':isDashboard,'isShowShippingItemDetail':isShowShippingItemDetail}).data

    def get_supplier(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if int(isDashboard) == 1:
                return getSupplierInfo(self,instance)
    
    def get_salesOrderItemList(self,instance):
        isShowItem = getIsShowItem(self)
        languageId = getLanguage(self)
        salesOrderItem = SalesOrderItem.objects.filter(salesOrderId = instance.shippingItemId.id)
        return SalesOrderItemSerializer(salesOrderItem,many=True,context={'languageId':languageId,'isShowItem':isShowItem}).data

#Item Subscription

class ItemSubscriptionSerializer(serializers.ModelSerializer):
    subscriptionType = serializers.SerializerMethodField()
    subscriptionStatus = serializers.SerializerMethodField()
    class Meta:
        model = ItemSubscription
        fields = [
            'id',
            'price',
            'discount',
            'serviceCost',
            'startDate',
            'endDate',
            'createdDate',
            'updatedDate',
            'itemId',
            'subscriptionTypeId',
            'subscriptionStatusId',
            'subscriptionType',
            'subscriptionStatus'
        ]
    def get_subscriptionType(self, instance):
        return getSubscriptionType(self,instance)
    
    def get_subscriptionStatus(self, instance):
        return getSubscriptionStatus(self,instance)

#Purchase Shipping Item

class PurchaseShippingItemSerializer(serializers.ModelSerializer):
    itemCost = serializers.SerializerMethodField()
    sku = serializers.SerializerMethodField()
    canRequestReturnAgain = serializers.SerializerMethodField()
    requestReturnList = serializers.SerializerMethodField()
    class Meta:
        model = PurchaseShippingItem
        fields = [
            'id',
            'offeredPrice',
            'skuPrice',
            'skuDiscount',
            'deliveredQuantity',
            'returnedQuantity',
            'canceledQuantity',
            'createdDate',
            'updatedDate',
            'itemCostId',
            'skuId',
            'shippingItemId',
            'itemCost',
            'sku',
            'canRequestReturnAgain',
            'requestReturnList'
        ]
    def get_itemCost(self,instance):
        languageId = getLanguage(self)
        if instance.itemCostId is not None:
            itemCost = ItemCost.objects.get(id=str(instance.itemCostId.id))
            return ItemCostSerializer(itemCost,context={'languageId':languageId}).data
  
    def get_sku(self, instance):
        return getSKU(self,instance)
    
    def get_requestReturnList(self,instance):
        isShowRequestReturnDetail = getIsShowRequestReturnDetail(self)
        if isShowRequestReturnDetail is not None:
            if int(isShowRequestReturnDetail) == 1:
                languageId = getLanguage(self)
                isDashboard = getIsDashboard(self)
                requestReturn = RequestReturn.objects.filter(purchaseShippingItemId = instance.id)
                return RequestReturnSerializer(requestReturn,many=True,context={'languageId':languageId,'isDashboard':isDashboard}).data
    
    def get_canRequestReturnAgain(self,instance):
        if instance.deliveredQuantity > 0:
            data = RequestReturn.objects.filter(purchaseShippingItemId=instance.id)
            if data.__len__() > 0:
                sum = 0
                for d in data:
                    sum += d.requestedQuantity
                if instance.deliveredQuantity > 0:
                    diff = instance.deliveredQuantity - sum
                    if diff > 0:
                        return True
                    if diff == 0:
                        return False
                    else:
                        return False
                else: 
                    return False
            else:
                return True
        else:
            return False

#Request Return

class RequestReturnSerializer(serializers.ModelSerializer):
    returnCost = serializers.SerializerMethodField()
    returnStatus = serializers.SerializerMethodField()
    class Meta:
        model = RequestReturn
        fields = [
            'id',
            'headLine',
            'requestedQuantity',
            'issue',
            'note',
            'createdDate',
            'updatedDate',
            'returnCostId',
            'returnStatusId',
            'purchaseShippingItemId',
            'returnStatus',
            'returnCost',
        ]
    
    def get_returnCost(self,instance):
        return getReturnCost(self,instance)

    def get_returnStatus(self, instance):
        return getReturnStatus(self,instance)

class RequestReturnSerializerDetail(serializers.ModelSerializer):
    returnCost = serializers.SerializerMethodField()
    returnStatus = serializers.SerializerMethodField()
    purchaseShippingItem = serializers.SerializerMethodField()
    class Meta:
        model = RequestReturn
        fields = [
            'id',
            'headLine',
            'requestedQuantity',
            'issue',
            'note',
            'createdDate',
            'updatedDate',
            'returnCostId',
            'returnStatusId',
            'purchaseShippingItemId',
            'returnStatus',
            'returnCost',
            'purchaseShippingItem',
        ]
    
    def get_returnCost(self,instance):
        return getReturnCost(self,instance)

    def get_returnStatus(self, instance):
        return getReturnStatus(self,instance)
    
    def get_purchaseShippingItem(self,instance):
        languageId = getLanguage(self)
        isDashboard = getIsDashboard(self)
        isShowItem = getIsShowItem(self)
        purchaseShippingItem = PurchaseShippingItem.objects.get(id = str(instance.purchaseShippingItemId.id))
        return PurchaseShippingItemSerializer(purchaseShippingItem,context={'languageId':languageId,'isDashboard':isDashboard,'isShowItem':isShowItem}).data

#Sales Order Item

class SalesOrderItemSerializer(serializers.ModelSerializer):
    sku = serializers.SerializerMethodField()
    class Meta:
        model = SalesOrderItem
        fields = [
            'id',
            'quantity',
            'price',
            'createdDate',
            'updatedDate',
            'skuId',
            'salesOrderId',
            'sku',
        ]
    def get_sku(self, instance):
        return getSKU(self,instance)

#Client Item Rate Review

class ClientItemRateReviewSerializer(serializers.ModelSerializer):
    client = serializers.SerializerMethodField()
    class Meta:
        model = ClientItemRateReview
        fields = [
            'id',
            'headLine',
            'rate',
            'comment',
            'createdDate',
            'updatedDate',
            'itemId',
            'clientId',
            'client'
        ]
    def get_client(self, instance):
        return getClient(self,instance)

#Client Supplier Rate Review

class ClientSupplierRateReviewSerializer(serializers.ModelSerializer):
    client = serializers.SerializerMethodField()
    class Meta:
        model = ClientSupplierRateReview
        fields = [
            'id',
            'headLine',
            'rate',
            'comment',
            'createdDate',
            'updatedDate',
            'supplierId',
            'clientId',
            'client'
        ]
    def get_client(self, instance):
        return getClient(self,instance)

#Feedback

class FeedbackSerializer(serializers.ModelSerializer):
    client = serializers.SerializerMethodField()
    class Meta:
        model = Feedback
        fields = [
            'id',        
            'headLine',
            'rate',
            'comment',
            'createdDate',
            'updatedDate',
            'clientId',
            'client'
        ]
    def get_client(self, instance):
        return getClient(self,instance)

#Request

class RequestSerializer(serializers.ModelSerializer):
    client = serializers.SerializerMethodField()
    class Meta:
        model = Request
        fields = [
            'id',
            'headLine',
            'description',
            'response',
            'isDone',
            'createdDate',
            'updatedDate',
            'clientId',
            'client'
        ]
    def get_client(self, instance):
        return getClient(self,instance)

#Policy Type

class PolicyTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = PolicyType
        fields = [
            'id',
            'name',
            'createdDate',
            'updatedDate',
        ]

#Policy Header

class PolicyHeaderSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    descriptionList = serializers.SerializerMethodField()
    class Meta:
        model = PolicyHeader
        fields = [
            'id',
            'name',
            'languageList',
            'createdDate',
            'updatedDate',
            'policyTypeId',
            'descriptionList'
        ]
    def get_name(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            policyHeaderLanguageList = PolicyHeaderLanguage.objects.filter(policyHeaderId=str(instance.id)).filter(languageId=languageId)
            if policyHeaderLanguageList.__len__() > 0:
                return policyHeaderLanguageList[0].name
    
    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                policyHeaderLanguageList = PolicyHeaderLanguage.objects.filter(policyHeaderId=str(instance.id))
                if policyHeaderLanguageList.__len__() > 0:
                    return PolicyHeaderLanguageSerializer(policyHeaderLanguageList,many=True).data

    def get_descriptionList(self,instance):
        languageId = getLanguage(self)
        isDashboard = getIsDashboard(self)
        descriptionList = PolicyDescription.objects.filter(policyHeaderId = instance.id)
        return PolicyDescriptionSerializer(descriptionList,context={'languageId':languageId,'isDashboard':isDashboard},many=True).data

class PolicyHeaderLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = PolicyHeaderLanguage
        fields = [
            'id',
            'name',
            'policyHeaderId',
            'languageId'
        ]

#Policy Description

class PolicyDescriptionSerializer(serializers.ModelSerializer):
    description = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    class Meta:
        model = PolicyDescription
        fields = [
            'id',
            'description',
            'languageList',
            'createdDate',
            'updatedDate',
            'policyHeaderId',
        ]
    def get_description(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            policyDescriptionLanguageList = PolicyDescriptionLanguage.objects.filter(policyDescriptionId=str(instance.id)).filter(languageId=languageId)
            if policyDescriptionLanguageList.__len__() > 0:
                return policyDescriptionLanguageList[0].description
    
    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                policyDescriptionLanguageList = PolicyDescriptionLanguage.objects.filter(policyDescriptionId=str(instance.id))
                if policyDescriptionLanguageList.__len__() > 0:
                    return PolicyDescriptionLanguageSerializer(policyDescriptionLanguageList,many=True).data

class PolicyDescriptionLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = PolicyDescriptionLanguage
        fields = [
            'id',
            'description',
            'policyDescriptionId',
            'languageId'
        ]

#Payment

class RechargeSerializer(serializers.ModelSerializer):
    taxes = serializers.SerializerMethodField()
    paymentMethod = serializers.SerializerMethodField()
    isPaid = serializers.SerializerMethodField()
    
    sumTaxes = 0
    transactionFees = 0
    total = 0

    class Meta:
        model = Recharge
        fields = [
            'id',
            'subTotal',
            'service',
            'sessionId',
            'paymentIntentId',
            'description',
            'createdDate',
            'updatedDate',
            'taxesId',
            'paymentMethodId',
            'taxes',
            'paymentMethod',
            'isPaid',
        ]

    def get_taxes(self,instance):
        return getTaxes(self,instance)

    def get_paymentMethod(self,instance):
        return getPaymentMethod(self,instance)
    
    def get_isPaid(self,instance):
        isCheck = False
        paymentList = Payment.objects.filter(rechargeId = instance.id).filter(taxesId = instance.taxesId)
        if paymentList.__len__() > 0:
            isCheck = True
        return isCheck
    
    def to_representation(self, instance):
        ret = super().to_representation(instance)
        
        self.sumTaxes = 0
        self.transactionFees = 0
        self.total = 0

        if instance.service > 0:
            self.total = instance.subTotal + instance.service
        else:
            self.total = instance.subTotal

        if instance.taxesId is not None:
            currentValue = 0
            if instance.service > 0:
                currentValue = instance.service
            else:
                currentValue = self.total
            
            self.sumTaxes = self.sumTaxes + ((currentValue * instance.taxesId.price) / 100)
            self.total = self.total + self.sumTaxes
        
        if instance.paymentMethodId is not None:
            if instance.paymentMethodId.isPercentage == True:
                preTotal = self.total + instance.paymentMethodId.fixedPrice
                preTotal = preTotal / (1 - (instance.paymentMethodId.price / 100))
                self.transactionFees = preTotal - self.total
            else:
                self.transactionFees  = self.transactionFees + instance.paymentMethodId.price
            
            self.total = self.total + self.transactionFees

        ret['sumTaxes'] = self.sumTaxes
        ret['transactionFees'] = self.transactionFees
        ret['total'] = self.total
        return ret

#Payment

class PaymentSerializer(serializers.ModelSerializer):
    transactionType = serializers.SerializerMethodField()
    class Meta:
        model = Payment
        fields = [
            'id',
            'paid',
            'change',
            'total',
            'description',
            'descriptionDetail',
            'createdDate',
            'updatedDate',
            'transactionTypeId',
            'purchaseOrderId',
            'taxesId',
            'salesOrderId',
            'deliveryManId',
            'requestReturnId',
            'subscriptionId',
            'itemSubscriptionId',
            'rechargeId',
            'transactionType',
        ]
    def get_transactionType(self,instance):
        return getTransactionType(self,instance)

#Accessory

class AccessorySerializer(serializers.ModelSerializer):
    amount = serializers.SerializerMethodField()
    class Meta:
        model = Accessory
        fields = [
            'id',
            'name',
            'price',
            'description',
            'createdDate',
            'updatedDate',
            'amount'
        ]
    def get_amount(self,instance):
        inTrans = TransactionType.objects.filter(keyName__icontains='InTrans')
        outTrans = TransactionType.objects.filter(keyName__icontains='OutTrans')
        amountInData = AccessoryTransaction.objects.filter(accessoryId = instance.id).filter(transactionTypeId = inTrans[0].id).aggregate(Sum('quantity'))
        amountOutData = AccessoryTransaction.objects.filter(accessoryId = instance.id).filter(transactionTypeId = outTrans[0].id).aggregate(Sum('quantity'))

        amountIn = 0
        amountOut = 0

        if amountInData['quantity__sum'] is not None:
            amountIn = amountInData['quantity__sum']
        
        if amountOutData['quantity__sum'] is not None:
            amountOut = amountOutData['quantity__sum']

        amount = amountIn - amountOut
        return amount

#Accessory Transaction

class AccessoryTransactionSerializer(serializers.ModelSerializer):
    total = serializers.SerializerMethodField()
    transactionType = serializers.SerializerMethodField()
    class Meta:
        model = AccessoryTransaction
        fields = [
            'id',
            'quantity',
            'price',
            'createdDate',
            'updatedDate',
            'transactionTypeId',
            'accessoryId',
            'total',
            'transactionType'
        ]
    def get_total(self,instance):
        return instance.quantity * instance.price

    def get_transactionType(self,instance):
        return getTransactionType(self,instance)

#Employee

class EmployeeSerializer(serializers.ModelSerializer):
    class Meta:
        model = Employee
        fields = [
            'id',
            'fullName',
            'email',
            'password',
            'createdDate',
            'updatedDate',
        ]

#Item

class ItemSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    description = serializers.SerializerMethodField()
    department = serializers.SerializerMethodField()
    brand = serializers.SerializerMethodField()
    supplier = serializers.SerializerMethodField()
    newArrival = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    skuList = serializers.SerializerMethodField()
    rate = serializers.SerializerMethodField()
    isFavoriteWithClient = serializers.SerializerMethodField()
    featureBulletList = serializers.SerializerMethodField()
    externalLink = serializers.SerializerMethodField()
    amount = serializers.SerializerMethodField()
    class Meta:
        model = Item
        fields = [
            'id',
            'keyName',
            'name',
            'description',
            'isApproved',
            'isTop',
            'isFavorite',
            'isElectronic',
            'totalViews',
            'createdDate',
            'updatedDate',
            'departmentId',
            'brandId',
            'supplierId',
            'newArrivalId',
            'linkId',
            'isFavoriteWithClient',
            'department',
            'brand',
            'supplier',
            'newArrival',
            'languageList',
            'externalLink',
            'skuList',
            'featureBulletList',
            'rate',
            'amount'
        ]
    def get_isFavoriteWithClient(self,instance):
        return getIsFavoriteWithCurrentClient(self,instance)
    
    def get_amount(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                count = SKU.objects.filter(itemId=instance.id).aggregate(sum_amount=Sum('amount'))
                return count['sum_amount']
        return 0

    def get_externalLink(self,instance):
        languageId = getLanguage(self)
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                if instance.linkId is not None:
                    selectedExternalLink = Externalink.objects.get(id=instance.linkId.id)
                    if selectedExternalLink is not None:
                        return ExternalinkSerializer(selectedExternalLink,context={'languageId':languageId,'isDashboard':isDashboard}).data

    def get_skuList(self,instance):
        languageId = getLanguage(self)
        isDashboard = getIsDashboard(self)
        isMultipleSKU = getIsMultipleSKU(self)
        isShowItem = getIsShowItem(self)
        maxPrice = 0
        minPrice = 0
        selectedSKUList = []
        if isMultipleSKU is not None:
            if int(isMultipleSKU) == 1:  
                selectedSKUList = SKU.objects.filter(itemId=instance.id).order_by('price')
                return SKUSerializer(selectedSKUList,context={'languageId':languageId,'isDashboard':isDashboard,'isShowItem':isShowItem},many=True).data

            else:
                isSelectedRange = False
                    
                if(self.context.__contains__('request')):
                    minPrice = self.context['request'].query_params.get('min_price', None)
                    maxPrice = self.context['request'].query_params.get('max_price', None)

                if minPrice is not None and maxPrice is not None:
                    if float(minPrice) > 0 and float(maxPrice) > 0:
                        isSelectedRange = True
                        selectedSKUList = SKU.objects.filter(itemId = instance.id).filter(price__gte = minPrice).filter(price__lte = maxPrice)

                elif minPrice is not None:
                    if float(minPrice) > 0:
                        isSelectedRange = True
                        selectedSKUList = SKU.objects.filter(itemId = instance.id).filter(price__gte = minPrice)

                elif maxPrice is not None:
                    if float(maxPrice) > 0:
                        isSelectedRange = True
                        selectedSKUList = SKU.objects.filter(itemId = instance.id).filter(price__lte = maxPrice)
                        
                else:
                    isSelectedRange = False
                        
                if isSelectedRange == False:
                    selectedSKUList = SKU.objects.filter(itemId = instance.id).order_by('price')
                    
                if selectedSKUList.__len__() > 0:
                    tempList = []
                    tempList.append(selectedSKUList[0])
                    return SKUSerializer(tempList,context={'languageId':languageId,'isShowItem':isShowItem},many=True).data    
                else:
                    return []
        else:
            return []
        
    def get_rate(self, instance):
        totalRate = ClientItemRateReview.objects.filter(itemId=str(instance.id)).aggregate(Sum('rate'))
        count = ClientItemRateReview.objects.filter(itemId=str(instance.id)).count()
        if totalRate['rate__sum'] is not None:
            return totalRate['rate__sum'] / count
        else:
            return 0
    
    def get_department(self, instance):
        return getDepartment(self,instance)

    def get_brand(self, instance):
        languageId = getLanguage(self)
        brand = Brand.objects.get(id=str(instance.brandId.id))
        return BrandDisplaySerializer(brand,context={'languageId':languageId}).data

    def get_supplier(self, instance):
        return getSupplierInfo(self,instance)

    def get_newArrival(self,instance):
        languageId = getLanguage(self)
        isDashboard = getIsDashboard(self)
        newArrival = NewArrival.objects.get(id=str(instance.newArrivalId.id))
        return NewArrivalDisplaySerializer(newArrival,context={'languageId':languageId,'isDashboard':isDashboard}).data

    def get_featureBulletList(self,instance):
        languageId = getLanguage(self)
        isDashboard = getIsDashboard(self)
        isShowFeatureBullet = getIsShowFeatureBullet(self)
        if isShowFeatureBullet is not None:
            if int(isShowFeatureBullet) == 1:
                featureBulletList = FeatureBullet.objects.filter(itemId=str(instance.id))
                return FeatureBulletSerializer(featureBulletList,context={'languageId':languageId,'isDashboard':isDashboard},many=True).data
            return []
        return []

    def get_name(self, instance):
        languageId = getLanguage(self)
        itemLanguageList = ItemLanguage.objects.filter(itemId=str(instance.id)).filter(languageId=languageId)
        if itemLanguageList.__len__() > 0:
            return itemLanguageList[0].name
    
    def get_description(self, instance):
        languageId = getLanguage(self)
        itemLanguageList = ItemLanguage.objects.filter(itemId=str(instance.id)).filter(languageId=languageId)
        if itemLanguageList.__len__() > 0:
            return itemLanguageList[0].description
    
    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                itemLanguageList = ItemLanguage.objects.filter(itemId=str(instance.id))
                if itemLanguageList.__len__() > 0:
                    return ItemLanguageSerializer(itemLanguageList,many=True).data

class ItemLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ItemLanguage
        fields = [
            'id',
            'name',
            'description',
            'itemId',
            'languageId'
        ]

#Feature Bullet

class FeatureBulletSerializer(serializers.ModelSerializer):
    description = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    class Meta:
        model = FeatureBullet
        fields = [
            'id',
            'keyName',
            'description',
            'languageList',
            'createdDate',
            'updatedDate',
            'itemId',
        ]
    def get_description(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            featureBullerLanguageList = FeatureBulletLanguage.objects.filter(featureBulletId=str(instance.id)).filter(languageId=languageId)
            if featureBullerLanguageList.__len__() > 0:
                return featureBullerLanguageList[0].description
    
    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                featureBullerLanguageList = FeatureBulletLanguage.objects.filter(featureBulletId=str(instance.id))
                if featureBullerLanguageList.__len__() > 0:
                    return FeatureBulletLanguageSerializer(featureBullerLanguageList,many=True).data

class FeatureBulletLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = FeatureBulletLanguage
        fields = [
            'id',
            'description',
            'featureBulletId',
            'languageId'
        ]

#Change Start

class EligibleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Eligible
        fields = [
            'id',
            'createdDate',
            'updatedDate',
            'itemId',
            'eligibleTypeId'
        ]

#Change End

#SKU

class SKUBasicSerializer(serializers.ModelSerializer):
    class Meta:
        model = SKU
        fields = [
            'name',
            'image',
            'createdDate',
        ]

class SKUSerializer(serializers.ModelSerializer):
    skuOptionList = serializers.SerializerMethodField()
    skuDescriptionList = serializers.SerializerMethodField()
    item = serializers.SerializerMethodField()
    class Meta:
        model = SKU
        fields = [
            'id',
            'keyName',
            'name',
            'price',
            'discount',
            'amount',
            'image',
            'createdDate',
            'updatedDate',
            'itemId',
            'skuOptionList',
            'skuDescriptionList',
            'item',
        ]
    def get_skuOptionList(self,instance):
        languageId = getLanguage(self)
        isDashboard = getIsDashboard(self)
        skuOptionList = SKUOption.objects.filter(skuId = instance.id)
        return SKUOptionSerializer(skuOptionList,context={'languageId':languageId,'isDashboard':isDashboard},many=True).data

    def get_skuDescriptionList(self,instance):
        languageId = getLanguage(self)
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                skuDescriptionList = SKUDescription.objects.filter(skuId = instance.id)
                return SKUDescriptionSerializer(skuDescriptionList,context={'languageId':languageId,'isDashboard':isDashboard},many=True).data

    def get_item(self,instance):
        isShowItem = getIsShowItem(self)
        if isShowItem is not None:
            if int(isShowItem) == 1:
                clientId = getClientId(self)
                languageId = getLanguage(self)
                isDashboard = getIsDashboard(self)
                
                item = Item.objects.get(id=str(instance.itemId.id))
                return ItemDisplaySerializer(item,context={'clientId':clientId,'languageId':languageId,'isDashboard':isDashboard}).data

#SKU Option

class SKUOptionSerializer(serializers.ModelSerializer):
    title = serializers.SerializerMethodField()
    value = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    class Meta:
        model = SKUOption
        fields = [
            'id',
            'keyName',
            'title',
            'value',
            'languageList',
            'createdDate',
            'updatedDate',
            'skuId',
        ]

    def get_title(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            skuOptionLanguageList = SKUOptionLanguage.objects.filter(skuOptionId=str(instance.id)).filter(languageId=languageId)
            if skuOptionLanguageList.__len__() > 0:
                return skuOptionLanguageList[0].title

    def get_value(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            skuOptionLanguageList = SKUOptionLanguage.objects.filter(skuOptionId=str(instance.id)).filter(languageId=languageId)
            if skuOptionLanguageList.__len__() > 0:
                return skuOptionLanguageList[0].value
    
    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                skuOptionLanguageList = SKUOptionLanguage.objects.filter(skuOptionId=str(instance.id))
                if skuOptionLanguageList.__len__() > 0:
                    return SKUOptionLanguageSerializer(skuOptionLanguageList,many=True).data

class SKUOptionLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = SKUOptionLanguage
        fields = [
            'id',
            'title',
            'value',
            'skuOptionId',
            'languageId'
        ]

#SKU Description

class SKUDescriptionSerializer(serializers.ModelSerializer):
    title = serializers.SerializerMethodField()
    value = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    class Meta:
        model = SKUDescription
        fields = [
            'id',
            'keyName',
            'title',
            'value',
            'languageList',
            'createdDate',
            'updatedDate',
            'skuId',
        ]

    def get_title(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            skuDescriptionLanguageList = SKUDescriptionLanguage.objects.filter(skuDescriptionId=str(instance.id)).filter(languageId=languageId)
            if skuDescriptionLanguageList.__len__() > 0:
                return skuDescriptionLanguageList[0].title

    def get_value(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            skuDescriptionLanguageList = SKUDescriptionLanguage.objects.filter(skuDescriptionId=str(instance.id)).filter(languageId=languageId)
            if skuDescriptionLanguageList.__len__() > 0:
                return skuDescriptionLanguageList[0].value
    
    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                skuDescriptionLanguageList = SKUDescriptionLanguage.objects.filter(skuDescriptionId=str(instance.id))
                if skuDescriptionLanguageList.__len__() > 0:
                    return SKUDescriptionLanguageSerializer(skuDescriptionLanguageList,many=True).data

class SKUDescriptionLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = SKUDescriptionLanguage
        fields = [
            'id',
            'title',
            'value',
            'skuDescriptionId',
            'languageId'
        ]

class ItemDisplaySerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    supplier = serializers.SerializerMethodField()
    isFavoriteWithClient = serializers.SerializerMethodField()
    externalLink = serializers.SerializerMethodField()
    class Meta:
        model = Item
        fields = [
            'id',
            'name',
            'isApproved',
            'isTop',
            'isFavorite',
            'isElectronic',
            'totalViews',
            'createdDate',
            'updatedDate',
            'departmentId',
            'brandId',
            'supplierId',
            'newArrivalId',
            'linkId',
            'isFavoriteWithClient',
            'supplier',
            'externalLink',
        ]

    def get_isFavoriteWithClient(self,instance):
        return getIsFavoriteWithCurrentClient(self,instance)
  
    def get_name(self, instance):
        languageId = getLanguage(self)
        itemLanguageList = ItemLanguage.objects.filter(itemId=str(instance.id)).filter(languageId=languageId)
        if itemLanguageList.__len__() > 0:
            return itemLanguageList[0].name

    def get_supplier(self, instance):
        return getSupplierInfo(self,instance)

    def get_externalLink(self, instance):
        languageId = getLanguage(self)
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                if instance.linkId is not None:
                    selectedLink = Externalink.objects.get(id = instance.linkId.id)
                    if selectedLink is not None:
                        return ExternalinkDisplaySerializer(selectedLink,context={'languageId':languageId,'isDashboard':isDashboard}).data

#Favortie

class FavoriteSerializer(serializers.ModelSerializer):
    item = serializers.SerializerMethodField()
    class Meta:
        model = Favorite
        fields = [
            'id',
            'createdDate',
            'updatedDate',
            'itemId',
            'clientId',
            'item',
        ]
    def get_item(self, instance):
        clientId = getClientId(self)
        languageId = getLanguage(self)
        isDashboard = getIsDashboard(self)
        isMultipleSKU = getIsMultipleSKU(self)
        item = Item.objects.get(id=str(instance.itemId.id))
        return ItemSerializer(item,context={'clientId':clientId,'languageId':languageId,'isDashboard':isDashboard,'isMultipleSKU':isMultipleSKU}).data

#Shopping Cart

class ShoppingCartSerializer(serializers.ModelSerializer):
    client = serializers.SerializerMethodField()
    class Meta:
        model = ShoppingCart
        fields = [
            'createdDate',
            'updatedDate',
            'clientId',
            'client'
        ]
    def get_client(self,instance):
        if instance.clientId is not None:
            return getClient(self,instance)

#Shopping Cart Item

class ShoppingCartItemSerializer(serializers.ModelSerializer):
    sku = serializers.SerializerMethodField()
    class Meta:
        model = ShoppingCartItem
        fields = [
            'id',
            'quantity',
            'createdDate',
            'updatedDate',
            'shoppingCartId',
            'skuId',
            'sku'
        ]
    def get_sku(self, instance):
        return getSKU(self,instance)

# Link Status

class LinkTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = LinkType
        fields = [
            'id',
            'name',
            'createdDate',
            'updatedDate'
        ]

# Link Status

class LinkStatusSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    languageList = serializers.SerializerMethodField()
    class Meta:
        model = LinkStatus
        fields = [
            'id',
            'keyName',
            'name',
            'languageList',
            'createdDate',
            'updatedDate'
        ]

    def get_name(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            linkStatusLanguageList = LinkStatusLanguage.objects.filter(linkStatusId=str(instance.id)).filter(languageId=languageId)
            if linkStatusLanguageList.__len__() > 0:
                return linkStatusLanguageList[0].name
    
    def get_languageList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                linkStatusLanguageList = LinkStatusLanguage.objects.filter(linkStatusId=str(instance.id))
                if linkStatusLanguageList.__len__() > 0:
                    return LinkStatusLanguageSerializer(linkStatusLanguageList,many=True).data

class LinkStatusLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = LinkStatusLanguage
        fields = [
            'id',
            'name',
            'linkStatusId',
            'languageId'
        ]

# External Link

class ExternalinkSerializer(serializers.ModelSerializer):
    url = serializers.SerializerMethodField()
    urlList = serializers.SerializerMethodField()
    supplier = serializers.SerializerMethodField()
    class Meta:
        model = Externalink
        fields = [
            'id',
            'url',
            'urlList',
            'createdDate',
            'updatedDate',
            'linkTypeId',
            'supplierId',
            'linkStatusId',
            'supplier',
        ]

    def get_supplier(self,instance):
        return getSupplierInfo(self,instance)

    def get_url(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            linkLanguageList = ExternalLinkLanguage.objects.filter(externalLinkId=str(instance.id)).filter(languageId=languageId)
            if linkLanguageList.__len__() > 0:
                return linkLanguageList[0].url
    
    def get_urlList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                linkLanguageList = ExternalLinkLanguage.objects.filter(externalLinkId=str(instance.id))
                if linkLanguageList.__len__() > 0:
                    return ExternalLinkLanguageSerializer(linkLanguageList,many=True).data

class ExternalinkDisplaySerializer(serializers.ModelSerializer):
    url = serializers.SerializerMethodField()
    urlList = serializers.SerializerMethodField()
    supplier = serializers.SerializerMethodField()
    class Meta:
        model = Externalink
        fields = [
            'id',
            'url',
            'urlList',
            'createdDate',
            'updatedDate',
            'linkTypeId',
            'supplierId',
            'linkStatusId',
            'supplier',
        ]

    def get_url(self, instance):
        languageId = getLanguage(self)
        if languageId is not None:
            linkLanguageList = ExternalLinkLanguage.objects.filter(externalLinkId=str(instance.id)).filter(languageId=languageId)
            if linkLanguageList.__len__() > 0:
                return linkLanguageList[0].url
    
    def get_urlList(self,instance):
        isDashboard = getIsDashboard(self)
        if isDashboard is not None:
            if bool(isDashboard) == bool('1'):
                linkLanguageList = ExternalLinkLanguage.objects.filter(externalLinkId=str(instance.id))
                if linkLanguageList.__len__() > 0:
                    return ExternalLinkLanguageSerializer(linkLanguageList,many=True).data

    def get_supplier(self,instance):
        return getSupplierInfo(self,instance)

class ExternalLinkLanguageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ExternalLinkLanguage
        fields = [
            'id',
            'url',
            'externalLinkId',
            'languageId'
        ]
