Exemplo n.º 1
0
if __name__ == "__main__":

    try:
        # Connect to Database
        print "Connecting to Database..."
        db = mydb.connect(HOST, USER, PASSWORD, DATABASE)
        print colored("Connection to %s successful.\n" % DATABASE, "green", attrs=["bold"])

        # Get Users without Clients
        cursor = db.cursor(mydb.cursors.DictCursor)
        cursor.execute(query)
        invoices = cursor.fetchall()

        for invoice in invoices:
            invoice_id = invoice.get("id")
            FreshbooksInvoice.send(invoice_id)
            cursor.execute(query_2, [invoice_id])
            print colored("Invoice Sent:", "blue"), invoice_id

        print colored("Total Invoices Sent:", "yellow"), len(invoices)

    except mydb.Error as e:
        print colored("MySQL Error %d: %s" % (e.args[0], e.args[1]), "red", attrs=["bold"])
        sys.exit(1)  # Exit with Error Code if unsuccessful

    except Exception as e:
        print colored("Something went wrong...", "red", attrs=["bold"])
        print e
        sys.exit(2)

    else:
Exemplo n.º 2
0
            cursor.execute(query_2, [usr_id])
            orders = cursor.fetchall()

            print colored("USER:"******"blue", attrs=["bold"]), usr_id
            print colored("ORDERS:", "green", attrs=["bold"]), len(orders)

            for order in orders:
                # Get Latest Invoice or Make One
                client_id = order.get("freshbooks_id")
                order_id = order.get("id")
                cursor.execute(query_3, [client_id])
                invoice = cursor.fetchone()
                # Check If Invoice Exists
                if not invoice:
                    print colored("Making New Invoice..", "green", attrs=["bold"])
                    invoice_id = FreshbooksInvoice.create(client_id)
                    cursor.execute(query_4, [client_id, invoice_id])
                else:
                    invoice_id = invoice.get("freshbooks_id")
                # Add Order Line
                line_id = FreshbooksInvoice.add_line(invoice=invoice_id, **order)
                # Update Order with Invoice ID & Line
                cursor.execute(query_5, [invoice_id, line_id, order_id])
                print colored("\tORDER: %s, LINE: %s" % (order_id, line_id), "yellow", attrs=["bold"])

    except mydb.Error as e:
        print colored("MySQL Error %d: %s" % (e.args[0], e.args[1]), "red", attrs=["bold"])
        sys.exit(1)  # Exit with Error Code if unsuccessful

    except Exception as e:
        print colored("Something went wrong...", "red", attrs=["bold"])