er_wei_info.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # -*- coding: utf-8 -*-
  2. from odoo.http import request
  3. from .indicator import Indicator
  4. from odoo.utils.jinja_util import change_to_json
  5. from .report_field import ReportField
  6. class ErWeiInfo(object):
  7. __slots__ = ['column_field', 'row_field', 'row_show', 'indicator_list', 'column_head_list', 'rows',
  8. '_column_length', 'first_column_width', 'other_column_width', '_column_width_list',
  9. 'column_archive_model']
  10. def __init__(self):
  11. self.column_field = ''
  12. self.column_archive_model = ''
  13. self.row_field = ''
  14. self.row_show = ''
  15. self.indicator_list = []
  16. self.column_head_list = [] # 查询结果:表头
  17. self.rows = [] # 查询结果
  18. self._column_length = 0 # 查询结果总列数
  19. self.first_column_width = '80' # 第一列宽度,px
  20. self.other_column_width = '200' # 其他列宽度,指双表头的第一行,px
  21. self._column_width_list = []
  22. return
  23. def set_column(self, column_field, column_archive_model):
  24. self.column_field = column_field
  25. self.column_archive_model = column_archive_model
  26. return
  27. def set_row(self, row_field, row_show):
  28. self.row_field = row_field
  29. self.row_show = row_show
  30. return
  31. def set_column_width(self, first_column_width, other_column_width):
  32. self.first_column_width = first_column_width
  33. self.other_column_width = other_column_width
  34. return
  35. def add_indicator(self, field, show):
  36. indicator = Indicator(field, show)
  37. self.indicator_list.append(indicator)
  38. return
  39. def query_er_wei(self, where_expression, param, source):
  40. column_list = self._query_er_wei_column(where_expression, param, source)
  41. self._column_width_list.append(self.first_column_width)
  42. for index in range(len(column_list)):
  43. self._column_width_list.append(self.other_column_width)
  44. column_list = self._query_name(column_list)
  45. self._query_er_wei_data(where_expression, param, column_list, source)
  46. return
  47. def _query_name(self, column_list):
  48. domain = [('id', 'in', column_list)]
  49. data = request.env[self.column_archive_model].sudo().search_read(domain, ['id', 'name'])
  50. return [[row['id'], row['name']] for row in data]
  51. def get(self, table_height, table_width):
  52. result_rows = self._get_rows()
  53. res = {
  54. "is_single_head": change_to_json(False),
  55. "is_table": change_to_json(True),
  56. "headers": self.column_head_list,
  57. "rows": result_rows,
  58. "lock_column_count": 1,
  59. "table_height": table_height,
  60. "table_width": table_width,
  61. "column_width": self._column_width_list
  62. }
  63. # print 'er wei res:', res
  64. return change_to_json(res)
  65. def _query_er_wei_column(self, where_expression, param, source):
  66. sql_format = """select {} from {} {}"""
  67. sql = sql_format.format('distinct ' + self.column_field, source, where_expression)
  68. cr = request.env.cr
  69. if where_expression:
  70. cr.execute(sql, param)
  71. else:
  72. cr.execute(sql)
  73. result = cr.fetchall()
  74. # result = [list(row) for row in result]
  75. # self._change_none_(result)
  76. return [row[0] for row in result]
  77. def _query_er_wei_data(self, where_expression, param, column_list, source):
  78. sql_format = """select {} from {} {} group by {} order by {}"""
  79. field_expression_list = self._get_indicator_expression(column_list)
  80. field_expression_list = [self.row_field] + field_expression_list
  81. self._column_length = len(field_expression_list)
  82. field_expressions = ','.join(field_expression_list)
  83. sql = sql_format.format(field_expressions, source, where_expression, self.row_field, self.row_field)
  84. cr = request.env.cr
  85. if where_expression:
  86. cr.execute(sql, param)
  87. else:
  88. cr.execute(sql)
  89. result = cr.fetchall()
  90. result = [list(row) for row in result]
  91. self._change_none_(result, self._column_length)
  92. self.rows = result
  93. def _change_none_(self, rows, _length):
  94. for r in rows:
  95. for index in range(_length):
  96. if not r[index]:
  97. r[index] = ''
  98. return
  99. def _get_indicator_expression(self, column_value_list):
  100. res = []
  101. heads_list = [self.row_show]
  102. for _id, name in column_value_list:
  103. for indicator in self.indicator_list:
  104. res.append("sum(case when {}={} then {} end)".format(self.column_field, _id, indicator.field))
  105. heads_list.append("{}|{}".format(name, indicator.show))
  106. self.column_head_list = heads_list
  107. return res
  108. def _get_rows(self):
  109. rows = []
  110. _len = self._column_length
  111. for row in self.rows:
  112. new_row = []
  113. if _len <= len(row):
  114. self._add_data_for_row(_len, new_row, row)
  115. else:
  116. _len = len(row)
  117. self._add_data_for_row(_len, new_row, row)
  118. rows.append(new_row)
  119. sum_row = self._get_sum_row(self.rows)
  120. if sum_row:
  121. rows.append(sum_row)
  122. return rows
  123. def _add_data_for_row(self, _len, new_row, row):
  124. for index in range(_len):
  125. # field = self.fields[index]
  126. value = row[index]
  127. # data = field.get_data(value)
  128. data = [value, '', '' if index == 0 else 'class=text-right']
  129. new_row.append(data)
  130. return
  131. def _get_sum_row(self, data_rows):
  132. res = []
  133. _len = self._column_length
  134. count = 1
  135. s = ReportField()
  136. color = ''
  137. background_color = 'darkgoldenrod'
  138. res.append(s.get_assign_data(u'合计', color, background_color, is_style_value=True))
  139. for index in range(_len):
  140. if index < count:
  141. continue
  142. v = self._get_sum_column(index, data_rows)
  143. res.append(s.get_assign_data(v, color, background_color, is_style_value=True))
  144. return res
  145. def _get_sum_column(self, index, common_rows):
  146. _sum = 0
  147. for row in common_rows:
  148. if len(row) <= index:
  149. continue
  150. _sum += row[index] if row[index] else 0
  151. return _sum