Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implementation of event invoice generation & calculation #6121

Merged
merged 2 commits into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from app.api.helpers.auth import AuthManager
from app.api.helpers.scheduled_jobs import send_after_event_mail, send_event_fee_notification, \
send_event_fee_notification_followup, change_session_state_on_event_completion, \
expire_pending_tickets
expire_pending_tickets, send_monthly_event_invoice
from app.models.event import Event
from app.models.role_invite import RoleInvite
from app.views.healthcheck import health_check_celery, health_check_db, health_check_migrations, check_migrations
Expand Down Expand Up @@ -242,6 +242,7 @@ def update_sent_state(sender=None, headers=None, **kwargs):
scheduler.add_job(send_event_fee_notification_followup, 'cron', day=15)
scheduler.add_job(change_session_state_on_event_completion, 'cron', hour=5, minute=30)
scheduler.add_job(expire_pending_tickets, 'cron', minute=45)
scheduler.add_job(send_monthly_event_invoice, 'cron', day=1, month='1-12')
scheduler.start()


Expand Down
21 changes: 21 additions & 0 deletions app/api/helpers/event_invoices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import datetime

from app.models.event_invoice import EventInvoice


def fetch_event_invoices(invoice_status):
"""
Helper function to fetch event invoices based on status
"""

if invoice_status == 'due':
event_invoices = EventInvoice.query.filter(EventInvoice.created_at + datetime.timedelta(days=30) <=
datetime.datetime.now(),
EventInvoice.paid_via is None).all()
elif invoice_status == 'paid':
event_invoices = EventInvoice.query.filter(EventInvoice.paid_via is not None).all()
elif invoice_status == 'upcoming':
event_invoices = EventInvoice.query.filter(EventInvoice.created_at + datetime.timedelta(days=30) >
datetime.datetime.now(),
EventInvoice.paid_via is None).all()
return event_invoices
33 changes: 32 additions & 1 deletion app/api/helpers/scheduled_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytz
from dateutil.relativedelta import relativedelta
from flask import render_template

from app.api.helpers.db import safe_query, save_to_db
from app.api.helpers.mail import send_email_after_event, send_email_for_monthly_fee_payment, \
Expand All @@ -10,14 +11,17 @@
send_notif_after_event
from app.api.helpers.query import get_upcoming_events, get_user_event_roles_by_role_name
from app.api.helpers.utilities import monthdelta
from app.api.helpers.files import create_save_pdf
from app.api.helpers.storage import UPLOAD_PATHS
from app.models import db
from app.models.event import Event
from app.models.event_invoice import EventInvoice
from app.models.order import Order
from app.models.speaker import Speaker
from app.models.session import Session
from app.models.ticket import Ticket
from app.models.ticket_fee import get_fee
from app.models.ticket_fee import TicketFees, get_fee

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redefinition of unused 'TicketFees' from line 19

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redefinition of unused 'TicketFees' from line 22


from app.settings import get_settings


Expand Down Expand Up @@ -154,3 +158,30 @@ def expire_pending_tickets():
(Order.created_at + datetime.timedelta(minutes=30)) <= datetime.datetime.now()).\
update({'status': 'expired'})
db.session.commit()


def send_monthly_event_invoice():
from app import current_app as app
with app.app_context():
events = Event.query.all()
for event in events:
# calculate net & gross revenues
currency = event.payment_currency
ticket_fee_object = db.session.query(TicketFees).filter_by(currency=currency).one()
ticket_fee_percentage = ticket_fee_object.service_fee

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local variable 'ticket_fee_percentage' is assigned to but never used

ticket_fee_maximum = ticket_fee_object.maximum_fee

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local variable 'ticket_fee_maximum' is assigned to but never used

orders = Order.query.filter_by(event=event).all()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local variable 'orders' is assigned to but never used

gross_revenue = event.calc_monthly_revenue()
ticket_fees = event.tickets_sold * ticket_fee_percentage
if ticket_fees > ticket_fee_maximum:
ticket_fees = ticket_fee_maximum
net_revenue = gross_revenue - ticket_fees

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local variable 'net_revenue' is assigned to but never used

# save invoice as pdf
pdf = create_save_pdf(render_template('pdf/event_invoice.html', orders=orders,
ticket_fee_object=ticket_fee_object, gross_revenue=gross_revenue,
net_revenue=net_revenue), UPLOAD_PATHS['pdf']['event_invoice'],
dir_path='/static/uploads/pdf/event_invoices/', identifier=event.identifier)
# save event_invoice info to DB

event_invoice = EventInvoice(amount=net_revenue, invoice_pdf_url=pdf, event_id=event.id)
save_to_db(event_invoice)
3 changes: 2 additions & 1 deletion app/api/helpers/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
'pdf': {
'ticket_attendee': 'attendees/tickets/pdf/{identifier}',
'order': 'orders/invoices/pdf/{identifier}',
'tickets_all': 'orders/tickets/pdf/{identifier}'
'tickets_all': 'orders/tickets/pdf/{identifier}',
'event_invoice': 'events/organizer/invoices/pdf/{identifier}'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SyntaxError: invalid syntax

}
}

Expand Down
10 changes: 10 additions & 0 deletions app/models/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,16 @@ def calc_revenue(self):
revenue = 0
return revenue

def calc_monthly_revenue(self):
"""Returns revenue of current month. Invoice sent every 1st of the month for the previous month"""
previous_month = datetime.datetime.now().month - 1
iamareebjamal marked this conversation as resolved.
Show resolved Hide resolved
monthly_revenue = db.session.query(func.sum(Order.amount)).filter(Order.event_id == self.id,
Order.completed_at.month == previous_month,
Order.status == 'completed').scalar()
if monthly_revenue is None:
monthly_revenue = 0
return monthly_revenue

@property
def tickets_available(self):
return self.calc_total_tickets_count()
Expand Down
Empty file.