Add insert items

This commit is contained in:
Enrique Barcelli 2024-08-11 11:40:08 +08:00
parent 97aec38b4e
commit 857c76cf03
Signed by: kikobar
GPG key ID: 006C13A68E25D3B7
3 changed files with 46 additions and 1 deletions

View file

@ -6,6 +6,7 @@ import re
from insert_order import insert_order
from insert_hourly import insert_hourly
from insert_payment import insert_payment
from insert_item import insert_item
# Import Oder History
path = "./upload/Order_History*.csv"
@ -41,6 +42,21 @@ for filename in glob.glob(path):
insert_hourly (date[0], row[0], row[1], row[2], row[3], row[4], row[5], row[6])
shutil.move(filename, "history/")
# Import Items
path = "./upload/Product_Mix*.csv"
for filename in glob.glob(path):
print(filename)
date = re.search(r'[0-9][0-9][0-9][0-9][-][0-9][0-9][-][0-9][0-9]', filename)
print(date[0])
with open(filename) as file_obj:
heading = next(file_obj)
reader_obj = csv.reader(file_obj)
for row in reader_obj:
if row[0] == 'Product':
print(row)
insert_item (date[0], row[2], row[3],'','', row[4], row[5], row[6], row[8])
shutil.move(filename, "history/")
# Import Payments
path = "./upload/*.csv"
for filename in glob.glob(path):

29
insert_item.py Normal file
View file

@ -0,0 +1,29 @@
import sys
import psycopg2
from config import load_config
def insert_item(date, cla, name, sku, barcode, category, subcategory, quantity, total):
""" Insert a new record into the items table """
sql = """INSERT INTO items(date, class, name, sku, barcode, category, subcategory, quantity, total)
VALUES(%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, (date, cla, name, sku, barcode, category, subcategory, quantity, total))
# commit the changes to the database
conn.commit()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
print('success')
if __name__ == '__main__':
insert_item(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])

View file

@ -4,7 +4,7 @@ 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 """
""" Insert a new order into the payments 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);"""