# -*- coding: utf-8 -*- from odoo import http from odoo.http import request import json, re import logging _logger = logging.getLogger(__name__) class FinancialFormController(http.Controller): @http.route('/financial/form/', type='http', auth='public', website=True, csrf=False) def financial_form_page(self, employee_id, **kwargs): """显示财务数据收集表单""" try: # 获取表单配置 form_config = self._get_financial_form_config(employee_id) # 获取下拉框选项 dropdown_options = self._get_dropdown_options() return request.render('yuthon_account_report.financial_form_template', { 'form_config': form_config, 'dropdown_options': dropdown_options }) except Exception as e: _logger.error(f'加载财务表单失败: {str(e)}') return f"表单加载失败,请稍后重试: {str(e)}" @http.route('/financial/form/submit', type='http', auth='public', methods=['POST'], csrf=False) def submit_financial_form(self, **kwargs): # 这里拿到的是字典示例如下{'company_name': '123', 'report_period': '2024Q2', 'operating_income': '23123112', 'operating_in': '312312312312', 'income_analysis[current_period]': '31231321', 'income_analysis[previous_period]': '1231313', 'income_analysis[item_name]': 'dzfdsfsfsdf'} """处理财务表单提交""" try: # 解析JSON数据 data = kwargs tar_data = {} detail_dict = {} for key, value in data.items(): if 'E' in key: # 说明是员工 tar_data['employee_id'] = int(''.join(filter(str.isdigit, key))) elif '[' in key: # 说明是项目第一个数字是项目的id,中括号内是key,value是值 match = re.match(r'(\d+)\[(.*)\]', key) if match: collection_id = int(match.group(1)) # 数字部分 english = match.group(2) # 英文部分 if collection_id not in detail_dict.keys(): detail_dict[collection_id] = { 'collection_id': collection_id, english: value } else: detail_dict[collection_id][english] = value else: tar_data[key] = value if 'employee_id' not in tar_data.keys(): _logger.error(f'表单提交错误: 缺乏员工ID') return { 'success': False, 'message': f'服务器错误: 缺乏员工ID' } detail_list = [] for v in detail_dict.values(): detail_list.append((0, 0, v)) tar_data['sheet_line_ids'] = detail_list # 保存数据 records = request.env['financial.sheet.collection'].sudo().create(tar_data) return { 'success': True, 'message': '财务数据提交成功!', 'redirect_url': '/custom/success' } except Exception as e: _logger.error(f'表单提交错误: {str(e)}') return { 'success': False, 'message': f'服务器错误: {str(e)}' } def _get_financial_form_config(self, employee_id): # 数据格式 # { # 'id': 'company_name', # 'label': '公司名称', # 'field_type': 'text', # 'required': True, # 'placeholder': '请输入公司全称', # 'max_length': 100 # }, # { # 'id': 'report_period', # 'label': '报告期间', # 'field_type': 'select', # 'required': True, # 'placeholder': '请选择报告期间', # 'options_source': 'report_periods' # }, # { # 'id': 'operating_income', # 'label': '营业收入', # 'field_type': 'number', # 'required': True, # 'min_value': 0, # 'placeholder': '0.00', # 'help_text': '单位:元' # }, # { # 'id': 'operating_in', # 'label': '多行输入', # 'field_type': 'textarea', # 'required': True, # 'min_value': 0, # 'placeholder': '0.00', # 'help_text': '这里面可以输入多行' # }, # { # 'id': 'income_analysis', # 'label': '收入分析表', # 'field_type': 'table', # 'required': True, # 'table_config': { # 'row_count': 3, # 固定3行数据 # 'columns': [ # { # 'name': 'current_period', # 'label': '本期金额', # 'type': 'number', # 'required': True, # 'placeholder': '请输入本期金额' # }, # { # 'name': 'previous_period', # 'label': '上期金额', # 'type': 'number', # 'required': True, # 'placeholder': '请输入上期金额' # }, # { # 'name': 'item_name', # 'label': '说明', # 'type': 'text', # 'required': False, # 'placeholder': '请输入说明' # } # ] # } # } """获取财务表单配置""" form_config = { 'form_title': '财务数据收集表', 'form_description': '请根据实际情况填写以下财务数据', 'form_type': 'financial', 'sections': [{ 'id': 'essential', 'label': '财务信息', 'fields': [] }] } tar = form_config['sections'][0]['fields'] employee = request.env['hr.employee'].sudo().browse(int(employee_id)) if employee: tar.append({ 'id': 'E' + str(employee_id), 'label': '提报人', 'value': employee.name, 'field_type': 'text', 'required': True, 'readonly': True }) company = employee.company_id tar.append({ 'id': 'C' + str(company.id), 'label': '提报公司', 'value': company.name, 'field_type': 'text', 'required': True, 'readonly': True }) cons = request.env['sheet.collection.setting'].get_feild_form(int(employee_id)) for con in cons: one = { 'id': str(con.get('id')), 'label': con.get('label'), 'field_type': 'table', 'required': True, 'table_config': { 'row_count': 3, # 固定3行数据 'columns': [ { 'name': 'this_issue_number', 'label': '本期金额', 'type': 'number', 'required': True, 'placeholder': '请输入本期金额' }, { 'name': 'same_last_year', 'label': '上期金额', 'type': 'number', 'required': True, 'placeholder': '请输入上期金额' }, { 'name': 'remake', 'label': '说明', 'type': 'text', 'required': False, 'placeholder': '请输入说明' } ] } } tar.append(one) return form_config def _get_dropdown_options(self): """获取下拉框选项""" return { 'report_periods': [ {'value': '1', 'label': '1'}, {'value': '2', 'label': '2'}, {'value': '3', 'label': '3'}, {'value': '4', 'label': '4'}, {'value': '5', 'label': '5'}, {'value': '6', 'label': '6'}, {'value': '7', 'label': '7'}, {'value': '8', 'label': '8'}, {'value': '9', 'label': '9'}, {'value': '10', 'label': '10'}, {'value': '11', 'label': '11'}, {'value': '12', 'label': '12'} ], 'currencies': [ {'value': 'CNY', 'label': '人民币'}, {'value': 'USD', 'label': '美元'}, {'value': 'EUR', 'label': '欧元'}, {'value': 'JPY', 'label': '日元'}, {'value': 'HKD', 'label': '港币'} ] } @http.route('/information/form/', type='http', auth='public', website=True, csrf=False) def information_form_page(self, employee_id, **kwargs): """显示财务数据收集表单""" try: # 获取表单配置 form_config = self._get_information_form_config(employee_id) # 获取下拉框选项 dropdown_options = self._get_dropdown_options() return request.render('yuthon_account_report.information_form_template', { 'form_config': form_config, 'dropdown_options': dropdown_options }) except Exception as e: _logger.error(f'加载财务表单失败: {str(e)}') return f"表单加载失败,请稍后重试: {str(e)}" def _get_information_form_config(self, employee_id): # 数据格式 # { # 'id': 'company_name', # 'label': '公司名称', # 'field_type': 'text', # 'required': True, # 'placeholder': '请输入公司全称', # 'max_length': 100 # }, # { # 'id': 'report_period', # 'label': '报告期间', # 'field_type': 'select', # 'required': True, # 'placeholder': '请选择报告期间', # 'options_source': 'report_periods' # }, # { # 'id': 'operating_income', # 'label': '营业收入', # 'field_type': 'number', # 'required': True, # 'min_value': 0, # 'placeholder': '0.00', # 'help_text': '单位:元' # }, # { # 'id': 'operating_in', # 'label': '多行输入', # 'field_type': 'textarea', # 'required': True, # 'min_value': 0, # 'placeholder': '0.00', # 'help_text': '这里面可以输入多行' # }, # { # 'id': 'income_analysis', # 'label': '收入分析表', # 'field_type': 'table', # 'required': True, # 'table_config': { # 'row_count': 3, # 固定3行数据 # 'columns': [ # { # 'name': 'current_period', # 'label': '本期金额', # 'type': 'number', # 'required': True, # 'placeholder': '请输入本期金额' # }, # { # 'name': 'previous_period', # 'label': '上期金额', # 'type': 'number', # 'required': True, # 'placeholder': '请输入上期金额' # }, # { # 'name': 'item_name', # 'label': '说明', # 'type': 'text', # 'required': False, # 'placeholder': '请输入说明' # } # ] # } # } """获取财务表单配置""" form_config = { 'form_title': '信息汇总收集表', 'form_description': '请根据实际情况填写以下财务数据', 'form_type': 'financial', 'sections': [{ 'id': 'essential', 'label': '财务信息', 'fields': [] }] } tar = form_config['sections'][0]['fields'] employee = request.env['hr.employee'].sudo().browse(int(employee_id)) if employee: tar.append({ 'id': 'E' + str(employee_id), 'label': '提报人', 'value': employee.name, 'field_type': 'text', 'required': True, 'readonly': True }) company = employee.company_id tar.append({ 'id': 'C' + str(company.id), 'label': '提报公司', 'value': company.name, 'field_type': 'text', 'required': True, 'readonly': True }) tar.append({ 'id': 'year', 'label': '填报年份', 'field_type': 'number', 'readonly': True, 'value': 2025, 'required': True, 'min_value': 0, }) tar.append({ 'id': 'month', 'label': '填报月份', 'readonly': True, 'value': '12', 'field_type': 'select', 'required': True, 'placeholder': '请选择填报月份', 'options_source': 'report_periods' }) cons = request.env['summary.table.setting'].get_feild_form(int(employee_id)) for con in cons: one = { 'id': str(con.get('id')), 'label': con.get('label'), 'field_type': 'table', 'required': True, 'table_config': { 'row_count': 2, # 固定3行数据 'columns': [ { 'name': 'this_year', 'label': '本期金额', 'type': 'number', 'required': True, 'placeholder': '请输入本期金额' }, { 'name': 'remake', 'label': '说明', 'type': 'text', 'required': False, 'placeholder': '请输入说明' } ] } } tar.append(one) return form_config @http.route('/information/form/submit', type='http', auth='public', methods=['POST'], csrf=False) def submit_information_form(self, **kwargs): # 这里拿到的是字典示例如下{'company_name': '123', 'report_period': '2024Q2', 'operating_income': '23123112', 'operating_in': '312312312312', 'income_analysis[current_period]': '31231321', 'income_analysis[previous_period]': '1231313', 'income_analysis[item_name]': 'dzfdsfsfsdf'} """处理财务表单提交""" try: # 解析JSON数据 data = kwargs tar_data = {} detail_dict = {} for key, value in data.items(): if 'E' in key: # 说明是员工 tar_data['employee_id'] = int(''.join(filter(str.isdigit, key))) elif '[' in key: # 说明是项目第一个数字是项目的id,中括号内是key,value是值 match = re.match(r'(\d+)\[(.*)\]', key) if match: collection_id = int(match.group(1)) # 数字部分 english = match.group(2) # 英文部分 if collection_id not in detail_dict.keys(): detail_dict[collection_id] = { 'table_id': collection_id, english: value } else: detail_dict[collection_id][english] = value else: if key == 'year': tar_data[key] = int(value) else: tar_data[key] = value if 'employee_id' not in tar_data.keys(): _logger.error(f'表单提交错误: 缺乏员工ID') return { 'success': False, 'message': f'服务器错误: 缺乏员工ID' } detail_list = [] for v in detail_dict.values(): detail_list.append((0, 0, v)) tar_data['summary_line'] = detail_list # 保存数据 records = request.env['information.summary.table'].sudo().create(tar_data) return { 'success': True, 'message': '财务数据提交成功!', 'redirect_url': '/custom/success' } except Exception as e: _logger.error(f'表单提交错误: {str(e)}') return { 'success': False, 'message': f'服务器错误: {str(e)}' } @http.route('/custom/success', type='http', auth='public', website=True, csrf=False) def custom_success_page(self, **kwargs): return request.render('yuthon_account_report.simple_success_page', {})