-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
255 lines (210 loc) · 8.07 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# Imports
try:
import os
import datetime
import subprocess
import webbrowser
from dotenv import load_dotenv
from mysql.connector import connect
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import Screen, ScreenManager
print('All packages loaded.')
except ImportError as ie:
print(ie)
# Database Connection
load_dotenv(".env")
try:
connection = connect(
host='localhost',
user='root',
passwd=os.getenv("passwd"),
database='medmart'
)
cursor = connection.cursor()
print("Connection to DB established.")
except Exception as error:
print(error)
# Variables
medList = []
# ------------------- Screens ----------------------------
class WelcomeScreen(Screen):
pass
class RegisterScreen(Screen):
firstname = ObjectProperty(None)
lastname = ObjectProperty(None)
username = ObjectProperty(None)
password = ObjectProperty(None)
def register(self):
# making sure no details are empty
if self.firstname.text and self.lastname.text and self.username.text and self.password.text is not None and self.password.text != "":
cursor.execute("""INSERT INTO userdata VALUES("{}", "{}", "{}", "{}")""".format(
self.firstname.text,
self.lastname.text,
self.username.text,
self.password.text
))
connection.commit()
print("Data saved.")
popup = Popup(
title="Message",
content=Label(text="Registered Successfully.", color="white"),
size_hint=(None, None),
size=(300, 200)
)
popup.open()
return True
else:
popup = Popup(
title="Message",
content=Label(text="All fields are required.", color="white"),
size_hint=(None, None),
size=(300, 200)
)
popup.open()
return False
class LoginScreen(Screen):
username = ObjectProperty(None)
password = ObjectProperty(None)
def login(self):
verify = (self.username.text, self.password.text)
cursor.execute("SELECT username, password FROM userdata")
data = cursor.fetchall()
if verify in data:
popup = Popup(
title="Message",
content=Label(text="Logged In.", color="white"),
size_hint=(None, None),
size=(300, 200)
)
popup.open()
return True
else:
popup = Popup(
title="Message",
content=Label(text="Username or Password Incorrect.", color="white"),
size_hint=(None, None),
size=(300, 200)
)
popup.open()
return False
class HomeScreen(Screen):
@staticmethod
def logout():
popup = Popup(
title="Message",
content=Label(text="Logged Out.", color="white"),
size_hint=(None, None),
size=(300, 200)
)
popup.open()
@staticmethod
def exitApp():
exit()
class BuyMedicinesScreen(Screen):
medicine = ObjectProperty(None)
def addToCart(self):
if self.medicine.text.strip(): # no blank input box
if self.medicine.text.isalpha() or "," in self.medicine.text: # no special characters
li = list(self.medicine.text.split(","))
for item in li:
item = item.lower().strip() # remove whitespaces
medList.append(item)
popup = Popup(
title="Message",
content=Label(text="Med(s) added to cart.", color="white"),
size_hint=(None, None),
size=(300, 200)
)
popup.open()
return True
else:
popup = Popup(
title="Message",
content=Label(text="Following are not allowed:-\nSpecial Characters, Numbers\nexcept commas (,)", color="white"),
size_hint=(None, None),
size=(300, 200)
)
popup.open()
return False
else:
popup = Popup(
title="Message",
content=Label(text="No medicine name was mentioned.", color="white"),
size_hint=(None, None),
size=(300, 200)
)
popup.open()
return False
class CartScreen(Screen):
@staticmethod
def infoCrocin():
return "Uses: Cold, Flu, Headache etc.\nContents: Paracetamol, Caffeine\nPrice: Rs 30\n(1 leaf has 6 tablets)"
@staticmethod
def infoNexium():
return "Uses: Acidity, Gastric Ulcer etc.\nContents: Magnesium, R-Isomers\nPrice: Rs 25\n(1 leaf has 6 tablets)"
@staticmethod
def infoParacetamol():
return "Uses: Acute Pain, Aches etc.\nContents: Purified Talc, Starches\nPrice: Rs 27\n(1 leaf has 6 tablets)"
@staticmethod
def infoAspirin():
return "Uses: Swelling, Cold, Aches\nContents: Cellulose, Triacetin\nPrices: Rs 32\n(1 leaf has 6 tablets)"
def billing(self):
cursor.execute("SELECT * FROM prices")
data = cursor.fetchall()
priceList = [] # to sum up the price of all meds
with open("bills.txt", "w") as bill:
bill.write("-------------- BILL RECEIPT --------------\n\n")
bill.write(f"Date: {datetime.datetime.today().date()}\nTime: {datetime.datetime.today().time()}\n\nMedicines Purchased:-\n\n")
for med in medList: # compare meds in medlist with meds in table
for item in data: # item = (name of med, price)
if item[0] == med:
priceList.append(item[1])
bill.write("{}: Rs {}\n".format(str(item[0]).capitalize(), item[1]))
bill.write("\nTotal Price: Rs {}\n\nThank you for your purchase!\n".format(sum(priceList)))
bill.write("--------------------------------------------\n\n")
# popups for purchasing meds with or without adding them to cart
if len(priceList) > 0:
popup = Popup(
title="Message",
content=Label(text="Bill Receipt has been generated.", color="white"),
size_hint=(None, None),
size=(300, 200)
)
popup.open()
subprocess.Popen(['notepad.exe', 'bills.txt']) # for showing bill in notepad
else:
popup = Popup(
title="Message",
content=Label(text="You didn't purchase anything.\nBill couldn't be generated.", color="white"),
size_hint=(None, None),
size=(300, 200)
)
popup.open()
# reset variables
medList.clear()
priceList.clear()
class AboutScreen(Screen):
@staticmethod
def information():
return "This app is made by Tamonud Sharma, Siddhant Ghosh & Sarvesh Sai as a part of our 12th grade Computer\nScience project. Click the 'Source Code' button to head over to source code for this app. Click 'User Profile' to\ncheck out my profile on github. Thank you!"
@staticmethod
def linkToRepo():
webbrowser.open("https://github.com/spidey711/Medmart")
@staticmethod
def linkToProfile():
webbrowser.open("https://github.com/spidey711")
class WindowManager(ScreenManager):
pass
# -------------------- App Setup -------------------------
class MedmartApp(App):
# load UI into app
def build(self):
return Builder.load_file("medmart.kv")
if __name__ == "__main__":
MedmartApp().run()