164 lines
5.8 KiB
Python
164 lines
5.8 KiB
Python
from PyQt6 import uic
|
|
from PyQt6.QtWidgets import QDialog, QMessageBox
|
|
|
|
from data.connect import connect, Post, User
|
|
|
|
|
|
class EmployeeDialogWidget(QDialog):
|
|
def __init__(self, department, employees, parent, user=None):
|
|
super().__init__()
|
|
|
|
uic.loadUi('ui/employee_dialog_window.ui', self)
|
|
|
|
self.lineEdit_5.setText(department.title)
|
|
|
|
self.user = user
|
|
self.department = department
|
|
self.parent = parent
|
|
|
|
for employee in employees:
|
|
self.comboBox_2.addItem(f'{employee.last_name} {employee.first_name} - {str(employee.id)}')
|
|
|
|
if user is not None:
|
|
if user.helper_id == employee.id:
|
|
self.comboBox_2.setCurrentText(f'{employee.last_name} {employee.first_name} - {str(employee.id)}')
|
|
|
|
with connect() as session:
|
|
self.posts = session.query(Post).all()
|
|
|
|
for post in self.posts:
|
|
self.comboBox.addItem(f'{post.title} - {str(post.id)}')
|
|
|
|
if user is not None:
|
|
if post.id == user.post_id:
|
|
self.comboBox.setCurrentText(f'{post.title} - {str(post.id)}')
|
|
|
|
self.pushButton_2.clicked.connect(self.check_form)
|
|
|
|
if user is not None:
|
|
self.lineEdit_2.setText(user.first_name)
|
|
self.lineEdit.setText(user.last_name)
|
|
self.lineEdit_3.setText(user.patronymic)
|
|
self.lineEdit_6.setText(user.work_phone)
|
|
self.lineEdit_4.setText(user.phone)
|
|
self.lineEdit_8.setText(user.office)
|
|
self.dateEdit.setDate(user.birthday)
|
|
self.lineEdit_9.setText(user.other_info)
|
|
self.lineEdit_7.setText(user.email)
|
|
|
|
def check_form(self):
|
|
if not self.lineEdit.text() or not self.lineEdit_2.text() or not self.lineEdit_3.text():
|
|
QMessageBox.warning(
|
|
self,
|
|
'Внимание',
|
|
'Поля: Фамилия, имя, отчество обязательны для заполнения'
|
|
)
|
|
return
|
|
|
|
if not self.lineEdit_7.text():
|
|
QMessageBox.warning(
|
|
self,
|
|
'Внимание',
|
|
'Поле электронной почты обязательно для заполнения'
|
|
)
|
|
return
|
|
|
|
if not self.lineEdit_8.text():
|
|
QMessageBox.warning(
|
|
self,
|
|
'Внимание',
|
|
'Поле кабинета обязательно для заполнения'
|
|
)
|
|
return
|
|
|
|
email_text = self.lineEdit_7.text()
|
|
|
|
# 'andrei@duvakin.ru'
|
|
|
|
if '@' in email_text:
|
|
splited_email = email_text.split('@')
|
|
|
|
if len(splited_email) == 2:
|
|
user, server = splited_email
|
|
|
|
splited_server = server.split('.')
|
|
|
|
if len(splited_server) != 2:
|
|
QMessageBox.warning(
|
|
self,
|
|
'Внимание',
|
|
'Неверный формат почты'
|
|
)
|
|
return
|
|
else:
|
|
QMessageBox.warning(
|
|
self,
|
|
'Внимание',
|
|
'Неверный формат почты'
|
|
)
|
|
return
|
|
else:
|
|
QMessageBox.warning(
|
|
self,
|
|
'Внимание',
|
|
'Неверный формат почты'
|
|
)
|
|
return
|
|
|
|
if self.user is None:
|
|
new_user = User(
|
|
first_name=self.lineEdit_2.text(),
|
|
last_name=self.lineEdit.text(),
|
|
patronymic=self.lineEdit_3.text(),
|
|
work_phone=self.lineEdit_6.text(),
|
|
phone=self.lineEdit_4.text(),
|
|
office=self.lineEdit_8.text(),
|
|
birthday=self.dateEdit.date().toPyDate(),
|
|
other_info=self.lineEdit_9.text(),
|
|
email=self.lineEdit_7.text(),
|
|
helper_id=int(self.comboBox_2.currentText().split()[-1]),
|
|
post_id=int(self.comboBox.currentText().split()[-1]),
|
|
department_id=self.department.id
|
|
)
|
|
with connect() as session:
|
|
session.add(
|
|
new_user
|
|
)
|
|
session.commit()
|
|
|
|
QMessageBox.information(
|
|
self,
|
|
'Данные сохранены',
|
|
'Сотрудник был успешно добавлен'
|
|
)
|
|
self.parent.select_department(self.department.id)
|
|
|
|
self.close()
|
|
return
|
|
else:
|
|
with connect() as session:
|
|
user = session.query(User).filter(User.id == self.user.id).first()
|
|
|
|
user.first_name = self.lineEdit_2.text(),
|
|
user.last_name = self.lineEdit.text(),
|
|
user.patronymic = self.lineEdit_3.text(),
|
|
user.work_phone = self.lineEdit_6.text(),
|
|
user.phone = self.lineEdit_4.text(),
|
|
user.office = self.lineEdit_8.text(),
|
|
user.birthday = self.dateEdit.date().toPyDate(),
|
|
user.other_info = self.lineEdit_9.text(),
|
|
user.email = self.lineEdit_7.text(),
|
|
user.helper_id = int(self.comboBox_2.currentText().split()[-1]),
|
|
user.post_id = int(self.comboBox.currentText().split()[-1]),
|
|
user.department_id = self.department.id
|
|
|
|
session.commit()
|
|
|
|
QMessageBox.information(
|
|
self,
|
|
'Данные сохранены',
|
|
'Данные сотрудника были обновлены'
|
|
)
|
|
self.parent.select_department(self.department.id)
|
|
return
|