Repository: fossasia/open-event-server

Issue:
Incorrect calculation of discounts if cost is less than 1USD
If I enter 99% or 41.50USD discount of a 42USD ticket, the ticketing system does not calculate Cents. It is also not possible to give 20.50 USD discount.

Expected: Please ensure, Cents and other currencies, that use discounts that result in anything behind the fullstop (or comma) are calculated as well.


Discussion:

Resolving patch:
diff --git a/app/helpers/scheduled_jobs.py b/app/helpers/scheduled_jobs.py
--- a/app/helpers/scheduled_jobs.py
+++ b/app/helpers/scheduled_jobs.py
@@ -87,7 +87,7 @@ def send_event_fee_notification():
                 for order_ticket in order.tickets:
                     ticket = TicketingManager.get_ticket(order_ticket.ticket_id)
                     if order.paid_via != 'free' and order.amount > 0 and ticket.price > 0:
-                        fee = ticket.price * (get_fee(order.event.payment_currency) / 100)
+                        fee = ticket.price * (get_fee(order.event.payment_currency) / 100.0)
                         fee_total += fee
 
             if fee_total > 0:
@@ -96,7 +96,7 @@ def send_event_fee_notification():
                 if event.discount_code_id and event.discount_code:
                     r = relativedelta(datetime.utcnow(), event.created_at)
                     if r <= event.discount_code.max_quantity:
-                        new_invoice.amount = fee_total - (fee_total * (event.discount_code.value / 100))
+                        new_invoice.amount = fee_total - (fee_total * (event.discount_code.value / 100.0))
                         new_invoice.discount_code_id = event.discount_code_id
 
                 save_to_db(new_invoice)
diff --git a/app/helpers/ticketing.py b/app/helpers/ticketing.py
--- a/app/helpers/ticketing.py
+++ b/app/helpers/ticketing.py
@@ -244,15 +244,11 @@ def create_order(form, from_organizer=False):
                         amount += float(ticket_subtotals[index])
                     else:
                         amount += (order_ticket.ticket.price * order_ticket.quantity)
-                        if discount and str(order_ticket.ticket.id) in discount.tickets.split(","):
-                            if discount.type == "amount":
-                                total_discount += discount.value * order_ticket.quantity
-                            else:
-                                total_discount += discount.value * order_ticket.ticket.price *\
-                                                 order_ticket.quantity/100.0
-
-        if discount:
-            order.amount = max(amount - total_discount,0)
+
+        if discount and discount.type == "amount":
+            order.amount = max(amount-discount.value,0)
+        elif discount:
+            order.amount=amount-(discount.value*amount/100.0)
         else:
             order.amount = amount
 
diff --git a/app/models/discount_code.py b/app/models/discount_code.py
--- a/app/models/discount_code.py
+++ b/app/models/discount_code.py
@@ -11,7 +11,7 @@ class DiscountCode(db.Model):
 
     id = db.Column(db.Integer, primary_key=True)
     code = db.Column(db.String)
-    value = db.Column(db.Integer)
+    value = db.Column(db.Float)
     type = db.Column(db.String)
     is_active = db.Column(db.Boolean)
     tickets_number = db.Column(db.Integer)  # For event level discount this holds the max. uses


Test patch: