# -*- coding: utf-8 -*- from odoo import models, fields, api from odoo.exceptions import ValidationError 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 import util from odoo.utils.model_util.const import DEFAULT_COUNT_PER_PAGE DEFAULT_WIDTH = 100 DEFAULT_HEIGHT = 800 analyse_result_dic = {} # 缓存。导出Excel、翻页时,就不用再查询一遍 class ReportSet(models.Model): _name = 'jc_bi.report_set' _description = '报表集' _module = 'jc_bi' name = fields.Char(string=u'名称', required=True) width = fields.Integer('宽度%') # 不设置取100% height = fields.Integer('高度') # 单位:px。不设置取800 detail = fields.One2many('jc_bi.report_set_detail', 'report_set_id', string=u'报表集明细', copy=True) @api.model def add_report_set(self, name): val = { 'name': name, } return self.env['jc_bi.report_set'].sudo().create(val) @api.model def save_sequence(self, report_set_id, style_sequence_list): bill = self.env[self._name].sudo().browse(int(report_set_id)) for style_id, sequence in style_sequence_list: style_id = int(style_id) sequence = int(sequence) detail = [d for d in bill.detail if d.report_style_id.id == style_id] for d in detail: d.sequence = sequence return @api.model def save_style_name_and_sequence(self, label_id, report_set_id, style_list): bill = self.env[self._name].sudo().browse(int(report_set_id)) exist_list, delete_list, new_list = ReportSet._find_exist_delete_new(bill, style_list) for style_id, style_name, sequence, detail in exist_list: if detail.sequence != sequence: detail.sequence = sequence if detail.report_style_id.name != style_name: detail.report_style_id.name = style_name for item in delete_list: item.unlink() for style_id, style_name, sequence in new_list: val = { 'report_set_id': bill.id, 'report_style_id': style_id, 'sequence': sequence, } self.env['jc_bi.report_set_detail'].sudo().create(val) record = self.env['jc_bi.report_style'].sudo().browse(style_id) if record and record.name != style_name: record.name = style_name return label_id @staticmethod def _find_exist_delete_new(bill, style_list): exist_ids = [] exist_list = [] delete_list = [] new_list = [] for style_id, style_name, sequence in style_list: style_id = int(style_id) sequence = int(sequence) detail = [d for d in bill.detail if d.report_style_id.id == style_id] if detail: detail = detail[0] exist_list.append((style_id, style_name, sequence, detail)) exist_ids.append(style_id) else: new_list.append((style_id, style_name, sequence)) for d in bill.detail: if d.report_style_id.id not in exist_ids: delete_list.append(d) return exist_list, delete_list, new_list @api.model def query_style_info(self, label_id, report_set_id): bill = self.env[self._name].sudo().browse(int(report_set_id)) if not bill: not_used_list = self.env['jc_bi.report_style'].sudo().search_read([], ['name']) return [label_id, report_set_id], [], [[x['id'], x['name']] for x in not_used_list] exist_style_list = [[d.report_style_id.id, d.report_style_id.name, d.sequence] for d in bill.detail] exist_style_list.sort(key=lambda pair: pair[2]) used_style_list = [pair[0] for pair in exist_style_list] domain = [('id', 'not in', used_style_list)] not_used_list = self.env['jc_bi.report_style'].sudo().search_read(domain, []) return [label_id, report_set_id], exist_style_list, [[x['id'], x['name']] for x in not_used_list] @api.model def query_report_set_info(self, report_set_id): bill = self.env[self._name].sudo().browse(int(report_set_id)) if not bill: return [[], []] report_set_info = [bill.width if bill.width else DEFAULT_WIDTH, bill.height if bill.height else DEFAULT_HEIGHT] style_list = [ReportSet._get_style_info(d) for d in bill.detail] return [report_set_info, style_list] @staticmethod def _get_style_info(detail): _list = detail.report_style_id.get_style() _list.append(detail.sequence) return _list @api.model def add_style(self, report_set_id, style_info): # print('report_set_id:', report_set_id) # print('style_info:', style_info) report_set = self.browse(int(report_set_id)) if not report_set: return False style_id = self.env['jc_bi.report_style'].save_style(0, style_info) values = { 'report_set_id': report_set_id, 'report_style_id': style_id, } self.env['jc_bi.report_set_detail'].sudo().create(values) return @api.model def remove_style(self, report_set_id, style_id): # print('report_set_id:', report_set_id) # print('style_id:', style_id) report_set = self.sudo().browse(int(report_set_id)) if not report_set: return False style_id = int(style_id) for d in report_set.detail: # print('report_style_id:', d.report_style_id.id) if d.report_style_id.id == style_id: d.unlink() return True return False def query_set_info(self, report_set_id): report_set = self.sudo().browse(int(report_set_id)) if not report_set: return [report_set_id, DEFAULT_WIDTH, DEFAULT_HEIGHT], [] res = [] for style in report_set.detail: width, height = self._get_width_height(style) _res = [style.report_style_id.id, width, height] res.append(_res) width = report_set.width if report_set.width else DEFAULT_WIDTH height = report_set.height if report_set.height else DEFAULT_HEIGHT return [report_set_id, width, height], res def _get_width_height(self, style): width = style.width height = style.height if not width: width = style.report_style_id.width if not width: width = DEFAULT_WIDTH if not height: height = style.report_style_id.height if not height: height = DEFAULT_HEIGHT return width, height