prb_1/Desktop/events_list_dialog.py
2025-01-16 21:21:14 +05:00

483 lines
16 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import datetime
from PyQt6 import uic
from PyQt6.QtWidgets import QDialog, QWidget, QHBoxLayout, QLabel, QDateEdit, QMessageBox, QVBoxLayout, QPushButton
from data.connect import connect, Event, UserEvent, EventType, Attendance, VacationTimetable, WorkingCalendar, User
class EventsListDialog(QDialog):
def __init__(self, user, parent, department):
super().__init__()
uic.loadUi('ui/events_dialog_window.ui', self)
with connect() as session:
self.curses = session.query(Event).join(UserEvent).join(EventType).filter(
UserEvent.user_id == user.id,
EventType.title == 'Обучение'
).all()
self.attendances = session.query(Attendance).filter(Attendance.user_id == user.id).all()
self.vacations = session.query(VacationTimetable).filter(VacationTimetable.user_id == user.id).all()
self.past = False
self.current = False
self.future = False
self.user = user
self.parent = parent
self.department = department
self.dateEdit.dateChanged.connect(self.set_minimum_date)
self.dateEdit.setDate(datetime.date.today())
self.pushButton_3.clicked.connect(self.change_past)
self.pushButton_2.clicked.connect(self.change_current)
self.pushButton_4.clicked.connect(self.change_future)
self.label_6.setVisible(False)
self.dateEdit_2.setVisible(False)
self.label_7.setVisible(False)
self.lineEdit.setVisible(False)
self.comboBox.currentTextChanged.connect(self.change_type_event)
self.pushButton_5.clicked.connect(self.add_new_event)
self.pushButton_6.clicked.connect(self.dismiss_employee)
self.change_future()
self.change_current()
def dismiss_employee(self):
future_curser = list(
filter(
lambda x: x.datetime_event.date() > datetime.date.today(),
self.curses
)
)
if future_curser:
QMessageBox.warning(
self,
'Внимание',
'Увольнение сотрудников с предстоящими обучениями запрещено!'
)
return
self.accept_dissmiss = AcceptDismissEmployee(self.user, self)
self.accept_dissmiss.exec()
def add_new_event(self):
if self.comboBox.currentText() == 'Обучение':
with connect() as session:
type_edu_event = session.query(EventType).filter(EventType.title == 'Обучение').first()
new_event = Event(
type_id=type_edu_event.id,
datetime_event=self.dateEdit.date().toPyDate()
)
for attendance in self.attendances:
if self.dateEdit.date().toPyDate() == attendance.date:
QMessageBox.warning(
self,
'Внимание',
'Обучение и отгул не могут быть в одни и те же даты'
)
return
session.add(new_event)
session.flush()
new_user_event = UserEvent(
event_id=new_event.id,
user_id=self.user.id
)
session.add(new_user_event)
session.commit()
elif self.comboBox.currentText() == 'Отгул':
new_attendance = Attendance(
date=self.dateEdit.date().toPyDate(),
reason=self.lineEdit.text() if self.lineEdit.text() else None,
user_id=self.user.id,
)
for vacation in self.vacations:
if vacation.start_date <= new_attendance.date <= vacation.end_date:
QMessageBox.warning(
self,
'Внимание',
'Отпуск и отгул не могут быть в одни и те же даты'
)
return
for curse in self.curses:
if curse.datetime_event.date() == vacation.date:
QMessageBox.warning(
self,
'Внимание',
'Обучение и отгул не могут быть в одни и те же даты'
)
return
with connect() as session:
calendars = session.query(WorkingCalendar).all()
for calendar in calendars:
if not calendar.isworkingday and calendar.exceptiondate == vacation.date:
QMessageBox.warning(
self,
'Внимание',
'Отгул не может быть взят на выходной день'
)
return
with connect() as session:
session.add(new_attendance)
session.commit()
elif self.comboBox.currentText() == 'Отпуск':
new_vacation = VacationTimetable(
user_id=self.user.id,
start_date=self.dateEdit.date().toPyDate(),
end_date=self.dateEdit_2.date().toPyDate()
)
for attendance in self.attendances:
if new_vacation.start_date <= attendance.date <= new_vacation.end_date:
QMessageBox.warning(
self,
'Внимание',
'Отпуск и отгул не могут быть в одни и те же даты'
)
return
with connect() as session:
session.add(new_vacation)
session.commit()
self.lineEdit.clear()
QMessageBox.information(
self,
'Данные сохранены',
'Новое событие было добавлено'
)
with connect() as session:
self.curses = session.query(Event).join(UserEvent).join(EventType).filter(
UserEvent.user_id == self.user.id,
EventType.title == 'Обучение'
).all()
self.attendances = session.query(Attendance).filter(Attendance.user_id == self.user.id).all()
self.vacations = session.query(VacationTimetable).filter(VacationTimetable.user_id == self.user.id).all()
self.display_list()
def set_minimum_date(self):
self.dateEdit_2.setMinimumDate(
self.dateEdit.date().toPyDate() + datetime.timedelta(days=1)
)
def change_type_event(self):
if self.comboBox.currentText() == 'Обучение':
self.label_6.setVisible(False)
self.dateEdit_2.setVisible(False)
self.label_7.setVisible(False)
self.lineEdit.setVisible(False)
elif self.comboBox.currentText() == 'Отгул':
self.label_6.setVisible(False)
self.dateEdit_2.setVisible(False)
self.label_7.setVisible(True)
self.lineEdit.setVisible(True)
elif self.comboBox.currentText() == 'Отпуск':
self.label_6.setVisible(True)
self.dateEdit_2.setVisible(True)
self.label_7.setVisible(False)
self.lineEdit.setVisible(False)
def change_future(self):
self.future = not self.future
if self.future:
self.pushButton_4.setStyleSheet('''
QPushButton {
background-color: #2f9836;
}
''')
else:
self.pushButton_4.setStyleSheet('''
QPushButton {
background-color: rgb(228, 244, 204);
}
''')
self.display_list()
def change_current(self):
self.current = not self.current
if self.current:
self.pushButton_2.setStyleSheet('''
QPushButton {
background-color: #2f9836;
}
''')
else:
self.pushButton_2.setStyleSheet('''
QPushButton {
background-color: rgb(228, 244, 204);
}
''')
self.display_list()
def change_past(self):
self.past = not self.past
if self.past:
self.pushButton_3.setStyleSheet('''
QPushButton {
background-color: #2f9836;
}
''')
else:
self.pushButton_3.setStyleSheet('''
QPushButton {
background-color: rgb(228, 244, 204);
}
''')
self.display_list()
def display_list(self):
for i in range(self.verticalLayout.count()):
self.verticalLayout.itemAt(i).widget().close()
for i in range(self.verticalLayout_2.count()):
self.verticalLayout_2.itemAt(i).widget().close()
for i in range(self.verticalLayout_3.count()):
self.verticalLayout_3.itemAt(i).widget().close()
curses = sorted(self.curses, key=lambda x: x.datetime_event)
attendances = sorted(self.attendances, key=lambda x: x.date)
vacations = sorted(self.vacations, key=lambda x: x.start_date)
if self.past:
past_curser = list(
filter(
lambda x: x.datetime_event.date() < datetime.date.today(),
curses
)
)
for i in past_curser:
event_widget = EventWidget(i.datetime_event)
self.verticalLayout.addWidget(event_widget)
past_attendances = list(
filter(
lambda x: x.date < datetime.date.today(),
attendances
)
)
for i in past_attendances:
event_widget = EventWidget(i.date, reason=i.reason)
self.verticalLayout_2.addWidget(event_widget)
past_vacations = list(
filter(
lambda x: x.start_date < datetime.date.today(),
vacations
)
)
for i in past_vacations:
event_widget = EventWidget(i.start_date, i.end_date)
self.verticalLayout_3.addWidget(event_widget)
if self.current:
current_curser = list(
filter(
lambda x: x.datetime_event.date() == datetime.date.today(),
curses
)
)
for i in current_curser:
event_widget = EventWidget(i.datetime_event)
self.verticalLayout.addWidget(event_widget)
current_attendances = list(
filter(
lambda x: x.date == datetime.date.today(),
attendances
)
)
for i in current_attendances:
event_widget = EventWidget(i.date, reason=i.reason)
self.verticalLayout_2.addWidget(event_widget)
current_vacations = list(
filter(
lambda x: x.start_date <= datetime.date.today() <= x.end_date,
vacations
)
)
for i in current_vacations:
event_widget = EventWidget(i.start_date, i.end_date)
self.verticalLayout_3.addWidget(event_widget)
if self.future:
future_curser = list(
filter(
lambda x: x.datetime_event.date() > datetime.date.today(),
curses
)
)
for i in future_curser:
event_widget = EventWidget(i.datetime_event)
self.verticalLayout.addWidget(event_widget)
future_attendances = list(
filter(
lambda x: x.date > datetime.date.today(),
attendances
)
)
for i in future_attendances:
event_widget = EventWidget(i.date, reason=i.reason)
self.verticalLayout_2.addWidget(event_widget)
future_vacations = list(
filter(
lambda x: x.start_date > datetime.date.today(),
vacations
)
)
for i in future_vacations:
event_widget = EventWidget(i.start_date, i.end_date)
self.verticalLayout_3.addWidget(event_widget)
class AcceptDismissEmployee(QDialog):
def __init__(self, user, parent):
super().__init__()
self.setWindowTitle('Увольнение сотрудника')
self.lyt = QVBoxLayout()
self.user = user
self.parent = parent
lbl1 = QLabel(f'Вы уверены, что хотите уволить сотрудника: {user.last_name} {user.first_name}?')
self.lyt.addWidget(lbl1)
h_lyt = QHBoxLayout()
button1 = QPushButton('Уволить')
button1.clicked.connect(self.dismiss_employee)
h_lyt.addWidget(button1)
button2 = QPushButton('Отмена')
h_lyt.addWidget(button2)
button2.clicked.connect(self.close)
self.lyt.addLayout(h_lyt)
self.setLayout(self.lyt)
def dismiss_employee(self):
with connect() as session:
user = session.query(User).filter(User.id == self.user.id).first()
user.is_dismissed = True
user.dismiss_date = datetime.date.today()
session.commit()
QMessageBox.information(
self,
'Внимание',
'Сотрудник был уволен'
)
future_attendances = list(
filter(
lambda x: x.date > datetime.date.today(),
self.parent.attendances
)
)
future_vacations = list(
filter(
lambda x: x.start_date > datetime.date.today(),
self.parent.vacations
)
)
with connect() as session:
for i in future_attendances:
session.delete(i)
for i in future_vacations:
session.delete(i)
self.parent.parent.select_department(self.parent.department.id)
self.parent.close()
self.close()
class EventWidget(QWidget):
def __init__(self, start_date, end_date=None, reason=None):
super().__init__()
self.setMinimumHeight(90)
self.setMaximumHeight(90)
self.layout = QHBoxLayout()
label1 = QLabel(
str(start_date) if end_date is None else 'Дата начала:\n' + str(start_date)
)
self.layout.addWidget(label1)
if not end_date is None:
label2 = QLabel(
'Дата окончания:\n' + str(end_date)
)
self.layout.addWidget(label2)
if not reason is None:
label3 = QLabel('Причина:\n' + reason)
self.layout.addWidget(label3)
wdg = QWidget()
wdg.setLayout(self.layout)
wdg.setStyleSheet('''
QWidget {
border: 1px solid black;
}
QLabel {
border: none;
}
''')
lyt = QHBoxLayout()
lyt.addWidget(wdg)
self.setLayout(lyt)