12334-generic-products-name-in-invoice #18

Merged
mravi merged 5 commits from 12334-generic-products-name-in-invoice into master 2024-03-22 11:25:49 +00:00
3 changed files with 68 additions and 7 deletions

View file

@ -0,0 +1,54 @@
from django.core.management.base import BaseCommand
from datacenterlight.tasks import handle_metadata_and_emails
from datacenterlight.models import StripePlan
from opennebula_api.models import OpenNebulaManager
from membership.models import CustomUser
from hosting.models import GenericProduct
import logging
import json
import sys
import stripe
logger = logging.getLogger(__name__)
class Command(BaseCommand):
help = '''Stripe plans created before version 3.4 saved the plan name like generic-{subscription_id}-amount. This
command aims at replacing this with the actual product name
'''
def handle(self, *args, **options):
cnt = 0
self.stdout.write(
self.style.SUCCESS(
'In Fix generic stripe plan product names'
)
)
plans_to_change = StripePlan.objects.filter(stripe_plan_id__startswith='generic')
for plan in plans_to_change:
response = input("Press 'y' to continue: ")
# Check if the user entered 'y'
if response.lower() == 'y':
plan_name = plan.stripe_plan_id
first_index_hyphen = plan_name.index("-") + 1
product_id = plan_name[
first_index_hyphen:(plan_name[first_index_hyphen:].index("-")) + first_index_hyphen]
gp = GenericProduct.objects.get(id=product_id)
if gp:
cnt += 1
# update stripe
sp = stripe.Plan.retrieve(plan_name)
pr = stripe.Product.retrieve(sp.product)
pr.name = gp.product_name
pr.save()
# update local
spl = StripePlan.objects.get(stripe_plan_id=plan_name)
spl.stripe_plan_name = gp.product_name
spl.save()
print("%s. %s => %s" % (cnt, plan_name, gp.product_name))
else:
print("Invalid input. Please try again.")
sys.exit()
print("Done")

View file

@ -144,7 +144,7 @@ def get_line_item_from_stripe_invoice(invoice):
""".format(
vm_id=vm_id if vm_id > 0 else "",
ip_addresses=mark_safe(get_ip_addresses(vm_id)) if vm_id > 0 else
mark_safe(get_product_name(plan_name)),
mark_safe(get_product_name(plan_name)) if plan_name.startswith("generic-") else plan_name,
period=mark_safe("%s — %s" % (
datetime.datetime.fromtimestamp(start_date).strftime('%Y-%m-%d'),
datetime.datetime.fromtimestamp(end_date).strftime('%Y-%m-%d'))),
@ -160,8 +160,7 @@ def get_product_name(plan_name):
product_name = ""
if plan_name and plan_name.startswith("generic-"):
first_index_hyphen = plan_name.index("-") + 1
product_id = plan_name[first_index_hyphen:
(plan_name[first_index_hyphen:].index("-")) + first_index_hyphen]
product_id = plan_name[first_index_hyphen:(plan_name[first_index_hyphen:].index("-")) + first_index_hyphen]
try:
product = GenericProduct.objects.get(id=product_id)
product_name = product.product_name

View file

@ -42,6 +42,8 @@ from .utils import (
get_cms_integration, create_vm, clear_all_session_vars, validate_vat_number
)
from datacenterlight.templatetags.custom_tags import get_product_name
logger = logging.getLogger(__name__)
@ -900,15 +902,21 @@ class OrderConfirmationView(DetailView, FormView):
2
)
)
plan_name = "generic-{0}-{1:.2f}".format(
stripe_plan_id = "generic-{0}-{1:.2f}".format(
request.session['generic_payment_details']['product_id'],
amount_to_be_charged
)
stripe_plan_id = plan_name
try:
product = GenericProduct.objects.get(id=request.session['generic_payment_details']['product_id'])
plan_name = product.product_name
except Exception as ex:
logger.debug("Error {}" % str(ex))
plan_name = get_product_name(stripe_plan_id)
recurring_interval = request.session['generic_payment_details']['recurring_interval']
if recurring_interval == "year":
plan_name = "{}-yearly".format(plan_name)
stripe_plan_id = plan_name
stripe_plan_id = "{}-yearly".format(stripe_plan_id)
plan_name = "{} (yearly)".format(plan_name)
logger.debug("Plan name = {}, Stripe Plan id = {}".format(plan_name, stripe_plan_id))
else:
template = request.session.get('template')
specs = request.session.get('specs')