-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support: Set discounted price instead percentage for customers.
This allows us to set the price of a plan exactly as discussed with the customer.
- Loading branch information
Showing
26 changed files
with
704 additions
and
282 deletions.
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
76 changes: 76 additions & 0 deletions
76
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,76 @@ | ||
# 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 | ||
|
||
from corporate.lib.stripe import get_price_per_license | ||
|
||
|
||
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: | ||
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) | ||
|
||
Customer.objects.bulk_update( | ||
customers_to_update, ["annual_discounted_price", "monthly_discounted_price"] | ||
) | ||
|
||
|
||
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.