Repository: fossasia/open-event-server

Issue:
Access codes not working - giving error "Promotional Code not valid"
After the organizer has set up an access code and new attendees make an order with this access code, the error message comes up "The promotional code entered is not valid. No offer has been applied to this order."

Expected: User can make an order of the hidden ticket.

![screenshot from 2017-01-18 22 26 48](https://cloud.githubusercontent.com/assets/1658742/22083694/7dada852-ddcd-11e6-885e-b8fb98c64274.png)



Discussion:
The logic that needs to be implemented is:
a) If an "Access Code" exists, it should allow the order and display "You access code is applied an your and you can make this order now."
b) If a "Discount Code" exists" the system should act as of now.
c) If an "Access Code" and a "Discount Code" with the same name exists, both Access Code and Discount Code should be applied to tickets and the relevant messages displayed.
@mariobehling @hpdang I'll look into it.
@magdalenesuo the discount calculation in the pre_payment_page has again been broken. Please fix that also. Thanks.

Resolving patch:
diff --git a/app/helpers/ticketing.py b/app/helpers/ticketing.py
--- a/app/helpers/ticketing.py
+++ b/app/helpers/ticketing.py
@@ -240,16 +240,24 @@ def create_order(form, from_organizer=False):
         discount=None
         if ticket_discount:
             discount=TicketingManager.get_discount_code(form.get('event_id'), form.get('promo_code',''))
-            if not discount:
-                flash('The promotional code entered is not valid. No offer has been applied to this order.', 'danger')
-            else:
+            access_code = TicketingManager.get_access_code(form.get('event_id'), form.get('promo_code', ''))
+            if access_code and discount:
                 order.discount_code = discount
-                flash('The promotional code entered is valid.offer has been applied to this order.', 'success')
+                flash('Both access code and discount code has been applied. You can make this order now.', 'success')
+            elif discount:
+                order.discount_code = discount
+                flash('The promotional code entered is valid. Offer has been applied to this order.', 'success')
+            elif access_code:
+                flash('Your access code is applied and you can make this order now.', 'success')
+            else:
+                flash('The promotional code entered is not valid. No offer has been applied to this order.', 'danger')
+                
         ticket_subtotals = []
         if from_organizer:
             ticket_subtotals = form.getlist('ticket_subtotals[]')
 
         amount = 0
+        total_discount = 0
         fees = DataGetter.get_fee_settings_by_currency(DataGetter.get_event(order.event_id).payment_currency)
         for index, id in enumerate(ticket_ids):
             if not string_empty(id) and int(ticket_quantity[index]) > 0:
@@ -262,6 +270,13 @@ def create_order(form, from_organizer=False):
                     if from_organizer:
                         amount += float(ticket_subtotals[index])
                     else:
+                        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 order_ticket.ticket.absorb_fees or not fees:
                             amount += (order_ticket.ticket.price * order_ticket.quantity)
                         else:
@@ -271,8 +286,8 @@ def create_order(form, from_organizer=False):
                             else:
                                 amount += (order_ticket.ticket.price * order_ticket.quantity) + order_fee
 
-        if discount and discount.type == "amount":
-            order.amount = max(amount-discount.value, 0)
+        if discount:
+            order.amount = max(amount - total_discount,0)
         elif discount:
             order.amount = amount-(discount.value*amount/100.0)
         else:


Test patch: