prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Do not show deleted orders in organiser ui and do not auto delete expired orders
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `app/helpers/scheduled_jobs.py`
  Content:
  ```
  1 from datetime import datetime, timedelta
  2 
  3 from dateutil.relativedelta import relativedelta
  4 from flask import url_for
  5 from sqlalchemy_continuum import transaction_class
  6 
  7 from app.helpers.data import DataManager, delete_from_db, save_to_db
  8 from app.helpers.data_getter import DataGetter
  9 from app.helpers.helpers import send_after_event, monthdelta, send_followup_email_for_monthly_fee_payment
  10 from app.helpers.helpers import send_email_for_expired_orders, send_email_for_monthly_fee_payment
  11 from app.helpers.payment import get_fee
  12 from app.helpers.ticketing import TicketingManager
  13 from app.models.event import Event
  14 from app.models.event_invoice import EventInvoice
  15 from app.models.order import Order
  16 from app.models.session import Session
  17 from app.models.user import User
  18 
  19 
  20 def empty_trash():
  21     from app import current_app as app
  22     with app.app_context():
  23         events = Event.query.filter_by(in_trash=True)
  24         users = User.query.filter_by(in_trash=True)
  25         sessions = Session.query.filter_by(in_trash=True)
  26         orders = Order.query.filter_by(status="deleted")
  27         pending_orders = Order.query.filter_by(status="pending")
  28         expired_orders = Order.query.filter_by(status="expired")
  29         for event in events:
  30             if datetime.now() - event.trash_date >= timedelta(days=30):
  31                 DataManager.delete_event(event.id)
  32 
  33         for user in users:
  34             if datetime.now() - user.trash_date >= timedelta(days=30):
  35                 transaction = transaction_class(Event)
  36                 transaction.query.filter_by(user_id=user.id).delete()
  37                 delete_from_db(user, "User deleted permanently")
  38 
  39         for session_ in sessions:
  40             if datetime.now() - session_.trash_date >= timedelta(days=30):
  41                 delete_from_db(session_, "Session deleted permanently")
  42 
  43         for order in orders:
  44             if datetime.now() - order.trashed_at >= timedelta(days=30):
  45                 delete_from_db(order, "Order deleted permanently")
  46 
  47         for pending_order in pending_orders:
  48             if datetime.now() - pending_order.created_at >= timedelta(days=3):
  49                 pending_order.status = "expired"
  50                 save_to_db(pending_order, "Pending order expired.")
  51 
  52         for expired_order in expired_orders:
  53             if datetime.now() - expired_order.created_at >= timedelta(days=6):
  54                 expired_order.status = "deleted"
  55                 expired_order.trashed_at = datetime.now()
  56                 save_to_db(expired_order, "Expired order deleted")
  57 
  58 
  59 def send_after_event_mail():
  60     from app import current_app as app
  61     with app.app_context():
  62         events = Event.query.all()
  63         for event in events:
  64             upcoming_events = DataGetter.get_upcoming_events()
  65             organizers = DataGetter.get_user_event_roles_by_role_name(
  66                 event.id, 'organizer')
  67             speakers = DataGetter.get_user_event_roles_by_role_name(event.id,
  68                                                                     'speaker')
  69             if datetime.now() > event.end_time:
  70                 for speaker in speakers:
  71                     send_after_event(speaker.user.email, event.id,
  72                                      upcoming_events)
  73                 for organizer in organizers:
  74                     send_after_event(organizer.user.email, event.id,
  75                                      upcoming_events)
  76 
  77 
  78 def send_mail_to_expired_orders():
  79     from app import current_app as app
  80     with app.app_context():
  81         orders = DataGetter.get_expired_orders()
  82         for order in orders:
  83             send_email_for_expired_orders(order.user.email, order.event.name, order.get_invoice_number(),
  84                                           url_for('ticketing.view_order_after_payment',
  85                                                   order_identifier=order.identifier, _external=True))
  86 
  87 
  88 def send_event_fee_notification():
  89     from app import current_app as app
  90     with app.app_context():
  91         events = Event.query.all()
  92         for event in events:
  93             latest_invoice = EventInvoice.filter_by(event_id=event.id).order_by(EventInvoice.created_at.desc()).first()
  94 
  95             if latest_invoice:
  96                 orders = Order.query \
  97                     .filter_by(event_id=event.id) \
  98                     .filter_by(status='completed') \
  99                     .filter(Order.completed_at > latest_invoice.created_at).all()
  100             else:
  101                 orders = Order.query.filter_by(event_id=event.id).filter_by(status='completed').all()
  102 
  103             fee_total = 0
  104             for order in orders:
  105                 for order_ticket in order.tickets:
  106                     ticket = TicketingManager.get_ticket(order_ticket.ticket_id)
  107                     if order.paid_via != 'free' and order.amount > 0 and ticket.price > 0:
  108                         fee = ticket.price * (get_fee(order.event.payment_currency) / 100.0)
  109                         fee_total += fee
  110 
  111             if fee_total > 0:
  112                 new_invoice = EventInvoice(amount=fee_total, event_id=event.id, user_id=event.creator_id)
  113 
  114                 if event.discount_code_id and event.discount_code:
  115                     r = relativedelta(datetime.utcnow(), event.created_at)
  116                     if r <= event.discount_code.max_quantity:
  117                         new_invoice.amount = fee_total - (fee_total * (event.discount_code.value / 100.0))
  118                         new_invoice.discount_code_id = event.discount_code_id
  119 
  120                 save_to_db(new_invoice)
  121                 prev_month = monthdelta(new_invoice.created_at, 1).strftime("%b %Y")  # Displayed as Aug 2016
  122                 send_email_for_monthly_fee_payment(new_invoice.user.email,
  123                                                    event.name,
  124                                                    prev_month,
  125                                                    new_invoice.amount,
  126                                                    url_for('event_invoicing.view_invoice',
  127                                                            invoice_identifier=new_invoice.identifier, _external=True))
  128 
  129 
  130 def send_event_fee_notification_followup():
  131     from app import current_app as app
  132     with app.app_context():
  133         incomplete_invoices = EventInvoice.query.filter(EventInvoice.status != 'completed').all()
  134         for incomplete_invoice in incomplete_invoices:
  135             if incomplete_invoice.amount > 0:
  136                 prev_month = monthdelta(incomplete_invoice.created_at, 1).strftime("%b %Y")  # Displayed as Aug 2016
  137                 send_followup_email_for_monthly_fee_payment(incomplete_invoice.user.email,
  138                                                             incomplete_invoice.event.name,
  139                                                             prev_month,
  140                                                             incomplete_invoice.amount,
  141                                                             url_for('event_invoicing.view_invoice',
  142                                                                     invoice_identifier=incomplete_invoice.identifier,
  143                                                                     _external=True))
  144
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/app/helpers/scheduled_jobs.py b/app/helpers/scheduled_jobs.py
  --- a/app/helpers/scheduled_jobs.py
  +++ b/app/helpers/scheduled_jobs.py
  @@ -23,9 +23,8 @@
           events = Event.query.filter_by(in_trash=True)
           users = User.query.filter_by(in_trash=True)
           sessions = Session.query.filter_by(in_trash=True)
  -        orders = Order.query.filter_by(status="deleted")
           pending_orders = Order.query.filter_by(status="pending")
  -        expired_orders = Order.query.filter_by(status="expired")
  +
           for event in events:
               if datetime.now() - event.trash_date >= timedelta(days=30):
                   DataManager.delete_event(event.id)
  @@ -40,21 +39,11 @@
               if datetime.now() - session_.trash_date >= timedelta(days=30):
                   delete_from_db(session_, "Session deleted permanently")
   
  -        for order in orders:
  -            if datetime.now() - order.trashed_at >= timedelta(days=30):
  -                delete_from_db(order, "Order deleted permanently")
  -
           for pending_order in pending_orders:
               if datetime.now() - pending_order.created_at >= timedelta(days=3):
                   pending_order.status = "expired"
                   save_to_db(pending_order, "Pending order expired.")
   
  -        for expired_order in expired_orders:
  -            if datetime.now() - expired_order.created_at >= timedelta(days=6):
  -                expired_order.status = "deleted"
  -                expired_order.trashed_at = datetime.now()
  -                save_to_db(expired_order, "Expired order deleted")
  -
   
   def send_after_event_mail():
       from app import current_app as app
  
verification_info: {"golden_diff": "diff --git a/app/helpers/scheduled_jobs.py b/app/helpers/scheduled_jobs.py\n--- a/app/helpers/scheduled_jobs.py\n+++ b/app/helpers/scheduled_jobs.py\n@@ -23,9 +23,8 @@\n         events = Event.query.filter_by(in_trash=True)\n         users = User.query.filter_by(in_trash=True)\n         sessions = Session.query.filter_by(in_trash=True)\n-        orders = Order.query.filter_by(status=\"deleted\")\n         pending_orders = Order.query.filter_by(status=\"pending\")\n-        expired_orders = Order.query.filter_by(status=\"expired\")\n+\n         for event in events:\n             if datetime.now() - event.trash_date >= timedelta(days=30):\n                 DataManager.delete_event(event.id)\n@@ -40,21 +39,11 @@\n             if datetime.now() - session_.trash_date >= timedelta(days=30):\n                 delete_from_db(session_, \"Session deleted permanently\")\n \n-        for order in orders:\n-            if datetime.now() - order.trashed_at >= timedelta(days=30):\n-                delete_from_db(order, \"Order deleted permanently\")\n-\n         for pending_order in pending_orders:\n             if datetime.now() - pending_order.created_at >= timedelta(days=3):\n                 pending_order.status = \"expired\"\n                 save_to_db(pending_order, \"Pending order expired.\")\n \n-        for expired_order in expired_orders:\n-            if datetime.now() - expired_order.created_at >= timedelta(days=6):\n-                expired_order.status = \"deleted\"\n-                expired_order.trashed_at = datetime.now()\n-                save_to_db(expired_order, \"Expired order deleted\")\n-\n \n def send_after_event_mail():\n     from app import current_app as app\n", "issue": "Do not show deleted orders in organiser ui and do not auto delete expired orders\n\n", "before_files": [{"content": "from datetime import datetime, timedelta\n\nfrom dateutil.relativedelta import relativedelta\nfrom flask import url_for\nfrom sqlalchemy_continuum import transaction_class\n\nfrom app.helpers.data import DataManager, delete_from_db, save_to_db\nfrom app.helpers.data_getter import DataGetter\nfrom app.helpers.helpers import send_after_event, monthdelta, send_followup_email_for_monthly_fee_payment\nfrom app.helpers.helpers import send_email_for_expired_orders, send_email_for_monthly_fee_payment\nfrom app.helpers.payment import get_fee\nfrom app.helpers.ticketing import TicketingManager\nfrom app.models.event import Event\nfrom app.models.event_invoice import EventInvoice\nfrom app.models.order import Order\nfrom app.models.session import Session\nfrom app.models.user import User\n\n\ndef empty_trash():\n    from app import current_app as app\n    with app.app_context():\n        events = Event.query.filter_by(in_trash=True)\n        users = User.query.filter_by(in_trash=True)\n        sessions = Session.query.filter_by(in_trash=True)\n        orders = Order.query.filter_by(status=\"deleted\")\n        pending_orders = Order.query.filter_by(status=\"pending\")\n        expired_orders = Order.query.filter_by(status=\"expired\")\n        for event in events:\n            if datetime.now() - event.trash_date >= timedelta(days=30):\n                DataManager.delete_event(event.id)\n\n        for user in users:\n            if datetime.now() - user.trash_date >= timedelta(days=30):\n                transaction = transaction_class(Event)\n                transaction.query.filter_by(user_id=user.id).delete()\n                delete_from_db(user, \"User deleted permanently\")\n\n        for session_ in sessions:\n            if datetime.now() - session_.trash_date >= timedelta(days=30):\n                delete_from_db(session_, \"Session deleted permanently\")\n\n        for order in orders:\n            if datetime.now() - order.trashed_at >= timedelta(days=30):\n                delete_from_db(order, \"Order deleted permanently\")\n\n        for pending_order in pending_orders:\n            if datetime.now() - pending_order.created_at >= timedelta(days=3):\n                pending_order.status = \"expired\"\n                save_to_db(pending_order, \"Pending order expired.\")\n\n        for expired_order in expired_orders:\n            if datetime.now() - expired_order.created_at >= timedelta(days=6):\n                expired_order.status = \"deleted\"\n                expired_order.trashed_at = datetime.now()\n                save_to_db(expired_order, \"Expired order deleted\")\n\n\ndef send_after_event_mail():\n    from app import current_app as app\n    with app.app_context():\n        events = Event.query.all()\n        for event in events:\n            upcoming_events = DataGetter.get_upcoming_events()\n            organizers = DataGetter.get_user_event_roles_by_role_name(\n                event.id, 'organizer')\n            speakers = DataGetter.get_user_event_roles_by_role_name(event.id,\n                                                                    'speaker')\n            if datetime.now() > event.end_time:\n                for speaker in speakers:\n                    send_after_event(speaker.user.email, event.id,\n                                     upcoming_events)\n                for organizer in organizers:\n                    send_after_event(organizer.user.email, event.id,\n                                     upcoming_events)\n\n\ndef send_mail_to_expired_orders():\n    from app import current_app as app\n    with app.app_context():\n        orders = DataGetter.get_expired_orders()\n        for order in orders:\n            send_email_for_expired_orders(order.user.email, order.event.name, order.get_invoice_number(),\n                                          url_for('ticketing.view_order_after_payment',\n                                                  order_identifier=order.identifier, _external=True))\n\n\ndef send_event_fee_notification():\n    from app import current_app as app\n    with app.app_context():\n        events = Event.query.all()\n        for event in events:\n            latest_invoice = EventInvoice.filter_by(event_id=event.id).order_by(EventInvoice.created_at.desc()).first()\n\n            if latest_invoice:\n                orders = Order.query \\\n                    .filter_by(event_id=event.id) \\\n                    .filter_by(status='completed') \\\n                    .filter(Order.completed_at > latest_invoice.created_at).all()\n            else:\n                orders = Order.query.filter_by(event_id=event.id).filter_by(status='completed').all()\n\n            fee_total = 0\n            for order in orders:\n                for order_ticket in order.tickets:\n                    ticket = TicketingManager.get_ticket(order_ticket.ticket_id)\n                    if order.paid_via != 'free' and order.amount > 0 and ticket.price > 0:\n                        fee = ticket.price * (get_fee(order.event.payment_currency) / 100.0)\n                        fee_total += fee\n\n            if fee_total > 0:\n                new_invoice = EventInvoice(amount=fee_total, event_id=event.id, user_id=event.creator_id)\n\n                if event.discount_code_id and event.discount_code:\n                    r = relativedelta(datetime.utcnow(), event.created_at)\n                    if r <= event.discount_code.max_quantity:\n                        new_invoice.amount = fee_total - (fee_total * (event.discount_code.value / 100.0))\n                        new_invoice.discount_code_id = event.discount_code_id\n\n                save_to_db(new_invoice)\n                prev_month = monthdelta(new_invoice.created_at, 1).strftime(\"%b %Y\")  # Displayed as Aug 2016\n                send_email_for_monthly_fee_payment(new_invoice.user.email,\n                                                   event.name,\n                                                   prev_month,\n                                                   new_invoice.amount,\n                                                   url_for('event_invoicing.view_invoice',\n                                                           invoice_identifier=new_invoice.identifier, _external=True))\n\n\ndef send_event_fee_notification_followup():\n    from app import current_app as app\n    with app.app_context():\n        incomplete_invoices = EventInvoice.query.filter(EventInvoice.status != 'completed').all()\n        for incomplete_invoice in incomplete_invoices:\n            if incomplete_invoice.amount > 0:\n                prev_month = monthdelta(incomplete_invoice.created_at, 1).strftime(\"%b %Y\")  # Displayed as Aug 2016\n                send_followup_email_for_monthly_fee_payment(incomplete_invoice.user.email,\n                                                            incomplete_invoice.event.name,\n                                                            prev_month,\n                                                            incomplete_invoice.amount,\n                                                            url_for('event_invoicing.view_invoice',\n                                                                    invoice_identifier=incomplete_invoice.identifier,\n                                                                    _external=True))\n", "path": "app/helpers/scheduled_jobs.py"}], "after_files": [{"content": "from datetime import datetime, timedelta\n\nfrom dateutil.relativedelta import relativedelta\nfrom flask import url_for\nfrom sqlalchemy_continuum import transaction_class\n\nfrom app.helpers.data import DataManager, delete_from_db, save_to_db\nfrom app.helpers.data_getter import DataGetter\nfrom app.helpers.helpers import send_after_event, monthdelta, send_followup_email_for_monthly_fee_payment\nfrom app.helpers.helpers import send_email_for_expired_orders, send_email_for_monthly_fee_payment\nfrom app.helpers.payment import get_fee\nfrom app.helpers.ticketing import TicketingManager\nfrom app.models.event import Event\nfrom app.models.event_invoice import EventInvoice\nfrom app.models.order import Order\nfrom app.models.session import Session\nfrom app.models.user import User\n\n\ndef empty_trash():\n    from app import current_app as app\n    with app.app_context():\n        events = Event.query.filter_by(in_trash=True)\n        users = User.query.filter_by(in_trash=True)\n        sessions = Session.query.filter_by(in_trash=True)\n        pending_orders = Order.query.filter_by(status=\"pending\")\n\n        for event in events:\n            if datetime.now() - event.trash_date >= timedelta(days=30):\n                DataManager.delete_event(event.id)\n\n        for user in users:\n            if datetime.now() - user.trash_date >= timedelta(days=30):\n                transaction = transaction_class(Event)\n                transaction.query.filter_by(user_id=user.id).delete()\n                delete_from_db(user, \"User deleted permanently\")\n\n        for session_ in sessions:\n            if datetime.now() - session_.trash_date >= timedelta(days=30):\n                delete_from_db(session_, \"Session deleted permanently\")\n\n        for pending_order in pending_orders:\n            if datetime.now() - pending_order.created_at >= timedelta(days=3):\n                pending_order.status = \"expired\"\n                save_to_db(pending_order, \"Pending order expired.\")\n\n\ndef send_after_event_mail():\n    from app import current_app as app\n    with app.app_context():\n        events = Event.query.all()\n        for event in events:\n            upcoming_events = DataGetter.get_upcoming_events()\n            organizers = DataGetter.get_user_event_roles_by_role_name(\n                event.id, 'organizer')\n            speakers = DataGetter.get_user_event_roles_by_role_name(event.id,\n                                                                    'speaker')\n            if datetime.now() > event.end_time:\n                for speaker in speakers:\n                    send_after_event(speaker.user.email, event.id,\n                                     upcoming_events)\n                for organizer in organizers:\n                    send_after_event(organizer.user.email, event.id,\n                                     upcoming_events)\n\n\ndef send_mail_to_expired_orders():\n    from app import current_app as app\n    with app.app_context():\n        orders = DataGetter.get_expired_orders()\n        for order in orders:\n            send_email_for_expired_orders(order.user.email, order.event.name, order.get_invoice_number(),\n                                          url_for('ticketing.view_order_after_payment',\n                                                  order_identifier=order.identifier, _external=True))\n\n\ndef send_event_fee_notification():\n    from app import current_app as app\n    with app.app_context():\n        events = Event.query.all()\n        for event in events:\n            latest_invoice = EventInvoice.filter_by(event_id=event.id).order_by(EventInvoice.created_at.desc()).first()\n\n            if latest_invoice:\n                orders = Order.query \\\n                    .filter_by(event_id=event.id) \\\n                    .filter_by(status='completed') \\\n                    .filter(Order.completed_at > latest_invoice.created_at).all()\n            else:\n                orders = Order.query.filter_by(event_id=event.id).filter_by(status='completed').all()\n\n            fee_total = 0\n            for order in orders:\n                for order_ticket in order.tickets:\n                    ticket = TicketingManager.get_ticket(order_ticket.ticket_id)\n                    if order.paid_via != 'free' and order.amount > 0 and ticket.price > 0:\n                        fee = ticket.price * (get_fee(order.event.payment_currency) / 100.0)\n                        fee_total += fee\n\n            if fee_total > 0:\n                new_invoice = EventInvoice(amount=fee_total, event_id=event.id, user_id=event.creator_id)\n\n                if event.discount_code_id and event.discount_code:\n                    r = relativedelta(datetime.utcnow(), event.created_at)\n                    if r <= event.discount_code.max_quantity:\n                        new_invoice.amount = fee_total - (fee_total * (event.discount_code.value / 100.0))\n                        new_invoice.discount_code_id = event.discount_code_id\n\n                save_to_db(new_invoice)\n                prev_month = monthdelta(new_invoice.created_at, 1).strftime(\"%b %Y\")  # Displayed as Aug 2016\n                send_email_for_monthly_fee_payment(new_invoice.user.email,\n                                                   event.name,\n                                                   prev_month,\n                                                   new_invoice.amount,\n                                                   url_for('event_invoicing.view_invoice',\n                                                           invoice_identifier=new_invoice.identifier, _external=True))\n\n\ndef send_event_fee_notification_followup():\n    from app import current_app as app\n    with app.app_context():\n        incomplete_invoices = EventInvoice.query.filter(EventInvoice.status != 'completed').all()\n        for incomplete_invoice in incomplete_invoices:\n            if incomplete_invoice.amount > 0:\n                prev_month = monthdelta(incomplete_invoice.created_at, 1).strftime(\"%b %Y\")  # Displayed as Aug 2016\n                send_followup_email_for_monthly_fee_payment(incomplete_invoice.user.email,\n                                                            incomplete_invoice.event.name,\n                                                            prev_month,\n                                                            incomplete_invoice.amount,\n                                                            url_for('event_invoicing.view_invoice',\n                                                                    invoice_identifier=incomplete_invoice.identifier,\n                                                                    _external=True))\n", "path": "app/helpers/scheduled_jobs.py"}]}