Repository navigation
Expand file tree
/
Copy pathdatabase.py
More file actions
359 lines (292 loc) · 14.3 KB
/
Copy pathdatabase.py
File metadata and controls
359 lines (292 loc) · 14.3 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
from PyQt6.QtSql import QSqlDatabase, QSqlQuery
from PyQt6.QtCore import Qt
class Database:
def __init__(self):
"""Ensure database exists and establish connection."""
self.ensure_database()
# Initialize connection
self.db = QSqlDatabase.addDatabase("QMYSQL")
self.db.setHostName("localhost")
self.db.setUserName("root")
self.db.setPassword("Bhavya7645@")
self.db.setDatabaseName("storeflow")
if not self.db.open():
print(f"❌ Database connection failed: {self.db.lastError().text()}")
return
print("✅ Database connection established.")
print("Database driver in use:", self.db.driverName())
self.create_table()
def ensure_database(self):
"""Ensures the 'storeflow' database exists before connecting."""
temp_db = QSqlDatabase.addDatabase("QMYSQL", "TempConnection")
temp_db.setHostName("localhost")
temp_db.setUserName("root")
temp_db.setPassword("Bhavya7645@")
if not temp_db.open():
print(f"❌ Cannot connect to MySQL server: {temp_db.lastError().text()}")
return
query = QSqlQuery(temp_db)
query.exec("CREATE DATABASE IF NOT EXISTS storeflow")
temp_db.close()
QSqlDatabase.removeDatabase("TempConnection")
def create_table(self):
"""Creates inventory and transactions tables with payment details."""
query = QSqlQuery()
# Inventory table (unchanged)
query.exec("""
CREATE TABLE IF NOT EXISTS inventory (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE,
gtin CHAR(13) UNIQUE,
unit_of_measurement VARCHAR(50),
selling_price DECIMAL(10,2) CHECK (selling_price >= 0),
mrp DECIMAL(10,2) CHECK (mrp >= 0),
cost_price DECIMAL(10,2) CHECK (cost_price >= 0),
stock DECIMAL(10,3),
reorder_point DECIMAL(10,3) CHECK (reorder_point >= 0)
)
""")
# Transactions table with payment details
query.exec("""
CREATE TABLE IF NOT EXISTS transactions (
id INT AUTO_INCREMENT PRIMARY KEY,
inventory_id INT NOT NULL,
inventory_name VARCHAR(255) NOT NULL, -- Store item name for easy reference
transaction_type ENUM('sale', 'purchase', 'adjustment') NOT NULL,
quantity DECIMAL(10,3) CHECK (quantity >= 0),
price DECIMAL(10,2) CHECK (price >= 0),
discount DECIMAL(10,2) CHECK (discount >= 0),
payment_mode ENUM('cash', 'credit', 'UPI', 'other') NOT NULL,
cash_received DECIMAL(10,2) DEFAULT NULL,
return_amount DECIMAL(10,2) DEFAULT NULL,
reference_no VARCHAR(255) DEFAULT NULL,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (inventory_id) REFERENCES inventory(id) ON DELETE CASCADE
)
""")
if query.lastError().isValid():
print(f"❌ Table creation error: {query.lastError().text()}")
else:
print("✅ Database and tables initialized successfully.")
def add_item(self, name, gtin, unit, selling_price, mrp, cost_price, stock, reorder_point):
"""Adds a new item to the inventory."""
query = QSqlQuery()
query.prepare("""
INSERT INTO inventory (name, gtin, unit_of_measurement, selling_price, mrp, cost_price, stock, reorder_point)
VALUES (:name, :gtin, :unit, :selling_price, :mrp, :cost_price, :stock, :reorder_point)
""")
query.bindValue(":name", name)
query.bindValue(":gtin", gtin)
query.bindValue(":unit", unit)
query.bindValue(":selling_price", selling_price)
query.bindValue(":mrp", mrp)
query.bindValue(":cost_price", cost_price)
query.bindValue(":stock", stock)
query.bindValue(":reorder_point", reorder_point)
if not query.exec():
print(f"❌ Insert error: {query.lastError().text()}")
return False
print(f"✅ Item '{name}' added successfully!")
return True
def delete_item(self, name: str) -> bool:
"""Deletes an item from inventory."""
query = QSqlQuery()
query.prepare("DELETE FROM inventory WHERE name = :name")
query.bindValue(":name", name)
if not query.exec():
print(f"❌ Query error: {query.lastError().text()}")
return False
if query.numRowsAffected() > 0:
return True # Item was successfully deleted
print("⚠️ No item deleted (Item may not exist)")
return False # Item didn't exist or was not deleted
def is_name_unique(self, name: str) -> bool:
"""Checks if an item name is unique in the inventory."""
query = QSqlQuery()
query.prepare("SELECT COUNT(*) FROM inventory WHERE name = :name")
query.bindValue(":name", name)
if not query.exec():
print(f"❌ Query error: {query.lastError().text()}")
return False
if query.next():
return query.value(0) == 0 # True if name is unique
return False
def is_gtin_unique(self, gtin: str) -> bool:
"""Checks if an item EAN is unique in the inventory."""
query = QSqlQuery()
query.prepare("SELECT COUNT(*) FROM inventory WHERE gtin = :gtin")
query.bindValue(":gtin", gtin)
if not query.exec():
print(f"❌ Query error: {query.lastError().text()}")
return False
if query.next():
return query.value(0) == 0 # True if gtin is unique
return False
def get_inventory_items(self, search_query=""):
"""Fetch inventory items from the database, optionally filtering by search_query."""
query = QSqlQuery()
if search_query:
query.prepare("""
SELECT id, name, gtin, unit_of_measurement, selling_price, mrp, cost_price, stock, reorder_point
FROM inventory
WHERE name LIKE :search_query OR gtin LIKE :search_query
""")
query.bindValue(":search_query", f"%{search_query}%")
else:
query.prepare("SELECT id, name, gtin, unit_of_measurement, selling_price, mrp, cost_price, stock, reorder_point FROM inventory")
query.exec()
inventory_items = []
while query.next():
item = {
"id": query.value(0),
"name": query.value(1),
"gtin": query.value(2),
"unit": query.value(3),
"selling_price": query.value(4),
"mrp": query.value(5),
"cost_price": query.value(6),
"stock": query.value(7),
"reorder_point": query.value(8),
}
inventory_items.append(item)
return inventory_items
def record_transaction(self, inventory_name, transaction_type, quantity, price, discount=0.00,
payment_mode="cash", cash_received=None, return_amount=None, reference_no=None, timestamp=None):
"""Records a transaction using inventory name instead of ID."""
# Fetch inventory ID using inventory name
query = QSqlQuery()
query.prepare("SELECT id FROM inventory WHERE name = :inventory_name")
query.bindValue(":inventory_name", inventory_name)
if not query.exec() or not query.next():
print(f"❌ Inventory item '{inventory_name}' not found.")
return False
inventory_id = query.value(0)
if not self.update_stock(inventory_name, -quantity):
return False # Stock update failed
query.prepare("""
INSERT INTO transactions (inventory_name, inventory_id, transaction_type, quantity, price, discount,
payment_mode, cash_received, return_amount, reference_no, timestamp)
VALUES (:inventory_name, :inventory_id, :transaction_type, :quantity, :price, :discount,
:payment_mode, :cash_received, :return_amount, :reference_no, :timestamp)
""")
query.bindValue(":inventory_name", inventory_name)
query.bindValue(":inventory_id", inventory_id)
query.bindValue(":transaction_type", transaction_type)
query.bindValue(":quantity", quantity)
query.bindValue(":price", price)
query.bindValue(":discount", discount)
query.bindValue(":payment_mode", payment_mode)
# Handle payment fields
if payment_mode == "cash":
query.bindValue(":cash_received", cash_received if cash_received is not None else 0.00)
query.bindValue(":return_amount", return_amount if return_amount is not None else 0.00)
query.bindValue(":reference_no", None)
else: # UPI or credit
query.bindValue(":cash_received", None)
query.bindValue(":return_amount", None)
query.bindValue(":reference_no", reference_no)
# Bind timestamp
query.bindValue(":timestamp", timestamp)
if not query.exec():
print(f"❌ Transaction error: {query.lastError().text()}")
return False
print(f"✅ Transaction recorded for '{inventory_name}' with payment mode {payment_mode}.")
return True
def get_transactions(self, inventory_name=None):
"""Fetch transactions based on inventory name."""
query = QSqlQuery()
if inventory_name:
query.prepare("""
SELECT id, inventory_name, inventory_id, transaction_type, quantity, price, discount, payment_mode, cash_received, return_amount, reference_no, timestamp
FROM transactions WHERE inventory_name = :inventory_name ORDER BY timestamp DESC
""")
query.bindValue(":inventory_name", inventory_name)
else:
query.prepare("""
SELECT id, inventory_name, inventory_id, transaction_type, quantity, price, discount, payment_mode, cash_received, return_amount, reference_no, timestamp
FROM transactions ORDER BY timestamp DESC
""")
query.exec()
transactions = []
while query.next():
transaction = {
"id": query.value(0),
"inventory_name": query.value(1), # No need to fetch separately
"inventory_id": query.value(2), # Still available if needed
"transaction_type": query.value(3),
"quantity": query.value(4),
"price": query.value(5),
"discount": query.value(6),
"payment_mode": query.value(7),
"cash_received": query.value(8),
"return_amount": query.value(9),
"reference_no": query.value(10),
"timestamp": query.value(11),
}
transactions.append(transaction)
return transactions
def update_stock(self, inventory_name, quantity) -> bool:
"""Updates stock quantity by adding the given quantity."""
query = QSqlQuery()
query.prepare("UPDATE inventory SET stock = stock + :quantity WHERE name = :inventory_name")
query.bindValue(":quantity", quantity)
query.bindValue(":inventory_name", inventory_name)
if not query.exec():
print(f"❌ Stock update failed: {query.lastError().text()}")
return False
print(f"✅ Stock updated for inventory name '{inventory_name}'.")
return True
def has_transactions(self, inventory_name: str) -> bool:
"""Returns True if the specified inventory item has transactions."""
query = QSqlQuery()
query.prepare("SELECT COUNT(*) FROM transactions WHERE inventory_name = :inventory_name")
query.bindValue(":inventory_name", inventory_name)
if not query.exec():
print(f"❌ Query error: {query.lastError().text()}")
return False # Assume no transactions if query fails
if query.next():
return query.value(0) > 0 # Check if count > 0
return False
def update_item(self, name, unit, selling_price, mrp, cost_price, stock, reorder_point):
"""Updates an inventory item based on its unique name."""
query = QSqlQuery()
query.prepare("""
UPDATE inventory
SET unit_of_measurement = :unit, selling_price = :selling_price, mrp = :mrp,
cost_price = :cost_price, stock = :stock, reorder_point = :reorder_point
WHERE name = :name
""")
query.bindValue(":name", name)
query.bindValue(":unit", unit)
query.bindValue(":selling_price", selling_price)
query.bindValue(":mrp", mrp)
query.bindValue(":cost_price", cost_price)
query.bindValue(":stock", stock)
query.bindValue(":reorder_point", reorder_point)
if query.exec():
return True
else:
print("Inventory update failed:", query.lastError().text())
return False
def record_edit_transaction(self, inventory_name, changes):
"""Logs an inventory edit as a transaction with change details in reference_no."""
# Fetch inventory ID using inventory name
query = QSqlQuery()
query.prepare("SELECT id FROM inventory WHERE name = :inventory_name")
query.bindValue(":inventory_name", inventory_name)
if not query.exec() or not query.next():
print(f"❌ Inventory item '{inventory_name}' not found.")
return False
inventory_id = query.value(0)
query = QSqlQuery()
query.prepare("""
INSERT INTO transactions (inventory_name, inventory_id, transaction_type, quantity, price, discount,
payment_mode, reference_no)
VALUES (:inventory_name, :inventory_id, 'adjustment', 0, 0, 0, 'other', :reference_no)
""")
query.bindValue(":inventory_name", inventory_name)
query.bindValue(":inventory_id", inventory_id)
query.bindValue(":reference_no", changes) # Storing changes as a string
print(query)
if not query.exec():
print("Transaction log failed:", query.lastError().text())