def setUp(self): super(TestNewDomainSubscription, self).setUp() self.domain = Domain( name="test-domain-sub", is_active=True, ) self.domain.save() self.domain2 = Domain( name="test-domain-sub2", is_active=True, ) self.domain2.save() self.admin_user = generator.arbitrary_web_user() self.admin_user.add_domain_membership(self.domain.name, is_admin=True) self.admin_user.save() self.account = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by=self.admin_user.username)[0] self.account2 = BillingAccount.get_or_create_account_by_domain( self.domain2.name, created_by=self.admin_user.username)[0] self.standard_plan = DefaultProductPlan.get_default_plan_by_domain( self.domain.name, edition=SoftwarePlanEdition.STANDARD) self.advanced_plan = DefaultProductPlan.get_default_plan_by_domain( self.domain.name, edition=SoftwarePlanEdition.ADVANCED)
def setUp(self): super(TestSubscriptionForm, self).setUp() self.domain = Domain( name="test-sub-form", is_active=True ) self.domain.save() self.domain2 = Domain( name="test-sub-form-2", is_active=True ) self.domain2.save() self.web_user = WebUser.create( self.domain.name, generator.create_arbitrary_web_user_name(), 'testpwd' ) self.account = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by=self.web_user.username )[0] self.account.save() self.customer_account = BillingAccount.get_or_create_account_by_domain( self.domain2.name, created_by=self.web_user.username )[0] self.customer_account.is_customer_billing_account = True self.customer_account.save() self.plan = DefaultProductPlan.get_default_plan_version(edition=SoftwarePlanEdition.ADVANCED) self.customer_plan = DefaultProductPlan.get_default_plan_version(edition=SoftwarePlanEdition.ADVANCED) self.customer_plan.plan.is_customer_software_plan = True
def account(self): """ First try to grab the account used for the last subscription. If an account is not found, create it. """ account, _ = BillingAccount.get_or_create_account_by_domain(self.domain.name, self.__class__.__name__) return account
def create_wire_credits_invoice(domain_name, account_created_by, account_entry_point, amount, invoice_items, contact_emails): account = BillingAccount.get_or_create_account_by_domain( domain_name, created_by=account_created_by, created_by_invoicing=True, entry_point=account_entry_point )[0] wire_invoice = WirePrepaymentInvoice.objects.create( domain=domain_name, date_start=datetime.datetime.utcnow(), date_end=datetime.datetime.utcnow(), date_due=None, balance=amount, account=account, ) wire_invoice.items = invoice_items record = WirePrepaymentBillingRecord.generate_record(wire_invoice) try: record.send_email(contact_emails=contact_emails) except Exception as e: logger.error("[BILLING] %s" % e)
def test_community_over_limit(self): """ For a domain under community (no subscription) with users over the community limit, make sure that: - base_description is None - base_cost is 0.0 - unit_description is not None - unit_cost is equal to the per_excess_fee on the user rate - quantity is equal to number of commcare users in that domain minus the monthly_limit on the user rate - total and subtotals are equal to number of extra users * per_excess_fee """ domain = generator.arbitrary_domain() num_active = generator.create_excess_community_users(domain) account = BillingAccount.get_or_create_account_by_domain( domain, created_by=self.dimagi_user)[0] billing_contact = generator.arbitrary_contact_info(account, self.dimagi_user) billing_contact.save() account.date_confirmed_extra_charges = datetime.date.today() account.save() tasks.generate_invoices() subscriber = Subscriber.objects.get(domain=domain.name) invoice = Invoice.objects.filter(subscription__subscriber=subscriber).get() user_line_item = invoice.lineitem_set.get_feature_by_type(FeatureType.USER).get() self.assertIsNone(user_line_item.base_description) self.assertEqual(user_line_item.base_cost, Decimal('0.0000')) num_to_charge = num_active - self.community_plan.user_limit self.assertIsNotNone(user_line_item.unit_description) self.assertEqual(user_line_item.quantity, num_to_charge) self.assertEqual(user_line_item.unit_cost, self.user_rate.per_excess_fee) self.assertEqual(user_line_item.subtotal, num_to_charge * self.user_rate.per_excess_fee) self.assertEqual(user_line_item.total, num_to_charge * self.user_rate.per_excess_fee) domain.delete()
def create_wire_credits_invoice(domain_name, account_created_by, account_entry_point, amount, invoice_items, contact_emails): account = BillingAccount.get_or_create_account_by_domain( domain_name, created_by=account_created_by, entry_point=account_entry_point )[0] wire_invoice = WirePrepaymentInvoice.objects.create( domain=domain_name, date_start=datetime.datetime.utcnow(), date_end=datetime.datetime.utcnow(), date_due=None, balance=amount, account=account, ) wire_invoice.items = invoice_items record = WirePrepaymentBillingRecord.generate_record(wire_invoice) if record.should_send_email: try: record.send_email(contact_emails=contact_emails) except Exception as e: log_accounting_error( "Error sending email for WirePrepaymentBillingRecord %d: %s" % (record.id, e.message), show_stack_trace=True, ) else: record.skipped_email = True record.save()
def setUp(self): super(OptTestCase, self).setUp() self.domain = "opt-test" self.domain_obj = Domain(name=self.domain) self.domain_obj.save() generator.instantiate_accounting_for_tests() self.account = BillingAccount.get_or_create_account_by_domain( self.domain_obj.name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan_by_domain( self.domain_obj, edition=SoftwarePlanEdition.ADVANCED ) self.subscription = Subscription.new_domain_subscription( self.account, self.domain_obj.name, plan ) self.subscription.is_active = True self.subscription.save() self.backend = TestSMSBackend(is_global=True) self.backend.save() self.backend_mapping = BackendMapping( is_global=True, prefix="*", backend_id=self.backend._id, ) self.backend_mapping.save()
def handle(self, *args, **options): if len(args) != 1: print "Invalid arguments: %s" % str(args) return domain = Domain.get_by_name(args[0]) if not domain: print "Invalid domain name: %s" % args[0] return account, _ = BillingAccount.get_or_create_account_by_domain( domain.name, account_type=BillingAccountType.CONTRACT, created_by="management command", ) enterprise_plan_version = SoftwarePlanVersion.objects.filter( plan__edition=SoftwarePlanEdition.ENTERPRISE )[0] try: subscription = Subscription.new_domain_subscription( account, domain.name, enterprise_plan_version ) except NewSubscriptionError as e: print e.message return subscription.is_active = True subscription.save() print 'Domain %s has been upgraded to enterprise level.' % domain.name
def create_domain(self, domain): domain_obj = Domain(name=domain) domain_obj.use_default_sms_response = True domain_obj.default_sms_response = "Default SMS Response" domain_obj.save() # I tried making this class inherit from BaseSMSTest, but somehow # the multiple inheritance was causing the postgres connection to # get in a weird state where it wasn't commiting any changes. So # for now, keeping this subscription setup code as is. generator.instantiate_accounting_for_tests() self.account = BillingAccount.get_or_create_account_by_domain( domain_obj.name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan_by_domain( domain_obj, edition=SoftwarePlanEdition.ADVANCED ) self.subscription = Subscription.new_domain_subscription( self.account, domain_obj.name, plan ) self.subscription.is_active = True self.subscription.save() return domain_obj
def update_credits(self, payment_record): amount = payment_record.amount for invoice in self.invoices: deduct_amount = min(amount, invoice.balance) amount -= deduct_amount if deduct_amount > 0: if self.account and self.account.is_customer_billing_account: customer_invoice = invoice subscription_invoice = None account = self.account else: customer_invoice = None subscription_invoice = invoice account = invoice.subscription.account # TODO - refactor duplicated functionality CreditLine.add_credit( deduct_amount, account=account, payment_record=payment_record, ) CreditLine.add_credit( -deduct_amount, account=account, invoice=subscription_invoice, customer_invoice=customer_invoice ) invoice.update_balance() invoice.save() if amount: account = BillingAccount.get_or_create_account_by_domain(self.domain)[0] CreditLine.add_credit( amount, account=account, payment_record=payment_record, )
def setUp(self): super(TestDeleteDomain, self).setUp() self.domain = Domain(name="test", is_active=True) self.domain.save() self.domain.convert_to_commtrack() self.current_subscription = Subscription.new_domain_subscription( BillingAccount.get_or_create_account_by_domain(self.domain.name, created_by='tests')[0], self.domain.name, DefaultProductPlan.get_default_plan_version(SoftwarePlanEdition.ADVANCED), date_start=date.today() - relativedelta(days=1), ) self.domain2 = Domain(name="test2", is_active=True) self.domain2.save() self.domain2.convert_to_commtrack() LocationType.objects.create( domain='test', name='facility', ) LocationType.objects.create( domain='test2', name='facility', ) LocationType.objects.create( domain='test', name='facility2', ) LocationType.objects.create( domain='test2', name='facility2', )
def setUp(self): super(TestCreditTransfers, self).setUp() self.product_credit_amt = Decimal("500.00") self.feature_credit_amt = Decimal("200.00") self.subscription_credit_amt = Decimal("600.00") self.domain = generator.arbitrary_domain() self.account = BillingAccount.get_or_create_account_by_domain(self.domain, created_by="*****@*****.**")[0]
def setUp(self): super(TestUserRoleSubscriptionChanges, self).setUp() self.domain = generator.arbitrary_domain() UserRole.init_domain_with_presets(self.domain.name) self.user_roles = UserRole.by_domain(self.domain.name) self.custom_role = UserRole.get_or_create_with_permissions( self.domain.name, Permissions(edit_apps=True, edit_web_users=True), "Custom Role" ) self.custom_role.save() self.read_only_role = UserRole.get_read_only_role_by_domain(self.domain.name) self.admin_user = generator.arbitrary_web_user() self.admin_user.add_domain_membership(self.domain.name, is_admin=True) self.admin_user.save() self.web_users = [] self.commcare_users = [] for role in [self.custom_role] + self.user_roles: web_user = generator.arbitrary_web_user() web_user.add_domain_membership(self.domain.name, role_id=role.get_id) web_user.save() self.web_users.append(web_user) commcare_user = generator.arbitrary_commcare_user( domain=self.domain.name) commcare_user.set_role(self.domain.name, role.get_qualified_id()) commcare_user.save() self.commcare_users.append(commcare_user) self.account = BillingAccount.get_or_create_account_by_domain( self.domain.name,created_by=self.admin_user.username)[0] self.advanced_plan = DefaultProductPlan.get_default_plan_by_domain( self.domain.name,edition=SoftwarePlanEdition.ADVANCED)
def setUp(self): super(TestRenewSubscriptions, self).setUp() self.domain = Domain( name="test-domain-sub", is_active=True, ) self.domain.save() self.admin_user = generator.arbitrary_web_user() self.admin_user.add_domain_membership(self.domain.name, is_admin=True) self.admin_user.save() self.account = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by=self.admin_user.username)[0] self.standard_plan = DefaultProductPlan.get_default_plan_by_domain( self.domain.name, edition=SoftwarePlanEdition.STANDARD) today = datetime.date.today() yesterday = today + datetime.timedelta(days=-1) tomorrow = today + datetime.timedelta(days=1) self.subscription = Subscription.new_domain_subscription( self.account, self.domain.name, self.standard_plan, web_user=self.admin_user.username, date_start=yesterday, date_end=tomorrow, ) self.subscription.save()
def setUp(self): super(BaseReminderTestCase, self).setUp() self.domain_obj = Domain(name="test") self.domain_obj.save() # Prevent resource conflict self.domain_obj = Domain.get(self.domain_obj._id) self.account, _ = BillingAccount.get_or_create_account_by_domain( self.domain_obj.name, created_by="tests" ) advanced_plan_version = DefaultProductPlan.get_default_plan_by_domain( self.domain_obj, edition=SoftwarePlanEdition.ADVANCED) self.subscription = Subscription.new_domain_subscription( self.account, self.domain_obj.name, advanced_plan_version ) self.subscription.is_active = True self.subscription.save() self.sms_backend = TestSMSBackend(named="MOBILE_BACKEND_TEST", is_global=True) self.sms_backend.save() self.sms_backend_mapping = BackendMapping(is_global=True,prefix="*",backend_id=self.sms_backend._id) self.sms_backend_mapping.save()
def test_community_invoice(self): """ For an unsubscribed domain with any charges over the community limit for the month of invoicing, make sure that an invoice is generated in addition to a subscription for that month to the community plan. """ domain = generator.arbitrary_domain() generator.create_excess_community_users(domain) account = BillingAccount.get_or_create_account_by_domain( domain, created_by=self.dimagi_user)[0] billing_contact = generator.arbitrary_contact_info(account, self.dimagi_user) billing_contact.save() account.date_confirmed_extra_charges = datetime.date.today() account.save() tasks.generate_invoices() subscriber = Subscriber.objects.get(domain=domain.name) invoices = Invoice.objects.filter(subscription__subscriber=subscriber) self.assertEqual(invoices.count(), 1) invoice = invoices.get() self.assertEqual(invoice.subscription.subscriber.domain, domain.name) self.assertEqual(invoice.subscription.date_start, invoice.date_start) self.assertEqual( invoice.subscription.date_end - datetime.timedelta(days=1), invoice.date_end ) domain.delete()
def assign_explicit_community_subscription(domain_name, start_date): future_subscriptions = Subscription.objects.filter( CONSISTENT_DATES_CHECK ).filter( date_start__gt=start_date, subscriber__domain=domain_name, ) if future_subscriptions.exists(): end_date = future_subscriptions.latest('date_start').date_start else: end_date = None return Subscription.new_domain_subscription( account=BillingAccount.get_or_create_account_by_domain( domain_name, created_by='assign_explicit_community_subscriptions', entry_point=EntryPoint.SELF_STARTED, )[0], domain=domain_name, plan_version=DefaultProductPlan.get_default_plan_version(), date_start=start_date, date_end=end_date, skip_invoicing_if_no_feature_charges=True, adjustment_method=SubscriptionAdjustmentMethod.TASK, internal_change=True, service_type=SubscriptionType.PRODUCT, )
def assign_explicit_community_subscription(domain_name, start_date, method, account=None, web_user=None): future_subscriptions = Subscription.visible_objects.filter( date_start__gt=start_date, subscriber__domain=domain_name, ) if future_subscriptions.exists(): end_date = future_subscriptions.earliest('date_start').date_start else: end_date = None if account is None: account = BillingAccount.get_or_create_account_by_domain( domain_name, created_by='assign_explicit_community_subscriptions', entry_point=EntryPoint.SELF_STARTED, )[0] return Subscription.new_domain_subscription( account=account, domain=domain_name, plan_version=DefaultProductPlan.get_default_plan_version(), date_start=start_date, date_end=end_date, skip_invoicing_if_no_feature_charges=True, adjustment_method=method, internal_change=True, service_type=SubscriptionType.PRODUCT, web_user=web_user, )
def ensure_full_coverage(self, subscriptions): plan_version = DefaultProductPlan.get_default_plan_by_domain( self.domain, edition=SoftwarePlanEdition.COMMUNITY ).plan.get_version() if not plan_version.feature_charges_exist_for_domain(self.domain): return community_ranges = self.get_community_ranges(subscriptions) if not community_ranges: return do_not_invoice = any([s.do_not_invoice for s in subscriptions]) account = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by=self.__class__.__name__, created_by_invoicing=True)[0] if account.date_confirmed_extra_charges is None: if self.domain.is_active: subject = "[%s] Invoice Generation Issue" % self.domain.name email_content = render_to_string( 'accounting/invoice_error_email.html', { 'project': self.domain.name, 'error_msg': "This project is incurring charges on their " "Community subscription, but they haven't " "agreed to the charges yet. Someone should " "follow up with this project to see if everything " "is configured correctly or if communication " "needs to happen between Dimagi and the project's" "admins. For now, the invoices generated are " "marked as Do Not Invoice.", } ) send_HTML_email( subject, settings.BILLING_EMAIL, email_content, email_from="Dimagi Billing Bot <%s>" % settings.DEFAULT_FROM_EMAIL ) do_not_invoice = True if not BillingContactInfo.objects.filter(account=account).exists(): # No contact information exists for this account. # This shouldn't happen, but if it does, we can't continue # with the invoice generation. raise BillingContactInfoError( "Project %s has incurred charges, but does not have their " "Billing Contact Info filled out. Someone should follow up " "on this." % self.domain.name ) # First check to make sure none of the existing subscriptions is set # to do not invoice. Let's be on the safe side and not send a # community invoice out, if that's the case. for c in community_ranges: # create a new community subscription for each # date range that the domain did not have a subscription community_subscription = Subscription( account=account, plan_version=plan_version, subscriber=self.subscriber, date_start=c[0], date_end=c[1], do_not_invoice=do_not_invoice, ) community_subscription.save() subscriptions.append(community_subscription)
def _setup_accounting(cls): call_command('cchq_prbac_bootstrap') cls.account, _ = BillingAccount.get_or_create_account_by_domain( cls.domain.name, created_by='') plan_version = DefaultProductPlan.get_default_plan_version( SoftwarePlanEdition.ADVANCED) cls.subscription = Subscription.new_domain_subscription( cls.account, cls.domain.name, plan_version)
def account(self): account = BillingAccount.get_or_create_account_by_domain( self.domain, created_by=self.couch_user.username, account_type=BillingAccountType.USER_CREATED, entry_point=EntryPoint.SELF_STARTED, )[0] return account
def account(self): account = BillingAccount.get_or_create_account_by_domain( self.domain, created_by=self.couch_user.username, account_type=BillingAccountType.USER_CREATED, entry_point=EntryPoint.SELF_STARTED, )[0] return account
def setUp(self): self.domain = Domain(name='test-domain') self.domain.save() self.payment_method = PaymentMethod() self.payment_method.save() self.account, _ = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by='*****@*****.**' )
def setUpClass(cls): super(TestCreditStripePaymentHandler, cls).setUpClass() cls.domain = Domain(name='test-domain') cls.domain.save() cls.payment_method = PaymentMethod() cls.payment_method.save() cls.account, _ = BillingAccount.get_or_create_account_by_domain( cls.domain.name, created_by='*****@*****.**')
def create_wire_invoice(self, balance): # Gather relevant invoices if self.account and self.account.is_customer_billing_account: invoices = CustomerInvoice.objects.filter(account=self.account) else: invoices = Invoice.objects.filter( subscription__subscriber__domain=self.domain.name, is_hidden=False, date_paid__exact=None ).order_by('-date_start') self.account = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by=self.__class__.__name__, entry_point=EntryPoint.SELF_STARTED )[0] # If no start date supplied, default earliest start date of unpaid invoices if self.date_start: date_start = self.date_start else: date_start = invoices.aggregate(Min('date_start'))['date_start__min'] # If no end date supplied, default latest end date of unpaid invoices if self.date_end: date_end = self.date_end else: date_end = invoices.aggregate(Max('date_end'))['date_end__max'] if not date_end: date_end = datetime.datetime.today() date_due = date_end + datetime.timedelta(DEFAULT_DAYS_UNTIL_DUE) wire_invoice = WireInvoice.objects.create( domain=self.domain.name, date_start=date_start, date_end=date_end, date_due=date_due, balance=balance, ) record = WireBillingRecord.generate_record(wire_invoice) if record.should_send_email: try: for email in self.contact_emails: record.send_email(contact_email=email) except InvoiceEmailThrottledError as e: # Currently wire invoices are never throttled if not self.logged_throttle_error: log_accounting_error(six.text_type(e)) self.logged_throttle_error = True else: record.skipped_email = True record.save() return wire_invoice
def create_wire_invoice(self, balance): # Gather relevant invoices if self.account and self.account.is_customer_billing_account: invoices = CustomerInvoice.objects.filter(account=self.account) else: invoices = Invoice.objects.filter( subscription__subscriber__domain=self.domain.name, is_hidden=False, date_paid__exact=None ).order_by('-date_start') self.account = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by=self.__class__.__name__, entry_point=EntryPoint.SELF_STARTED )[0] # If no start date supplied, default earliest start date of unpaid invoices if self.date_start: date_start = self.date_start else: date_start = invoices.aggregate(Min('date_start'))['date_start__min'] # If no end date supplied, default latest end date of unpaid invoices if self.date_end: date_end = self.date_end else: date_end = invoices.aggregate(Max('date_end'))['date_end__max'] if not date_end: date_end = datetime.datetime.today() date_due = date_end + datetime.timedelta(DEFAULT_DAYS_UNTIL_DUE) wire_invoice = WireInvoice.objects.create( domain=self.domain.name, date_start=date_start, date_end=date_end, date_due=date_due, balance=balance, ) record = WireBillingRecord.generate_record(wire_invoice) if record.should_send_email: try: for email in self.contact_emails: record.send_email(contact_email=email) except InvoiceEmailThrottledError as e: # Currently wire invoices are never throttled if not self.logged_throttle_error: log_accounting_error(six.text_type(e)) self.logged_throttle_error = True else: record.skipped_email = True record.save() return wire_invoice
def setUpClass(cls): super(TestCreditStripePaymentHandler, cls).setUpClass() cls.domain = Domain(name='test-domain') cls.domain.save() cls.payment_method = PaymentMethod() cls.payment_method.save() cls.account, _ = BillingAccount.get_or_create_account_by_domain( cls.domain.name, created_by='*****@*****.**' )
def setUp(self): super(TestCreditStripePaymentHandler, self).setUp() self.domain = Domain(name='test-domain') self.domain.save() self.payment_method = PaymentMethod() self.payment_method.save() self.account, _ = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by='*****@*****.**' )
def setUp(self): super(TestCreditStripePaymentHandler, self).setUp() generator.instantiate_accounting() self.domain = Domain(name='test-domain') self.domain.save() self.payment_method = PaymentMethod() self.payment_method.save() self.account, _ = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by='*****@*****.**')
def account(self): """ First try to grab the account used for the last subscription. If an account is not found, create it. """ account, _ = BillingAccount.get_or_create_account_by_domain(self.domain.name, self.__class__.__name__) # todo: fix so that contact_emails gets correctly populated. BillingContactInfo.objects.get_or_create(account=account) return account
def setup_subscription(cls, domain_name, software_plan): generator.instantiate_accounting() plan = DefaultProductPlan.get_default_plan_version(edition=software_plan) cls.account = BillingAccount.get_or_create_account_by_domain( domain_name, created_by="automated-test" + cls.__name__ )[0] cls.subscription = Subscription.new_domain_subscription(cls.account, domain_name, plan) cls.subscription.is_active = True cls.subscription.save()
def setup_subscription(cls, domain_name, software_plan): generator.instantiate_accounting() plan = DefaultProductPlan.get_default_plan_version(edition=software_plan) cls.account = BillingAccount.get_or_create_account_by_domain( domain_name, created_by="automated-test" + cls.__name__ )[0] cls.subscription = Subscription.new_domain_subscription(cls.account, domain_name, plan) cls.subscription.is_active = True cls.subscription.save()
def setUp(self): super(TestCreditTransfers, self).setUp() self.product_credit_amt = Decimal('500.00') self.feature_credit_amt = Decimal('200.00') self.subscription_credit_amt = Decimal('600.00') self.domain = generator.arbitrary_domain() self.account = BillingAccount.get_or_create_account_by_domain( self.domain, created_by="*****@*****.**", )[0]
def make_new_enterprise_subscription(self, domain): account, _ = BillingAccount.get_or_create_account_by_domain( domain.name, account_type=BillingAccountType.CONTRACT, created_by="management command", ) Subscription.new_domain_subscription( account, domain.name, self.enterprise_plan_version, )
def setUpClass(cls): super(TestOdataFeed, cls).setUpClass() cls.client = Client() cls.domain = Domain(name='test_domain') cls.domain.save() cls.web_user = WebUser.create(cls.domain.name, 'test_user', 'my_password') cls.account, _ = BillingAccount.get_or_create_account_by_domain(cls.domain.name, created_by='') plan_version = DefaultProductPlan.get_default_plan_version(SoftwarePlanEdition.STANDARD) cls.subscription = Subscription.new_domain_subscription(cls.account, cls.domain.name, plan_version)
def create_wire_invoice(self, balance): # Gather relevant invoices invoices = Invoice.objects.filter( subscription__subscriber__domain=self.domain, is_hidden=False, date_paid__exact=None, ).order_by('-date_start') account = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by=self.__class__.__name__, created_by_invoicing=True, entry_point=EntryPoint.SELF_STARTED, )[0] # If no start date supplied, default earliest start date of unpaid invoices if self.date_start: date_start = self.date_start else: date_start = invoices.aggregate(Min('date_start'))['date_start__min'] # If no end date supplied, default latest end date of unpaid invoices if self.date_end: date_end = self.date_end else: date_end = invoices.aggregate(Max('date_end'))['date_end__max'] if not date_end: date_end = datetime.datetime.today() date_due = date_end + datetime.timedelta(DEFAULT_DAYS_UNTIL_DUE) # TODO: figure out how to handle line items wire_invoice = WireInvoice.objects.create( domain=self.domain.name, date_start=date_start, date_end=date_end, date_due=date_due, balance=balance, account=account ) record = WireBillingRecord.generate_record(wire_invoice) try: record.send_email(contact_emails=self.contact_emails) except InvoiceEmailThrottledError as e: # Currently wire invoices are never throttled if not self.logged_throttle_error: logger.error("[BILLING] %s" % e) self.logged_throttle_error = True return wire_invoice
def setUp(self): self._clear_docs() self.domain = create_domain(DOMAIN) self.account = BillingAccount.get_or_create_account_by_domain(DOMAIN, created_by="automated-test")[0] plan = DefaultProductPlan.get_default_plan_by_domain(DOMAIN, edition=SoftwarePlanEdition.ADVANCED) self.subscription = Subscription.new_domain_subscription(self.account, DOMAIN, plan) self.subscription.is_active = True self.subscription.save() self.couch_user = WebUser.create(None, "test", "foobar") self.couch_user.add_domain_membership(DOMAIN, is_admin=True) self.couch_user.save()
def setUpClass(cls): super(TestCreditTransfers, cls).setUpClass() cls.product_credit_amt = Decimal('500.00') cls.feature_credit_amt = Decimal('200.00') cls.subscription_credit_amt = Decimal('600.00') cls.domain = generator.arbitrary_domain() cls.account = BillingAccount.get_or_create_account_by_domain( cls.domain, created_by="*****@*****.**", )[0] cls.web_user_name = generator.create_arbitrary_web_user_name() cls.other_account = generator.billing_account(cls.web_user_name, cls.web_user_name)
def setUpClass(cls): super(TestCreditTransfers, cls).setUpClass() cls.product_credit_amt = Decimal('500.00') cls.feature_credit_amt = Decimal('200.00') cls.subscription_credit_amt = Decimal('600.00') cls.domain = generator.arbitrary_domain() cls.account = BillingAccount.get_or_create_account_by_domain( cls.domain, created_by="*****@*****.**", )[0] cls.web_user_name = generator.create_arbitrary_web_user_name() cls.other_account = generator.billing_account(cls.web_user_name, cls.web_user_name)
def _setup_subscription(cls, domain_name, software_plan): plan = DefaultProductPlan.get_default_plan_version( edition=software_plan) account = BillingAccount.get_or_create_account_by_domain( domain_name, created_by="automated-test" + cls.__name__)[0] subscription = Subscription.new_domain_subscription( account, domain_name, plan, date_start=datetime.date.today() - datetime.timedelta(days=1), date_end=datetime.date.today() + datetime.timedelta(days=5)) subscription.save()
def setUpClass(cls): super(TestEmailEnterpriseReport, cls).setUpClass() cls.domain = Domain.get_or_create_with_name('test', is_active=True) cls.couch_user = WebUser.create(cls.domain.name, "enterprise-tasks-tests", "*******", None, None, is_admin=True) cls.couch_user.save() cls.billing_account = BillingAccount.get_or_create_account_by_domain( cls.domain.name, created_by=cls.domain.name)[0]
def create_wire_invoice(self, balance): # Gather relevant invoices invoices = Invoice.objects.filter( subscription__subscriber__domain=self.domain, is_hidden=False, date_paid__exact=None, ).order_by('-date_start') account = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by=self.__class__.__name__, created_by_invoicing=True, entry_point=EntryPoint.SELF_STARTED, )[0] # If no start date supplied, default earliest start date of unpaid invoices if self.date_start: date_start = self.date_start else: date_start = invoices.aggregate( Min('date_start'))['date_start__min'] # If no end date supplied, default latest end date of unpaid invoices if self.date_end: date_end = self.date_end else: date_end = invoices.aggregate(Max('date_end'))['date_end__max'] if not date_end: date_end = datetime.datetime.today() date_due = date_end + datetime.timedelta(DEFAULT_DAYS_UNTIL_DUE) # TODO: figure out how to handle line items wire_invoice = WireInvoice.objects.create(domain=self.domain.name, date_start=date_start, date_end=date_end, date_due=date_due, balance=balance, account=account) record = WireBillingRecord.generate_record(wire_invoice) try: record.send_email(contact_emails=self.contact_emails) except InvoiceEmailThrottledError as e: # Currently wire invoices are never throttled if not self.logged_throttle_error: logger.error("[BILLING] %s" % e) self.logged_throttle_error = True return wire_invoice
def bootrap_domain_for_zapier(domain_name): domain_object = Domain.get_or_create_with_name(domain_name, is_active=True) account = BillingAccount.get_or_create_account_by_domain(domain_name, created_by="automated-test")[0] plan = DefaultProductPlan.get_default_plan_version(edition=SoftwarePlanEdition.STANDARD) subscription = Subscription.new_domain_subscription(account, domain_name, plan) subscription.is_active = True subscription.save() web_user = WebUser.create(domain_name, 'test', '******') api_key_object, _ = ApiKey.objects.get_or_create(user=web_user.get_django_user()) return ZapierDomainConfig(domain_object, web_user, api_key_object.key)
def setUpClass(cls): super(TestUserSubscriptionChangeTransfers, cls).setUpClass() generator.bootstrap_test_software_plan_versions() generator.init_default_currency() cls.billing_contact = generator.create_arbitrary_web_user_name() cls.domain = generator.arbitrary_domain() cls.account = BillingAccount.get_or_create_account_by_domain( cls.domain, created_by=cls.billing_contact, )[0] generator.arbitrary_contact_info(cls.account, cls.billing_contact)
def prepare_domain(domain_name): from corehq.apps.commtrack.tests.util import bootstrap_domain domain = bootstrap_domain(domain_name) previous = None for name, administrative in [ ("MOHSW", True), ("MSDZONE", True), ("REGION", True), ("DISTRICT", True), ("FACILITY", False) ]: previous, _ = LocationType.objects.get_or_create( domain=domain_name, name=name, parent_type=previous, administrative=administrative, ) generator.instantiate_accounting() account = BillingAccount.get_or_create_account_by_domain( domain.name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan( edition=SoftwarePlanEdition.ADVANCED ) commtrack = domain.commtrack_settings commtrack.actions.append( CommtrackActionConfig(action='receipts', keyword='delivered', caption='Delivered') ) commtrack.save() subscription = Subscription.new_domain_subscription( account, domain.name, plan ) subscription.is_active = True subscription.save() ils_config = ILSGatewayConfig(enabled=True, domain=domain.name, all_stock_data=True) ils_config.save() fields_definition = CustomDataFieldsDefinition.get_or_create(domain.name, 'LocationFields') fields_definition.fields.append(CustomDataField( slug='group', label='Group', is_required=False, choices=['A', 'B', 'C'], is_multiple_choice=False )) fields_definition.save() return domain
def setUp(self): super(TestNewDomainSubscription, self).setUp() self.domain = Domain( name="test-domain-sub", is_active=True, ) self.domain.save() self.domain2 = Domain( name="test-domain-sub2", is_active=True, ) self.domain2.save() self.admin_user_name = generator.create_arbitrary_web_user_name() self.account = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by=self.admin_user_name)[0] self.account2 = BillingAccount.get_or_create_account_by_domain( self.domain2.name, created_by=self.admin_user_name)[0] self.standard_plan = DefaultProductPlan.get_default_plan_version(edition=SoftwarePlanEdition.STANDARD) self.advanced_plan = DefaultProductPlan.get_default_plan_version(edition=SoftwarePlanEdition.ADVANCED)
def setUp(self): super(TestUserRoleSubscriptionChanges, self).setUp() self.domain = Domain( name="test-sub-changes", is_active=True, ) self.domain.save() self.other_domain = Domain( name="other-domain", is_active=True, ) self.other_domain.save() UserRole.init_domain_with_presets(self.domain.name) self.user_roles = UserRole.by_domain(self.domain.name) self.custom_role = UserRole.get_or_create_with_permissions( self.domain.name, Permissions( edit_apps=True, edit_web_users=True, view_web_users=True, view_roles=True, ), "Custom Role") self.custom_role.save() self.read_only_role = UserRole.get_read_only_role_by_domain( self.domain.name) self.admin_username = generator.create_arbitrary_web_user_name() self.web_users = [] self.commcare_users = [] for role in [self.custom_role] + self.user_roles: web_user = WebUser.create( self.other_domain.name, generator.create_arbitrary_web_user_name(), 'test123', None, None) web_user.is_active = True web_user.add_domain_membership(self.domain.name, role_id=role.get_id) web_user.save() self.web_users.append(web_user) commcare_user = generator.arbitrary_commcare_user( domain=self.domain.name) commcare_user.set_role(self.domain.name, role.get_qualified_id()) commcare_user.save() self.commcare_users.append(commcare_user) self.account = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by=self.admin_username)[0] self.advanced_plan = DefaultProductPlan.get_default_plan_version( edition=SoftwarePlanEdition.ADVANCED)
def setUp(self): super(TestExportUtils, self).setUp() self.domain = generator.arbitrary_domain() self.account, _ = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by='*****@*****.**') self.account.save() subscription = Subscription.new_domain_subscription( self.account, self.domain.name, DefaultProductPlan.get_default_plan_version( edition=SoftwarePlanEdition.COMMUNITY), date_start=date.today() - timedelta(days=3)) subscription.is_active = True subscription.save()
def setUp(self): super(TestCreditTransfers, self).setUp() self.product_credit_amt = Decimal('500.00') self.feature_credit_amt = Decimal('200.00') self.subscription_credit_amt = Decimal('600.00') self.domain = generator.arbitrary_domain() self.account = BillingAccount.get_or_create_account_by_domain( self.domain, created_by="*****@*****.**", )[0] self.web_user = generator.arbitrary_web_user() # TODO - refactor interface to generator.billing_account so web user object is not required self.other_account = generator.billing_account(self.web_user, self.web_user)
def prepare_domain(domain_name): domain = create_domain(domain_name) domain.convert_to_commtrack() domain.save() set_domain_default_backend_to_test_backend(domain.name) def _make_loc_type(name, administrative=False, parent_type=None): return LocationType.objects.get_or_create( domain=domain_name, name=name, administrative=administrative, parent_type=parent_type, )[0] country = _make_loc_type(name="country", administrative=True) _make_loc_type(name="Central Medical Store", parent_type=country) _make_loc_type(name="Teaching Hospital", parent_type=country) region = _make_loc_type(name="region", administrative=True, parent_type=country) _make_loc_type(name="Regional Medical Store", parent_type=region) _make_loc_type(name="Regional Hospital", parent_type=region) district = _make_loc_type(name="district", administrative=True, parent_type=region) _make_loc_type(name="Clinic", parent_type=district) _make_loc_type(name="District Hospital", parent_type=district) _make_loc_type(name="Health Centre", parent_type=district) _make_loc_type(name="CHPS Facility", parent_type=district) _make_loc_type(name="Hospital", parent_type=district) _make_loc_type(name="Psychiatric Hospital", parent_type=district) _make_loc_type(name="Polyclinic", parent_type=district) _make_loc_type(name="facility", parent_type=district) generator.instantiate_accounting() account = BillingAccount.get_or_create_account_by_domain( domain.name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan( edition=SoftwarePlanEdition.ADVANCED) subscription = Subscription.new_domain_subscription( account, domain.name, plan) subscription.is_active = True subscription.save() ews_config = EWSGhanaConfig(enabled=True, domain=domain.name) ews_config.save() return domain
def setUp(self): super(TestDomainInvoiceFactory, self).setUp() self.invoice_start, self.invoice_end = get_previous_month_date_range() self.domain = generator.arbitrary_domain() self.account = BillingAccount.get_or_create_account_by_domain( domain=self.domain.name, created_by="TEST")[0] self.community = DefaultProductPlan.get_default_plan_version() generator.arbitrary_commcare_users_for_domain( self.domain.name, self.community.user_limit + 1) self.invoice_factory = DomainInvoiceFactory(self.invoice_start, self.invoice_end, self.domain)
def setup_subscription(cls, domain_name, software_plan): generator.bootstrap_test_software_plan_versions() plan = DefaultProductPlan.get_default_plan_version(edition=software_plan) account = BillingAccount.get_or_create_account_by_domain( domain_name, created_by="automated-test" + cls.__name__ )[0] subscription = Subscription.new_domain_subscription(account, domain_name, plan) subscription.is_active = True subscription.save() cls.__subscriptions = cls.__subscriptions or {} cls.__subscriptions[domain_name] = subscription cls.__accounts = cls.__accounts or {} cls.__accounts[domain_name] = account
def setUpClass(cls): super().setUpClass() cls.project = create_domain(DOMAIN_NAME) account, __ = BillingAccount.get_or_create_account_by_domain( DOMAIN_NAME, created_by=ADMIN_USER) plan = DefaultProductPlan.get_default_plan_version( edition=SoftwarePlanEdition.ADVANCED) Subscription.new_domain_subscription( account, DOMAIN_NAME, plan, web_user=ADMIN_USER, )
def setup_domain(cls, name): domain_obj = Domain(name=name, sms_mobile_worker_registration_enabled=True) domain_obj.save() plan = DefaultProductPlan.get_default_plan_version(edition=SoftwarePlanEdition.ADVANCED) account = BillingAccount.get_or_create_account_by_domain( name, created_by="automated-test-" + cls.__name__ )[0] subscription = Subscription.new_domain_subscription(account, name, plan) subscription.is_active = True subscription.save() domain_obj = Domain.get(domain_obj.get_id) return (domain_obj, account, subscription)
def setUpClass(cls): super(TestZapierCreateCaseAction, cls).setUpClass() cls.domain_object = Domain.get_or_create_with_name('fruit', is_active=True) cls.domain = cls.domain_object.name account = BillingAccount.get_or_create_account_by_domain(cls.domain, created_by="automated-test")[0] plan = DefaultProductPlan.get_default_plan_version(edition=SoftwarePlanEdition.STANDARD) subscription = Subscription.new_domain_subscription(account, cls.domain, plan) subscription.is_active = True subscription.save() cls.query_string = "?domain=fruit&case_type=watermelon&owner_id=test_user&user=test" cls.data = {'case_name': 'test1', 'price': '11'} cls.accessor = CaseAccessors(cls.domain) cls.user = WebUser.create(cls.domain, 'test', '******') api_key_object, _ = ApiKey.objects.get_or_create(user=cls.user.get_django_user()) cls.api_key = api_key_object.key
def setUpClass(cls): super().setUpClass() cls.domain = Domain(name="test-enterprise-001", is_active=True) cls.domain.save() cls.enterprise_admin = WebUser.create( cls.domain.name, generator.create_arbitrary_web_user_name(), 'testpwd', None, None) cls.other_domain_user = WebUser.create( cls.domain.name, generator.create_arbitrary_web_user_name(), 'testpwd', None, None) cls.account = BillingAccount.get_or_create_account_by_domain( cls.domain.name, created_by=cls.enterprise_admin.username)[0] cls.account.is_customer_billing_account = True cls.account.enterprise_admin_emails = [cls.enterprise_admin.username] cls.account.save()
def setUpClass(cls): super(TestOdataFeed, cls).setUpClass() cls.client = Client() cls.domain = Domain(name='test_domain') cls.domain.save() cls.web_user = WebUser.create(cls.domain.name, 'test_user', 'my_password') cls.account, _ = BillingAccount.get_or_create_account_by_domain( cls.domain.name, created_by='') plan_version = DefaultProductPlan.get_default_plan_version( SoftwarePlanEdition.STANDARD) cls.subscription = Subscription.new_domain_subscription( cls.account, cls.domain.name, plan_version)
def setUp(self): super(TestSubscriptionPermissionsChanges, self).setUp() self.project = Domain( name="test-sub-changes", is_active=True, ) self.project.save() self.admin_username = generator.create_arbitrary_web_user_name() self.account = BillingAccount.get_or_create_account_by_domain( self.project.name, created_by=self.admin_username)[0] self.advanced_plan = DefaultProductPlan.get_default_plan_version( edition=SoftwarePlanEdition.ADVANCED) self._init_pro_with_rb_plan_and_version()
def setUp(self): super(TestExportUtils, self).setUp() self.domain = Domain.get_or_create_with_name('test-export-utils', is_active=True) self.account, _ = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by='*****@*****.**') self.account.save() subscription = Subscription.new_domain_subscription( self.account, self.domain.name, DefaultProductPlan.get_default_plan_version( edition=SoftwarePlanEdition.ENTERPRISE), date_start=date.today() - timedelta(days=3)) subscription.is_active = True subscription.save()
def ensure_full_coverage(self, subscriptions): plan_version = DefaultProductPlan.get_default_plan_by_domain( self.domain, edition=SoftwarePlanEdition.COMMUNITY ).plan.get_version() if not plan_version.feature_charges_exist_for_domain(self.domain): return community_ranges = self.get_community_ranges(subscriptions) if not community_ranges: return do_not_invoice = any([s.do_not_invoice for s in subscriptions]) account = BillingAccount.get_or_create_account_by_domain( self.domain.name, created_by=self.__class__.__name__, created_by_invoicing=True)[0] if account.date_confirmed_extra_charges is None: logger.error( "[BILLING] " "Domain '%s' is going to get charged on " "Community, but they haven't formally acknowledged this. " "Someone on ops should reconcile this soon. To be on the " "safe side, we've marked the invoices as Do Not Invoice." % self.domain.name ) do_not_invoice = True if not BillingContactInfo.objects.filter(account=account).exists(): # No contact information exists for this account. # This shouldn't happen, but if it does, we can't continue # with the invoice generation. raise InvoiceError("No Billing Contact Info could be found " "for domain '%s'." % self.domain.name) # First check to make sure none of the existing subscriptions is set # to do not invoice. Let's be on the safe side and not send a # community invoice out, if that's the case. for c in community_ranges: # create a new community subscription for each # date range that the domain did not have a subscription community_subscription = Subscription( account=account, plan_version=plan_version, subscriber=self.subscriber, date_start=c[0], date_end=c[1], do_not_invoice=do_not_invoice, ) community_subscription.save() subscriptions.append(community_subscription)