from oscar.apps.checkout import session
from oscar_vat_moss import vat

from . import assert_required_fields_set_correctly

# Make sure OSCAR_REQUIRED_ADDRESS_FIELDS is set correctly for VAT
# assessment
assert_required_fields_set_correctly()


class VATCheckoutSessionMixin(session.CheckoutSessionMixin):

    def get_context_data(self, **kwargs):
        ctx = super(VATCheckoutSessionMixin,
                    self).get_context_data(**kwargs)

        # Oscar's checkout templates look for this variable which specifies to
        # break out the tax totals into a separate subtotal.
        ctx['show_tax_separately'] = True

        return ctx


class FixedRateVATCheckoutSessionMixin(VATCheckoutSessionMixin):

    def check_a_valid_shipping_address_is_captured(self):
        """Check that the just-captured shipping address is valid.

        Normally, this method is only used to perform a general
        shipping address check. With the FixedRateVATStrategy however,
        we rely on the default shipping address to figure out a user's
        applicable VAT rate. Therefore, when we are satisfied that the
        shipping address is valid, we also set it as the default for
        shipping. This will cause a minor nuisance for any user who
        doesn't expect that the default shipping address is simply the
        one used most recently, but in this case, the benefits
        outweigh the disadvantages.

        """
        # The superclass does the VAT validation check. Once that has
        # completed, we can set the shipping address to be the new
        # default.
        super(FixedRateVATCheckoutSessionMixin, self)

        shipping_address = self.get_shipping_address(
            basket=self.request.basket)
        shipping_address.is_default_for_shipping = True
        shipping_address.save()


class DeferredVATCheckoutSessionMixin(VATCheckoutSessionMixin):

    def build_submission(self, **kwargs):
        submission = super(DeferredVATCheckoutSessionMixin,
                           self).build_submission(**kwargs)

        assess_tax = (submission['shipping_method'] and
                      submission['shipping_address'] and
                      submission['shipping_address'].phone_number)
        if assess_tax:
            try:
                vat.apply_to(submission)
                # Recalculate order total to ensure we have a
                # tax-inclusive total
                submission['order_total'] = self.get_order_totals(
                    submission['basket'], submission['shipping_charge'])
            except vat.VATAssessmentException:
                pass
        return submission


# FixedRateVATCheckoutSessionMixin is the safer option,
# make that the default
CheckoutSessionMixin = FixedRateVATCheckoutSessionMixin

@@ -244,16 +244,24 @@
 %0A%0Aclass 
+Deferred
 VATCheck
@@ -370,16 +370,24 @@
 = super(
+Deferred
 VATCheck
@@ -664,1258 +664,8 @@
 tx%0A%0A
-%0Aclass FixedRateVATCheckoutSessionMixin(VATCheckoutSessionMixin):%0A%0A    def check_a_valid_shipping_address_is_captured(self):%0A        %22%22%22Check that the just-captured shipping address is valid.%0A%0A        Normally, this method is only used to perform a general%0A        shipping address check. With the FixedRateVATStrategy however,%0A        we rely on the default shipping address to figure out a user's%0A        applicable VAT rate. Therefore, when we are satisfied that the%0A        shipping address is valid, we also set it as the default for%0A        shipping. This will cause a minor nuisance for any user who%0A        doesn't expect that the default shipping address is simply the%0A        one used most recently, but in this case, the benefits%0A        outweigh the disadvantages.%0A%0A        %22%22%22%0A        # The superclass does the VAT validation check. Once that has%0A        # completed, we can set the shipping address to be the new%0A        # default.%0A        super(FixedRateVATCheckoutSessionMixin, self)%0A%0A        shipping_address = self.get_shipping_address(%0A            basket=self.request.basket)%0A        shipping_address.is_default_for_shipping = True%0A        shipping_address.save()%0A%0A%0Aclass DeferredVATCheckoutSessionMixin(VATCheckoutSessionMixin):%0A%0A
     
@@ -1422,142 +1422,4 @@
 ion%0A
-%0A%0A# FixedRateVATCheckoutSessionMixin is the safer option,%0A# make that the default%0ACheckoutSessionMixin = FixedRateVATCheckoutSessionMixin%0A

Remove FixedRateVATCheckoutSessionMixin