Repository: fossasia/open-event-server

Issue:
Generate Order Invoice properly(akin to Tickets)
**Describe the bug**
The order invoice which is currently being generated is a screenshot of the page. Instead of this, we need to replace this with a generation of a proper PDF, similar to how the tickets are generated.

**To Reproduce**
Steps to reproduce the behavior:
1. Go to any event ticket
2. Click on Order Invoice

**Expected behavior**
A page should be printed with proper fields, rather than a screenshot.

**Additional context**
Working on this.


Discussion:
@CosmicCoder96 What are the fields which we exactly need to include?
As of now, the ones we have _tried_ to incorporate on the frontend. See the current invoice structure, also all the billing info.

Resolving patch:
diff --git a/app/api/auth.py b/app/api/auth.py
--- a/app/api/auth.py
+++ b/app/api/auth.py
@@ -304,3 +304,24 @@ def ticket_attendee_authorized(order_identifier):
             return ForbiddenError({'source': ''}, 'Unauthorized Access').respond()
     else:
         return ForbiddenError({'source': ''}, 'Authentication Required to access ticket').respond()
+
+
+@ticket_blueprint.route('/orders/invoices/<string:order_identifier>')
+@jwt_required()
+def order_invoices(order_identifier):
+    if current_user:
+        try:
+            order = Order.query.filter_by(identifier=order_identifier).first()
+            user_id = order.user.id
+        except NoResultFound:
+            return NotFoundError({'source': ''}, 'Order Invoice not found').respond()
+        if current_user.id == user_id:
+            key = UPLOAD_PATHS['pdf']['order'].format(identifier=order_identifier)
+            file_path = '../generated/invoices/{}/{}/'.format(key, generate_hash(key)) + order_identifier + '.pdf'
+            response = make_response(send_file(file_path))
+            response.headers['Content-Disposition'] = 'attachment; filename=invoice-%s.zip' % order_identifier
+            return response
+        else:
+            return ForbiddenError({'source': ''}, 'Unauthorized Access').respond()
+    else:
+        return ForbiddenError({'source': ''}, 'Authentication Required to access Invoice').respond()
diff --git a/app/api/helpers/order.py b/app/api/helpers/order.py
--- a/app/api/helpers/order.py
+++ b/app/api/helpers/order.py
@@ -47,13 +47,14 @@ def set_expiry_for_order(order, override=False):
 
 def create_pdf_tickets_for_holder(order):
     """
-    Create tickets for the holders of an order.
+    Create tickets and invoices for the holders of an order.
     :param order: The order for which to create tickets for.
     """
     if order.status == 'completed':
         pdf = create_save_pdf(render_template('pdf/ticket_purchaser.html', order=order),
                               UPLOAD_PATHS['pdf']['ticket_attendee'],
                               dir_path='/static/uploads/pdf/tickets/', identifier=order.identifier, upload_dir='generated/tickets/')
+
         order.tickets_pdf_url = pdf
 
         for holder in order.ticket_holders:
@@ -67,6 +68,11 @@ def create_pdf_tickets_for_holder(order):
                 pdf = order.tickets_pdf_url
             holder.pdf_url = pdf
             save_to_db(holder)
+
+        # create order invoices pdf
+        create_save_pdf(render_template('pdf/order_invoice.html', order=order, event=order.event),
+                        UPLOAD_PATHS['pdf']['order'], dir_path='/static/uploads/pdf/tickets/',
+                        identifier=order.identifier, upload_dir='generated/invoices/')
         save_to_db(order)
 
 
diff --git a/app/api/helpers/storage.py b/app/api/helpers/storage.py
--- a/app/api/helpers/storage.py
+++ b/app/api/helpers/storage.py
@@ -79,7 +79,8 @@
         'system_image': 'event_topic/{event_topic_id}/system_image'
     },
     'pdf': {
-        'ticket_attendee': 'attendees/tickets/pdf/{identifier}'
+        'ticket_attendee': 'attendees/tickets/pdf/{identifier}',
+        'order': 'orders/invoices/pdf/{identifier}'
     }
 }
 


Test patch: