report_set.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # -*- coding: utf-8 -*-
  2. from odoo import models, fields, api
  3. from odoo.exceptions import ValidationError
  4. from odoo.utils.constant import REPORT_TYPE, ANALYSE, ER_WEI, CHART, SINGLE_HEAD, DOUBLE_HEAD, TABLE_HEAD_TYPE, \
  5. CHART_TYPE, LINE, BAR
  6. from odoo.utils import util
  7. from odoo.utils.model_util.const import DEFAULT_COUNT_PER_PAGE
  8. DEFAULT_WIDTH = 100
  9. DEFAULT_HEIGHT = 800
  10. analyse_result_dic = {} # 缓存。导出Excel、翻页时,就不用再查询一遍
  11. class ReportSet(models.Model):
  12. _name = 'jc_bi.report_set'
  13. _description = '报表集'
  14. _module = 'jc_bi'
  15. name = fields.Char(string=u'名称', required=True)
  16. width = fields.Integer('宽度%') # 不设置取100%
  17. height = fields.Integer('高度') # 单位:px。不设置取800
  18. detail = fields.One2many('jc_bi.report_set_detail', 'report_set_id', string=u'报表集明细', copy=True)
  19. @api.model
  20. def add_report_set(self, name):
  21. val = {
  22. 'name': name,
  23. }
  24. return self.env['jc_bi.report_set'].sudo().create(val)
  25. @api.model
  26. def save_sequence(self, report_set_id, style_sequence_list):
  27. bill = self.env[self._name].sudo().browse(int(report_set_id))
  28. for style_id, sequence in style_sequence_list:
  29. style_id = int(style_id)
  30. sequence = int(sequence)
  31. detail = [d for d in bill.detail if d.report_style_id.id == style_id]
  32. for d in detail:
  33. d.sequence = sequence
  34. return
  35. @api.model
  36. def save_style_name_and_sequence(self, label_id, report_set_id, style_list):
  37. bill = self.env[self._name].sudo().browse(int(report_set_id))
  38. exist_list, delete_list, new_list = ReportSet._find_exist_delete_new(bill, style_list)
  39. for style_id, style_name, sequence, detail in exist_list:
  40. if detail.sequence != sequence:
  41. detail.sequence = sequence
  42. if detail.report_style_id.name != style_name:
  43. detail.report_style_id.name = style_name
  44. for item in delete_list:
  45. item.unlink()
  46. for style_id, style_name, sequence in new_list:
  47. val = {
  48. 'report_set_id': bill.id,
  49. 'report_style_id': style_id,
  50. 'sequence': sequence,
  51. }
  52. self.env['jc_bi.report_set_detail'].sudo().create(val)
  53. record = self.env['jc_bi.report_style'].sudo().browse(style_id)
  54. if record and record.name != style_name:
  55. record.name = style_name
  56. return label_id
  57. @staticmethod
  58. def _find_exist_delete_new(bill, style_list):
  59. exist_ids = []
  60. exist_list = []
  61. delete_list = []
  62. new_list = []
  63. for style_id, style_name, sequence in style_list:
  64. style_id = int(style_id)
  65. sequence = int(sequence)
  66. detail = [d for d in bill.detail if d.report_style_id.id == style_id]
  67. if detail:
  68. detail = detail[0]
  69. exist_list.append((style_id, style_name, sequence, detail))
  70. exist_ids.append(style_id)
  71. else:
  72. new_list.append((style_id, style_name, sequence))
  73. for d in bill.detail:
  74. if d.report_style_id.id not in exist_ids:
  75. delete_list.append(d)
  76. return exist_list, delete_list, new_list
  77. @api.model
  78. def query_style_info(self, label_id, report_set_id):
  79. bill = self.env[self._name].sudo().browse(int(report_set_id))
  80. if not bill:
  81. not_used_list = self.env['jc_bi.report_style'].sudo().search_read([], ['name'])
  82. return [label_id, report_set_id], [], [[x['id'], x['name']] for x in not_used_list]
  83. exist_style_list = [[d.report_style_id.id, d.report_style_id.name, d.sequence] for d in bill.detail]
  84. exist_style_list.sort(key=lambda pair: pair[2])
  85. used_style_list = [pair[0] for pair in exist_style_list]
  86. domain = [('id', 'not in', used_style_list)]
  87. not_used_list = self.env['jc_bi.report_style'].sudo().search_read(domain, [])
  88. return [label_id, report_set_id], exist_style_list, [[x['id'], x['name']] for x in not_used_list]
  89. @api.model
  90. def query_report_set_info(self, report_set_id):
  91. bill = self.env[self._name].sudo().browse(int(report_set_id))
  92. if not bill:
  93. return [[], []]
  94. report_set_info = [bill.width if bill.width else DEFAULT_WIDTH, bill.height if bill.height else DEFAULT_HEIGHT]
  95. style_list = [ReportSet._get_style_info(d) for d in bill.detail]
  96. return [report_set_info, style_list]
  97. @staticmethod
  98. def _get_style_info(detail):
  99. _list = detail.report_style_id.get_style()
  100. _list.append(detail.sequence)
  101. return _list
  102. @api.model
  103. def add_style(self, report_set_id, style_info):
  104. # print('report_set_id:', report_set_id)
  105. # print('style_info:', style_info)
  106. report_set = self.browse(int(report_set_id))
  107. if not report_set:
  108. return False
  109. style_id = self.env['jc_bi.report_style'].save_style(0, style_info)
  110. values = {
  111. 'report_set_id': report_set_id,
  112. 'report_style_id': style_id,
  113. }
  114. self.env['jc_bi.report_set_detail'].sudo().create(values)
  115. return
  116. @api.model
  117. def remove_style(self, report_set_id, style_id):
  118. # print('report_set_id:', report_set_id)
  119. # print('style_id:', style_id)
  120. report_set = self.sudo().browse(int(report_set_id))
  121. if not report_set:
  122. return False
  123. style_id = int(style_id)
  124. for d in report_set.detail:
  125. # print('report_style_id:', d.report_style_id.id)
  126. if d.report_style_id.id == style_id:
  127. d.unlink()
  128. return True
  129. return False
  130. def query_set_info(self, report_set_id):
  131. report_set = self.sudo().browse(int(report_set_id))
  132. if not report_set:
  133. return [report_set_id, DEFAULT_WIDTH, DEFAULT_HEIGHT], []
  134. res = []
  135. for style in report_set.detail:
  136. width, height = self._get_width_height(style)
  137. _res = [style.report_style_id.id, width, height]
  138. res.append(_res)
  139. width = report_set.width if report_set.width else DEFAULT_WIDTH
  140. height = report_set.height if report_set.height else DEFAULT_HEIGHT
  141. return [report_set_id, width, height], res
  142. def _get_width_height(self, style):
  143. width = style.width
  144. height = style.height
  145. if not width:
  146. width = style.report_style_id.width
  147. if not width:
  148. width = DEFAULT_WIDTH
  149. if not height:
  150. height = style.report_style_id.height
  151. if not height:
  152. height = DEFAULT_HEIGHT
  153. return width, height