Odoo-Tutorial/cenhomes_student/models/models_phong_thu_tuc.py
2025-03-04 16:53:07 +07:00

223 lines
11 KiB
Python

import os
import re
from odoo import models, fields, api, _
from odoo.exceptions import ValidationError
from .template import SimpleTemplate
from ..utils.mail import send_email
path_file = os.path.dirname(__file__)
path_template_thu_xac_nhan = f'{path_file}/../static/template/thu_xac_nhan_dang_ky'
template_online = SimpleTemplate(open(f'{path_template_thu_xac_nhan}/online-khoale.html').read())
template_chuyendoivanbang = SimpleTemplate(open(f'{path_template_thu_xac_nhan}/chuyendoivanbang.html').read())
template_dacob1 = SimpleTemplate(open(f'{path_template_thu_xac_nhan}/dacob1.html').read())
template_duhockep = SimpleTemplate(open(f'{path_template_thu_xac_nhan}/duhockep.html').read())
convert_value_enum_to_text = {
False: '',
'y': '',
'n': 'Không',
'duhockep': 'Du học kép',
'chuyendoivanbang': 'Chuyển đổi văn bằng',
'dacob1': 'Đã có B1',
'online-khoale': 'Online - Khóa lẻ',
'hanoi': 'Hà Nội',
'hochiminh': 'Hồ Chí Minh',
'daotaotaptrung': 'Đào tạo tập trung',
'daotaoonline': 'Đào tạo online',
}
class CenhomesHocVienPhongThuTuc(models.Model):
_name = 'cenhomes.hoc_vien_phong_thu_tuc'
_description = 'DUHOCKEP học viên phòng thủ tục'
_sql_constraints = [
('email_unique', 'unique(email)', 'Email đã tồn tại trong hệ thống!'),
('phone_unique', 'unique(so_dien_thoai_lien_he)', 'Số điện thoại đã tồn tại trong hệ thống!')
]
ma_ho_so = fields.Char(string='Mã hồ sơ')
ten = fields.Char(string='Tên', required=True)
ho = fields.Char(string='Họ', required=True)
dia_chi_thuong_tru = fields.Char(string='Địa chỉ thường trú')
dia_chi_hien_tai = fields.Char(string='Địa chỉ hiện tại')
so_dien_thoai_lien_he = fields.Char(string='Số điện thoại liên hệ', required=True)
email = fields.Char(string='Địa chỉ Email', required=True)
ngay_thang_nam_sinh = fields.Date(string='Ngày sinh')
cccd = fields.Char(string='Số CCCD')
cccd_ngay_cap = fields.Date(string='Ngày cấp CCCD')
cccd_noi_cap = fields.Char(string='Nơi cấp CCCD')
chuong_trinh_dang_ky = fields.Selection(string='Chương trình đăng ký',
selection=[('duhockep', 'Du học kép'),
('chuyendoivanbang', 'Chuyển đổi văn bằng'),
('dacob1', 'Đã có B1'),
('online-khoale', 'Online - Khóa lẻ')])
truong_cap_iii = fields.Char(string='Trường cấp III/PTTH')
truong_cap_iii_nam_tot_nghiep = fields.Integer(string='Năm tốt nghiệp', help="Năm tốt nghiệp trường cấp III/PTTH ")
truong_cap_iii_dia_chi = fields.Char(string='Địa chỉ')
tren_cap_iii = fields.Char(string='Trường Trung cấp/Cao đẳng/Đại học')
tren_cap_iii_nganh_hoc = fields.Char(string='Ngành học')
tren_cap_iii_nam_hoc = fields.Integer(string='Năm học')
tren_cap_iii_dia_chi = fields.Char(string='Địa chỉ')
bo_ho_ten = fields.Char(string='Họ tên bố')
bo_nam_sinh = fields.Integer(string='Năm sinh', help='Năm sinh vợ chồng')
bo_nghe_nghiep = fields.Char(string='Nghề nghiệp', help='Nghề nghiệp vợ chồng')
bo_so_dien_thoai = fields.Char(string='Số điện thoại', help='Số điện thoại vợ chồng')
me_ho_ten = fields.Char(string='Họ tên mẹ')
me_nam_sinh = fields.Integer(string='Năm sinh', help='Năm sinh mẹ')
me_nghe_nghiep = fields.Char(string='Nghề nghiệp', help='Nghề nghiệp mẹ')
me_so_dien_thoai = fields.Char(string='Số điện thoại', help='Số điện thoại mẹ')
vo_chong_ho_ten = fields.Char(string='Họ tên vợ/chồng', help='Họ tên vợ/chồng (nếu có)')
vo_chong_nam_sinh = fields.Integer(string='Năm sinh', help='Năm sinh vợ chồng')
vo_chong_nghe_nghiep = fields.Char(string='Nghề nghiệp', help='Nghề nghiệp vợ chồng')
vo_chong_so_dien_thoai = fields.Char(string='Số điện thoại', help='Số điện thoại vợ chồng')
con_ruot = fields.Char(string='Họ tên con')
anh_chi_em = fields.Text(string='Họ tên anh/chị/em/ruột, nghề nghiệp, địa chỉ (nếu có)')
benh_tat = fields.Text(
string='Bạn đã từng mắc các bệnh truyền nhiễm, các bệnh về tâm thần, các bệnh hạn chế vận động nào bao giờ '
'chưa? Nếu có ghi cụ thể')
nguoi_than_tai_duc = fields.Text(
string='Bạn có người thân hiện sinh sống tại CHLB Đức không? Quan hệ thế nào? Người thân đang làm gì? ở bang '
'nào nước Đức?')
viec_lam_sau_tot_nghiep = fields.Text(
string='Sau khi tốt nghiệp (bậc học cao nhất), bạn đã làm việc gì, ở đâu, vào khoảng thời gian nào? Vui lòng '
'ghi chi tiết?')
thong_tin_tieng_duc = fields.Text(
string='Bạn đã từng học tiếng Đức hay chưa? Nếu có đã đạt trình độ nào (A1, A2, B1, B2, C1, C2)? Chứng chỉ B1 '
'được cấp khi nào? Chứng chỉ của đơn vị nào cấp?')
hieu_biet_cenacademy_qua_nguon = fields.Text(
string='Bạn/gia đình bạn biết đến Chương trình của Cen Academy qua nguồn nào?')
dang_ky_hoc_tieng_duc = fields.Selection(string='Đăng ký học tiếng Đức',
selection=[
('y', ''),
('n', 'Không')], default=None)
noi_dao_tao = fields.Selection(string='Nơi đào tạo',
selection=[('hanoi', 'Hà Nội'),
('hochiminh', 'Hồ Chí Minh')], default=None)
khoa_hoc = fields.Char(string='Khóa học')
hinh_thuc_dao_tao = fields.Selection(string='Hình thức đào tạo',
selection=[
('daotaotaptrung', 'Đào tạo tập trung'),
('daotaoonline', 'Đào tạo online')], default=None)
nganh_hoc_mong_muon = fields.Char(string='Ngành học mong muốn')
gui_mail = fields.Boolean(string='Trạng thái mail')
sale = fields.Many2one(comodel_name='res.users', string='Nhân viên kinh doanh phụ trách',
domain=lambda self: [
("groups_id", "=", self.env.ref("cenhomes_student.group_cenhomes_sale").id),
("groups_id", "!=", self.env.ref("cenhomes_student.group_cenhomes_admin").id)])
def write(self, vals):
if 'ma_ho_so' in vals and self.ma_ho_so != vals['ma_ho_so']:
hoc_viens = self.env['cenhomes.hoc_vien'].sudo().search([])
if vals['ma_ho_so'] in {h.ma_ho_so: '' for h in hoc_viens}.keys():
raise ValidationError(_('Mã hồ sơ đã tồn tại'))
if len(vals.keys()) == 0:
return
return super().write(vals)
@api.constrains('email')
def _check_valid_email(self):
regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b'
if not re.fullmatch(regex, self.email):
raise ValidationError(_('Email không đúng định dạng'))
hocviens = self.search([]) - self
value = [x.email.lower() for x in hocviens]
if self.email and self.email.lower() in value:
raise ValidationError(_('Email đã tồn tại'))
return True
@api.constrains('so_dien_thoai_lien_he')
def _check_valid_so_dien_thoai_lien_he(self):
regex = r'^[0-9]*$'
if not re.fullmatch(regex, self.so_dien_thoai_lien_he):
raise ValidationError(_('Số điện thoại không đúng định dạng'))
hocviens = self.search([]) - self
value = [x.so_dien_thoai_lien_he.lower() for x in hocviens]
if self.so_dien_thoai_lien_he and self.so_dien_thoai_lien_he.lower() in value:
raise ValidationError(_('Số điện thoại đã tồn tại'))
return True
def action_send_email_notification(self):
if not self.email or not self.ma_ho_so or self.gui_mail:
if not self.email:
message = "Thiếu Email"
elif not self.ma_ho_so:
message = "Thiếu mã học viên"
else:
message = "Học viên đã được gửi mail thông báo"
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'message': message,
'type': 'danger',
'sticky': False
},
}
result = send_email(to=self.email,
subject=f'[CEN ACADEMY] XÁC NHẬN ĐĂNG KÝ THAM GIA CHƯƠNG TRÌNH VÀ '
f'HƯỚNG DẪN HOÀN TẤT THỦ TỤC NHẬP HỌC',
content=self._create_email_xac_nhan_dang_ky(type_template=self.chuong_trinh_dang_ky),
images=[(f'{path_template_thu_xac_nhan}/cenacademy_tk_ngan_hang.png',
'cenacademy_tk_ngan_hang')] if self.chuong_trinh_dang_ky != 'online-khoale' else [])
if result is None:
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'message': 'Loại chương trình đăng ký không hợp lệ',
'type': 'danger',
'sticky': False
},
}
self.write({'gui_mail': True})
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'message': f'Gửi thông báo thành công tới email {self.email}',
'type': 'success',
'sticky': False
},
}
def _create_email_xac_nhan_dang_ky(self, type_template: str):
if type_template not in ['chuyendoivanbang', 'dacob1', 'duhockep', 'online-khoale']:
return None
data = self.copy_data()[0]
for k in data.keys():
if k in ['dang_ky_hoc_tieng_duc', 'hinh_thuc_dao_tao', 'nganh_hoc_mong_muon', 'noi_dao_tao']:
data[k] = convert_value_enum_to_text.get(data[k]) if data[k] in convert_value_enum_to_text.keys() else data[k]
elif not data[k]:
data[k] = ''
if type_template == 'chuyendoivanbang':
return template_chuyendoivanbang.render(data)
elif type_template == 'dacob1':
return template_dacob1.render(data)
elif type_template == 'duhockep':
return template_duhockep.render(data)
return template_online.render(data)
@api.model
def create(self, vals_list):
if self.env.user.has_group('cenhomes_student.group_cenhomes_sale') and \
not self.env.user.has_group('cenhomes_student.group_cenhomes_phong_thu_tuc'):
vals_list['sale'] = self.env.user.id
return super(CenhomesHocVienPhongThuTuc, self).create(vals_list)