|
39 | 39 | from app.models.mail import PASSWORD_RESET, PASSWORD_CHANGE, \ |
40 | 40 | PASSWORD_RESET_AND_VERIFY |
41 | 41 | 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 |
42 | 44 | from app.models.order import Order |
| 45 | +from app.models.ticket import Ticket |
43 | 46 | from app.models.user import User |
44 | 47 | from app.models.event_invoice import EventInvoice |
45 | 48 |
|
@@ -503,3 +506,49 @@ def resend_emails(): |
503 | 506 | "Only placed and completed orders have confirmation").respond() |
504 | 507 | else: |
505 | 508 | 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