def index(): db = connect_db() seed_db(db) invoice = Invoice.objects().first() results = process_invoice(invoice=invoice, return_dict=True) labels = [ 'Date', 'Ending Def Rev', 'Cumulative Revenue', 'CR Sales Revenue', 'DR Deferred Revenue', 'DR Reserve Refunds', 'DR Contra-Revenue', 'CR Refunds Payable', 'DR Reserve Grace Period', 'CR Contra-Revenue' ] data = [] for (date, v) in sorted(results['revrec_schedule'].iteritems(), key=lambda (k, v): (k, v)): row = [ pretty_date(date), v['ending_defrev'], v['cumul_rev'], v['cr_rev'], v['dr_defrev'], v['dr_reserve_ref'], v['dr_contra_rev'], v['cr_ref_payable'], v['dr_reserve_graceperiod'], v['cr_contra_rev'] ] data.append(row) return render_template('base.html', labels=labels, data=data, invoice=invoice, payment=results['payment'], invoice_items=results['invoice_items'], refunds=results['refunds'], gp_notes=results['gp_notes'])
def index(): db = connect_db() seed_db(db) invoice = Invoice.objects().first() results = process_invoice(invoice=invoice, return_dict=True) labels = ['Date', 'Ending Def Rev', 'Cumulative Revenue', 'CR Sales Revenue', 'DR Deferred Revenue', 'DR Reserve Refunds', 'DR Contra-Revenue', 'CR Refunds Payable', 'DR Reserve Grace Period', 'CR Contra-Revenue'] data = [] for (date, v) in sorted(results['revrec_schedule'].iteritems(), key=lambda (k, v): (k, v)): row = [pretty_date(date), v['ending_defrev'], v['cumul_rev'], v['cr_rev'], v['dr_defrev'], v['dr_reserve_ref'], v['dr_contra_rev'], v['cr_ref_payable'], v['dr_reserve_graceperiod'], v['cr_contra_rev']] data.append(row) return render_template('base.html', labels=labels, data=data, invoice=invoice, payment=results['payment'], invoice_items=results['invoice_items'], refunds=results['refunds'], gp_notes=results['gp_notes'])
def apply_grace_period(revrec_schedule, item, payment_date): """Adjusts the specified revrec schedule for late payment, which requires journal entries related to grace period. :param revrec_schedule: Dictionary of debits and credits by day to be adjusted due to application of grace period. :param item: InvoiceItem object :param payment_date: Date of payment :return list. Supporting notes on grace period calculations. Displayed in UI output. """ gp_notes = [] service_start = item.service_start service_end = item.service_end # Identify the details of the prev paid service period. shift = {'Monthly': 30, 'Yearly': 365, 'Biyearly': 730} prev_service_end = day_before(service_start) prev_service_start = prev_service_end - timedelta(shift[item.billperiod] - 1) prev_service_term = days_elapsed(prev_service_start, prev_service_end) prev_amount = item.total_amount prev_amort = prev_amount / prev_service_term # Financial reporting days. Important for determining debit and credit entries. current_reporting_day = datetime( service_start.year, service_start.month, last_day_of_month(service_start.year, service_start.month)) prev_reporting_day = day_before( datetime(prev_service_end.year, prev_service_end.month, 1)) # Notes for display in the UI output. gp_notes.append('JOURNAL ENTRIES FOR GRACE PERIOD') gp_notes.append('---' * 60) gp_notes.append( 'payment date: %s, current term: %s -> %s, prev term: %s -> %s' % (pretty_date(payment_date), pretty_date(service_start), pretty_date(service_end), pretty_date(prev_service_start), pretty_date(prev_service_end))) gp_notes.append( 'current reporting day: %s, prev reporting day: %s, days reserved for: %s -> %s' % (pretty_date(current_reporting_day), pretty_date(prev_reporting_day), prev_service_start, prev_reporting_day)) # For each date that payment is late... running_total_dr_reserve = 0 for date in daterange(service_start, day_before(payment_date)): # The previous service term is extended by the grace period used revised_service_term = prev_service_term + days_elapsed( service_start, date) # Revenue amortization for the extended service term revised_amort = prev_amount / revised_service_term # Difference in the daily amortization for the previous service term amort_difference = prev_amort - revised_amort # The difference b/n revenue that should have been recognized vs. what was recognized in the period # prior to the previous reporting day. This amount has already been reserved for, so the journal # entry is a debit against the reserve for grace periods. revised_dr_reserve_graceperiod = days_elapsed( prev_service_start, prev_reporting_day) * amort_difference # We have been debiting the reserve for each day late, this day's debit amount is equal to the # incremental debit against the reserve. The other side of the entry is credit contra-revenue. dr_reserve_graceperiod = revised_dr_reserve_graceperiod - running_total_dr_reserve cr_contra_rev = dr_reserve_graceperiod revrec_schedule[date][ 'dr_reserve_graceperiod'] = dr_reserve_graceperiod revrec_schedule[date]['cr_contra_rev'] = cr_contra_rev gp_notes.append( '%s, PrevTerm: %s->%s, PrevAmort: $%s->$%s, Value: %s, DaysReservedFor: %s, \ PrevTotalReserve %s, TotalReserve: %s, DR reserve: %s' % (pretty_date(date), prev_service_term, revised_service_term, round(prev_amort, 3), round( revised_amort, 3), revised_service_term * revised_amort, (prev_reporting_day - prev_service_start).days + 1, round(running_total_dr_reserve, 3), round(revised_dr_reserve_graceperiod, 3), round(dr_reserve_graceperiod, 3))) running_total_dr_reserve += dr_reserve_graceperiod gp_notes.append('---' * 60) return gp_notes
def apply_grace_period(revrec_schedule, item, payment_date): """Adjusts the specified revrec schedule for late payment, which requires journal entries related to grace period. :param revrec_schedule: Dictionary of debits and credits by day to be adjusted due to application of grace period. :param item: InvoiceItem object :param payment_date: Date of payment :return list. Supporting notes on grace period calculations. Displayed in UI output. """ gp_notes = [] service_start = item.service_start service_end = item.service_end # Identify the details of the prev paid service period. shift = {'Monthly': 30, 'Yearly': 365, 'Biyearly': 730} prev_service_end = day_before(service_start) prev_service_start = prev_service_end - timedelta(shift[item.billperiod] - 1) prev_service_term = days_elapsed(prev_service_start, prev_service_end) prev_amount = item.total_amount prev_amort = prev_amount / prev_service_term # Financial reporting days. Important for determining debit and credit entries. current_reporting_day = datetime(service_start.year, service_start.month, last_day_of_month(service_start.year, service_start.month)) prev_reporting_day = day_before(datetime(prev_service_end.year, prev_service_end.month, 1)) # Notes for display in the UI output. gp_notes.append('JOURNAL ENTRIES FOR GRACE PERIOD') gp_notes.append('---'*60) gp_notes.append('payment date: %s, current term: %s -> %s, prev term: %s -> %s' % ( pretty_date(payment_date), pretty_date(service_start), pretty_date(service_end), pretty_date(prev_service_start), pretty_date(prev_service_end))) gp_notes.append('current reporting day: %s, prev reporting day: %s, days reserved for: %s -> %s' % ( pretty_date(current_reporting_day), pretty_date(prev_reporting_day), prev_service_start, prev_reporting_day)) # For each date that payment is late... running_total_dr_reserve = 0 for date in daterange(service_start, day_before(payment_date)): # The previous service term is extended by the grace period used revised_service_term = prev_service_term + days_elapsed(service_start, date) # Revenue amortization for the extended service term revised_amort = prev_amount / revised_service_term # Difference in the daily amortization for the previous service term amort_difference = prev_amort - revised_amort # The difference b/n revenue that should have been recognized vs. what was recognized in the period # prior to the previous reporting day. This amount has already been reserved for, so the journal # entry is a debit against the reserve for grace periods. revised_dr_reserve_graceperiod = days_elapsed(prev_service_start, prev_reporting_day) * amort_difference # We have been debiting the reserve for each day late, this day's debit amount is equal to the # incremental debit against the reserve. The other side of the entry is credit contra-revenue. dr_reserve_graceperiod = revised_dr_reserve_graceperiod - running_total_dr_reserve cr_contra_rev = dr_reserve_graceperiod revrec_schedule[date]['dr_reserve_graceperiod'] = dr_reserve_graceperiod revrec_schedule[date]['cr_contra_rev'] = cr_contra_rev gp_notes.append('%s, PrevTerm: %s->%s, PrevAmort: $%s->$%s, Value: %s, DaysReservedFor: %s, \ PrevTotalReserve %s, TotalReserve: %s, DR reserve: %s' % ( pretty_date(date), prev_service_term, revised_service_term, round(prev_amort, 3), round(revised_amort, 3), revised_service_term * revised_amort, (prev_reporting_day - prev_service_start).days + 1, round(running_total_dr_reserve, 3), round(revised_dr_reserve_graceperiod, 3), round(dr_reserve_graceperiod,3))) running_total_dr_reserve += dr_reserve_graceperiod gp_notes.append('---'*60) return gp_notes
word_tup[1]) / frequencies[word_tup[0]]['freq'] frequencies[word_tup[0]]['puzzles'].append(board['puzzle_id']) puzzle_dates[board['puzzle_id']] = board['print_date'] # output results somehow (for now, stdout) print("Mini Crosswords:", len(data)) print("Individual Words:", len(frequencies)) print('{0:.5g}'.format( sum(1 for n in frequencies if frequencies[n]['freq'] > 1) / len(frequencies) * 100) + "% of words had frequency > 1\n") # print table of results print('| word'.ljust(10) + '| occurances'.ljust(15) + '| average solve time'.ljust(23) + '| most recent use'.ljust(20) + '|') print('-' * 69) for word in sorted(frequencies, key=lambda item: frequencies[item]['freq'], reverse=True): avg_freq = '{0:.5g}'.format(frequencies[word]['freq']) avg_time = '{0:.5g}'.format(frequencies[word]['average_time']) + "s" date = pretty_date(puzzle_dates[frequencies[word]['puzzles'][-1]]) print(("| " + word).ljust(10) + ('| ' + avg_freq).ljust(15) + ('| ' + avg_time).ljust(23) + ('| ' + date).ljust(20) + '|') with open('word-data.json', 'w') as f: f.write(json.dumps(frequencies))
def book(isbn): if request.method == "POST": # grab info from form rating = request.form.get("rating") review = request.form.get("review") if not rating or int(rating) < 1 or int(rating) > 5: flash("Review Failed, Invalid Rating!") elif not review: flash("Review Failed, Plese enter some text!") else: # we have rating & review # check if user already left review rows = db.execute("SELECT * from reviews WHERE book_isbn = :isbn AND user_id = :uid", { "isbn": isbn, "uid": session["user_id"]}) if rows.rowcount > 0: flash("Review Failed, Already left a review!") else: # able to post review db.execute('INSERT INTO reviews ("user_id", "review", "rating", "book_isbn") VALUES (:uid, :review, :rating, :isbn)', { "uid": session["user_id"], "review": review, "rating": rating, "isbn": isbn }) db.commit() rows = db.execute( "SELECT * FROM books WHERE books.isbn = :isbn", {"isbn": isbn}) if rows.rowcount < 1: return render_template("book.html", book={}) book = rows.fetchone() # get goodreads review data gr_res = get_goodreads_reviews(book[0]) # get reviews rows = db.execute( "SELECT reviews.*, users.username FROM reviews INNER JOIN users ON reviews.user_id = users.id WHERE book_isbn = :isbn ORDER BY reviews.created_at DESC", {"isbn": isbn}) reviews = rows.fetchall() # formatted text for easy templating f_text = dict() for i in range(len(reviews)): rating_string = '' for j in range(5): if j < reviews[i][3]: rating_string += '\u2605 ' else: rating_string += '\u2606 ' f_text[reviews[i][0]] = { "star_string": rating_string, "date_string": pretty_date(reviews[i][5]) } return render_template("book.html", book=book, reviews=reviews, f_text=f_text, gr_res=gr_res)
def format_pretty_date(d): return pretty_date(d)