Repository: fossasia/open-event-server

Issue:
Fix Invoice for Ticket Buyers
The ticket invoice for ticket buyers has a number of issues. Please resolve the list below:

* Fix "Post codes" - They do not show zeros at the beginning. If the postcode starts with a leading zero, e.g. 09853 show the leading zero.
* On the **top left** show the data of the ticket buyer
   * delete the number, amount, and discount code
   * Show Organisation [Company/Org of buyer]
   * Name [of buyer]
   * Address [of buyer]
   * Email [of buyer]
   * Tax ID [of buyer]
   * Invoice Date
   * Due Date: Upon Receipt
   * Payment mode. It currently always shows "None". It should show "Paypal, Credit Card, Cash, Purchase on Invoice etc."
   * Payment status: e.g. Order placed, Completed etc.
* On the **top right** of the invoice show the data and contact of organizers (it currently shows the data of ticket buyers)
   * Organisation [Company/Org of Organizer]
   * Name [of Organizer]
   * Address [of Organizer]
   * Phone [of Organizer]
   * Email [of Organizer]
   * Tax ID [of Organizer]
* **On table** 
   * Change "Name" to "Description"
   * Add date of event behind name of the event in the first column
   * Add discount code if any was used  in the first column
   * Move the column "subtotal" to the very right
   * Improve the tax display
      * Unify "VAT" and "Tax Amount" into a column "Tax" 
      * Display if tax is included or added on top of the rate before the tax info
      * Show the number with the percentage in brackets below
      * It should look like e.g. incl. `EUR 150.40 (19%)` or plus `USD 150.40 (6.5%)` Only show something behind the dot of percentages if it is not zero
   * Total: Show Subtotal, Tax and Total in the same way as it is shown on the organizer invoice
![Screenshot from 2021-09-03 18-18-30](https://user-images.githubusercontent.com/1583873/132037568-e1df6b9f-954c-4bb8-82fe-0054ec65445b.png)

* **Other**
   * Payments miss a space between currency and the number and they miss to show two digits after the dot, e.g. it should be EUR 12.50 instead of EUR12.50

* **Translations**: Ensure all text is translatable
![KIX Pro 18 Basis Administration-Invoice-a6faa42f-edcb-48b9-84f5-937ede0415dc](https://user-images.githubusercontent.com/1583873/132012994-b69112a6-9129-4632-a136-4115c6024d0b.png)


![rechnungcorrect](https://user-images.githubusercontent.com/1583873/126496655-e69c5b3c-890a-4a6a-9d82-2a7d0aeae833.jpg)



Discussion:
The following is missing:

* Same as in the web UI please add Total, Tax, Grand Total.
![Screenshot from 2021-09-23 04-59-00](https://user-images.githubusercontent.com/1583873/134448755-8d2eaa7d-9b82-49e5-aa3d-569da7b830ab.png)

Also check spacing:
1. Only one space between "USD" and "amount (e.g. 20.00) and 
2. a space in front of (gst)

![Screenshot from 2021-09-23 05-03-00](https://user-images.githubusercontent.com/1583873/134449130-096d26bf-7e0b-4e8b-8377-5125d64db1d1.png)

On the final PR, please also test phone numbers and post codes starting with 0. 
Please also take out "(net)" from the heading behind Sub-Total.
This does not seem to be completed yet. A test on https://test.eventyay.com shows that the data on the top right is still missing.

![Screenshot from 2021-10-04 19-06-18](https://user-images.githubusercontent.com/1583873/135894140-ae3205a5-68bf-4090-96e5-b0da044267a8.png)

The order overview web page shows the data correctly.

![Screenshot from 2021-10-04 19-05-30](https://user-images.githubusercontent.com/1583873/135894166-a4d156e1-4c8b-43ee-9a18-85f2df7b2ba2.png)

Furthermore on the top left it should show "Purchase on invoice" instead of only "invoice".

I'll test and fix it.
I got confused with owner info for organizer info. As we do not ask for name during tax info. Now it should work as expected.
@pc-beast Thank you, but this is still buggy: When I order from the same event several times filling in different info for the invoice - 
* The company information of a previous order shows up
* All ticket orders have the same invoice number ending 0001
* State and Country are missing from the organizer 

![Screenshot from 2021-10-04 22-28-44](https://user-images.githubusercontent.com/1583873/135920221-331f21bb-15a5-4f25-b070-a95e16dcfee7.png)

How to replicate the issue?
1. Make an order and fill in sample information of one company.
2. Make an order from the same account and fill in sample information of another company.

Outcome: Order 2 will show the same information and invoice number as order 1.
Remove 
![image](https://user-images.githubusercontent.com/56963647/136038183-2ce8a375-d0ff-41fc-9462-f87a259e24d2.png)
from invoice (just heading)

Resolving patch:
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
@@ -106,8 +106,6 @@ def create_pdf_tickets_for_holder(order):
         # create order invoices pdf
         order_tickets = OrderTicket.query.filter_by(order_id=order.id).all()
 
-        attendee = TicketHolder.query.filter_by(order_id=order.id).first()
-
         tickets = []
         for order_ticket in order_tickets:
             ticket = dict(
@@ -127,7 +125,6 @@ def create_pdf_tickets_for_holder(order):
                 event=order.event,
                 tax=order.event.tax,
                 order_tickets=order_tickets,
-                attendee=attendee,
                 event_starts_at=order.event.starts_at_tz.strftime('%d %B %Y'),
                 created_at=order.created_at.strftime('%d %B %Y'),
                 admin_info=admin_info,
diff --git a/app/models/event.py b/app/models/event.py
--- a/app/models/event.py
+++ b/app/models/event.py
@@ -2,6 +2,8 @@
 from argparse import Namespace
 from datetime import datetime
 
+from sqlalchemy.sql.expression import or_, and_
+
 import flask_login as login
 import pytz
 from flask import current_app
@@ -490,6 +492,15 @@ def notify_staff(self):
         """Who receive notifications about event"""
         return self.organizers + [self.owner]
 
+    @property
+    def tickets_placed_or_completed_count(self):
+        obj = (
+            db.session.query(Order.event_id)
+            .filter(and_(Order.event_id==self.id, or_(Order.status=='completed', Order.status=='placed')))
+            .join(TicketHolder)
+        )
+        return obj.count()
+
 
 @event.listens_for(Event, 'after_update')
 @event.listens_for(Event, 'after_insert')


Test patch: