Files
gzth/yuthon_catering/controllers/controllers.py
T
2025-12-29 17:48:47 +08:00

100 lines
4.2 KiB
Python

# -*- coding: utf-8 -*-
import calendar
from collections import defaultdict
from datetime import date, datetime
from odoo import http
from odoo.http import request
class CateringController(http.Controller):
# 登记页面
@http.route(['/catering/registration/<int:employee_id>'], type='http', auth='public', methods=['GET', 'POST'], csrf=False)
def catering_portal(self, employee_id, **kwargs):
employee = request.env['hr.employee'].sudo().browse(employee_id)
today = datetime.today().strftime('%Y-%m-%d')
# 今日菜单
menu_setting = request.env['yuthon.menu.setting'].search([('menu_date', '=', today)])
menu_map = defaultdict(list)
for menu in menu_setting:
menu_map['breakfast'].extend(menu.breakfast_ids.mapped('name'))
menu_map['lunch'].extend(menu.lunch_ids.mapped('name'))
# 构建qweb上下文
qcontext = {
'employee': employee, # 员工
'today': today, # 日期
'breakfast_menu': list(set(menu_map['breakfast'])), # 早餐
'lunch_menu': list(set(menu_map['lunch'])), # 午餐
**kwargs # post请求的数据
}
if request.httprequest.method == 'GET':
# 渲染 QWeb 模板
return request.render('yuthon_catering.catering_registration_template', qcontext)
else:
# 创建数据
# [XSK] TODO 可以添加判断条件,如果今天已经登记则修改q_context['error']为错误信息 -2025/12/6 16:10
request.env['yuthon.catering'].create({
'date': kwargs.get('date'),
'employee_id': employee.id,
'breakfast': kwargs.get('breakfast') == 'true',
'lunch': kwargs.get('lunch') == 'true',
})
qcontext.update({
'tips': '提交成功',
})
# 渲染 QWeb 模板
return request.render('yuthon_catering.catering_registration_template', qcontext)
# 统计页面
@http.route(['/catering/statistics/<int:employee_id>'], type='http', auth='public', methods=['GET'], csrf=False)
def catering_create(self, employee_id, **kwargs):
# 公司信息
company = request.env.company
# 当前员工
employee = request.env['hr.employee'].sudo().browse(employee_id)
today = datetime.today().strftime('%Y-%m-%d')
# 获取筛选模式 默认筛选模式为天
filter_mode = kwargs.get('filter_mode', 'day')
# 筛选数据
filter_date = kwargs.get('filter_date')
filter_month = kwargs.get('filter_month')
department_id = kwargs.get('department_id')
if filter_mode == 'day' and not filter_date:
filter_date = today
if filter_mode == 'month' and not filter_month:
filter_month = datetime.today().strftime('%Y-%m')
# 获取所有部门
departments = request.env['hr.department'].sudo().search([])
# 构建查询条件
domain = []
# 根据筛选模式添加条件
if filter_mode == 'day' and filter_date:
domain.append(('date', '=', filter_date))
elif filter_mode == 'month' and filter_month:
# 解析月份
year, month = map(int, filter_month.split('-'))
from_date = date(year, month, 1)
to_date = date(year, month, calendar.monthrange(year, month)[1])
domain.append(('date', '>=', from_date))
domain.append(('date', '<=', to_date))
else:
if department_id:
domain.append(('dept_id', '=', department_id))
result = request.env['yuthon.catering'].sudo().search(domain)
qcontext = {
**kwargs,
'employee': employee,
'departments': departments,
'today': datetime.today().strftime('%Y-%m-%d'),
'filter_month': filter_month,
'filter_mode': kwargs.get('filter_mode', 'day'),
'breakfast': len(result.filtered(lambda x: x.breakfast)),
'lunch': len(result.filtered(lambda x: x.lunch)),
'total': sum(result.mapped('amount')),
}
return request.render('yuthon_catering.catering_statistics_template', qcontext)