Repository: fossasia/open-event-server

Issue:
Cannot edit ticket details
The editing of tickets is blocked with the info "Orders have been placed". As a matter of fact orders have not been placed. There are possibly pending orders though. However as filed in an issue elsewhere pending orders do not seem to expire. In any case they should not block the editing of tickets.

Expected:
* [ ] It should be possible to edit and delete tickets, that do not have "completed orders".
* [ ] It should also be possible to edit the ticket name at any time even when tickets were sold already

![screenshot from 2017-01-19 22-32-36](https://cloud.githubusercontent.com/assets/1583873/22126547/660990be-de98-11e6-905e-8e9577c40a2c.png)



Discussion:
@mariobehling right now the limitation is that there is pending orders. If expired orders, you can edit but for pending orders you can't. You want to allow edit for all case except "completed orders". How about a order is in "pending" state, someone is about to make payment when you make the changes. So he sees $5 ticket thinks cool and buys but you change it to $15 when he actually buys. How do we handle such situations then?
@SaptakS Firstly pending orders right now, never seem to expire and I see pending orders that are 19 days old in one event.

> How about a order is in "pending" state, someone is about to make payment when you make the changes. So he sees $5 ticket thinks cool and buys but you change it to $15 when he actually buys. 

This could happen, but then how often is this case actually happening? There could just be a quick message "We are sorry, but your ticket sale was not successful. Unfortunately the ticket is no longer available. Please check for other options."

Right now we have the issue, that tickets with small errors are piling up in e.g. the FOSSASIA event and we cannot edit, change or delete anything that is wrong even though there are no orders. So, easiest for now: 

* [ ] Allow changes, except for the change of the rate.
* [ ] Allow deletion, if no orders are completed

Do not allow "Deletion" if there are tickets sold (Completed), but still allow changes (except rate).
Ok. I will look into this. Also I will look into the auto expiring issue for tickets. Solving both together will do the trick I feel.

Resolving patch:
diff --git a/app/models/ticket.py b/app/models/ticket.py
--- a/app/models/ticket.py
+++ b/app/models/ticket.py
@@ -1,4 +1,4 @@
-from app.models.order import OrderTicket
+from app.models.order import OrderTicket, Order
 from . import db
 
 ticket_tags_table = db.Table('association', db.Model.metadata,
@@ -73,6 +73,21 @@ def has_order_tickets(self):
         count = get_count(OrderTicket.query.filter_by(ticket_id=self.id))
         return bool(count > 0)
 
+    def has_completed_order_tickets(self):
+        """Returns True if ticket has already placed orders.
+        Else False.
+        """
+        from app.helpers.helpers import get_count
+        order_tickets = OrderTicket.query.filter_by(ticket_id=self.id)
+
+        count = 0
+        for order_ticket in order_tickets:
+            order = Order.query.filter_by(id=order_ticket.order_id).first()
+            if order.status == "completed" or order.status == "placed":
+                count += 1
+
+        return bool(count > 0)
+
     def tags_csv(self):
         """Return list of Tags in CSV.
         """
@@ -108,6 +123,7 @@ def serialize(self):
             'max_order': self.max_order,
             'tags_string': '',
             'has_orders': self.has_order_tickets(),
+            'has_completed_orders': self.has_completed_order_tickets(),
             'absorb_fees': self.absorb_fees
         }
 


Test patch: