from django.utils import timezone

from oscar.core.loading import get_model

from ocyan.plugin.oscar_partner.partner.constants import EU_APPLICABLE_COUNTRIES

Order = get_model("order", "Order")


def get_local_vat_order_queryset():
    year = timezone.now().year
    orders = Order.objects.filter(
        shipping_address__country__in=EU_APPLICABLE_COUNTRIES,
        date_placed__year=year,
        status__in=["paid", "complete"],
    )

    # This exists because the vat rules for 2021 only apply from july 1st until december 31st. From 2022 all orders count
    if year == 2021:
        orders = orders.filter(date_placed__month__gt=6)

    return orders
