-
Notifications
You must be signed in to change notification settings - Fork 0
/
view_project.py
295 lines (231 loc) · 12.2 KB
/
view_project.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
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
from PyQt6 import QtWidgets, uic, QtCore
from PyQt6.QtCore import QDate, QTimer
from PyQt6.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem, QVBoxLayout, QWidget, QHeaderView
import sys
import pyodbc
import connectionString
from worker_to_project import WorkerToProject
from update_worker import UpdateWorker
class ViewProject(QtWidgets.QMainWindow):
def __init__(self, projectID):
# Call the inherited classes __init__ method
super().__init__()
# Load the .ui file
uic.loadUi('Screens/ViewProject.ui', self)
self.setWindowTitle("Project Details")
self.ngoName.setDisabled(True)
self.projectName.setDisabled(True)
self.projectScale.setDisabled(True)
self.projectStartDate.setDisabled(True)
self.projectEndDate.setDisabled(True)
self.projectSaveBtn.setDisabled(True)
self.totalAmount.setDisabled(True)
self.addWorkerBtn.clicked.connect(lambda: self.addWorker(projectID))
self.removeWorkerBtn.clicked.connect(lambda: self.removeWorker(projectID))
self.projectEditBtn.clicked.connect(self.editProject)
self.projectSaveBtn.clicked.connect(lambda: self.saveProject(projectID))
self.endProjectBtn.clicked.connect(lambda: self.endProject(projectID))
self.donationSearchBtn.clicked.connect(lambda: self.searchDonation(projectID))
self.loadProjectData(projectID)
self.loadDonationData(projectID)
def refreshData(self, projectID):
self.loadProjectData(projectID)
self.loadDonationData(projectID)
def loadDonationData(self, projectID):
connection = pyodbc.connect(connectionString.connection_string)
cursor = connection.cursor()
select_query = """
SELECT u.userName, d.amount, d.donationDateTime
FROM Users u, Donation d
WHERE u.userID = d.userID and d.projectID = ?
"""
header = self.donationDetails.horizontalHeader()
for i in range(4):
header.setSectionResizeMode(i, QHeaderView.ResizeMode.Stretch)
self.donationDetails.clearContents()
self.donationDetails.setRowCount(0)
cursor.execute(select_query, projectID)
# columns are Name, Amount, Time, and Date in donationDetails
for row_index, row_data in enumerate(cursor.fetchall()):
self.donationDetails.insertRow(row_index)
for col_index, cell_data in enumerate(row_data):
if col_index == 2:
date_time = str(cell_data).split(" ")
date = QTableWidgetItem(date_time[0])
time = QTableWidgetItem(date_time[1])
self.donationDetails.setItem(row_index, col_index, time)
self.donationDetails.setItem(row_index, col_index + 1, date)
self.donationDetails.item(row_index, col_index).setTextAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.donationDetails.item(row_index, col_index + 1).setTextAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
else:
item = QTableWidgetItem(str(cell_data))
self.donationDetails.setItem(row_index, col_index, item)
self.donationDetails.item(row_index, col_index).setTextAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
# Close the database connection
connection.close()
# sum the column of amount
total = 0
for row in range(self.donationDetails.rowCount()):
total += float(self.donationDetails.item(row, 1).text())
self.totalAmount.setText(str(total))
def searchDonation(self, projectID):
self.loadDonationData(projectID)
# get date from startDate dateEdit
if self.startDate.date() == QDate(2000, 1, 1):
Dialog = QtWidgets.QMessageBox()
Dialog.setWindowTitle("Error")
Dialog.setText("Please Set Start Date Greater Than 2000-01-01")
Dialog.exec()
return
# check if startDate is greater than endDate
if self.startDate.date() > self.endDate.date():
Dialog = QtWidgets.QMessageBox()
Dialog.setWindowTitle("Error")
Dialog.setText("Start Date Cannot Be Greater Than End Date")
Dialog.exec()
return
# check if startDate is greater than current date
if self.startDate.date() > QDate.currentDate():
Dialog = QtWidgets.QMessageBox()
Dialog.setWindowTitle("Error")
Dialog.setText("Start Date Cannot Be Greater Than Current Date")
Dialog.exec()
return
# check if endDate is greater than current date
if self.endDate.date() > QDate.currentDate():
Dialog = QtWidgets.QMessageBox()
Dialog.setWindowTitle("Error")
Dialog.setText("End Date Cannot Be Greater Than Current Date")
Dialog.exec()
return
for row in range(self.donationDetails.rowCount() - 1, -1, -1):
if self.donationDetails.item(row, 3).text() < self.startDate.date().toString("yyyy-MM-dd") or self.donationDetails.item(row, 3).text() > self.endDate.date().toString("yyyy-MM-dd"):
self.donationDetails.removeRow(row)
if self.donorName.text() != "":
for row in range(self.donationDetails.rowCount() - 1, -1, -1):
if self.donationDetails.item(row, 0).text() != self.donorName.text():
self.donationDetails.removeRow(row)
# sum the column of amount
total = 0
for row in range(self.donationDetails.rowCount()):
total += float(self.donationDetails.item(row, 1).text())
self.totalAmount.setText(str(total))
def loadProjectData(self, projectID):
connection = pyodbc.connect(connectionString.connection_string)
cursor = connection.cursor()
select_query = """
SELECT n.name, p.projectName, p.scale, p.startDate, p.endDate
FROM NGO n, Project p
WHERE n.ngoID = p.ngoID and p.projectID = ?
"""
cursor.execute(select_query, projectID)
projectDetails = cursor.fetchall()[0]
self.ngoName.setText(projectDetails[0])
self.projectName.setText(projectDetails[1])
self.projectScale.setText(str(projectDetails[2]))
self.projectStartDate.setDate(projectDetails[3])
if projectDetails[4] == None:
self.projectEndDate.setText(" -")
else:
#convert datetime to sting
self.projectEndDate.setText(projectDetails[4].strftime("%Y-%m-%d"))
self.endProjectBtn.setDisabled(True)
self.projectName.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
select_query = """
SELECT w.workerEmail, w.workerName, w.gender, w.age
FROM Worker w, WorkerProject wp
WHERE w.workerID = wp.workerID and wp.projectID = ?
"""
header = self.workerDetails.horizontalHeader()
for i in range(4):
header.setSectionResizeMode(i, QHeaderView.ResizeMode.Stretch)
self.workerDetails.clearContents()
self.workerDetails.setRowCount(0)
cursor.execute(select_query, projectID)
for row_index, row_data in enumerate(cursor.fetchall()):
self.workerDetails.insertRow(row_index)
for col_index, cell_data in enumerate(row_data):
item = QTableWidgetItem(str(cell_data))
self.workerDetails.setItem(row_index, col_index, item)
self.workerDetails.item(row_index, col_index).setTextAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
# Close the database connection
connection.close()
def addWorker(self, projectID):
self.add_worker = WorkerToProject(projectID)
self.add_worker.show()
self.add_worker.addWorkerDoneBtn.clicked.connect(lambda: self.refreshData(projectID))
def removeWorker(self, projectID):
Dialog = QtWidgets.QMessageBox()
Dialog.setWindowTitle("Confirmation Box")
Dialog.setText("Are You Sure You Want To Remove This Worker?")
Dialog.setStandardButtons(QtWidgets.QMessageBox.StandardButton.Yes | QtWidgets.QMessageBox.StandardButton.No)
Option = Dialog.exec()
if Option == QtWidgets.QMessageBox.StandardButton.Yes:
# do the delete procedure of project
workerEmail = self.workerDetails.item(self.workerDetails.currentRow(), 0).text()
connection = pyodbc.connect(connectionString.connection_string)
cursor = connection.cursor()
cursor.execute("SELECT workerID FROM Worker WHERE workerEmail = ?", workerEmail)
workerID = cursor.fetchall()[0][0]
delete_query = """
DELETE FROM WorkerProject
WHERE workerID = ? and projectID = ?
"""
cursor.execute(delete_query, workerID, projectID)
connection.commit()
connection.close()
self.refreshData(projectID)
def editProject(self):
self.projectName.setDisabled(False)
self.projectScale.setDisabled(False)
self.projectStartDate.setDisabled(False)
self.projectSaveBtn.setDisabled(False)
self.projectEditBtn.setText("Cancel")
self.projectEditBtn.clicked.connect(self.cancelEdit)
self.endProjectBtn.setDisabled(True)
def cancelEdit(self):
self.projectName.setDisabled(True)
self.projectScale.setDisabled(True)
self.projectStartDate.setDisabled(True)
self.projectSaveBtn.setDisabled(True)
self.projectEditBtn.setText("Edit")
self.projectEditBtn.clicked.connect(self.editProject)
if self.projectEndDate.text() == " -":
self.endProjectBtn.setDisabled(False)
else:
self.endProjectBtn.setDisabled(True)
def saveProject(self, projectID):
Dialog = QtWidgets.QMessageBox()
Dialog.setWindowTitle("Confirmation Box")
Dialog.setText("Are You Sure You Want To Save The Changes?")
Dialog.setStandardButtons(QtWidgets.QMessageBox.StandardButton.Yes | QtWidgets.QMessageBox.StandardButton.No)
Option = Dialog.exec()
if Option == QtWidgets.QMessageBox.StandardButton.Yes:
projectName = self.projectName.text()
projectScale = self.projectScale.text()
projectStartDate = self.projectStartDate.date().toString("yyyy-MM-dd")
connection = pyodbc.connect(connectionString.connection_string)
cursor = connection.cursor()
cursor.execute("UPDATE Project SET projectName = ?, scale = ?, startDate = ? WHERE projectID = ?", projectName, projectScale, projectStartDate, projectID)
connection.commit()
connection.close()
self.projectName.setDisabled(True)
self.projectScale.setDisabled(True)
self.projectStartDate.setDisabled(True)
self.projectSaveBtn.setDisabled(True)
self.projectEditBtn.setText("Edit")
self.projectEditBtn.clicked.connect(self.editProject)
def endProject(self, projectID):
Dialog = QtWidgets.QMessageBox()
Dialog.setWindowTitle("Confirmation Box")
Dialog.setText("Are You Sure You Want To End This Project?")
Dialog.setStandardButtons(QtWidgets.QMessageBox.StandardButton.Yes | QtWidgets.QMessageBox.StandardButton.No)
Option = Dialog.exec()
if Option == QtWidgets.QMessageBox.StandardButton.Yes:
connection = pyodbc.connect(connectionString.connection_string)
cursor = connection.cursor()
cursor.execute("UPDATE Project SET endDate = ? WHERE projectID = ?", QDate.currentDate().toString("yyyy-MM-dd"), projectID)
connection.commit()
connection.close()
self.projectEndDate.setText(QDate.currentDate().toString("yyyy-MM-dd"))
self.endProjectBtn.setDisabled(True)