def test_monthdelta(): """Method to test difference in months result""" test_date = datetime.datetime(2000, 6, 18) test_future_date = monthdelta(test_date, 3) assert test_future_date == datetime.datetime(2000, 9, 18) test_date = datetime.datetime(2000, 1, 1) test_past_date = monthdelta(test_date, -1) assert test_past_date == datetime.datetime(1999, 12, 1) test_date = datetime.datetime(2000, 3, 1) test_past_date = monthdelta(test_date, -1) assert test_past_date == datetime.datetime(2000, 2, 1)
def send_event_fee_notification_followup(follow_up=True): if not follow_up: logger.warning('Not valid follow up request: %s', follow_up) return query = EventInvoice.query.filter( EventInvoice.amount > 0, EventInvoice.status != 'paid', EventInvoice.status != 'resolved', EventInvoice.status != 'refunded', EventInvoice.status != 'refunding', ) this_month = this_month_date() if follow_up != 'post_due': query = query.filter(EventInvoice.issued_at >= this_month) else: # For post due invoices, we want invoices of previous month only # Because it gets executed on 3rd day of the next month from issued date last_month = monthdelta(this_month, -1) query = query.filter( EventInvoice.issued_at >= last_month, EventInvoice.issued_at < this_month ) incomplete_invoices = query.all() logger.info( 'Sending notification %s for %d event invoices', follow_up, len(incomplete_invoices), ) for incomplete_invoice in incomplete_invoices: send_invoice_notification.delay(incomplete_invoice.id, follow_up=follow_up)
def test_monthdelta(self): """Method to test difference in months result""" with app.test_request_context(): test_date = datetime.datetime(2000, 6, 18) test_future_date = monthdelta(test_date, 3) self.assertEqual(test_future_date, datetime.datetime(2000, 9, 18))
def send_event_fee_notification_followup(): from app.instance import current_app as app with app.app_context(): incomplete_invoices = EventInvoice.query.filter( EventInvoice.status != 'paid' ).all() for incomplete_invoice in incomplete_invoices: if incomplete_invoice.amount > 0: prev_month = monthdelta(incomplete_invoice.created_at, 1).strftime( "%b %Y" ) # Displayed as Aug 2016 app_name = get_settings()['app_name'] frontend_url = get_settings()['frontend_url'] link = '{}/event-invoice/{}/review'.format( frontend_url, incomplete_invoice.identifier ) send_followup_email_for_monthly_fee_payment( incomplete_invoice.user.email, incomplete_invoice.event.name, prev_month, incomplete_invoice.amount, app_name, link, ) send_followup_notif_monthly_fee_payment( incomplete_invoice.user, incomplete_invoice.event.name, prev_month, incomplete_invoice.amount, app_name, link, incomplete_invoice.event.id, )
def send_event_fee_notification_followup(): from app import current_app as app with app.app_context(): incomplete_invoices = EventInvoice.query.filter(EventInvoice.status != 'completed').all() for incomplete_invoice in incomplete_invoices: if incomplete_invoice.amount > 0: prev_month = monthdelta(incomplete_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, incomplete_invoice.identifier) send_followup_email_for_monthly_fee_payment(incomplete_invoice.user.email, incomplete_invoice.event.name, prev_month, incomplete_invoice.amount, app_name, link) send_followup_notif_monthly_fee_payment(incomplete_invoice.user, incomplete_invoice.event.name, prev_month, incomplete_invoice.amount, app_name, link, incomplete_invoice.event.id)
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 test_send_monthly_invoice(db): """Method to test monthly invoices""" TicketFeesFactory(service_fee=10.23, maximum_fee=11, country='global') test_order = OrderSubFactory( status='completed', event__state='published', completed_at=monthdelta(this_month_date(), -1), amount=100, ) role, _ = get_or_create(Role, name='owner', title_name='Owner') UsersEventsRoles(user=UserFactory(), event=test_order.event, role=role) AttendeeSubFactory(event=test_order.event, order=test_order) db.session.commit() send_monthly_event_invoice() event_invoice = test_order.event.invoices[0] assert event_invoice.amount == 10.23
def send_monthly_event_invoice(send_notification: bool = True): this_month = this_month_date() last_month = monthdelta(this_month, -1) # Find all event IDs which had a completed order last month last_order_event_ids = Order.query.filter( Order.completed_at.between(last_month, this_month)).with_entities( distinct(Order.event_id)) # SQLAlchemy returns touples instead of list of IDs last_order_event_ids = [r[0] for r in last_order_event_ids] events = (Event.query.filter(Event.owner != None).filter( or_( Event.starts_at.between(last_month, this_month), Event.ends_at.between(last_month, this_month), Event.id.in_(last_order_event_ids), )).all()) for event in events: send_event_invoice.delay(event.id, send_notification=send_notification)
def previous_month_date(self): return monthdelta(self.issued_at, -1)