Add import orders

This commit is contained in:
Enrique Barcelli 2024-08-11 03:47:47 +08:00
commit 2f93f73c0e
Signed by: kikobar
GPG key ID: 006C13A68E25D3B7
13 changed files with 100 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
database.ini
*.csv

0
__init__.py Normal file
View file

Binary file not shown.

Binary file not shown.

20
config.py Normal file
View file

@ -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)

17
connect.py Normal file
View file

@ -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)

5
database.ini.sample Normal file
View file

@ -0,0 +1,5 @@
[postgresql]
host=localhost
database=dbname
user=user
password=password

0
failed/__init__.py Normal file
View file

0
history/__init__.py Normal file
View file

25
import_data.py Normal file
View file

@ -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/")

31
insert_order.py Normal file
View file

@ -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])

0
logs/__init__.py Normal file
View file

0
upload/__init__.py Normal file
View file