From 97aec38b4ef2e8c453e0a026f0fbc028a484eeef Mon Sep 17 00:00:00 2001 From: Enrique Barcelli Date: Sun, 11 Aug 2024 10:24:42 +0800 Subject: [PATCH] Add insert payments --- import_data.py | 13 +++++++++++++ insert_payment.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 insert_payment.py diff --git a/import_data.py b/import_data.py index 8b31e36..7e4aa16 100644 --- a/import_data.py +++ b/import_data.py @@ -5,6 +5,7 @@ import shutil import re from insert_order import insert_order from insert_hourly import insert_hourly +from insert_payment import insert_payment # Import Oder History path = "./upload/Order_History*.csv" @@ -39,4 +40,16 @@ for filename in glob.glob(path): print(row) insert_hourly (date[0], row[0], row[1], row[2], row[3], row[4], row[5], row[6]) shutil.move(filename, "history/") + +# Import Payments +path = "./upload/*.csv" +for filename in glob.glob(path): + print(filename) + with open(filename) as file_obj: + heading = next(file_obj) + reader_obj = csv.reader(file_obj) + for row in reader_obj: + print(row) + insert_payment (row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13]) + shutil.move(filename, "history/") diff --git a/insert_payment.py b/insert_payment.py new file mode 100644 index 0000000..4a21fa1 --- /dev/null +++ b/insert_payment.py @@ -0,0 +1,29 @@ +import sys +import psycopg2 +from config import load_config + + +def insert_payment(order_no, type, card_type, last_4_cc_digits, date, raw_date, station, employee, transport_id, transaction_status, customer_paid, customer_change, total, rounding_delta): + """ Insert a new order into the orders table """ + + sql = """INSERT INTO payments(order_no, type, card_type, last_4_cc_digits, date, raw_date, station, employee, transport_id, transaction_status, customer_paid, customer_change, total, rounding_delta) + VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);""" + + config = load_config() + + try: + with psycopg2.connect(**config) as conn: + with conn.cursor() as cur: + # execute the INSERT statement + cur.execute(sql, (order_no, type, card_type, last_4_cc_digits, date, raw_date, station, employee, transport_id, transaction_status, customer_paid, customer_change, total, rounding_delta)) + + # commit the changes to the database + conn.commit() + except (Exception, psycopg2.DatabaseError) as error: + print(error) + finally: + print('success') + + +if __name__ == '__main__': + insert_payment(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5], sys.argv[6], sys.argv[7], sys.argv[8], sys.argv[9], sys.argv[10], sys.argv[11], sys.argv[12], sys.argv[13], sys.argv[14])