123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- # -*- coding: utf-8 -*-
- from odoo.http import request
- from .indicator import Indicator
- from odoo.utils.jinja_util import change_to_json
- from .report_field import ReportField
- class ErWeiInfo(object):
- __slots__ = ['column_field', 'row_field', 'row_show', 'indicator_list', 'column_head_list', 'rows',
- '_column_length', 'first_column_width', 'other_column_width', '_column_width_list',
- 'column_archive_model']
- def __init__(self):
- self.column_field = ''
- self.column_archive_model = ''
- self.row_field = ''
- self.row_show = ''
- self.indicator_list = []
- self.column_head_list = [] # 查询结果:表头
- self.rows = [] # 查询结果
- self._column_length = 0 # 查询结果总列数
- self.first_column_width = '80' # 第一列宽度,px
- self.other_column_width = '200' # 其他列宽度,指双表头的第一行,px
- self._column_width_list = []
- return
- def set_column(self, column_field, column_archive_model):
- self.column_field = column_field
- self.column_archive_model = column_archive_model
- return
- def set_row(self, row_field, row_show):
- self.row_field = row_field
- self.row_show = row_show
- return
- def set_column_width(self, first_column_width, other_column_width):
- self.first_column_width = first_column_width
- self.other_column_width = other_column_width
- return
- def add_indicator(self, field, show):
- indicator = Indicator(field, show)
- self.indicator_list.append(indicator)
- return
- def query_er_wei(self, where_expression, param, source):
- column_list = self._query_er_wei_column(where_expression, param, source)
- self._column_width_list.append(self.first_column_width)
- for index in range(len(column_list)):
- self._column_width_list.append(self.other_column_width)
- column_list = self._query_name(column_list)
- self._query_er_wei_data(where_expression, param, column_list, source)
- return
- def _query_name(self, column_list):
- domain = [('id', 'in', column_list)]
- data = request.env[self.column_archive_model].sudo().search_read(domain, ['id', 'name'])
- return [[row['id'], row['name']] for row in data]
- def get(self, table_height, table_width):
- result_rows = self._get_rows()
- res = {
- "is_single_head": change_to_json(False),
- "is_table": change_to_json(True),
- "headers": self.column_head_list,
- "rows": result_rows,
- "lock_column_count": 1,
- "table_height": table_height,
- "table_width": table_width,
- "column_width": self._column_width_list
- }
- # print 'er wei res:', res
- return change_to_json(res)
- def _query_er_wei_column(self, where_expression, param, source):
- sql_format = """select {} from {} {}"""
- sql = sql_format.format('distinct ' + self.column_field, source, where_expression)
- cr = request.env.cr
- if where_expression:
- cr.execute(sql, param)
- else:
- cr.execute(sql)
- result = cr.fetchall()
- # result = [list(row) for row in result]
- # self._change_none_(result)
- return [row[0] for row in result]
- def _query_er_wei_data(self, where_expression, param, column_list, source):
- sql_format = """select {} from {} {} group by {} order by {}"""
- field_expression_list = self._get_indicator_expression(column_list)
- field_expression_list = [self.row_field] + field_expression_list
- self._column_length = len(field_expression_list)
- field_expressions = ','.join(field_expression_list)
- sql = sql_format.format(field_expressions, source, where_expression, self.row_field, self.row_field)
- cr = request.env.cr
- if where_expression:
- cr.execute(sql, param)
- else:
- cr.execute(sql)
- result = cr.fetchall()
- result = [list(row) for row in result]
- self._change_none_(result, self._column_length)
- self.rows = result
- def _change_none_(self, rows, _length):
- for r in rows:
- for index in range(_length):
- if not r[index]:
- r[index] = ''
- return
- def _get_indicator_expression(self, column_value_list):
- res = []
- heads_list = [self.row_show]
- for _id, name in column_value_list:
- for indicator in self.indicator_list:
- res.append("sum(case when {}={} then {} end)".format(self.column_field, _id, indicator.field))
- heads_list.append("{}|{}".format(name, indicator.show))
- self.column_head_list = heads_list
- return res
- def _get_rows(self):
- rows = []
- _len = self._column_length
- for row in self.rows:
- new_row = []
- if _len <= len(row):
- self._add_data_for_row(_len, new_row, row)
- else:
- _len = len(row)
- self._add_data_for_row(_len, new_row, row)
- rows.append(new_row)
- sum_row = self._get_sum_row(self.rows)
- if sum_row:
- rows.append(sum_row)
- return rows
- def _add_data_for_row(self, _len, new_row, row):
- for index in range(_len):
- # field = self.fields[index]
- value = row[index]
- # data = field.get_data(value)
- data = [value, '', '' if index == 0 else 'class=text-right']
- new_row.append(data)
- return
- def _get_sum_row(self, data_rows):
- res = []
- _len = self._column_length
- count = 1
- s = ReportField()
- color = ''
- background_color = 'darkgoldenrod'
- res.append(s.get_assign_data(u'合计', color, background_color, is_style_value=True))
- for index in range(_len):
- if index < count:
- continue
- v = self._get_sum_column(index, data_rows)
- res.append(s.get_assign_data(v, color, background_color, is_style_value=True))
- return res
- def _get_sum_column(self, index, common_rows):
- _sum = 0
- for row in common_rows:
- if len(row) <= index:
- continue
- _sum += row[index] if row[index] else 0
- return _sum
|