Skip to content

Instantly share code, notes, and snippets.

@sethu-payilagam
Created October 30, 2024 11:45
Show Gist options
  • Save sethu-payilagam/3992fe7f34d9ddfdd63916a50ac3a2f6 to your computer and use it in GitHub Desktop.
Save sethu-payilagam/3992fe7f34d9ddfdd63916a50ac3a2f6 to your computer and use it in GitHub Desktop.
import psycopg2
import psycopg2.extras
from fpdf import FPDF
from fpdf.enums import XPos, YPos
def fetch_BillAmount(in_item,in_qty):
hostname = 'localhost'
database = 'deptstore'
port_id = 5432
pwd = 'admin@1234'
username = 'postgres'
conn=None
cur=None
try:
conn = psycopg2.connect(
host = hostname,
dbname = database,
user = username,
password = pwd,
port=port_id
)
cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
#cur = conn.cursor()
select_script = '''Select ItemCode, ItemName, ItemPrice from StoreItems where ItemCode= %s'''
cur.execute(select_script,(in_item,))
dict_items = dict()
dict_items = cur.fetchall()
for dictItem in dict_items:
#print(dictItem) #test print statement to verify the dict of items for the given input
bill_amount = in_qty * dictItem['itemprice']
return dictItem['itemname'],bill_amount
except Exception as error:
print(error)
finally:
if conn is not None:
conn.close()
if cur is not None:
cur.close()
bill=0
final_bill_txt = ''
def billing():
global bill,final_bill_txt
in_item = input('Enter the item code:')
in_qty = int(input('Enter the quantity:'))
item_name,calc_bill = fetch_BillAmount(in_item,in_qty)
bill = bill + calc_bill
print(f'The Bill amount for {in_qty} of {item_name} : ${calc_bill}')
final_bill_txt = final_bill_txt + (f'The Bill amount for {in_qty} of {item_name} : ${calc_bill}\n')
def write2Pdf(in_text):
pdf = FPDF()
pdf.add_page()
pdf.set_font("helvetica", size = 15)
pdf.cell(200, 10, text = "Shoppers Mart",new_x=XPos.LEFT, new_y=YPos.NEXT, align = 'C')
pdf.cell(200, 10, text = final_bill_txt,new_x=XPos.LEFT, new_y=YPos.NEXT,align = 'L')
pdf.output("ShoppersReceipt.pdf")
print('Welcome to Shoppers Mart')
first_login = 'Y' #default initial condition
i=1 #default loop initial value
while i != 0:
if first_login != 'Y':
in_Opt = input('Do you wish to continue shopping?(Y/N)')
if first_login == 'Y' or in_Opt == 'Y':
billing()
first_login = 'N'
else:
print(f'Your total bill amount is ${bill}')
final_bill_txt = final_bill_txt + (f'\n\n\n\nYour total bill amount is ${bill}\n\n\n\n\n\n''Thank you for shopping!')
print(final_bill_txt)
write2Pdf(final_bill_txt)
#print('Thank you for shopping!')
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment