-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support: Set discounted price instead percentage for customers. #29957
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
corporate/migrations/0043_remove_customer_default_discount_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Generated by Django 5.0.5 on 2024-05-03 06:50 | ||
|
||
from decimal import Decimal | ||
|
||
from django.db import migrations, models | ||
from django.db.backends.base.schema import BaseDatabaseSchemaEditor | ||
from django.db.migrations.state import StateApps | ||
|
||
# It's generally unsafe to import product code from migrations, | ||
# because the migration will be run with the **current** version of | ||
# that code, not the version at the time of the migration. But because | ||
# this migration will only be run for Zulip Cloud, this is OK. | ||
from corporate.lib.stripe import get_price_per_license | ||
amanagr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def calculate_discounted_price_per_license( | ||
original_price_per_license: int, discount: Decimal | ||
) -> int: | ||
# There are no fractional cents in Stripe, so round down to nearest integer. | ||
return int(float(original_price_per_license * (1 - discount / 100)) + 0.00001) | ||
|
||
|
||
def calculate_discounted_price(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None: | ||
"""Migrates existing customers with a configured default discount to | ||
instead have discounted prices for their configured required_plan_tier. | ||
|
||
Does not operate on CustomerPlan fields, since those are already | ||
resolved into a price. | ||
""" | ||
BILLING_SCHEDULE_ANNUAL = 1 | ||
BILLING_SCHEDULE_MONTHLY = 2 | ||
|
||
Customer = apps.get_model("corporate", "Customer") | ||
customers_to_update = [] | ||
for customer in Customer.objects.all(): | ||
if not customer.required_plan_tier or not customer.default_discount: | ||
continue | ||
|
||
annual_price_per_license = get_price_per_license( | ||
customer.required_plan_tier, BILLING_SCHEDULE_ANNUAL | ||
) | ||
customer.annual_discounted_price = calculate_discounted_price_per_license( | ||
annual_price_per_license, customer.default_discount | ||
) | ||
monthly_price_per_license = get_price_per_license( | ||
customer.required_plan_tier, BILLING_SCHEDULE_MONTHLY | ||
) | ||
customer.monthly_discounted_price = calculate_discounted_price_per_license( | ||
monthly_price_per_license, customer.default_discount | ||
) | ||
customers_to_update.append(customer) | ||
amanagr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print( | ||
f"\nChanging price for {customer.id}: {customer.default_discount} => {customer.annual_discounted_price}/{customer.monthly_discounted_price}" | ||
) | ||
|
||
Customer.objects.bulk_update( | ||
customers_to_update, ["annual_discounted_price", "monthly_discounted_price"] | ||
) | ||
amanagr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("corporate", "0042_invoice_is_created_for_free_trial_upgrade_and_more"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="customer", | ||
name="annual_discounted_price", | ||
field=models.IntegerField(default=0), | ||
), | ||
migrations.AddField( | ||
model_name="customer", | ||
name="monthly_discounted_price", | ||
field=models.IntegerField(default=0), | ||
), | ||
# Populate the new discounted price fields based on existing default discount. | ||
migrations.RunPython(calculate_discounted_price), | ||
migrations.RemoveField( | ||
model_name="customer", | ||
name="default_discount", | ||
), | ||
# This automatically converts the decimal to string so we don't need to do anything here. | ||
migrations.AlterField( | ||
model_name="customerplan", | ||
name="discount", | ||
field=models.TextField(null=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be basically a stale
discount
percent display value? I guess we could do a migration to clear those if it was a priority.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, we don't want to do anything here. I just wanted to note that even if there is no discount displayed for the customer in the support table, that doesn't mean there is no discount applied to them. I am not sure where to best reflect that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I guess that won't be valid after #29957 (comment)