commit 2f93f73c0e6d8695d351a523c63695e75d1842e4 Author: Enrique Barcelli Date: Sun Aug 11 03:47:47 2024 +0800 Add import orders diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e9aa16e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +database.ini +*.csv diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/__pycache__/config.cpython-311.pyc b/__pycache__/config.cpython-311.pyc new file mode 100644 index 0000000..39a3537 Binary files /dev/null and b/__pycache__/config.cpython-311.pyc differ diff --git a/__pycache__/insert_order.cpython-311.pyc b/__pycache__/insert_order.cpython-311.pyc new file mode 100644 index 0000000..3d6f28e Binary files /dev/null and b/__pycache__/insert_order.cpython-311.pyc differ diff --git a/config.py b/config.py new file mode 100644 index 0000000..7c3fc46 --- /dev/null +++ b/config.py @@ -0,0 +1,20 @@ +from configparser import ConfigParser + +def load_config(filename='database.ini', section='postgresql'): + parser = ConfigParser() + parser.read(filename) + + # get section, default to postgresql + config = {} + if parser.has_section(section): + params = parser.items(section) + for param in params: + config[param[0]] = param[1] + else: + raise Exception('Section {0} not found in the {1} file'.format(section, filename)) + + return config + +if __name__ == '__main__': + config = load_config() + print(config) diff --git a/connect.py b/connect.py new file mode 100644 index 0000000..ab449be --- /dev/null +++ b/connect.py @@ -0,0 +1,17 @@ +import psycopg2 +from config import load_config + +def connect(config): + """ Connect to the PostgreSQL database server """ + try: + # connecting to the PostgreSQL server + with psycopg2.connect(**config) as conn: + print('Connected to the PostgreSQL server.') + return conn + except (psycopg2.DatabaseError, Exception) as error: + print(error) + + +if __name__ == '__main__': + config = load_config() + connect(config) diff --git a/database.ini.sample b/database.ini.sample new file mode 100644 index 0000000..00ded78 --- /dev/null +++ b/database.ini.sample @@ -0,0 +1,5 @@ +[postgresql] +host=localhost +database=dbname +user=user +password=password diff --git a/failed/__init__.py b/failed/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/history/__init__.py b/history/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/import_data.py b/import_data.py new file mode 100644 index 0000000..2016521 --- /dev/null +++ b/import_data.py @@ -0,0 +1,25 @@ +# Import necessary packages +import glob +import csv +import shutil +from insert_order import insert_order + +# Import Oder History +path = "./upload/Order_History*.csv" +for filename in glob.glob(path): + with open(filename) as file_obj: + # Skips the heading + # Using next() method + heading = next(file_obj) + + # Create reader object by passing the file + # object to reader method + reader_obj = csv.reader(file_obj) + + # Iterate over each row in the csv file + # using reader object + for row in reader_obj: + print(row) + insert_order (row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9]) + shutil.move(filename, "history/") + diff --git a/insert_order.py b/insert_order.py new file mode 100644 index 0000000..b7121f4 --- /dev/null +++ b/insert_order.py @@ -0,0 +1,31 @@ +import sys +import psycopg2 +from config import load_config + + +def insert_order(reporting_no, order_no, offline_no, created_by, first_opened, last_closed, net_sales, tax, final_total, payments_total): + """ Insert a new order into the orders table """ + + sql = """INSERT INTO orders(reporting_no, order_no, offline_no, created_by, first_opened, last_closed, net_sales, tax, final_total, payments_total) + VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s);""" + + vendor_id = None + config = load_config() + + try: + with psycopg2.connect(**config) as conn: + with conn.cursor() as cur: + # execute the INSERT statement + cur.execute(sql, (reporting_no, order_no, offline_no, created_by, first_opened, last_closed, net_sales, tax, final_total, payments_total)) + + # commit the changes to the database + conn.commit() + except (Exception, psycopg2.DatabaseError) as error: + print(error) + finally: + #return vendor_id + print('success') + + +if __name__ == '__main__': + insert_order(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]) diff --git a/logs/__init__.py b/logs/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/upload/__init__.py b/upload/__init__.py new file mode 100644 index 0000000..e69de29