from django.db.models import Sum, F, Q

from panel.KillHouse.helpers import get_difference_carcasses_percent
from panel.models import PoultryRequest, ProvinceKillRequest, KillHouse, KillHouseRequest, PercentageOfWageType, \
    WageType, SubSectorTransactions, BarDifferenceRequest


def get_vet_sub_sector_finance_info(vet,date1,date2):
    total_quantity = 0
    total_weight = 0
    total_wage = 0
    total_wage_type = WageType.objects.filter(trash=False)
    province_live_wage_amount = total_wage_type.filter(en_name='province-kill-request', trash=False).first().amount
    free_sell_carcasses_wage_amount = total_wage_type.filter(en_name='carcasse-sell', trash=False).first().amount
    out_poultry_request_wage_amount = total_wage_type.filter(en_name='poultry-sell-out-province',
                                                             trash=False).first().amount

    percentages_wage_type = PercentageOfWageType.objects.filter(trash=False)
    other_province_kill_request_percent = percentages_wage_type.filter(
        wage_type__en_name='province-kill-request', share_type__en_name='other').first().percent / 100
    other_free_sell_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-sell',
                                                                     share_type__en_name='other').first().percent / 100

    other_out_poultry_request_percent = percentages_wage_type.filter(wage_type__en_name='poultry-sell-out-province',
                                                                     share_type__en_name='other').first().percent / 100
    other_province_kill_request_amount = province_live_wage_amount * other_province_kill_request_percent
    other_free_sell_carcasses_amount = free_sell_carcasses_wage_amount * other_free_sell_carcasses_percent

    other_out_poultry_request_amount = out_poultry_request_wage_amount * other_out_poultry_request_percent

    kill_houses = KillHouse.objects.filter(out_province=False, trash=False).order_by('id')
    if date1:
        poultry_requests = PoultryRequest.objects.filter(trash=False, state_process__in=('pending', 'accepted'),
                                                         province_state__in=('pending', 'accepted'), out=True,
                                                         out_province_request_cancel=False, temporary_trash=False,
                                                         wage_pay=True, has_wage=True,
                                                         temporary_deleted=False, vet_farm=vet,
                                                         send_date__date__gte=date1,
                                                         send_date__date__lte=date2)
        kill_house_requests = KillHouseRequest.objects.filter(
            Q(Q(killhouse_user__in=kill_houses) & Q(killer__in=kill_houses)) | Q(
                Q(killhouse_user__in=kill_houses) & Q(killer__isnull=True)) | Q(
                Q(killhouse_user__in=kill_houses) | Q(killer__in=kill_houses)), archive_wage=False,
            province_request__poultry_request__vet_farm=vet,
            clearance_code__isnull=False,
            # clearance_code__isnull=False,
            trash=False, calculate_status=True, kill_request__recive_date__date__gte=date1,
            kill_request__recive_date__date__lte=date2
        )
        vet_deposit = SubSectorTransactions.objects.filter(trash=False, vet=vet, date__date__gte=date1,
                                                           date__date__lte=date2)
        difference_requests = BarDifferenceRequest.objects.filter(kill_house__in=kill_houses, trash=False,
                                                                  state='accepted', acceptor_date__date__gte=date1,
                                                                  acceptor_date__date__lte=date2)
    else:
        poultry_requests = PoultryRequest.objects.filter(trash=False, state_process__in=('pending', 'accepted'),
                                                         province_state__in=('pending', 'accepted'), out=True,
                                                         out_province_request_cancel=False, temporary_trash=False,
                                                         wage_pay=True, has_wage=True,
                                                         temporary_deleted=False, vet_farm=vet,
                                                         )
        kill_house_requests = KillHouseRequest.objects.filter(
            Q(Q(killhouse_user__in=kill_houses) & Q(killer__in=kill_houses)) | Q(
                Q(killhouse_user__in=kill_houses) & Q(killer__isnull=True)) | Q(
                Q(killhouse_user__in=kill_houses) | Q(killer__in=kill_houses)), archive_wage=False,
            province_request__poultry_request__vet_farm=vet,
            clearance_code__isnull=False,
            # clearance_code__isnull=False,
            trash=False, calculate_status=True
        )
        difference_requests = BarDifferenceRequest.objects.filter(kill_house__in=kill_houses, trash=False,
                                                                  state='accepted')
        vet_deposit = SubSectorTransactions.objects.filter(trash=False, vet=vet)

        # quarantine_code__isnull=False)
    out_province_poultry_request_quantity = poultry_requests.aggregate(total=Sum('quantity'))[
                                                'total'] or 0
    out_province_poultry_request_weight = poultry_requests.aggregate(total=Sum(F('quantity') * F('Index_weight')))[
                                              'total'] or 0

    # province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user__in=kill_houses, trash=False,
    #                                                             archive_wage=False, return_to_province=False,
    #                                                             state__in=('pending', 'accepted'),
    #                                                             province_request__poultry_request__vet_farm=vet,
    #                                                             province_request__poultry_request__quarantine_code__isnull=False,
    #
    #                                                             first_car_allocated_quantity=0).order_by('id')

    # province_live_weight = province_kill_requests.aggregate(total=Sum('total_killed_weight'))[
    #                            'total'] or 0
    # province_live_quantity = province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[
    #                              'total'] or 0
    province_live_weight = kill_house_requests.aggregate(total=Sum('accepted_real_weight'))['total'] or 0
    province_live_quantity = kill_house_requests.aggregate(total=Sum('accepted_real_quantity'))[
                                 'total'] or 0
    province_live_weight += \
        difference_requests.aggregate(total=Sum('weight'))['total'] or 0
    total_quantity += province_live_quantity + out_province_poultry_request_quantity
    total_weight += province_live_weight + out_province_poultry_request_weight

    province_carcasses_weight = province_live_weight * 0.75
    if date1:
         internal_pure_province_carcasses_weight = province_carcasses_weight * get_difference_carcasses_percent(date1,date2)
    else:
        internal_pure_province_carcasses_weight = province_carcasses_weight * get_difference_carcasses_percent()

    external_pure_province_carcasses_weight = province_carcasses_weight - internal_pure_province_carcasses_weight

    total_pure_internal_province_carcasses_amount = internal_pure_province_carcasses_weight * other_province_kill_request_amount
    total_pure_external_province_carcasses_amount = external_pure_province_carcasses_weight * other_free_sell_carcasses_amount
    out_province_poultry_request_amount = out_province_poultry_request_weight * other_out_poultry_request_amount

    vet_deposit_amount = vet_deposit.aggregate(total=Sum('amount'))['total'] or 0
    total_wage += total_pure_internal_province_carcasses_amount + total_pure_external_province_carcasses_amount + out_province_poultry_request_amount
    total_remain_wage = total_wage - vet_deposit_amount

    result = {
        "total_quantity": total_quantity,
        "total_weight": total_weight,
        "out_province_poultry_request_quantity": out_province_poultry_request_quantity,
        "out_province_poultry_request_weight": out_province_poultry_request_weight,
        "province_live_quantity": province_live_quantity,
        "province_live_weight": province_live_weight,
        "province_carcasses_weight": province_carcasses_weight,
        "internal_pure_province_carcasses_weight": internal_pure_province_carcasses_weight,
        "external_pure_province_carcasses_weight": external_pure_province_carcasses_weight,
        "total_pure_internal_province_carcasses_amount": total_pure_internal_province_carcasses_amount,
        "total_pure_external_province_carcasses_amount": total_pure_external_province_carcasses_amount,
        "out_province_poultry_request_amount": out_province_poultry_request_amount,
        "total_wage": total_wage,
        "number_of_deposit": len(vet_deposit),
        "vet_deposit_amount": vet_deposit_amount,
        "total_remain_wage": total_remain_wage,
    }
    return result


def get_vet_sub_sector_finance_info_with_date(vet,date1,date2):
    total_quantity = 0
    total_weight = 0
    total_wage = 0
    total_wage_type = WageType.objects.filter(trash=False)
    province_live_wage_amount = total_wage_type.filter(en_name='province-kill-request', trash=False).first().amount
    free_sell_carcasses_wage_amount = total_wage_type.filter(en_name='carcasse-sell', trash=False).first().amount
    out_poultry_request_wage_amount = total_wage_type.filter(en_name='poultry-sell-out-province',
                                                             trash=False).first().amount

    percentages_wage_type = PercentageOfWageType.objects.filter(trash=False)
    other_province_kill_request_percent = percentages_wage_type.filter(
        wage_type__en_name='province-kill-request', share_type__en_name='other').first().percent / 100
    other_free_sell_carcasses_percent = percentages_wage_type.filter(wage_type__en_name='carcasse-sell',
                                                                     share_type__en_name='other').first().percent / 100

    other_out_poultry_request_percent = percentages_wage_type.filter(wage_type__en_name='poultry-sell-out-province',
                                                                     share_type__en_name='other').first().percent / 100
    other_province_kill_request_amount = province_live_wage_amount * other_province_kill_request_percent
    other_free_sell_carcasses_amount = free_sell_carcasses_wage_amount * other_free_sell_carcasses_percent

    other_out_poultry_request_amount = out_poultry_request_wage_amount * other_out_poultry_request_percent

    kill_houses = KillHouse.objects.filter(out_province=False, trash=False).order_by('id')
    if date1:
        poultry_requests =  PoultryRequest.objects.filter(trash=False, state_process__in=('pending', 'accepted'),
                                                         province_state__in=('pending', 'accepted'), out=True,
                                                         out_province_request_cancel=False, temporary_trash=False,
                                                         wage_pay=True, has_wage=True,
                                                         temporary_deleted=False, vet_farm=vet, send_date__date__gte=date1,
                                                         send_date__date__lte=date2)
        kill_house_requests = KillHouseRequest.objects.filter(
            Q(Q(killhouse_user__in=kill_houses) & Q(killer__in=kill_houses)) | Q(
                Q(killhouse_user__in=kill_houses) & Q(killer__isnull=True)) | Q(
                Q(killhouse_user__in=kill_houses) | Q(killer__in=kill_houses)), archive_wage=False,
            province_request__poultry_request__vet_farm=vet,
            clearance_code__isnull=False,
            # clearance_code__isnull=False,
            trash=False, calculate_status=True, kill_request__recive_date__date__gte=date1,
            kill_request__recive_date__date__lte=date2
        )
        vet_deposit = SubSectorTransactions.objects.filter(trash=False, vet=vet, date__date__gte=date1,
                                                           date__date__lte=date2)
        difference_requests = BarDifferenceRequest.objects.filter(kill_house__in=kill_houses, trash=False,
                                                                  state='accepted',acceptor_date__date__gte=date1,acceptor_date__date__lte=date2)
    else:
        poultry_requests = PoultryRequest.objects.filter(trash=False, state_process__in=('pending', 'accepted'),
                                                         province_state__in=('pending', 'accepted'), out=True,
                                                         out_province_request_cancel=False, temporary_trash=False,
                                                         wage_pay=True, has_wage=True,
                                                         temporary_deleted=False, vet_farm=vet,
                                                         )
        kill_house_requests = KillHouseRequest.objects.filter(
            Q(Q(killhouse_user__in=kill_houses) & Q(killer__in=kill_houses)) | Q(
                Q(killhouse_user__in=kill_houses) & Q(killer__isnull=True)) | Q(
                Q(killhouse_user__in=kill_houses) | Q(killer__in=kill_houses)), archive_wage=False,
            province_request__poultry_request__vet_farm=vet,
            clearance_code__isnull=False,
            # clearance_code__isnull=False,
            trash=False, calculate_status=True
        )
        difference_requests = BarDifferenceRequest.objects.filter(kill_house__in=kill_houses, trash=False,
                                                                  state='accepted')
        vet_deposit = SubSectorTransactions.objects.filter(trash=False, vet=vet)

        # quarantine_code__isnull=False)
    out_province_poultry_request_quantity = poultry_requests.aggregate(total=Sum('quantity'))[
                                                'total'] or 0
    out_province_poultry_request_weight = poultry_requests.aggregate(total=Sum(F('quantity') * F('Index_weight')))[
                                              'total'] or 0

    # province_kill_requests = ProvinceKillRequest.objects.filter(killhouse_user__in=kill_houses, trash=False,
    #                                                             archive_wage=False, return_to_province=False,
    #                                                             state__in=('pending', 'accepted'),
    #                                                             province_request__poultry_request__vet_farm=vet,
    #                                                             province_request__poultry_request__quarantine_code__isnull=False,
    #
    #                                                             first_car_allocated_quantity=0).order_by('id')


    # province_live_weight = province_kill_requests.aggregate(total=Sum('total_killed_weight'))[
    #                            'total'] or 0
    # province_live_quantity = province_kill_requests.aggregate(total=Sum('total_killed_quantity'))[
    #                              'total'] or 0
    province_live_weight = kill_house_requests.aggregate(total=Sum('accepted_real_weight'))['total'] or 0
    province_live_quantity = kill_house_requests.aggregate(total=Sum('accepted_real_quantity'))[
                                  'total'] or 0
    province_live_weight += \
        difference_requests.aggregate(total=Sum('weight'))['total'] or 0
    total_quantity += province_live_quantity + out_province_poultry_request_quantity
    total_weight += province_live_weight + out_province_poultry_request_weight

    province_carcasses_weight = province_live_weight * 0.75
    internal_pure_province_carcasses_weight = province_carcasses_weight * get_difference_carcasses_percent(date1,date2)
    external_pure_province_carcasses_weight = province_carcasses_weight - internal_pure_province_carcasses_weight

    total_pure_internal_province_carcasses_amount = internal_pure_province_carcasses_weight * other_province_kill_request_amount
    total_pure_external_province_carcasses_amount = external_pure_province_carcasses_weight * other_free_sell_carcasses_amount
    out_province_poultry_request_amount = out_province_poultry_request_weight * other_out_poultry_request_amount

    vet_deposit_amount = vet_deposit.aggregate(total=Sum('amount'))['total'] or 0
    total_wage += total_pure_internal_province_carcasses_amount + total_pure_external_province_carcasses_amount + out_province_poultry_request_amount
    total_remain_wage = total_wage - vet_deposit_amount

    result = {
        "total_quantity": total_quantity,
        "total_weight": total_weight,
        "out_province_poultry_request_quantity": out_province_poultry_request_quantity,
        "out_province_poultry_request_weight": out_province_poultry_request_weight,
        "province_live_quantity": province_live_quantity,
        "province_live_weight": province_live_weight,
        "province_carcasses_weight": province_carcasses_weight,
        "internal_pure_province_carcasses_weight": internal_pure_province_carcasses_weight,
        "external_pure_province_carcasses_weight": external_pure_province_carcasses_weight,
        "total_pure_internal_province_carcasses_amount": total_pure_internal_province_carcasses_amount,
        "total_pure_external_province_carcasses_amount": total_pure_external_province_carcasses_amount,
        "out_province_poultry_request_amount": out_province_poultry_request_amount,
        "total_wage": total_wage,
        "number_of_deposit": len(vet_deposit),
        "vet_deposit_amount": vet_deposit_amount,
        "total_remain_wage": total_remain_wage,
    }
    return result
