def send_event_fee_notification(): with app.app_context(): events = Event.query.all() for event in events: latest_invoice = EventInvoice.filter_by(event_id=event.id).order_by(EventInvoice.created_at.desc()).first() if latest_invoice: orders = Order.query\ .filter_by(event_id=event.id)\ .filter_by(status='completed')\ .filter(Order.completed_at > latest_invoice.created_at).all() else: orders = Order.query.filter_by(event_id=event.id).filter_by(status='completed').all() fee_total = 0 for order in orders: for order_ticket in order.tickets: ticket = TicketingManager.get_ticket(order_ticket.ticket_id) if order.paid_via != 'free' and order.amount > 0 and ticket.price > 0: fee = ticket.price * (get_fee(order.event.payment_currency) / 100) fee_total += fee if fee_total > 0: new_invoice = EventInvoice(amount=fee_total, event_id=event.id, user_id=event.creator_id) save_to_db(new_invoice) prev_month = monthdelta(new_invoice.created_at, 1).strftime("%b %Y") # Displayed as Aug 2016 send_email_for_monthly_fee_payment(new_invoice.user.email, event.name, prev_month, new_invoice.amount, url_for('event_invoicing.view_invoice', invoice_identifier=new_invoice.identifier, _external=True))
def send_event_fee_notification(): from app.instance import current_app as app with app.app_context(): events = Event.query.filter_by(deleted_at=None, state='published').all() for event in events: latest_invoice = EventInvoice.query.filter_by( event_id=event.id).order_by( EventInvoice.created_at.desc()).first() if latest_invoice: orders = Order.query \ .filter_by(event_id=event.id) \ .filter_by(status='completed') \ .filter(Order.completed_at > latest_invoice.created_at).all() else: orders = Order.query.filter_by(event_id=event.id).filter_by( status='completed').all() fee_total = 0 for order in orders: for ticket in order.tickets: if order.paid_via != 'free' and order.amount > 0 and ticket.price > 0: fee = ticket.price * ( get_fee(event.payment_country, order.event.payment_currency) / 100.0) fee_total += fee if fee_total > 0: owner = get_user_event_roles_by_role_name(event.id, 'owner').first() new_invoice = EventInvoice(amount=fee_total, event_id=event.id, user_id=owner.user.id) if event.discount_code_id and event.discount_code: r = relativedelta(datetime.datetime.utcnow(), event.created_at) if r <= event.discount_code.valid_till: new_invoice.amount = fee_total - \ (fee_total * (event.discount_code.value / 100.0)) new_invoice.discount_code_id = event.discount_code_id save_to_db(new_invoice) prev_month = monthdelta(new_invoice.created_at, 1).strftime( "%b %Y") # Displayed as Aug 2016 app_name = get_settings()['app_name'] frontend_url = get_settings()['frontend_url'] link = '{}/invoices/{}'.format(frontend_url, new_invoice.identifier) send_email_for_monthly_fee_payment(new_invoice.user.email, event.name, prev_month, new_invoice.amount, app_name, link) send_notif_monthly_fee_payment(new_invoice.user, event.name, prev_month, new_invoice.amount, app_name, link, new_invoice.event_id)
def send_event_fee_notification(): from app import current_app as app with app.app_context(): events = Event.query.all() for event in events: latest_invoice = EventInvoice.query.filter_by( event_id=event.id).order_by(EventInvoice.created_at.desc()).first() if latest_invoice: orders = Order.query \ .filter_by(event_id=event.id) \ .filter_by(status='completed') \ .filter(Order.completed_at > latest_invoice.created_at).all() else: orders = Order.query.filter_by( event_id=event.id).filter_by(status='completed').all() fee_total = 0 for order in orders: for order_ticket in order.tickets: ticket = safe_query(db, Ticket, 'id', order_ticket.ticket_id, 'ticket_id') if order.paid_via != 'free' and order.amount > 0 and ticket.price > 0: fee = ticket.price * (get_fee(order.event.payment_currency) / 100.0) fee_total += fee if fee_total > 0: organizer = get_user_event_roles_by_role_name(event.id, 'organizer').first() new_invoice = EventInvoice( amount=fee_total, event_id=event.id, user_id=organizer.user.id) if event.discount_code_id and event.discount_code: r = relativedelta(datetime.utcnow(), event.created_at) if r <= event.discount_code.valid_till: new_invoice.amount = fee_total - \ (fee_total * (event.discount_code.value / 100.0)) new_invoice.discount_code_id = event.discount_code_id save_to_db(new_invoice) prev_month = monthdelta(new_invoice.created_at, 1).strftime( "%b %Y") # Displayed as Aug 2016 app_name = get_settings()['app_name'] frontend_url = get_settings()['frontend_url'] link = '{}/invoices/{}'.format(frontend_url, new_invoice.identifier) send_email_for_monthly_fee_payment(new_invoice.user.email, event.name, prev_month, new_invoice.amount, app_name, link) send_notif_monthly_fee_payment(new_invoice.user, event.name, prev_month, new_invoice.amount, app_name, link, new_invoice.event_id)
def send_event_fee_notification(): from app import current_app as app with app.app_context(): events = Event.query.all() for event in events: latest_invoice = EventInvoice.filter_by( event_id=event.id).order_by( EventInvoice.created_at.desc()).first() if latest_invoice: orders = Order.query \ .filter_by(event_id=event.id) \ .filter_by(status='completed') \ .filter(Order.completed_at > latest_invoice.created_at).all() else: orders = Order.query.filter_by(event_id=event.id).filter_by( status='completed').all() fee_total = 0 for order in orders: for order_ticket in order.tickets: ticket = TicketingManager.get_ticket( order_ticket.ticket_id) if order.paid_via != 'free' and order.amount > 0 and ticket.price > 0: fee = ticket.price * ( get_fee(order.event.payment_currency) / 100.0) fee_total += fee if fee_total > 0: organizer = DataGetter.get_user_event_roles_by_role_name( event.id, 'organizer').first() new_invoice = EventInvoice(amount=fee_total, event_id=event.id, user_id=organizer.user.id) if event.discount_code_id and event.discount_code: r = relativedelta(datetime.utcnow(), event.created_at) if r <= event.discount_code.max_quantity: new_invoice.amount = fee_total - \ (fee_total * (event.discount_code.value / 100.0)) new_invoice.discount_code_id = event.discount_code_id save_to_db(new_invoice) prev_month = monthdelta(new_invoice.created_at, 1).strftime( "%b %Y") # Displayed as Aug 2016 send_email_for_monthly_fee_payment( new_invoice.user.email, event.name, prev_month, new_invoice.amount, url_for('event_invoicing.view_invoice', invoice_identifier=new_invoice.identifier, _external=True))
def send_event_invoice( self, event_id: int, send_notification: bool = True, force: bool = False ): this_month = this_month_date() # Check if this month's invoice has been generated event_invoice = ( EventInvoice.query.filter_by(event_id=event_id) .filter(EventInvoice.issued_at >= this_month) .first() ) if not force and event_invoice: logger.warn( 'Event Invoice of this month for this event has already been created: %s', event_id, ) return event = Event.query.get(event_id) try: # For keeping invoice numbers gapless and non-repeating, we need to generate invoices # one at a time. Hence, we try acquiring an expiring lock for 20 seconds, and then retry. # To avoid the condition of a deadlock, lock automatically expires after 5 seconds saved = False pdf_url = None with redis_store.lock('event_invoice_generate', timeout=5, blocking_timeout=20): event_invoice = EventInvoice(event=event, issued_at=this_month) pdf_url = event_invoice.populate() if pdf_url: try: save_to_db(event_invoice) saved = True logger.info( 'Generated Event invoice %s for %s. Amount: %f', event_invoice, event, event_invoice.amount, ) except Exception as e: # For some reason, like duplicate identifier, the record might not be saved, so we # retry generating the invoice and hope the error doesn't happen again logger.exception('Error while saving invoice. Retrying') raise self.retry(exc=e) else: logger.warning('Failed to generate event invoice PDF %s', event) if saved and send_notification: logger.info('Sending Invoice Notification %s', event_invoice) event_invoice.send_notification() return pdf_url except LockError as e: logger.exception('Error while acquiring lock. Retrying') self.retry(exc=e)
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 ticket_fee_maximum = ticket_fee_object.maximum_fee orders = Order.query.filter_by(event=event).all() 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 # 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)
def send_monthly_event_invoice(): from app import current_app as app with app.app_context(): events = Event.query.filter_by(deleted_at=None, state='published').all() for event in events: # calculate net & gross revenues user = event.owner admin_info = get_settings() currency = event.payment_currency ticket_fee_object = db.session.query(TicketFees).filter_by(currency=currency).one() ticket_fee_percentage = ticket_fee_object.service_fee ticket_fee_maximum = ticket_fee_object.maximum_fee orders = Order.query.filter_by(event=event).all() gross_revenue = event.calc_monthly_revenue() ticket_fees = event.tickets_sold * (ticket_fee_percentage / 100) if ticket_fees > ticket_fee_maximum: ticket_fees = ticket_fee_maximum net_revenue = gross_revenue - ticket_fees payment_details = { 'tickets_sold': event.tickets_sold, 'gross_revenue': gross_revenue, 'net_revenue': net_revenue, 'amount_payable': ticket_fees } # save invoice as pdf pdf = create_save_pdf(render_template('pdf/event_invoice.html', orders=orders, user=user, admin_info=admin_info, currency=currency, event=event, ticket_fee_object=ticket_fee_object, payment_details=payment_details, 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)
def send_event_fee_notification(): from app import current_app as app with app.app_context(): events = Event.query.all() for event in events: latest_invoice = EventInvoice.filter_by(event_id=event.id).order_by( EventInvoice.created_at.desc()).first() if latest_invoice: orders = Order.query \ .filter_by(event_id=event.id) \ .filter_by(status='completed') \ .filter(Order.completed_at > latest_invoice.created_at).all() else: orders = Order.query.filter_by( event_id=event.id).filter_by(status='completed').all() fee_total = 0 for order in orders: for order_ticket in order.tickets: ticket = TicketingManager.get_ticket(order_ticket.ticket_id) if order.paid_via != 'free' and order.amount > 0 and ticket.price > 0: fee = ticket.price * (get_fee(order.event.payment_currency) / 100.0) fee_total += fee if fee_total > 0: organizer = DataGetter.get_user_event_roles_by_role_name( event.id, 'organizer').first() new_invoice = EventInvoice( amount=fee_total, event_id=event.id, user_id=organizer.user.id) if event.discount_code_id and event.discount_code: r = relativedelta(datetime.utcnow(), event.created_at) if r <= event.discount_code.max_quantity: new_invoice.amount = fee_total - \ (fee_total * (event.discount_code.value / 100.0)) new_invoice.discount_code_id = event.discount_code_id save_to_db(new_invoice) prev_month = monthdelta(new_invoice.created_at, 1).strftime( "%b %Y") # Displayed as Aug 2016 send_email_for_monthly_fee_payment(new_invoice.user.email, event.name, prev_month, new_invoice.amount, url_for('event_invoicing.view_invoice', invoice_identifier=new_invoice.identifier, _external=True))
def send_monthly_event_invoice(): events = Event.query.filter_by(deleted_at=None, state='published').all() for event in events: # calculate net & gross revenues user = event.owner admin_info = get_settings() currency = event.payment_currency try: ticket_fee_object = (db.session.query(TicketFees).filter_by( currency=currency).one()) except NoResultFound: logger.error( 'Ticket Fee not found for event id {id}'.format(id=event.id)) continue ticket_fee_percentage = ticket_fee_object.service_fee ticket_fee_maximum = ticket_fee_object.maximum_fee orders = Order.query.filter_by(event=event).all() gross_revenue = event.calc_monthly_revenue() invoice_amount = gross_revenue * (ticket_fee_percentage / 100) if invoice_amount > ticket_fee_maximum: invoice_amount = ticket_fee_maximum net_revenue = gross_revenue - invoice_amount payment_details = { 'tickets_sold': event.tickets_sold, 'gross_revenue': gross_revenue, 'net_revenue': net_revenue, 'amount_payable': invoice_amount, } # save invoice as pdf pdf = create_save_pdf( render_template( 'pdf/event_invoice.html', orders=orders, user=user, admin_info=admin_info, currency=currency, event=event, ticket_fee_object=ticket_fee_object, payment_details=payment_details, 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=invoice_amount, invoice_pdf_url=pdf, event_id=event.id) save_to_db(event_invoice)
def get_event_invoice(): set_settings(paypal_mode='sandbox', paypal_sandbox_username=environ.get('PAYPAL_SANDBOX_USERNAME', 'SQPTVKtS8YvItGQuvHFvwve4'), paypal_sandbox_password=environ.get('PAYPAL_SANDBOX_PASSWORD', 'SQPTVKtS8YvItGQuvHFvwve4'), paypal_sandbox_signature=environ.get('PAYPAL_SANDBOX_SIGNATURE', 'SQPTVKtS8YvItGQuvHFvwve4'), stripe_secret_key='sk_test_SQPTVKtS8YvItGQuvHFvwve4', stripe_publishable_key='pk_test_e2jsd4RNUlYesCUb2KJ9nkCm', secret='super secret key') event = ObjectMother.get_event() event.name = 'Super Event' event.state = 'Published' user = ObjectMother.get_user() save_to_db(user) save_to_db(event, "Event Saved") new_invoice = EventInvoice(amount=100, event_id=event.id, user_id=user.id) save_to_db(new_invoice, "Ticket Saved") return event, new_invoice