diff --git a/yuthon_salary/data/meal_cron.xml b/yuthon_salary/data/meal_cron.xml index 33cc43686..251ada9db 100644 --- a/yuthon_salary/data/meal_cron.xml +++ b/yuthon_salary/data/meal_cron.xml @@ -27,5 +27,18 @@ -1 + + 定时任务:每月第一个工作日发送餐费缴费提醒 + + code + model.cron_send_monthly_meal_reminder() + + + 1 + months + + -1 + + diff --git a/yuthon_salary/models/yuthon_meal_expenses.py b/yuthon_salary/models/yuthon_meal_expenses.py index d70a77f37..c181e5e5b 100644 --- a/yuthon_salary/models/yuthon_meal_expenses.py +++ b/yuthon_salary/models/yuthon_meal_expenses.py @@ -247,6 +247,120 @@ class YuthonMealExpenses(models.Model): }) return True + def cron_send_monthly_meal_reminder(self): + """定时任务:每月第一个工作日发送餐费缴费提醒""" + from datetime import datetime, timedelta + import calendar + + # 检查是否是工作日 + today = datetime.now() + if today.weekday() >= 5: # 0-4是工作日,5-6是周末 + # 记录日志 + self.env['ir.logging'].sudo().create({ + 'name': '餐费缴费提醒', + 'type': 'info', + 'message': f'今天{today.strftime("%Y-%m-%d")}是周末,跳过发送餐费缴费提醒', + 'path': __name__, + 'func': 'cron_send_monthly_meal_reminder', + }) + return True + + # 计算上月的年份和月份 + last_month = today - timedelta(days=today.day) + last_year = str(last_month.year) + last_month_num = str(last_month.month) + + # 计算当前月的年份和月份 + current_year = str(today.year) + current_month = str(today.month) + + # 查询所有状态为待缴的餐费记录 + meals = self.sudo().search([ + ('toll_state', '=', 'to_do'), + ('select_year', '=', last_year), + ('select_time', '=', last_month_num), + ]) + + # 按员工分组 + employee_meals = {} + for meal in meals: + if meal.employee_id not in employee_meals: + employee_meals[meal.employee_id] = meal + + # 发送提醒 + sent_count = 0 + for employee, meal in employee_meals.items(): + if not employee or not employee.user_id or not employee.wecom_userid: + continue + + # 检查是否已经发送过提醒(通过日志记录) + log_domain = [ + ('name', '=', '餐费缴费提醒'), + ('message', 'like', f'{last_year}年{last_month_num}月'), + ('message', 'like', employee.name), + ] + existing_log = self.env['ir.logging'].sudo().search(log_domain, limit=1) + if existing_log: + continue + + # 计算上月餐费金额 + last_month_amount = meal.total_payment + + # 计算当月餐费金额(如果有) + current_month_meal = self.sudo().search([ + ('employee_id', '=', employee.id), + ('select_year', '=', current_year), + ('select_time', '=', current_month), + ], limit=1) + current_month_amount = current_month_meal.total_payment if current_month_meal else 0 + + # 构建提醒内容 + employee_name = employee.name or '' + description = ( + f'【餐费缴费提醒】亲爱的同事,{last_year}年{last_month_num}月餐费明细已出,你的上月餐费金额为{last_month_amount}元,' + f'请尽快核对并缴费到指定账户:44001580909059000222广州市天河投资集团有限公司(转账时请备注"姓名+{last_month_num}月餐费")。' + f'有任何问题,随时找办公室小伙伴沟通呀!' + ) + + # 发送提醒 + try: + self.sudo().env["wecom.apps"].sync_send_message_textcard( + category='can', + user_id=employee.wecom_userid, + title="餐费缴费提醒", + description=description, + url=f'https://phone.thtzjt.com/phone/Meal/{employee.user_id.id}', + btntxt="详细信息", + ) + sent_count += 1 + + # 记录日志,避免重复发送 + self.env['ir.logging'].sudo().create({ + 'name': '餐费缴费提醒', + 'type': 'info', + 'message': f'已发送{last_year}年{last_month_num}月餐费缴费提醒给{employee_name}', + 'path': __name__, + 'func': 'cron_send_monthly_meal_reminder', + }) + except Exception as e: + self.env['ir.logging'].sudo().create({ + 'name': '餐费缴费提醒', + 'type': 'error', + 'message': f'发送{last_year}年{last_month_num}月餐费缴费提醒给{employee_name}失败:{str(e)}', + 'path': __name__, + 'func': 'cron_send_monthly_meal_reminder', + }) + + # 记录总发送情况 + self.env['ir.logging'].sudo().create({ + 'name': '餐费缴费提醒', + 'type': 'info', + 'message': f'成功发送{sent_count}条{last_year}年{last_month_num}月餐费缴费提醒', + 'path': __name__, + 'func': 'cron_send_monthly_meal_reminder', + }) + return True + def action_view_detail(self): self.ensure_one() date_start, date_end = self._get_date_range()