123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- # -*- coding: utf-8 -*-
- from odoo import models, fields, api, tools
- from odoo.exceptions import ValidationError
- # from . import base_infor
- from .report_sources.source_base import SourceBase, GROUP_FIELD, SUM_FIELD, EXPRESSION_FIELD
- from ..utils.report_field import ReportField
- from ..utils.report_info import ReportInfo
- from odoo.utils.constant import REPORT_TYPE, ANALYSE, ER_WEI, CHART, SINGLE_HEAD, DOUBLE_HEAD, TABLE_HEAD_TYPE, \
- CHART_TYPE, LINE, BAR
- from odoo.utils.model_util.query_model_helper import QueryModelHelper
- from odoo.utils.model_util import const_model
- from odoo.utils.model_util.const import DEFAULT_COUNT_PER_PAGE, get_digit_capacity_4_save_setting
- SOURCE = [(obj.name, obj.name) for obj in SourceBase.get_all_source()]
- DEFAULT_WIDTH = 100
- DEFAULT_HEIGHT = 800
- ER_WEI_DEFAULT_WIDTH = 80
- ER_WEI_DEFAULT_OTHER_WIDTH = 200
- ER_WEI_MIN_WIDTH = 20
- class ReportStyle(models.Model):
- _name = 'jc_bi.report_style'
- _description = '样式'
- _module = 'jc_bi'
- name = fields.Char(string=u'名称', required=True)
- width = fields.Integer('宽度%') # 不设置取100%
- height = fields.Integer('高度') # 单位:px。不设置取800
- source = fields.Selection(SOURCE, string=u'数据源')
- report_type = fields.Selection(REPORT_TYPE, string=u'报表类型', default=ANALYSE)
- # is_double_head = fields.Selection(TABLE_HEAD_TYPE, string=u'是否双表头', default=SINGLE_HEAD)
- # 分析表:
- lock_column_count = fields.Integer('锁定前N列')
- count_per_page = fields.Integer('查询结果每页行数')
- detail = fields.One2many('jc_bi.report_style_analyse_detail', 'report_style_id',
- string=u'报表样式设置分析表明细', copy=True)
- # 二维表
- row_name = fields.Char(u'行名') # table.field 第一列
- row_name_show = fields.Char(u'行名')
- first_column_width = fields.Integer(u'第一列宽度(px)') # 默认80
- column_name = fields.Char(u'列名') # table.field 可变列
- column_name_show = fields.Char(u'列名')
- other_column_width = fields.Integer(u'其他列宽度(px,第一行)') # 默认200
- indication_detail = fields.One2many('jc_bi.report_style_indication_detail', 'report_style_id',
- string=u'报表样式设置二维表明细', copy=True)
- # 图表
- unit_name = fields.Char(u'单位') # table.field
- unit_name_show = fields.Char(u'单位')
- x_name = fields.Char(u'X轴') # table.field
- x_name_show = fields.Char(u'X轴')
- chart_type = fields.Selection(CHART_TYPE, u'图类型', default=LINE)
- chart_detail = fields.One2many('jc_bi.report_style_chart_detail', 'report_style_id',
- string=u'报表样式设置图表明细', copy=True)
- def get_er_wei_column_width(self):
- return ReportStyle._get_valid_er_wei_width(self.first_column_width, ER_WEI_DEFAULT_WIDTH, ER_WEI_MIN_WIDTH), \
- ReportStyle._get_valid_er_wei_width(self.other_column_width, ER_WEI_DEFAULT_OTHER_WIDTH,
- ER_WEI_MIN_WIDTH)
- @staticmethod
- def _get_valid_er_wei_width(width, default_width, min_width):
- if not width:
- return default_width
- if width < min_width:
- width = min_width
- return width
- @api.model
- def save_style(self, style_id, style_info):
- # print('style_id:', style_id)
- # print('style_info:', style_info)
- report_type = style_info[0]
- if report_type == ANALYSE:
- return self._save_analyse(report_type, style_id, style_info)
- if report_type == ER_WEI:
- return self._save_indication(report_type, style_id, style_info)
- if report_type == CHART:
- return self._save_chart(report_type, style_id, style_info)
- raise ValidationError('报表类型错误:' + report_type)
- def _save_analyse(self, report_type, style_id, style_info):
- name, width, height, source = style_info[1]
- lock_column_count, count_per_page, field_list = style_info[2]
- style_id = int(style_id)
- setting = None
- if style_id:
- setting = self.env[self._name].sudo().browse(style_id)
- ReportStyle._save_diff_analyse(setting, name, width, height, source, report_type, lock_column_count,
- count_per_page)
- for d in setting.detail:
- d.unlink()
- # elif setting.report_type == ER_WEI:
- # for d in setting.indication_detail:
- # d.unlink()
- # elif setting.report_type == CHART:
- # for d in setting.chart_detail:
- # d.unlink()
- if not setting:
- val = dict(name=name, width=width, height=height, source=source, report_type=report_type,
- lock_column_count=lock_column_count, count_per_page=count_per_page)
- setting = self.env[self._name].sudo().create(val)
- self._add_analyse_detail(setting, field_list, 'jc_bi.report_style_analyse_detail')
- return setting.id
- def _save_indication(self, report_type, style_id, style_info):
- name, width, height, source = style_info[1]
- selected_indicator_list, row_id_name, column_id_name, first_column_width, other_column_width = style_info[2]
- style_id = int(style_id)
- setting = None
- if style_id:
- setting = self.env[self._name].sudo().browse(style_id)
- ReportStyle._save_diff_er_wei(setting, name, width, height, source, report_type, column_id_name,
- row_id_name, first_column_width, other_column_width)
- for d in setting.indication_detail:
- d.unlink()
- if not setting:
- val = dict(name=name, width=width, height=height, source=source, report_type=report_type,
- column_name=column_id_name[0], column_name_show=column_id_name[1], row_name=row_id_name[0],
- row_name_show=row_id_name[1], first_column_width=first_column_width,
- other_column_width=other_column_width)
- setting = self.env[self._name].sudo().create(val)
- self._add_chart_detail(setting, selected_indicator_list, 'jc_bi.report_style_indication_detail')
- return setting.id
- def _save_chart(self, report_type, style_id, style_info):
- name, width, height, source = style_info[1]
- selected_indicator_list, unit_id_name, x_id_name, chart_type = style_info[2]
- style_id = int(style_id)
- setting = None
- if style_id:
- setting = self.env[self._name].sudo().browse(style_id)
- ReportStyle._save_diff_chart(setting, name, width, height, source, report_type, unit_id_name, x_id_name,
- chart_type)
- for d in setting.chart_detail:
- d.unlink()
- if not setting:
- val = dict(name=name, width=width, height=height, source=source, report_type=report_type,
- unit_name=unit_id_name[0], unit_name_show=unit_id_name[1], x_name=x_id_name[0],
- x_name_show=x_id_name[1], chart_type=chart_type)
- setting = self.env[self._name].sudo().create(val)
- self._add_chart_detail(setting, selected_indicator_list, 'jc_bi.report_style_chart_detail')
- return setting.id
- @staticmethod
- def _save_diff_common(setting, name, width, height, source, report_type):
- if setting.name != name:
- setting.name = name
- if setting.width != width:
- setting.width = width
- if setting.height != height:
- setting.height = height
- if setting.source != source:
- # print('source different, old:', setting.source, ', new:', source)
- setting.source = source
- if setting.report_type != report_type:
- # print('report_type different, old:', setting.report_type, ', new:', report_type)
- setting.report_type = report_type
- return
- @staticmethod
- def _save_diff_analyse(setting, name, width, height, source, report_type, lock_column_count, count_per_page):
- ReportStyle._save_diff_common(setting, name, width, height, source, report_type)
- if setting.lock_column_count != lock_column_count:
- setting.lock_column_count = lock_column_count
- if setting.count_per_page != count_per_page:
- setting.count_per_page = count_per_page
- return
- @staticmethod
- def _save_diff_er_wei(setting, name, width, height, source, report_type, column_id_name, row_id_name,
- first_column_width, other_column_width):
- ReportStyle._save_diff_common(setting, name, width, height, source, report_type)
- if setting.column_name != column_id_name[0]:
- setting.column_name = column_id_name[0]
- if setting.column_name_show != column_id_name[1]:
- setting.column_name_show = column_id_name[1]
- if setting.row_name != row_id_name[0]:
- setting.row_name = row_id_name[0]
- if setting.row_name_show != row_id_name[1]:
- setting.row_name_show = row_id_name[1]
- if setting.first_column_width != first_column_width:
- setting.first_column_width = first_column_width
- if setting.other_column_width != other_column_width:
- setting.other_column_width = other_column_width
- return
- @staticmethod
- def _save_diff_chart(setting, name, width, height, source, report_type, unit_id_name, x_id_name, chart_type):
- ReportStyle._save_diff_common(setting, name, width, height, source, report_type)
- if setting.unit_name != unit_id_name[0]:
- setting.unit_name = unit_id_name[0]
- if setting.unit_name_show != unit_id_name[1]:
- setting.unit_name_show = unit_id_name[1]
- if setting.x_name != x_id_name[0]:
- setting.x_name = x_id_name[0]
- if setting.x_name_show != x_id_name[1]:
- setting.x_name_show = x_id_name[1]
- if setting.chart_type != chart_type:
- setting.chart_type = chart_type
- return
- def _add_analyse_detail(self, record, setting_list, detail_model):
- for row in setting_list:
- val = {
- 'report_style_id': record.id,
- 'field': row[0],
- 'sequence': row[1],
- 'show_name': row[2],
- 'new_show_name': row[3],
- 'is_sum': row[4],
- 'is_number': row[5],
- 'upper_limit': row[6],
- 'upper_characters_color': row[7],
- 'upper_background_color': row[8],
- 'lower_limit': row[9],
- 'lower_characters_color': row[10],
- 'lower_background_color': row[11],
- 'width': row[12],
- 'order_number': row[13],
- 'asc_desc': row[14],
- 'is_average': row[15],
- 'dividend': row[16],
- 'divisor': row[17],
- 'digit_capacity': get_digit_capacity_4_save_setting(row[18]),
- 'is_compute': row[19],
- 'is_compute_exp': row[20]}
- self.env[detail_model].sudo().create(val)
- return
- def _add_chart_detail(self, record, setting_list, detail_model):
- for row in setting_list:
- val = {
- 'report_style_id': record.id,
- 'indicator_field': row[0],
- 'indicator_name': row[1],
- 'indicator_show_name': row[2],
- }
- self.env[detail_model].sudo().create(val)
- return
- def query_setting(self, model_name):
- # model_id = self.env['ir.model']._get_id(model_name)
- # domain = [('model_id', '=', model_id)]
- # print('query_setting model_name:', model_name)
- # print('query_setting model_id:', model_id)
- # print('query_setting domain:', domain)
- domain = [('model', '=', model_name), '|', ('user_id', '=', self._uid), ('user_id', '=', False)]
- return self.sudo().search(domain, limit=1, order='id desc')
- @api.model
- def get_source_and_report_type(self, detail_field, reference_field):
- helper = self._get_bi_helper(detail_field, reference_field)
- helper.set_setting([], [], [[], []], None)
- field_list = helper.get_fields(['source', 'report_type'])
- # print('style info:', field_list)
- return field_list
- @api.model
- def query_style(self, style_id):
- # print('style_id:', style_id)
- record = self.sudo().browse(int(style_id))
- style_info = [style_id, record.name, record.width, record.height, record.source, record.report_type]
- detail_field, reference_field = ReportStyle._get_detail_field_info(record.source)
- source_type_info = self.sudo().get_source_and_report_type(detail_field, reference_field)
- return [style_info, source_type_info]
- @staticmethod
- def _get_detail_field_info(source):
- exist_source = SourceBase.get_source(source)
- if not exist_source:
- return None, None
- return exist_source.detail_field, exist_source.reference_field
- def _get_analyse_show(self):
- detail_list = [d.new_show_name if d.new_show_name else d.show_name for d in self.detail]
- detail_info = '】【'.join(detail_list)
- if detail_info:
- detail_info = '【' + detail_info + '】'
- else:
- detail_info = '无显示字段'
- return detail_info
- def _get_er_wei_show(self):
- detail_list = [d.indicator_show_name if d.indicator_show_name else d.indicator_name for d in
- self.indication_detail]
- detail_info = '】【'.join(detail_list)
- if detail_info:
- detail_info = '【' + detail_info + '】'
- else:
- detail_info = '无指标'
- _format = '{}({}px)/{}({}px): {}'
- return _format.format(self.row_name_show, self.first_column_width, self.column_name_show,
- self.other_column_width, detail_info)
- def _get_chart_show(self):
- detail_list = [d.indicator_show_name if d.indicator_show_name else d.indicator_name for d in self.chart_detail]
- detail_info = '】【'.join(detail_list)
- if detail_info:
- detail_info = '【' + detail_info + '】'
- else:
- detail_info = '无指标'
- _format = '{}/{}: {}'
- return _format.format(self.unit_name_show, self.x_name_show, detail_info)
- def get_style(self):
- report_type = self.get_report_type_show()
- if self.report_type == ANALYSE:
- detail_info = self._get_analyse_show()
- elif self.report_type == ER_WEI:
- detail_info = self._get_er_wei_show()
- elif self.report_type == CHART:
- detail_info = self._get_chart_show()
- else:
- detail_info = '不支持的报表类型'
- return [self.id, self.name, self.width, self.height, self.source, report_type, detail_info]
- def get_report_type_show(self):
- for key, name in REPORT_TYPE:
- if self.report_type == key:
- if key == CHART:
- chart_type_show = self._get_chart_type_show()
- name = '{}: {}'.format(name, chart_type_show)
- return name
- return REPORT_TYPE[0][1]
- def _get_chart_type_show(self):
- for key, name in CHART_TYPE:
- if self.chart_type == key:
- return name
- return CHART_TYPE[0][1]
- def get_width(self):
- return self.width if self.width else DEFAULT_WIDTH
- def get_height(self):
- return self.height if self.height else DEFAULT_HEIGHT
|