def parse_donately_transactions(qbo, payments): donately_transactions_file = data_file("donately", "transactions_file") donation_item = qbo.get_item_by_name(DONATION_ITEM_NAME) donately_fees_item = qbo.get_item_by_name("Donately Fees") stripe_fees_item = qbo.get_item_by_name("Stripe Fees (Donations)") credit_card_receivables_account = qbo.get_account_by_name( "Stripe Receivables") qbo_class = qbo.get_class_by_name("5 Fundraising") for row in csv_rows(donately_transactions_file): doc_number = donately_doc_number(row) transaction_id = row['Transaction Id'] donation_type = row['Donation Type'] order_date = datetime.strptime(row['Custom Donation Date'], "%Y-%m-%d %H:%M:%S %Z") total = Decimal(row['Amount in Dollars']) logger.debug("doc number is %s" % doc_number) logger.debug("donation type is %s" % donation_type) if qbo.already_processed(doc_number): continue elif donation_type == 'cc' and transaction_id not in payments: msg = "fee information not available yet, skipping %s" % doc_number logger.info(msg) continue customer = donately_customer(qbo, row) with CreateReceipt(qbo) as receipt: receipt.CustomerRef = customer.to_ref() receipt.DocNumber = doc_number receipt.TxnDate = order_date.strftime("%Y-%m-%d") notes = ["Imported via QBO API from Donately export."] if row['Comment']: notes.append(row['Comment']) note = u"\n".join(notes) receipt.PrivateNote = note line1 = transaction_line_item(total, "Donation", one, donation_item, order_date, qbo_class=qbo_class) if donation_type == 'cc': line2 = vendor_rate_line_item(total, CONFIG['donately']['rate'], donately_fees_item, order_date, qbo_class=qbo_class) line3 = stripe_fee_line_item(payments, total, row['Transaction Id'], stripe_fees_item, order_date, abs(line2.Amount), qbo_class=qbo_class) receipt.Line = [line1, line2, line3] receipt.DepositToAccountRef = credit_card_receivables_account.to_ref( ) else: receipt.Line = [line1]
def parse_vouchercart_transactions(qbo, payments): vouchercart_transactions_file = data_file("vouchercart", "transactions_file") stripe_fees_item = qbo.get_item_by_name("Stripe Fees (Vouchers)") credit_card_receivables_account = qbo.get_account_by_name( "Stripe Receivables") qbo_class = qbo.get_class_by_name("2 School") active_doc_number = None active_order = [] def process_order(): doc_number = active_doc_number first_row = active_order[0] logger.debug("processing order %s" % doc_number) customer = vouchercart_customer(qbo, first_row) order_date = datetime.strptime(first_row['OrderDate'], "%m/%d/%Y %H:%M") total = sum(Decimal(row["VoucherPrice"]) for row in active_order) with CreateReceipt(qbo, test_mode=True) as receipt: receipt.CustomerRef = customer.to_ref() receipt.DocNumber = doc_number receipt.TxnDate = order_date.strftime("%Y-%m-%d") receipt.PrivateNote = "Imported via QBO API from Vouchercart export." for row in active_order: assert vouchercart_doc_number(row) == doc_number voucher_line_item = transaction_line_item( Decimal(row['VoucherPrice']), row['VoucherTitle'], one, qbo.get_item_by_name(row['VoucherTitle']), order_date, qbo_class=qbo_class) receipt.Line.append(voucher_line_item) receipt.Line.append( stripe_fee_line_item(payments, total, first_row['PaymentReference'], stripe_fees_item, order_date, qbo_class=qbo_class)) receipt.DepositToAccountRef = credit_card_receivables_account.to_ref( ) # an order may have multiple rows # aggregate the rows for each order, then process all rows at once for row in csv_rows(vouchercart_transactions_file): doc_number = vouchercart_doc_number(row) if "Imported-" in row['OrderID']: logger.debug("skipping imported voucher") continue logger.debug("doc_number is %s" % doc_number) if qbo.already_processed(doc_number): continue elif row['PaymentReference'] not in payments: logger.debug("fee information not available yet, skipping %s" % doc_number) continue if doc_number == active_doc_number: logger.debug("appending additional row to current order") active_order.append(row) else: if active_doc_number is not None: process_order() active_doc_number = doc_number active_order = [row] # process the last order if active_doc_number is not None: process_order()
def test_stop_after(): for i, row in enumerate(csv_rows("tests/data/iris.csv", stop_after=10)): pass assert i == 9
def test_csv_rows(): for row in csv_rows("tests/data/iris.csv"): assert "sepal_length" in row
def parse_vbo_transactions(qbo, payments): vbo_transactions_file = data_file("vbo", "transactions_file") advance_sale_item = qbo.get_item_by_name("General Admission - Advance") door_sale_item = qbo.get_item_by_name("General Admission - Door") subscription_sale_item = qbo.get_item_by_name("Ticket Subscription") vbo_fees_item = qbo.get_item_by_name("VBO Fees") stripe_fees_item = qbo.get_item_by_name("Stripe Fees (Ticketing)") credit_card_receivables_account = qbo.get_account_by_name("Stripe Receivables") qbo_class = qbo.get_class_by_name("3 Shows") for row in csv_rows(vbo_transactions_file, skip_rows=CONFIG['vbo']['header_row']): if row['ItemDescription'] == "Total:": logger.debug("reached end of records") break doc_number = "VBO-%s" % row['OrderID'] order_date = datetime.strptime(row['Orders'], "%m/%d/%Y %I:%M:%S %p") total = to_dec(row['Total']) price = to_dec(row['Price']) qty = to_dec(row['Qty']) vbo_fee = to_dec(row['VBOFee']) is_credit_card = to_dec(row['CreditCard']) > 0 or to_dec(row['Amex']) > 0 is_exchange = row['Other Name'] == 'Exchange' try: event_date = datetime.strptime(row['Event Date'], "%m/%d/%Y %I:%M:%S %p") except ValueError: logger.debug("invalid event date %s" % row['Event Date']) event_date = None logger.debug("doc number is %s" % doc_number) if order_date < CONFIG['app']['start_date']: logger.debug("skipping because before %s" % CONFIG['app']['start_date']) continue elif qbo.already_processed(doc_number): continue elif total == 0.0: logger.debug("skipping because comp") continue elif is_exchange: logger.debug("skipping because exchange") elif is_credit_card and (doc_number not in payments): msg = "fee information not available yet, skipping %s" % doc_number logger.info(msg) continue customer = vbo_customer(qbo, row) with CreateReceipt(qbo) as receipt: receipt.CustomerRef = customer.to_ref() receipt.DocNumber = doc_number receipt.TxnDate = order_date.strftime("%Y-%m-%d") notes = ["Imported via QBO API from VBO export."] if row['Notes']: notes.append(row['Notes']) note = u"\n".join(notes) receipt.PrivateNote = note is_door_sale = ("Door" in row['ItemName']) if is_door_sale: item = door_sale_item elif ("Advance" in row['ItemName']) or ("Admission" in row['ItemName']): item = advance_sale_item elif "Pack" in row['ItemName']: item = subscription_sale_item else: logger.error("\n%s" % json.dumps(row, sort_keys=True, indent=4)) raise Exception("can't identify product '%s'" % row['ItemName']) description = u"%s; %s" % (row['ItemDescription'], row['Event Name']) receipt.Line.append(transaction_line_item(price, description, qty, item, event_date, qbo_class=qbo_class)) if is_door_sale: # VBO fees are not added on top pass else: receipt.Line.append(vendor_unit_fee_line_item(-1 * vbo_fee, qty, vbo_fees_item, order_date, qbo_class=qbo_class)) if is_credit_card: stripe_fees_line = stripe_fee_line_item(payments, total, doc_number, stripe_fees_item, order_date, qbo_class=qbo_class) receipt.Line.append(stripe_fees_line) receipt.DepositToAccountRef = credit_card_receivables_account.to_ref()