def setUpClass(cls):
        super(BaseCustomerInvoiceCase, cls).setUpClass()

        if cls.is_using_test_plans:
            generator.bootstrap_test_software_plan_versions()

        cls.billing_contact = generator.create_arbitrary_web_user_name()
        cls.dimagi_user = generator.create_arbitrary_web_user_name(is_dimagi=True)
        cls.account = generator.billing_account(
            cls.dimagi_user, cls.billing_contact)
        cls.domain = generator.arbitrary_domain()
        cls.account.is_customer_billing_account = True
        cls.account.save()

        cls.advanced_plan = DefaultProductPlan.get_default_plan_version(edition=SoftwarePlanEdition.ADVANCED)
        cls.advanced_plan.plan.is_customer_software_plan = True

        cls.subscription_length = 15  # months
        subscription_start_date = datetime.date(2016, 2, 23)
        subscription_end_date = add_months_to_date(subscription_start_date, cls.subscription_length)
        cls.subscription = generator.generate_domain_subscription(
            cls.account,
            cls.domain,
            date_start=subscription_start_date,
            date_end=subscription_end_date,
        )

        advanced_subscription_end_date = add_months_to_date(subscription_end_date, 2)
        cls.domain2 = generator.arbitrary_domain()
        cls.sub2 = generator.generate_domain_subscription(
            cls.account,
            cls.domain2,
            date_start=subscription_start_date,
            date_end=advanced_subscription_end_date,
            plan_version=cls.advanced_plan
        )

        cls.domain3 = generator.arbitrary_domain()
        cls.sub3 = generator.generate_domain_subscription(
            cls.account,
            cls.domain3,
            date_start=subscription_start_date,
            date_end=advanced_subscription_end_date,
            plan_version=cls.advanced_plan
        )

        # This subscription should not be included in any customer invoices in these tests
        cls.domain_community = generator.arbitrary_domain()
        cls.sub3 = generator.generate_domain_subscription(
            cls.account,
            cls.domain3,
            date_start=subscription_start_date,
            date_end=advanced_subscription_end_date,
            plan_version=DefaultProductPlan.get_default_plan_version(edition=SoftwarePlanEdition.COMMUNITY)
        )
Example #2
0
    def preg_month(self):
        if self.status == 'pregnant':
            base_window_start = add_months_to_date(self.edd, -9)
            non_adjusted_month = len(months_between(base_window_start, self.reporting_window_start)) - 1

            # the date to check one month after they first become eligible,
            # aka the end of their fourth month of pregnancy
            vhnd_date_to_check = add_months_to_date(self.preg_first_eligible_date, 1)

            month = self._adjust_for_vhnd_presence(non_adjusted_month, vhnd_date_to_check)
            if month < 4 or month > 9:
                raise InvalidRow('pregnancy month %s not valid' % month)
            return month
Example #3
0
    def setUp(self):
        super(TestBillingRecord, self).setUp()
        self.billing_contact = generator.arbitrary_web_user()
        self.dimagi_user = generator.arbitrary_web_user(is_dimagi=True)
        self.domain = Domain(name='test')
        self.domain.save()
        self.invoice_start, self.invoice_end = get_previous_month_date_range()
        self.currency = generator.init_default_currency()
        self.account = generator.billing_account(self.dimagi_user, self.billing_contact)

        self.subscription_length = 4  # months
        subscription_start_date = datetime.date(2016, 2, 23)
        subscription_end_date = add_months_to_date(subscription_start_date, self.subscription_length)
        self.subscription = generator.generate_domain_subscription(
            self.account,
            self.domain,
            date_start=subscription_start_date,
            date_end=subscription_end_date,
        )
        self.invoice = Invoice(
            subscription=self.subscription,
            date_start=self.invoice_start,
            date_end=self.invoice_end,
            is_hidden=False,
        )
        self.billing_record = BillingRecord(invoice=self.invoice)
Example #4
0
    def test_date_due_gets_set_autopay(self):
        """Date due always gets set for autopay """
        Subscription.objects.all().delete()
        plan = DefaultProductPlan.objects.get(
            edition=SoftwarePlanEdition.STANDARD,
            product_type=SoftwareProductType.COMMCARE,
            is_trial=False
        ).plan.get_version()

        subscription_length = 4
        subscription_start_date = datetime.date(2016, 2, 23)
        subscription_end_date = add_months_to_date(subscription_start_date, subscription_length)
        autopay_subscription = generator.generate_domain_subscription(
            self.account,
            self.domain,
            date_start=subscription_start_date,
            date_end=subscription_end_date,
            plan_version=plan
        )

        autopay_subscription.account.update_autopay_user(self.billing_contact.username, self.domain)
        invoice_date_autopay = utils.months_from_date(autopay_subscription.date_start, 1)
        tasks.generate_invoices(invoice_date_autopay)

        autopay_invoice = autopay_subscription.invoice_set.last()
        self.assertTrue(autopay_invoice.balance <= SMALL_INVOICE_THRESHOLD)
        self.assertIsNotNone(autopay_invoice.date_due)
Example #5
0
    def test_date_due_set_large_invoice(self):
        """Date Due only gets set for a large invoice (> $100)"""
        Subscription.objects.all().delete()
        plan = DefaultProductPlan.objects.get(
            edition=SoftwarePlanEdition.ADVANCED,
            product_type=SoftwareProductType.COMMCARE,
            is_trial=False
        ).plan.get_version()

        subscription_length = 5  # months
        subscription_start_date = datetime.date(2016, 2, 23)
        subscription_end_date = add_months_to_date(subscription_start_date, subscription_length)
        subscription = generator.generate_domain_subscription(
            self.account,
            self.domain,
            date_start=subscription_start_date,
            date_end=subscription_end_date,
            plan_version=plan
        )

        invoice_date_large = utils.months_from_date(subscription.date_start, 3)
        tasks.generate_invoices(invoice_date_large)
        large_invoice = subscription.invoice_set.last()

        self.assertTrue(large_invoice.balance > SMALL_INVOICE_THRESHOLD)
        self.assertIsNotNone(large_invoice.date_due)
Example #6
0
    def test_date_due_not_set_small_invoice(self):
        """Date Due doesn't get set if the invoice is small"""
        Subscription.objects.all().delete()
        plan = DefaultProductPlan.objects.get(
            edition=SoftwarePlanEdition.STANDARD,
            product_type=SoftwareProductType.COMMCARE,
            is_trial=False
        ).plan.get_version()

        subscription_length = 5  # months
        subscription_start_date = datetime.date(2016, 2, 23)
        subscription_end_date = add_months_to_date(subscription_start_date, subscription_length)
        subscription = generator.generate_domain_subscription(
            self.account,
            self.domain,
            date_start=subscription_start_date,
            date_end=subscription_end_date,
            plan_version=plan,
        )

        invoice_date_small = utils.months_from_date(subscription.date_start, 1)
        tasks.generate_invoices(invoice_date_small)
        small_invoice = subscription.invoice_set.first()

        self.assertTrue(small_invoice.balance <= SMALL_INVOICE_THRESHOLD)
        self.assertIsNone(small_invoice.date_due)
Example #7
0
    def test_invoice_credit(self):
        """
        Make sure that subscription and account level credits get applied to the invoice balance appropriately.
        """
        invoice_monthly_total = self.product_rate.monthly_fee + self.monthly_user_fee

        subscription_credit, account_credit = self._generate_subscription_and_account_invoice_credits(
            invoice_monthly_total, self.subscription, self.account
        )

        # other subscription credit that shouldn't count toward this invoice
        other_domain = generator.arbitrary_domain()
        # so that the other subscription doesn't draw from the same account credits, have it start 4 months later
        new_subscription_start = utils.months_from_date(self.subscription.date_start, 4)

        other_subscription = generator.generate_domain_subscription(
            self.account,
            other_domain,
            date_start=new_subscription_start,
            date_end=add_months_to_date(new_subscription_start, self.min_subscription_length),
        )

        # other account credit that shouldn't count toward this invoice
        other_account = generator.billing_account(self.dimagi_user, generator.create_arbitrary_web_user_name())

        self._generate_subscription_and_account_invoice_credits(
            invoice_monthly_total, other_subscription, other_account
        )

        self._test_final_invoice_balance()

        self._test_credit_use(subscription_credit)
        self._test_credit_use(account_credit)
        other_domain.delete()
Example #8
0
    def setUp(self):
        super(TestBillingAutoPay, self).setUp()
        self.account.created_by_domain = self.domain
        self.account.save()

        self.currency = generator.init_default_currency()

        self.web_user = generator.arbitrary_web_user()
        self.dimagi_user = generator.arbitrary_web_user(is_dimagi=True)
        self.fake_card = FakeStripeCard()
        self.fake_stripe_customer = FakeStripeCustomer(cards=[self.fake_card])

        self.account.update_autopay_user(self.web_user.username, self.domain)
        self.invoice_date = utils.months_from_date(self.subscription.date_start,
                                                   random.randint(2, self.subscription_length))

        self.account_2 = generator.billing_account(self.dimagi_user, self.web_user)
        self.domain_2 = generator.arbitrary_domain()

        self.subscription_length_2 = self.min_subscription_length  # months
        subscription_start_date = datetime.date(2016, 2, 23)
        subscription_end_date = add_months_to_date(subscription_start_date, self.subscription_length_2)
        self.subscription_2 = generator.generate_domain_subscription(
            self.account_2,
            self.domain_2,
            date_start=subscription_start_date,
            date_end=subscription_end_date,
        )

        tasks.generate_invoices(self.invoice_date)
Example #9
0
def send_gateway_fee_report_out():
    backend_api_ids = SmsGatewayFeeCriteria.objects.values_list('backend_api_id', flat=True).distinct()
    first_day_previous_month = add_months_to_date(date.today(), -1)
    billables_in_month = SmsBillable.objects.filter(
        date_sent__year=first_day_previous_month.year,
        date_sent__month=first_day_previous_month.month,
        is_valid=True,
    )

    costs_by_backend = defaultdict(list)
    for backend_api_id in backend_api_ids:
        billables_in_month_by_backend_api_id = billables_in_month.filter(
            gateway_fee__criteria__backend_api_id=backend_api_id
        )
        relevant_currencies = [
            Currency.objects.get(id=id)
            for id in billables_in_month_by_backend_api_id.values_list(
                'gateway_fee__currency', flat=True).distinct()
        ]
        for currency in relevant_currencies:
            cost_by_backend_and_currency = sum(
                billable.gateway_charge * (billable.gateway_fee_conversion_rate or 1)
                for billable in billables_in_month_by_backend_api_id.filter(
                    gateway_fee__currency=currency
                )
            )
            costs_by_backend[backend_api_id].append((cost_by_backend_and_currency, currency.code))

    subject = "[{}] Cost per SMS Gateway Monthly Summary".format(settings.SERVER_ENVIRONMENT)

    def _get_cost_string(cost, currency_code):
        cost_template = '%.2f %s'
        cost_string_in_original_currency = cost_template % (cost, currency_code)
        default_code = Currency.get_default().code
        if currency_code == default_code:
            return cost_string_in_original_currency
        else:
            cost_string_in_default_currency = cost_template % (
                cost / Currency.objects.get(code=currency_code).rate_to_default,
                default_code
            )
            return '%s (%s)' % (
                cost_string_in_original_currency,
                cost_string_in_default_currency
            )


    send_HTML_email(
        subject,
        settings.ACCOUNTS_EMAIL,
        ''.join(
            '<p>{}: {}</p>'.format(
                backend_api_id, '; '.join(
                    _get_cost_string(cost, currency_code) for (cost, currency_code) in cost_by_backend
                )
            )
            for (backend_api_id, cost_by_backend) in costs_by_backend.items()
        )
    )
 def test_birth_weight_monitored_second_trimester_no_lookback(self):
     form = _form_with_weight_monitor(datetime.combine(add_months_to_date(self.valid_edd, -4), time()))
     case = OPMCase(
         forms=[form],
         edd=self.valid_edd,
     )
     row = MockCaseRow(case, self.second_trimester_report)
     self.assertFalse(row.preg_weighed)
 def test_received_ifa_lookback_only_6_months(self):
     form = _form_with_ifa(datetime.combine(add_months_to_date(self.valid_edd, -7), time()))
     case = OPMCase(
         forms=[form],
         edd=self.valid_edd,
     )
     row = MockCaseRow(case, self.report)
     self.assertFalse(row.preg_received_ifa)
    def setUpClass(cls):
        super(BaseCustomerInvoiceCase, cls).setUpClass()

        cls.billing_contact = generator.create_arbitrary_web_user_name()
        cls.dimagi_user = generator.create_arbitrary_web_user_name(is_dimagi=True)
        cls.account = generator.billing_account(
            cls.dimagi_user, cls.billing_contact)
        cls.domain = generator.arbitrary_domain()
        cls.account.is_customer_billing_account = True
        cls.account.save()

        cls.advanced_plan = DefaultProductPlan.get_default_plan_version(edition=SoftwarePlanEdition.ADVANCED)
        cls.advanced_plan.plan.is_customer_software_plan = True

        cls.subscription_length = 15  # months
        subscription_start_date = datetime.date(2016, 2, 23)
        subscription_end_date = add_months_to_date(subscription_start_date, cls.subscription_length)
        cls.subscription = generator.generate_domain_subscription(
            cls.account,
            cls.domain,
            date_start=subscription_start_date,
            date_end=subscription_end_date,
        )
        cls.subscription.plan_version.plan.is_customer_software_plan = True

        advanced_subscription_end_date = add_months_to_date(subscription_end_date, 2)
        cls.domain2 = generator.arbitrary_domain()
        cls.sub2 = generator.generate_domain_subscription(
            cls.account,
            cls.domain2,
            date_start=subscription_start_date,
            date_end=advanced_subscription_end_date,
            plan_version=cls.advanced_plan
        )
        cls.sub2.plan_version.plan.is_customer_software_plan = True

        cls.domain3 = generator.arbitrary_domain()
        cls.sub3 = generator.generate_domain_subscription(
            cls.account,
            cls.domain3,
            date_start=subscription_start_date,
            date_end=advanced_subscription_end_date,
            plan_version=cls.advanced_plan
        )
        cls.sub3.plan_version.plan.is_customer_software_plan = True
 def test_available_last_month(self):
     data_provider = MockDataProvider(explicit_map={
         self.owner_id: {
             'vhnd_available': [add_months_to_date(self.report_date, -1)]
         }
     })
     row = self._make_row(data_provider)
     self.assertFalse(row.vhnd_available)
     self.assertTrue(row.last_month_row.vhnd_available)
 def test_service_unavailable_different_condition(self):
     date = add_months_to_date(self.report_date, -2)
     data_provider = MockDataProvider(explicit_map=self._service_map(date, [self.mismatched_service_key]))
     case = OPMCase(
         forms=[],
         dod=self.valid_dod,
         owner_id='mock_owner_id',
     )
     row = MockCaseRow(case, self.report, data_provider)
     self.assertEqual(True, self.get_condition(row))
Example #15
0
    def child_age(self):
        if self.status == 'mother':
            non_adjusted_month = len(months_between(self.dod, self.reporting_window_start)) - 1
            # anchor date should be their one month birthday
            anchor_date = add_months_to_date(self.dod, 1)

            month = self._adjust_for_vhnd_presence(non_adjusted_month, anchor_date)
            if month < 1:
                self.case_is_out_of_range = True
            return month
Example #16
0
    def preg_month(self):
        if self.status == 'pregnant':
            if not self.edd:
                raise InvalidRow('No edd found for pregnant mother.')
            base_window_start = add_months_to_date(self.edd, -9)
            try:
                non_adjusted_month = len(months_between(base_window_start, self.reporting_window_start)) - 1
            except AssertionError:
                self.case_is_out_of_range = True
                non_adjusted_month = 0

            # the date to check one month after they first become eligible,
            # aka the end of their fourth month of pregnancy
            vhnd_date_to_check = add_months_to_date(self.preg_first_eligible_date, 1)

            month = self._adjust_for_vhnd_presence(non_adjusted_month, vhnd_date_to_check)
            if month < 4 or month > 9:
                self.case_is_out_of_range = True
            return month
Example #17
0
 def filter_values(self):
     return dict(
         domain=self.domain,
         user_id=self.user_id,
         startdate=str(self.datespan.startdate_utc.date()),
         enddate=str(self.datespan.enddate_utc.date()),
         edd_startdate=self.datespan.startdate.date().isoformat(),
         edd_enddate=add_months_to_date(self.datespan.enddate_utc.date(), 5).isoformat(),
         test_account='111%',
     )
Example #18
0
    def child_age(self):
        if self.status == 'mother':
            non_adjusted_month = len(months_between(self.dod, self.reporting_window_start)) - 1
            # anchor date should be their one month birthday
            anchor_date = add_months_to_date(self.dod, 1)

            month = self._adjust_for_vhnd_presence(non_adjusted_month, anchor_date)
            if month < 1:
                raise InvalidRow('child month %s not valid' % month)

            return month
Example #19
0
 def test_next_subscription(self):
     this_subscription_date_end = self.subscription.date_end
     already_canceled_future_subscription = generator.generate_domain_subscription(  # noqa
         self.account,
         self.domain,
         date_start=this_subscription_date_end,
         date_end=this_subscription_date_end
     )
     next_future_subscription = generator.generate_domain_subscription(
         self.account,
         self.domain,
         date_start=this_subscription_date_end,
         date_end=add_months_to_date(this_subscription_date_end, 1),
     )
     self.assertEqual(self.subscription.next_subscription, next_future_subscription)
Example #20
0
 def _generate_non_autopayable_entities(self):
     """
     Create account, domain, and subscription linked to the autopay user, but that don't have autopay enabled
     """
     self.non_autopay_account = generator.billing_account(
         web_user_creator=generator.arbitrary_web_user(is_dimagi=True),
         web_user_contact=self.autopay_user
     )
     self.non_autopay_domain = generator.arbitrary_domain()
     # Non-autopay subscription has same parameters as the autopayable subscription
     self.non_autopay_subscription = generator.generate_domain_subscription(
         self.non_autopay_account,
         self.non_autopay_domain,
         date_start=self.subscription.date_start,
         date_end=add_months_to_date(self.subscription.date_start, self.subscription_length),
     )
Example #21
0
    def setUp(self):
        super(TestSubscription, self).setUp()
        self.billing_contact = generator.arbitrary_web_user()
        self.dimagi_user = generator.arbitrary_web_user(is_dimagi=True)
        self.domain = Domain(name='test')
        self.domain.save()
        self.currency = generator.init_default_currency()
        self.account = generator.billing_account(self.dimagi_user, self.billing_contact)

        self.subscription_length = 15  # months
        subscription_start_date = datetime.date(2016, 2, 23)
        subscription_end_date = add_months_to_date(subscription_start_date, self.subscription_length)
        self.subscription = generator.generate_domain_subscription(
            self.account,
            self.domain,
            date_start=subscription_start_date,
            date_end=subscription_end_date,
        )
Example #22
0
    def setUp(self):
        super(TestContractedInvoices, self).setUp()
        generator.delete_all_subscriptions()

        self.subscription_length = self.min_subscription_length
        subscription_start_date = datetime.date(2016, 2, 23)
        subscription_end_date = add_months_to_date(subscription_start_date, self.subscription_length)
        self.subscription = generator.generate_domain_subscription(
            self.account,
            self.domain,
            date_start=subscription_start_date,
            date_end=subscription_end_date,
            service_type=SubscriptionType.IMPLEMENTATION,
        )

        self.invoice_date = utils.months_from_date(
            self.subscription.date_start,
            random.randint(2, self.subscription_length)
        )
Example #23
0
    def setUp(self):
        super(BaseInvoiceTestCase, self).setUp()
        self.billing_contact = generator.arbitrary_web_user()
        self.dimagi_user = generator.arbitrary_web_user(is_dimagi=True)
        self.currency = generator.init_default_currency()
        self.account = generator.billing_account(
            self.dimagi_user, self.billing_contact)
        self.domain = generator.arbitrary_domain()

        self.subscription_length = 15  # months
        subscription_start_date = datetime.date(2016, 2, 23)
        subscription_end_date = add_months_to_date(subscription_start_date, self.subscription_length)
        self.subscription = generator.generate_domain_subscription(
            self.account,
            self.domain,
            date_start=subscription_start_date,
            date_end=subscription_end_date,
        )

        self.community_plan = DefaultProductPlan.get_default_plan_version()
Example #24
0
    def setUpClass(cls):
        super(BaseInvoiceTestCase, cls).setUpClass()

        if cls.is_using_test_plans:
            generator.bootstrap_test_software_plan_versions()

        cls.billing_contact = generator.create_arbitrary_web_user_name()
        cls.dimagi_user = generator.create_arbitrary_web_user_name(is_dimagi=True)
        cls.currency = generator.init_default_currency()
        cls.account = generator.billing_account(
            cls.dimagi_user, cls.billing_contact)
        cls.domain = generator.arbitrary_domain()

        cls.subscription_length = 15  # months
        subscription_start_date = datetime.date(2016, 2, 23)
        subscription_end_date = add_months_to_date(subscription_start_date, cls.subscription_length)
        cls.subscription = generator.generate_domain_subscription(
            cls.account,
            cls.domain,
            date_start=subscription_start_date,
            date_end=subscription_end_date,
        )
Example #25
0
 def _generate_non_autopayable_entities(cls):
     """
     Create account, domain, and subscription linked to the autopay user, but that don't have autopay enabled
     """
     cls.non_autopay_account = generator.billing_account(
         web_user_creator=generator.create_arbitrary_web_user_name(is_dimagi=True),
         web_user_contact=cls.autopay_user_email
     )
     cls.non_autopay_domain = generator.arbitrary_domain()
     # Non-autopay subscription has same parameters as the autopayable subscription
     cheap_plan = SoftwarePlan.objects.create(name='cheap')
     cheap_product_rate = SoftwareProductRate.objects.create(monthly_fee=100, name=cheap_plan.name)
     cheap_plan_version = SoftwarePlanVersion.objects.create(
         plan=cheap_plan,
         product_rate=cheap_product_rate,
         role=Role.objects.first(),
     )
     cls.non_autopay_subscription = generator.generate_domain_subscription(
         cls.non_autopay_account,
         cls.non_autopay_domain,
         plan_version=cheap_plan_version,
         date_start=cls.subscription.date_start,
         date_end=add_months_to_date(cls.subscription.date_start, cls.subscription_length),
     )
Example #26
0
 def test_no_shift(self):
     self.assertEqual(date(2014, 7, 15),
                      add_months_to_date(date(2014, 7, 15), 0))
Example #27
0
 def test_time_preserved(self):
     self.assertEqual(datetime(2014, 8, 15, 10, 30, 11),
                      add_months_to_date(datetime(2014, 7, 15, 10, 30, 11), 1))
Example #28
0
 def test_time_preserved_end_of_month(self):
     self.assertEqual(datetime(2014, 2, 28, 10, 30, 11),
                      add_months_to_date(datetime(2014, 1, 31, 10, 30, 11), 1))
Example #29
0
 def test_end_of_month(self):
     self.assertEqual(date(2014, 2, 28),
                      add_months_to_date(date(2014, 1, 31), 1))
 def edd(self):
     return add_months_to_date(self.report_date, 3)
Example #31
0
 def test_leap_year(self):
     self.assertEqual(date(2016, 2, 29),
                      add_months_to_date(date(2016, 1, 31), 1))
Example #32
0
 def test_time_preserved(self):
     self.assertEqual(
         datetime(2014, 8, 15, 10, 30, 11),
         add_months_to_date(datetime(2014, 7, 15, 10, 30, 11), 1))
Example #33
0
 def test_time_preserved_end_of_month(self):
     self.assertEqual(
         datetime(2014, 2, 28, 10, 30, 11),
         add_months_to_date(datetime(2014, 1, 31, 10, 30, 11), 1))
Example #34
0
 def preg_first_eligible_date(self):
     """
     The date we first start looking for mother data. This is the beginning of the 4th month of pregnancy.
     """
     if self.status == 'pregnant':
         return add_months_to_date(self.edd, -6)
Example #35
0
 def bad_edd(self):
     """The expected date of delivery is beyond program range"""
     # EDD is more than nine months from the date the report is run
     if self.edd and self.edd > add_months_to_date(datetime.date.today(), 9):
         return _("Incorrect EDD")
Example #36
0
 def test_normal_datetime_function(self):
     self.assertEqual(datetime(2014, 9, 15),
                      add_months_to_date(datetime(2014, 7, 15), 2))
Example #37
0
 def test_going_backwards(self):
     self.assertEqual(date(2014, 5, 15),
                      add_months_to_date(date(2014, 7, 15), -2))
Example #38
0
 def preg_first_eligible_date(self):
     """
     The date we first start looking for mother data. This is the beginning of the 4th month of pregnancy.
     """
     if self.status == 'pregnant':
         return add_months_to_date(self.edd, -6)
Example #39
0
 def test_spanning_multiple_years_backwards(self):
     self.assertEqual(date(2012, 9, 15),
                      add_months_to_date(date(2014, 7, 15), -22))
Example #40
0
 def bad_edd(self):
     """The expected date of delivery is beyond program range"""
     # EDD is more than nine months from the date the report is run
     if self.edd and self.edd > add_months_to_date(datetime.date.today(), 9):
         return _("Incorrect EDD")
Example #41
0
 def test_spanning_years_backwards(self):
     self.assertEqual(date(2014, 12, 15),
                      add_months_to_date(date(2015, 1, 15), -1))
Example #42
0
 def test_spanning_multiple_years(self):
     self.assertEqual(date(2016, 5, 15),
                      add_months_to_date(date(2014, 7, 15), 22))
Example #43
0
    def setUpClass(cls):
        super().setUpClass()

        cls.es = get_es_new()
        initialize_index_and_mapping(cls.es, USER_INDEX_INFO)
        initialize_index_and_mapping(cls.es, XFORM_INDEX_INFO)

        today = datetime.datetime.utcnow()

        one_year_ago = add_months_to_date(today.date(), -12)
        enterprise_plan = get_enterprise_software_plan()
        cls.billing_account = get_enterprise_account()
        cls.domains = [
            create_domain('test-emw-settings-001'),
            create_domain('test-emw-settings-002'),
        ]
        add_domains_to_enterprise_account(cls.billing_account, cls.domains,
                                          enterprise_plan, one_year_ago)

        cls.emw_settings = EnterpriseMobileWorkerSettings.objects.create(
            account=cls.billing_account,
            enable_auto_deactivation=True,
        )

        cls.active_user1 = CommCareUser.create(
            domain=cls.domains[0].name,
            username='******',
            password='******',
            created_by=None,
            created_via=None,
            is_active=True,
        )
        cls.active_user2 = CommCareUser.create(
            domain=cls.domains[0].name,
            username='******',
            password='******',
            created_by=None,
            created_via=None,
            is_active=True,
        )
        cls.active_user3 = CommCareUser.create(
            domain=cls.domains[1].name,
            username='******',
            password='******',
            created_by=None,
            created_via=None,
            is_active=True,
        )
        cls.active_user4 = CommCareUser.create(
            domain=cls.domains[1].name,
            username='******',
            password='******',
            created_by=None,
            created_via=None,
            is_active=True,
        )
        cls.active_user5 = CommCareUser.create(
            domain=cls.domains[1].name,
            username='******',
            password='******',
            created_by=None,
            created_via=None,
            is_active=True,
        )
        cls.active_user5.created_on = today - datetime.timedelta(
            days=cls.emw_settings.inactivity_period)
        cls.active_user5.save()
        cls.active_user6 = CommCareUser.create(
            domain=cls.domains[1].name,
            username='******',
            password='******',
            created_by=None,
            created_via=None,
            is_active=True,
        )

        cls.users = [
            cls.active_user1,
            cls.active_user2,
            cls.active_user3,
            cls.active_user4,
            cls.active_user5,
            cls.active_user6,
            CommCareUser.create(domain=cls.domains[0].name,
                                username='******',
                                password='******',
                                created_by=None,
                                created_via=None,
                                is_active=False),
            CommCareUser.create(domain=cls.domains[1].name,
                                username='******',
                                password='******',
                                created_by=None,
                                created_via=None,
                                is_active=False),
        ]

        form_submissions = [
            (TestFormMetadata(
                domain=cls.domains[0].name,
                received_on=today - datetime.timedelta(
                    days=cls.emw_settings.inactivity_period - 1),
                user_id=cls.active_user1.user_id,
                username=cls.active_user1.username,
            ), cls.active_user1),
            (TestFormMetadata(
                domain=cls.domains[0].name,
                received_on=today -
                datetime.timedelta(days=cls.emw_settings.inactivity_period),
                user_id=cls.active_user2.user_id,
                username=cls.active_user2.username,
            ), cls.active_user2),
            (TestFormMetadata(
                domain=cls.domains[1].name,
                received_on=today - datetime.timedelta(
                    days=cls.emw_settings.inactivity_period - 10),
                user_id=cls.active_user3.user_id,
                username=cls.active_user3.username,
            ), cls.active_user3),
            (TestFormMetadata(
                domain=cls.domains[1].name,
                received_on=today - datetime.timedelta(
                    days=cls.emw_settings.inactivity_period + 1),
                user_id=cls.active_user6.user_id,
                username=cls.active_user6.username,
            ), cls.active_user6),
        ]
        for form_metadata, user in form_submissions:
            # ensure users are as old as the received_on dates of their submissions
            user.created_on = form_metadata.received_on
            user.save()
            form_pair = make_es_ready_form(form_metadata)
            send_to_elasticsearch('forms', form_pair.json_form)
            mark_latest_submission(form_metadata.domain, user,
                                   form_metadata.app_id, "build-id", "2",
                                   {'deviceID': 'device-id'},
                                   form_metadata.received_on)

        for user in cls.users:
            fresh_user = CommCareUser.get_by_user_id(user.user_id)
            elastic_user = transform_user_for_elasticsearch(
                fresh_user.to_json())
            send_to_elasticsearch('users', elastic_user)

        cls.es.indices.refresh(USER_INDEX_INFO.alias)
        cls.es.indices.refresh(XFORM_INDEX_INFO.alias)