|
@ -3,7 +3,7 @@ |
|
|
# |
|
|
# |
|
|
# OpenERP, Open Source Management Solution |
|
|
# OpenERP, Open Source Management Solution |
|
|
# |
|
|
# |
|
|
#Copyright (c) 2013 Noviat nv/sa (www.noviat.com). All rights reserved. |
|
|
|
|
|
|
|
|
# Copyright (c) 2014 Noviat nv/sa (www.noviat.com). All rights reserved. |
|
|
# |
|
|
# |
|
|
# This program is free software: you can redistribute it and/or modify |
|
|
# This program is free software: you can redistribute it and/or modify |
|
|
# it under the terms of the GNU Affero General Public License as |
|
|
# it under the terms of the GNU Affero General Public License as |
|
@ -23,7 +23,9 @@ |
|
|
import xlwt |
|
|
import xlwt |
|
|
from xlwt.Style import default_style |
|
|
from xlwt.Style import default_style |
|
|
import cStringIO |
|
|
import cStringIO |
|
|
import datetime, time |
|
|
|
|
|
|
|
|
from datetime import datetime |
|
|
|
|
|
from openerp.osv.fields import datetime as datetime_field |
|
|
|
|
|
from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT |
|
|
import inspect |
|
|
import inspect |
|
|
from types import CodeType |
|
|
from types import CodeType |
|
|
from openerp.report.report_sxw import * |
|
|
from openerp.report.report_sxw import * |
|
@ -32,11 +34,13 @@ from openerp.tools.translate import translate, _ |
|
|
import logging |
|
|
import logging |
|
|
_logger = logging.getLogger(__name__) |
|
|
_logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AttrDict(dict): |
|
|
class AttrDict(dict): |
|
|
def __init__(self, *args, **kwargs): |
|
|
def __init__(self, *args, **kwargs): |
|
|
super(AttrDict, self).__init__(*args, **kwargs) |
|
|
super(AttrDict, self).__init__(*args, **kwargs) |
|
|
self.__dict__ = self |
|
|
self.__dict__ = self |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class report_xls(report_sxw): |
|
|
class report_xls(report_sxw): |
|
|
|
|
|
|
|
|
xls_types = { |
|
|
xls_types = { |
|
@ -55,18 +59,10 @@ class report_xls(report_sxw): |
|
|
# TO DO: move parameters infra to configurable data |
|
|
# TO DO: move parameters infra to configurable data |
|
|
|
|
|
|
|
|
# header/footer |
|
|
# header/footer |
|
|
DT_FORMAT = '%Y-%m-%d %H:%M:%S' |
|
|
|
|
|
hf_params = { |
|
|
hf_params = { |
|
|
'font_size': 8, |
|
|
'font_size': 8, |
|
|
'font_style': 'I', # B: Bold, I: Italic, U: Underline |
|
|
'font_style': 'I', # B: Bold, I: Italic, U: Underline |
|
|
} |
|
|
} |
|
|
xls_headers = { |
|
|
|
|
|
'standard': '' |
|
|
|
|
|
} |
|
|
|
|
|
xls_footers = { |
|
|
|
|
|
'standard': ('&L&%(font_size)s&%(font_style)s' + datetime.now().strftime(DT_FORMAT) + |
|
|
|
|
|
'&R&%(font_size)s&%(font_style)s&P / &N') %hf_params |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
# styles |
|
|
# styles |
|
|
_pfc = '26' # default pattern fore_color |
|
|
_pfc = '26' # default pattern fore_color |
|
@ -81,8 +77,12 @@ class report_xls(report_sxw): |
|
|
'fill': 'pattern: pattern solid, fore_color %s;' % _pfc, |
|
|
'fill': 'pattern: pattern solid, fore_color %s;' % _pfc, |
|
|
'fill_blue': 'pattern: pattern solid, fore_color 27;', |
|
|
'fill_blue': 'pattern: pattern solid, fore_color 27;', |
|
|
'fill_grey': 'pattern: pattern solid, fore_color 22;', |
|
|
'fill_grey': 'pattern: pattern solid, fore_color 22;', |
|
|
'borders_all': 'borders: left thin, right thin, top thin, bottom thin, ' \ |
|
|
|
|
|
'left_colour %s, right_colour %s, top_colour %s, bottom_colour %s;' %(_bc,_bc,_bc,_bc), |
|
|
|
|
|
|
|
|
'borders_all': |
|
|
|
|
|
'borders: ' |
|
|
|
|
|
'left thin, right thin, top thin, bottom thin, ' |
|
|
|
|
|
'left_colour %s, right_colour %s, ' |
|
|
|
|
|
'top_colour %s, bottom_colour %s;' |
|
|
|
|
|
% (_bc, _bc, _bc, _bc), |
|
|
'left': 'align: horz left;', |
|
|
'left': 'align: horz left;', |
|
|
'center': 'align: horz center;', |
|
|
'center': 'align: horz center;', |
|
|
'right': 'align: horz right;', |
|
|
'right': 'align: horz right;', |
|
@ -97,21 +97,26 @@ class report_xls(report_sxw): |
|
|
self.cr = cr |
|
|
self.cr = cr |
|
|
self.uid = uid |
|
|
self.uid = uid |
|
|
report_obj = self.pool.get('ir.actions.report.xml') |
|
|
report_obj = self.pool.get('ir.actions.report.xml') |
|
|
report_ids = report_obj.search(cr, uid, |
|
|
|
|
|
[('report_name', '=', self.name[7:])], context=context) |
|
|
|
|
|
|
|
|
report_ids = report_obj.search( |
|
|
|
|
|
cr, uid, [('report_name', '=', self.name[7:])], context=context) |
|
|
if report_ids: |
|
|
if report_ids: |
|
|
report_xml = report_obj.browse(cr, uid, report_ids[0], context=context) |
|
|
|
|
|
|
|
|
report_xml = report_obj.browse( |
|
|
|
|
|
cr, uid, report_ids[0], context=context) |
|
|
self.title = report_xml.name |
|
|
self.title = report_xml.name |
|
|
if report_xml.report_type == 'xls': |
|
|
if report_xml.report_type == 'xls': |
|
|
return self.create_source_xls(cr, uid, ids, data, context) |
|
|
return self.create_source_xls(cr, uid, ids, data, context) |
|
|
elif context.get('xls_export'): |
|
|
elif context.get('xls_export'): |
|
|
|
|
|
# use model from 'data' when no ir.actions.report.xml entry |
|
|
|
|
|
self.table = data.get('model') or self.table |
|
|
return self.create_source_xls(cr, uid, ids, data, context) |
|
|
return self.create_source_xls(cr, uid, ids, data, context) |
|
|
return super(report_xls, self).create(cr, uid, ids, data, context) |
|
|
return super(report_xls, self).create(cr, uid, ids, data, context) |
|
|
|
|
|
|
|
|
def create_source_xls(self, cr, uid, ids, data, context=None): |
|
|
def create_source_xls(self, cr, uid, ids, data, context=None): |
|
|
if not context: context = {} |
|
|
|
|
|
|
|
|
if not context: |
|
|
|
|
|
context = {} |
|
|
parser_instance = self.parser(cr, uid, self.name2, context) |
|
|
parser_instance = self.parser(cr, uid, self.name2, context) |
|
|
self.parser_instance = parser_instance |
|
|
self.parser_instance = parser_instance |
|
|
|
|
|
self.context = context |
|
|
objs = self.getObjects(cr, uid, ids, context) |
|
|
objs = self.getObjects(cr, uid, ids, context) |
|
|
parser_instance.set_context(objs, data, ids, 'xls') |
|
|
parser_instance.set_context(objs, data, ids, 'xls') |
|
|
objs = parser_instance.localcontext['objects'] |
|
|
objs = parser_instance.localcontext['objects'] |
|
@ -119,6 +124,18 @@ class report_xls(report_sxw): |
|
|
wb = xlwt.Workbook(encoding='utf-8') |
|
|
wb = xlwt.Workbook(encoding='utf-8') |
|
|
_p = AttrDict(parser_instance.localcontext) |
|
|
_p = AttrDict(parser_instance.localcontext) |
|
|
_xs = self.xls_styles |
|
|
_xs = self.xls_styles |
|
|
|
|
|
self.xls_headers = { |
|
|
|
|
|
'standard': '', |
|
|
|
|
|
} |
|
|
|
|
|
report_date = datetime_field.context_timestamp( |
|
|
|
|
|
cr, uid, datetime.now(), context) |
|
|
|
|
|
report_date = report_date.strftime(DEFAULT_SERVER_DATETIME_FORMAT) |
|
|
|
|
|
self.xls_footers = { |
|
|
|
|
|
'standard': ( |
|
|
|
|
|
'&L&%(font_size)s&%(font_style)s' + report_date + |
|
|
|
|
|
'&R&%(font_size)s&%(font_style)s&P / &N' |
|
|
|
|
|
) % self.hf_params, |
|
|
|
|
|
} |
|
|
self.generate_xls_report(_p, _xs, data, objs, wb) |
|
|
self.generate_xls_report(_p, _xs, data, objs, wb) |
|
|
wb.save(n) |
|
|
wb.save(n) |
|
|
n.seek(0) |
|
|
n.seek(0) |
|
@ -126,13 +143,14 @@ class report_xls(report_sxw): |
|
|
|
|
|
|
|
|
def render(self, wanted, col_specs, rowtype, render_space='empty'): |
|
|
def render(self, wanted, col_specs, rowtype, render_space='empty'): |
|
|
""" |
|
|
""" |
|
|
returns 'mako'-rendered col_specs |
|
|
|
|
|
|
|
|
returns 'evaluated' col_specs |
|
|
|
|
|
|
|
|
Input: |
|
|
Input: |
|
|
- wanted: element from the wanted_list |
|
|
- wanted: element from the wanted_list |
|
|
- col_specs : cf. specs[1:] documented in xls_row_template method |
|
|
- col_specs : cf. specs[1:] documented in xls_row_template method |
|
|
- rowtype : 'header' or 'data' |
|
|
- rowtype : 'header' or 'data' |
|
|
- render_space : type dict, (caller_space + localcontext) if not specified |
|
|
|
|
|
|
|
|
- render_space : type dict, (caller_space + localcontext) |
|
|
|
|
|
if not specified |
|
|
""" |
|
|
""" |
|
|
if render_space == 'empty': |
|
|
if render_space == 'empty': |
|
|
render_space = {} |
|
|
render_space = {} |
|
@ -145,7 +163,6 @@ class report_xls(report_sxw): |
|
|
if isinstance(row[i], CodeType): |
|
|
if isinstance(row[i], CodeType): |
|
|
row[i] = eval(row[i], render_space) |
|
|
row[i] = eval(row[i], render_space) |
|
|
row.insert(0, wanted) |
|
|
row.insert(0, wanted) |
|
|
#_logger.warn('row O = %s', row) |
|
|
|
|
|
return row |
|
|
return row |
|
|
|
|
|
|
|
|
def generate_xls_report(self, parser, xls_styles, data, objects, wb): |
|
|
def generate_xls_report(self, parser, xls_styles, data, objects, wb): |
|
@ -157,11 +174,13 @@ class report_xls(report_sxw): |
|
|
Returns a row template. |
|
|
Returns a row template. |
|
|
|
|
|
|
|
|
Input : |
|
|
Input : |
|
|
- 'wanted_list': list of Columns that will be returned in the row_template |
|
|
|
|
|
|
|
|
- 'wanted_list': list of Columns that will be returned in the |
|
|
|
|
|
row_template |
|
|
- 'specs': list with Column Characteristics |
|
|
- 'specs': list with Column Characteristics |
|
|
0: Column Name (from wanted_list) |
|
|
0: Column Name (from wanted_list) |
|
|
1: Column Colspan |
|
|
1: Column Colspan |
|
|
2: Column Size (unit = the width of the character ’0′ as it appears in the sheet’s default font) |
|
|
|
|
|
|
|
|
2: Column Size (unit = the width of the character ’0′ |
|
|
|
|
|
as it appears in the sheet’s default font) |
|
|
3: Column Type |
|
|
3: Column Type |
|
|
4: Column Data |
|
|
4: Column Data |
|
|
5: Column Formula (or 'None' for Data) |
|
|
5: Column Formula (or 'None' for Data) |
|
@ -180,7 +199,8 @@ class report_xls(report_sxw): |
|
|
if s_len > 5 and s[5] is not None: |
|
|
if s_len > 5 and s[5] is not None: |
|
|
c.append({'formula': s[5]}) |
|
|
c.append({'formula': s[5]}) |
|
|
else: |
|
|
else: |
|
|
c.append({'write_cell_func': report_xls.xls_types[c[3]]}) |
|
|
|
|
|
|
|
|
c.append({ |
|
|
|
|
|
'write_cell_func': report_xls.xls_types[c[3]]}) |
|
|
# Set custom cell style |
|
|
# Set custom cell style |
|
|
if s_len > 6 and s[6] is not None: |
|
|
if s_len > 6 and s[6] is not None: |
|
|
c.append(s[6]) |
|
|
c.append(s[6]) |
|
@ -195,23 +215,28 @@ class report_xls(report_sxw): |
|
|
col += c[1] |
|
|
col += c[1] |
|
|
break |
|
|
break |
|
|
if not found: |
|
|
if not found: |
|
|
_logger.warn("report_xls.xls_row_template, column '%s' not found in specs", w) |
|
|
|
|
|
|
|
|
_logger.warn("report_xls.xls_row_template, " |
|
|
|
|
|
"column '%s' not found in specs", w) |
|
|
return r |
|
|
return r |
|
|
|
|
|
|
|
|
def xls_write_row(self, ws, row_pos, row_data, row_style=default_style, set_column_size=False): |
|
|
|
|
|
|
|
|
def xls_write_row(self, ws, row_pos, row_data, |
|
|
|
|
|
row_style=default_style, set_column_size=False): |
|
|
r = ws.row(row_pos) |
|
|
r = ws.row(row_pos) |
|
|
for col, size, spec in row_data: |
|
|
for col, size, spec in row_data: |
|
|
data = spec[4] |
|
|
data = spec[4] |
|
|
formula = spec[5].get('formula') and xlwt.Formula(spec[5]['formula']) or None |
|
|
|
|
|
|
|
|
formula = spec[5].get('formula') and \ |
|
|
|
|
|
xlwt.Formula(spec[5]['formula']) or None |
|
|
style = spec[6] and spec[6] or row_style |
|
|
style = spec[6] and spec[6] or row_style |
|
|
if not data: |
|
|
if not data: |
|
|
# if no data, use default values |
|
|
# if no data, use default values |
|
|
data = report_xls.xls_types_default[spec[3]] |
|
|
data = report_xls.xls_types_default[spec[3]] |
|
|
if size != 1: |
|
|
if size != 1: |
|
|
if formula: |
|
|
if formula: |
|
|
ws.write_merge(row_pos, row_pos, col, col+size-1, data, style) |
|
|
|
|
|
|
|
|
ws.write_merge( |
|
|
|
|
|
row_pos, row_pos, col, col + size - 1, data, style) |
|
|
else: |
|
|
else: |
|
|
ws.write_merge(row_pos, row_pos, col, col+size-1, data, style) |
|
|
|
|
|
|
|
|
ws.write_merge( |
|
|
|
|
|
row_pos, row_pos, col, col + size - 1, data, style) |
|
|
else: |
|
|
else: |
|
|
if formula: |
|
|
if formula: |
|
|
ws.write(row_pos, col, formula, style) |
|
|
ws.write(row_pos, col, formula, style) |
|
|