Skip to content

Commit 8eb03ed

Browse files
feat: API to fetch total order amount by passing tickets and discount code
1 parent 250b920 commit 8eb03ed

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

app/api/auth.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@
3939
from app.models.mail import PASSWORD_RESET, PASSWORD_CHANGE, \
4040
PASSWORD_RESET_AND_VERIFY
4141
from app.models.notification import PASSWORD_CHANGE as PASSWORD_CHANGE_NOTIF
42+
from app.models.discount_code import DiscountCode
43+
from app.models.event import Event
4244
from app.models.order import Order
45+
from app.models.ticket import Ticket
4346
from app.models.user import User
4447
from app.models.event_invoice import EventInvoice
4548

@@ -503,3 +506,49 @@ def resend_emails():
503506
"Only placed and completed orders have confirmation").respond()
504507
else:
505508
return ForbiddenError({'source': ''}, "Co-Organizer Access Required").respond()
509+
510+
511+
@ticket_blueprint.route('/orders/calculate-amount', methods=['POST'])
512+
@jwt_required
513+
def calculate_amount():
514+
data = request.json['data']
515+
tickets = data['tickets']
516+
discount_code = None
517+
tax = None
518+
taxType = True
519+
event = None
520+
if 'event' in data:
521+
event_identifier = data['event']
522+
event = safe_query(db, Event, 'identifier', event_identifier, 'identifier')
523+
else:
524+
return UnprocessableEntityError({'source': 'data/event'},
525+
"Event info missing").respond()
526+
527+
if 'discount-code' in data:
528+
discount_code_id = data['discount-code']
529+
discount_code = safe_query(db, DiscountCode, 'id', discount_code_id, 'id')
530+
if event.tax:
531+
tax = event.tax
532+
taxType = tax.is_tax_included_in_price
533+
534+
amount = 0.0
535+
for ticket_info in tickets:
536+
ticket_identifier = ticket_info['id']
537+
quantity = ticket_info['quantity']
538+
ticket = safe_query(db, Ticket, 'id', ticket_identifier, 'id')
539+
if ticket.event.id != event.id:
540+
return UnprocessableEntityError({'source': 'data/tickets'},
541+
"Invalid Ticket").respond()
542+
price = ticket.price
543+
544+
if tax and not taxType:
545+
price = price * (100 + tax.rate)/100
546+
if discount_code:
547+
for code in ticket.discount_codes:
548+
if discount_code.id == code.id:
549+
if code.type == 'amount':
550+
price = price - code.value
551+
else:
552+
price = price * (100 - code.value)/100
553+
amount = amount + price * quantity
554+
return jsonify(amount=amount)

0 commit comments

Comments
 (0)