def __init__(self, *args, **kwargs):
     super(TestGenerateDocsArguments, self).__init__(*args, **kwargs)
     self.output = StringIO()
     self.good_output = 'Done. You can have a Club-Mate now. :)\n'
     self.date_string = '2016-06-01'
     self.date = generate_docs_date(self.date_string)
    def test_subscription_yearly_proration_isnt_borked_half(self):
        from django.db.models import Q

        # Set up the timescale.
        start_date = dt.date(2018, 1, 1)
        end_date = dt.date(2018, 7, 1)
        freezer = freeze_time('2018-07-01')
        first_billing_date = generate_docs_date('2018-01-01')
        end_billing_date = generate_docs_date('2018-07-02')

        feature_usage_set = dt.date(2018, 1, 1)
        fst_feature_usage_start = dt.date(2018, 1, 2)
        snd_feature_usage_end = dt.date(2018, 1, 30)

        feature_increment = dt.date(2018, 3, 1)
        snd_feature_usage_start = dt.date(2018, 3, 2)
        snd_feature_usage_end = dt.date(2018, 3, 30)

        ## Lots of test setup {{{

        provider = ProviderFactory.create(flow=Provider.FLOWS.INVOICE)

        customer = CustomerFactory.create(consolidated_billing=False,
                                          sales_tax_percent=Decimal('0.00'))
        currency = 'USD'

        plan = PlanFactory.create(
            interval=Plan.INTERVALS.YEAR,
            interval_count=1,
            generate_after=0,
            enabled=True,
            provider=provider,
            product_code=ProductCodeFactory(value="yearly-seat-plan"),
            amount=Decimal('10.00'),
            prebill_plan=False,
            currency=currency,
            trial_period_days=None,
            cycle_billing_duration=dt.timedelta(days=1),
        )
        plan.save()

        # Create the prorated subscription
        subscription = SubscriptionFactory.create(plan=plan,
                                                  start_date=start_date,
                                                  customer=customer)
        subscription.activate()
        subscription.save()

        ## }}}

        ## First test billing phase {{{
        call_command('generate_docs',
                     date=fst_feature_usage_start,
                     stdout=self.output)

        assert Invoice.objects.all().count() == 0

        # zomg time travel so l33t
        with freeze_time('2018-07-01') as T:
            subscription.cancel(when='now')
            subscription.save()

        call_command('generate_docs',
                     date=end_billing_date,
                     force_generate=True,
                     stdout=self.output)

        assert Invoice.objects.all().count() == 1
        print("  END OF YEAR  ")
        year_invoice = Invoice.objects.first()
        print_entries(year_invoice)

        # Final year total should be 10 for the whole base plan.
        assert Decimal(4.98) <= year_invoice.total <= Decimal(5.00)
Exemplo n.º 3
0
 def __init__(self, *args, **kwargs):
     super(TestGenerateDocsArguments, self).__init__(*args, **kwargs)
     self.output = StringIO()
     self.good_output = 'Done. You can have a Club-Mate now. :)\n'
     self.date_string = '2016-06-01'
     self.date =  generate_docs_date(self.date_string)
    def test_daily_billed_plan_issue_date_follows_start_date(self):
        """ Create a monthly plan that starts on 1/7, test that
        follow-up billing documents are generated accurately with
        MONTHISH interval on two separate months.
        """

        # Set up the timescale.
        start_date = dt.date(2018, 1, 7)
        end_of_start_month = dt.date(2018, 1, 31)
        no_invoice_issued_here = dt.date(2018, 2, 3)
        first_invoice_date = dt.date(2018, 2, 7)

        curr_billing_date = generate_docs_date('2018-02-08')

        seat_feature_usage_set = dt.date(2018, 1, 8)
        feature_usage_start = dt.date(2018, 1, 9)
        feature_usage_end = dt.date(2018, 1, 9)

        provider = self.provider
        customer = self.customer

        seat_feature = self.seat_feature
        seat_feature.save()

        metered_feature = self.metered_feature
        metered_feature.save()

        plan = self.plan(
            metered_features=[metered_feature],
            interval_count=1,
            interval=Plan.INTERVALS.MONTHISH,
            provider=provider,
        )
        plan.save()

        # Create the prorated subscription
        subscription = SubscriptionFactory.create(plan=plan,
                                                  start_date=start_date,
                                                  customer=customer)
        subscription.activate()
        subscription.save()

        call_command('generate_docs',
                     date=feature_usage_start,
                     stdout=self.output)

        # Track some usage
        mf = MeteredFeatureUnitsLogFactory.create(
            subscription=subscription,
            metered_feature=metered_feature,
            start_date=feature_usage_start,
            end_date=feature_usage_end,
            consumed_units=Decimal('20.00'))
        mf.save()

        # No invoices are generated here because the month hasn't passed
        call_command('generate_docs',
                     date=end_of_start_month,
                     stdout=self.output)

        assert Invoice.objects.all().count() == 0

        # Invoices SHOULD NOT be generated here because the billing
        # period shouldn't end.
        call_command('generate_docs',
                     date=no_invoice_issued_here,
                     stdout=self.output)

        assert Invoice.objects.all().count() == 0

        call_command('generate_docs',
                     date=curr_billing_date,
                     stdout=self.output)

        assert Invoice.objects.all().count() == 1
        invoice = Invoice.objects.all().first()

        assert invoice.issue_date == curr_billing_date
        assert Decimal(109.00) <= invoice.total <= Decimal(110.0)
        invoice.pay(paid_date='2018-02-09')
        invoice.save()

        call_command('generate_docs',
                     date=generate_docs_date('2018-02-10'),
                     stdout=self.output)

        # Next period
        start_date = dt.date(2018, 2, 7)  # + 30 days = 3/7/2018
        end_of_start_month = dt.date(2018, 2, 28)
        no_invoice_issued_here = dt.date(2018, 3, 3)
        first_invoice_date = dt.date(2018, 3, 7)

        curr_billing_date = generate_docs_date('2018-03-10')

        # seat_feature_usage_set = dt.date(2018, 2, 8)
        feature_usage_start = dt.date(2018, 2, 12)
        feature_usage_end = dt.date(2018, 2, 12)

        # Track some usage
        mf = MeteredFeatureUnitsLogFactory.create(
            subscription=subscription,
            metered_feature=metered_feature,
            start_date=feature_usage_start,
            end_date=feature_usage_end,
            consumed_units=Decimal('20.00'))
        mf.save()

        call_command('generate_docs',
                     date=no_invoice_issued_here,
                     stdout=self.output)

        assert Invoice.objects.all().count() == 1
        invoice = Invoice.objects.all().first()

        call_command('generate_docs',
                     date=curr_billing_date,
                     stdout=self.output)

        assert Invoice.objects.all().count() == 2
        invoice = Invoice.objects.all().first()

        assert subscription.state == Subscription.STATES.ACTIVE
        assert invoice.issue_date == curr_billing_date
        assert Decimal(109.00) <= invoice.total <= Decimal(110.0)
    def test_monthly_billed_plan_issue_date_follows_start_date(self):
        """ Create a monthly plan starting on 1/7, with the MONTHISH
        setting. Confirm that billing happens on 2/8.  """

        # Set up the timescale.
        start_date = dt.date(2018, 1, 7)
        end_of_start_month = dt.date(2018, 1, 31)
        no_invoice_issued_here = dt.date(2018, 2, 3)
        first_invoice_date = dt.date(2018, 2, 7)

        curr_billing_date = generate_docs_date('2018-02-08')

        seat_feature_usage_set = dt.date(2018, 1, 8)
        feature_usage_start = dt.date(2018, 1, 9)
        feature_usage_end = dt.date(2018, 1, 10)

        provider = self.provider
        customer = self.customer

        seat_feature = self.seat_feature
        seat_feature.save()

        metered_feature = self.metered_feature
        metered_feature.save()

        plan = self.plan(
            interval=Plan.INTERVALS.MONTHISH,
            interval_count=1,
            metered_features=[metered_feature],
            provider=provider,
        )
        plan.save()

        # Create the prorated subscription
        subscription = SubscriptionFactory.create(plan=plan,
                                                  start_date=start_date,
                                                  customer=customer)
        subscription.activate()
        subscription.save()

        call_command('generate_docs',
                     date=feature_usage_start,
                     stdout=self.output)

        # Track some usage
        mf = MeteredFeatureUnitsLogFactory.create(
            subscription=subscription,
            metered_feature=metered_feature,
            start_date=feature_usage_start,
            end_date=feature_usage_end,
            consumed_units=Decimal('20.00'))
        mf.save()

        # No invoices are generated here because the month hasn't passed
        call_command('generate_docs',
                     date=end_of_start_month,
                     stdout=self.output)

        assert Invoice.objects.all().count() == 0

        # Invoices SHOULD NOT be generated here because the billing
        # period shouldn't end.
        call_command('generate_docs',
                     date=no_invoice_issued_here,
                     stdout=self.output)

        assert Invoice.objects.all().count() == 0

        call_command('generate_docs',
                     date=curr_billing_date,
                     stdout=self.output)

        assert Invoice.objects.all().count() == 1
        invoice = Invoice.objects.all().first()

        assert invoice.issue_date == curr_billing_date
        assert invoice.total > Decimal(10.0)
    def test_issued_date_works_as_expected(self):
        """ Test that usage under and above a certain amount tracks with
        assumptions. """

        # Set up the timescale.
        start_date = dt.date(2018, 1, 1)
        first_invoice_date = dt.date(2018, 2, 1)

        prev_billing_date = generate_docs_date('2018-01-01')
        period_end_date = generate_docs_date('2018-01-30')
        curr_billing_date = generate_docs_date('2018-01-31')
        next_billing_date = generate_docs_date('2018-02-01')

        seat_feature_usage_set = dt.date(2018, 1, 1)
        feature_usage_start = dt.date(2018, 1, 2)
        feature_usage_end = dt.date(2018, 1, 30)

        provider = self.provider
        customer = self.customer
        currency = 'USD'

        seat_feature = self.seat_feature
        seat_feature.save()

        metered_feature = self.metered_feature
        metered_feature.save()

        plan = PlanFactory.create(
            interval=Plan.INTERVALS.MONTH,
            interval_count=1,
            generate_after=0,
            enabled=True,
            provider=provider,
            product_code=ProductCodeFactory(value="monthly-deliv-plan"),
            amount=Decimal('10.00'),
            prebill_plan=False,
            currency=currency,
            trial_period_days=None,
            cycle_billing_duration=dt.timedelta(days=1),
            metered_features=[metered_feature])
        plan.save()

        # Create the prorated subscription
        subscription = SubscriptionFactory.create(plan=plan,
                                                  start_date=start_date,
                                                  customer=customer)
        subscription.activate()
        subscription.save()

        call_command('generate_docs',
                     date=feature_usage_start,
                     stdout=self.output)

        # Track some usage
        mf = MeteredFeatureUnitsLogFactory.create(
            subscription=subscription,
            metered_feature=metered_feature,
            start_date=feature_usage_start,
            end_date=feature_usage_end,
            consumed_units=Decimal('20.00'))
        mf.save()

        call_command('generate_docs',
                     date=next_billing_date,
                     stdout=self.output)

        assert Invoice.objects.all().count() == 1
        invoice = Invoice.objects.all().first()

        assert invoice.issue_date == next_billing_date
        assert invoice.total == Decimal(110.0)