billing: Fix misuses of CustomerPlan.discount.

This commit is contained in:
Rishi Gupta 2019-01-24 17:14:07 -08:00
parent 8eac361fb5
commit 03c71dad17
2 changed files with 9 additions and 16 deletions

View File

@ -94,17 +94,12 @@ def next_renewal_date(plan: CustomerPlan) -> datetime:
def renewal_amount(plan: CustomerPlan) -> Optional[int]: # nocoverage: TODO
if plan.fixed_price is not None:
basis = plan.fixed_price
else:
return plan.fixed_price
last_ledger_entry = add_plan_renewal_to_license_ledger_if_needed(plan, timezone_now())
if last_ledger_entry.licenses_at_next_renewal is None:
return None
assert(plan.price_per_license is not None) # for mypy
basis = plan.price_per_license * last_ledger_entry.licenses_at_next_renewal
if plan.discount is None:
return basis
# TODO: figure out right thing to do with Decimal
return int(float(basis * (100 - plan.discount) / 100) + .00001)
return plan.price_per_license * last_ledger_entry.licenses_at_next_renewal
class BillingError(Exception):
# error messages
@ -313,13 +308,10 @@ def process_initial_upgrade(user: UserProfile, licenses: int, automanage_license
realm=realm, acting_user=user, event_time=billing_cycle_anchor,
event_type=RealmAuditLog.CUSTOMER_PLAN_CREATED,
extra_data=ujson.dumps(plan_params))
description = 'Zulip Standard'
if customer.default_discount is not None: # nocoverage: TODO
description += ' (%s%% off)' % (customer.default_discount,)
stripe.InvoiceItem.create(
currency='usd',
customer=customer.stripe_customer_id,
description=description,
description='Zulip Standard',
discountable=False,
period = {'start': datetime_to_timestamp(billing_cycle_anchor),
'end': datetime_to_timestamp(period_end)},

View File

@ -12,6 +12,7 @@ class Customer(models.Model):
stripe_customer_id = models.CharField(max_length=255, unique=True) # type: str
# Deprecated .. delete once everyone is migrated to new billing system
has_billing_relationship = models.BooleanField(default=False) # type: bool
# A percentage, like 85.
default_discount = models.DecimalField(decimal_places=4, max_digits=7, null=True) # type: Optional[Decimal]
def __str__(self) -> str:
@ -30,7 +31,7 @@ class CustomerPlan(models.Model):
price_per_license = models.IntegerField(null=True) # type: Optional[int]
fixed_price = models.IntegerField(null=True) # type: Optional[int]
# A percentage, like 85
# Discount that was applied. For display purposes only.
discount = models.DecimalField(decimal_places=4, max_digits=6, null=True) # type: Optional[Decimal]
billing_cycle_anchor = models.DateTimeField() # type: datetime.datetime