@ -1,5 +1,6 @@
# Author: Julien Coux
# Author: Julien Coux
# Copyright 2016 Camptocamp SA
# Copyright 2016 Camptocamp SA
# Copyright 2021 Tecnativa - João Marques
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import models
from odoo import models
@ -9,56 +10,37 @@ class AbstractReportXslx(models.AbstractModel):
_description = " Abstract XLSX Account Financial Report "
_description = " Abstract XLSX Account Financial Report "
_inherit = " report.report_xlsx.abstract "
_inherit = " report.report_xlsx.abstract "
def __init__ ( self , pool , cr ) :
# main sheet which will contains report
self . sheet = None
# columns of the report
self . columns = None
# row_pos must be incremented at each writing lines
self . row_pos = None
# Formats
self . format_right = None
self . format_left = None
self . format_right_bold_italic = None
self . format_bold = None
self . format_header_left = None
self . format_header_center = None
self . format_header_right = None
self . format_header_amount = None
self . format_amount = None
self . format_percent_bold_italic = None
def get_workbook_options ( self ) :
def get_workbook_options ( self ) :
return { " constant_memory " : True }
vals = super ( ) . get_workbook_options ( )
vals . update ( { " constant_memory " : True } )
return vals
def generate_xlsx_report ( self , workbook , data , objects ) :
def generate_xlsx_report ( self , workbook , data , objects ) :
report = objects
self . row_pos = 0
self . _define_formats ( workbook )
report_name = self . _get_report_name ( report , data = data )
# Initialize report variables
report_data = {
" workbook " : None ,
" sheet " : None , # main sheet which will contains report
" columns " : None , # columns of the report
" row_pos " : None , # row_pos must be incremented at each writing lines
" formats " : None ,
}
self . _define_formats ( workbook , report_data )
# Get report data
report_name = self . _get_report_name ( objects , data = data )
report_footer = self . _get_report_footer ( )
report_footer = self . _get_report_footer ( )
filters = self . _get_report_filters ( report )
self . columns = self . _get_report_columns ( report )
self . workbook = workbook
self . sheet = workbook . add_worksheet ( report_name [ : 31 ] )
self . _set_column_width ( )
self . _write_report_title ( report_name )
self . _write_filters ( filters )
self . _generate_report_content ( workbook , report , data )
self . _write_report_footer ( report_footer )
def _define_formats ( self , workbook ) :
filters = self . _get_report_filters ( objects )
report_data [ " columns " ] = self . _get_report_columns ( objects )
report_data [ " workbook " ] = workbook
report_data [ " sheet " ] = workbook . add_worksheet ( report_name [ : 31 ] )
self . _set_column_width ( report_data )
# Fill report
report_data [ " row_pos " ] = 0
self . _write_report_title ( report_name , report_data )
self . _write_filters ( filters , report_data )
self . _generate_report_content ( workbook , objects , data , report_data )
self . _write_report_footer ( report_footer , report_data )
def _define_formats ( self , workbook , report_data ) :
""" Add cell formats to current workbook.
""" Add cell formats to current workbook.
Those formats can be used on all cell .
Those formats can be used on all cell .
Available formats are :
Available formats are :
@ -72,77 +54,75 @@ class AbstractReportXslx(models.AbstractModel):
* format_amount
* format_amount
* format_percent_bold_italic
* format_percent_bold_italic
"""
"""
self . format_bold = workbook . add_format ( { " bold " : True } )
self . format_right = workbook . add_format ( { " align " : " right " } )
self . format_left = workbook . add_format ( { " align " : " left " } )
self . format_right_bold_italic = workbook . add_format (
{ " align " : " right " , " bold " : True , " italic " : True }
)
self . format_header_left = workbook . add_format (
{ " bold " : True , " border " : True , " bg_color " : " #FFFFCC " }
)
self . format_header_center = workbook . add_format (
{ " bold " : True , " align " : " center " , " border " : True , " bg_color " : " #FFFFCC " }
)
self . format_header_right = workbook . add_format (
{ " bold " : True , " align " : " right " , " border " : True , " bg_color " : " #FFFFCC " }
)
self . format_header_amount = workbook . add_format (
{ " bold " : True , " border " : True , " bg_color " : " #FFFFCC " }
)
currency_id = self . env [ " res.company " ] . _get_user_currency ( )
self . format_header_amount . set_num_format (
" #,##0. " + " 0 " * currency_id . decimal_places
)
self . format_amount = workbook . add_format ( )
self . format_amount . set_num_format ( " #,##0. " + " 0 " * currency_id . decimal_places )
self . format_amount_bold = workbook . add_format ( { " bold " : True } )
self . format_amount_bold . set_num_format (
" #,##0. " + " 0 " * currency_id . decimal_places
)
self . format_percent_bold_italic = workbook . add_format (
{ " bold " : True , " italic " : True }
)
self . format_percent_bold_italic . set_num_format ( " #,##0.00 % " )
currency_id = self . env [ " res.company " ] . _default_currency_id ( )
report_data [ " formats " ] = {
" format_bold " : workbook . add_format ( { " bold " : True } ) ,
" format_right " : workbook . add_format ( { " align " : " right " } ) ,
" format_left " : workbook . add_format ( { " align " : " left " } ) ,
" format_right_bold_italic " : workbook . add_format (
{ " align " : " right " , " bold " : True , " italic " : True }
) ,
" format_header_left " : workbook . add_format (
{ " bold " : True , " border " : True , " bg_color " : " #FFFFCC " }
) ,
" format_header_center " : workbook . add_format (
{ " bold " : True , " align " : " center " , " border " : True , " bg_color " : " #FFFFCC " }
) ,
" format_header_right " : workbook . add_format (
{ " bold " : True , " align " : " right " , " border " : True , " bg_color " : " #FFFFCC " }
) ,
" format_header_amount " : workbook . add_format (
{ " bold " : True , " border " : True , " bg_color " : " #FFFFCC " }
) . set_num_format ( " #,##0. " + " 0 " * currency_id . decimal_places ) ,
" format_amount " : workbook . add_format ( ) . set_num_format (
" #,##0. " + " 0 " * currency_id . decimal_places
) ,
" format_amount_bold " : workbook . add_format ( { " bold " : True } ) . set_num_format (
" #,##0. " + " 0 " * currency_id . decimal_places
) ,
" format_percent_bold_italic " : workbook . add_format (
{ " bold " : True , " italic " : True }
) . set_num_format ( " #,##0.00 % " ) ,
}
def _set_column_width ( self ) :
def _set_column_width ( self , report_data ) :
""" Set width for all defined columns.
""" Set width for all defined columns.
Columns are defined with `_get_report_columns` method .
Columns are defined with `_get_report_columns` method .
"""
"""
for position , column in self . columns . items ( ) :
self . sheet . set_column ( position , position , column [ " width " ] )
for position , column in report_data [ " columns " ] . items ( ) :
report_data [ " sheet " ] . set_column ( position , position , column [ " width " ] )
def _write_report_title ( self , title ) :
def _write_report_title ( self , title , report_data ) :
""" Write report title on current line using all defined columns width.
""" Write report title on current line using all defined columns width.
Columns are defined with `_get_report_columns` method .
Columns are defined with `_get_report_columns` method .
"""
"""
self . sheet . merge_range (
self . row_pos ,
report_data [ " sheet " ] . merge_range (
report_data [ " row_pos " ] ,
0 ,
0 ,
self . row_pos ,
len ( self . columns ) - 1 ,
report_data [ " row_pos " ] ,
len ( report_data [ " columns " ] ) - 1 ,
title ,
title ,
self . format_bold ,
report_data [ " formats " ] [ " format_bold " ] ,
)
)
self . row_pos + = 3
report_data [ " row_pos " ] + = 3
def _write_report_footer ( self , footer ) :
def _write_report_footer ( self , footer , report_data ) :
""" Write report footer .
""" Write report footer .
Columns are defined with `_get_report_columns` method .
Columns are defined with `_get_report_columns` method .
"""
"""
if footer :
if footer :
self . row_pos + = 1
self . sheet . merge_range (
self . row_pos ,
report_data [ " row_pos " ] + = 1
report_data [ " sheet " ] . merge_range (
report_data [ " row_pos " ] ,
0 ,
0 ,
self . row_pos ,
len ( self . columns ) - 1 ,
report_data [ " row_pos " ] ,
len ( report_data [ " columns " ] ) - 1 ,
footer ,
footer ,
self . format_left ,
report_data [ " formats " ] [ " format_left " ] ,
)
)
self . row_pos + = 1
report_data [ " row_pos " ] + = 1
def _write_filters ( self , filters ) :
def _write_filters ( self , filters , report_data ) :
""" Write one line per filters on starting on current line.
""" Write one line per filters on starting on current line.
Columns number for filter name is defined
Columns number for filter name is defined
with `_get_col_count_filter_name` method .
with `_get_col_count_filter_name` method .
@ -154,91 +134,102 @@ class AbstractReportXslx(models.AbstractModel):
col_count_filter_value = self . _get_col_count_filter_value ( )
col_count_filter_value = self . _get_col_count_filter_value ( )
col_value = col_name + col_count_filter_name + 1
col_value = col_name + col_count_filter_name + 1
for title , value in filters :
for title , value in filters :
self . sheet . merge_range (
self . row_pos ,
report_data [ " sheet " ] . merge_range (
report_data [ " row_pos " ] ,
col_name ,
col_name ,
self . row_pos ,
report_data [ " row_pos " ] ,
col_name + col_count_filter_name - 1 ,
col_name + col_count_filter_name - 1 ,
title ,
title ,
self . format_header_left ,
report_data [ " formats " ] [ " format_header_left " ] ,
)
)
self . sheet . merge_range (
self . row_pos ,
report_data [ " sheet " ] . merge_range (
report_data [ " row_pos " ] ,
col_value ,
col_value ,
self . row_pos ,
report_data [ " row_pos " ] ,
col_value + col_count_filter_value - 1 ,
col_value + col_count_filter_value - 1 ,
value ,
value ,
)
)
self . row_pos + = 1
self . row_pos + = 2
report_data [ " row_pos " ] + = 1
report_data [ " row_pos " ] + = 2
def write_array_title ( self , title ) :
def write_array_title ( self , title , report_data ) :
""" Write array title on current line using all defined columns width.
""" Write array title on current line using all defined columns width.
Columns are defined with `_get_report_columns` method .
Columns are defined with `_get_report_columns` method .
"""
"""
self . sheet . merge_range (
self . row_pos ,
report_data [ " sheet " ] . merge_range (
report_data [ " row_pos " ] ,
0 ,
0 ,
self . row_pos ,
len ( self . columns ) - 1 ,
report_data [ " row_pos " ] ,
len ( report_data [ " columns " ] ) - 1 ,
title ,
title ,
self . format_bold ,
report_data [ " formats " ] [ " format_bold " ] ,
)
)
self . row_pos + = 1
report_data [ " row_pos " ] + = 1
def write_array_header ( self ) :
def write_array_header ( self , report_data ) :
""" Write array header on current line using all defined columns name.
""" Write array header on current line using all defined columns name.
Columns are defined with `_get_report_columns` method .
Columns are defined with `_get_report_columns` method .
"""
"""
for col_pos , column in self . columns . items ( ) :
self . sheet . write (
self . row_pos , col_pos , column [ " header " ] , self . format_header_center
for col_pos , column in report_data [ " columns " ] . items ( ) :
report_data [ " sheet " ] . write (
report_data [ " row_pos " ] ,
col_pos ,
column [ " header " ] ,
report_data [ " formats " ] [ " format_header_center " ] ,
)
)
self . row_pos + = 1
report_data [ " row_pos " ] + = 1
def write_line ( self , line_object ) :
def write_line ( self , line_object , report_data ) :
""" Write a line on current line using all defined columns field name.
""" Write a line on current line using all defined columns field name.
Columns are defined with `_get_report_columns` method .
Columns are defined with `_get_report_columns` method .
"""
"""
for col_pos , column in self . columns . items ( ) :
for col_pos , column in report_data [ " columns " ] . items ( ) :
value = getattr ( line_object , column [ " field " ] )
value = getattr ( line_object , column [ " field " ] )
cell_type = column . get ( " type " , " string " )
cell_type = column . get ( " type " , " string " )
if cell_type == " many2one " :
if cell_type == " many2one " :
self . sheet . write_string (
self . row_pos , col_pos , value . name or " " , self . format_right
report_data [ " sheet " ] . write_string (
report_data [ " row_pos " ] ,
col_pos ,
value . name or " " ,
report_data [ " formats " ] [ " format_right " ] ,
)
)
elif cell_type == " string " :
elif cell_type == " string " :
if (
if (
hasattr ( line_object , " account_group_id " )
hasattr ( line_object , " account_group_id " )
and line_object . account_group_id
and line_object . account_group_id
) :
) :
self . sheet . write_string (
self . row_pos , col_pos , value or " " , self . format_bold
report_data [ " sheet " ] . write_string (
report_data [ " row_pos " ] ,
col_pos ,
value or " " ,
report_data [ " formats " ] [ " format_bold " ] ,
)
)
else :
else :
self . sheet . write_string ( self . row_pos , col_pos , value or " " )
report_data [ " sheet " ] . write_string (
report_data [ " row_pos " ] , col_pos , value or " "
)
elif cell_type == " amount " :
elif cell_type == " amount " :
if (
if (
hasattr ( line_object , " account_group_id " )
hasattr ( line_object , " account_group_id " )
and line_object . account_group_id
and line_object . account_group_id
) :
) :
cell_format = self . format_amount_bold
cell_format = report_data [ " formats " ] [ " format_amount_bold " ]
else :
else :
cell_format = self . format_amount
self . sheet . write_number (
self . row_pos , col_pos , float ( value ) , cell_format
cell_format = report_data [ " formats " ] [ " format_amount " ]
report_data [ " sheet " ] . write_number (
report_data [ " row_pos " ] , col_pos , float ( value ) , cell_format
)
)
elif cell_type == " amount_currency " :
elif cell_type == " amount_currency " :
if line_object . currency_id :
if line_object . currency_id :
format_amt = self . _get_currency_amt_format ( line_object )
self . sheet . write_number (
self . row_pos , col_pos , float ( value ) , format_amt
format_amt = self . _get_currency_amt_format ( line_object , report_data )
report_data [ " sheet " ] . write_number (
report_data [ " row_pos " ] , col_pos , float ( value ) , format_amt
)
)
self . row_pos + = 1
report_data [ " row_pos " ] + = 1
def write_line_from_dict ( self , line_dict ) :
def write_line_from_dict ( self , line_dict , report_data ) :
""" Write a line on current line """
""" Write a line on current line """
for col_pos , column in self . columns . items ( ) :
for col_pos , column in report_data [ " columns " ] . items ( ) :
value = line_dict . get ( column [ " field " ] , False )
value = line_dict . get ( column [ " field " ] , False )
cell_type = column . get ( " type " , " string " )
cell_type = column . get ( " type " , " string " )
if cell_type == " string " :
if cell_type == " string " :
@ -246,8 +237,11 @@ class AbstractReportXslx(models.AbstractModel):
line_dict . get ( " account_group_id " , False )
line_dict . get ( " account_group_id " , False )
and line_dict [ " account_group_id " ]
and line_dict [ " account_group_id " ]
) :
) :
self . sheet . write_string (
self . row_pos , col_pos , value or " " , self . format_bold
report_data [ " sheet " ] . write_string (
report_data [ " row_pos " ] ,
col_pos ,
value or " " ,
report_data [ " formats " ] [ " format_bold " ] ,
)
)
else :
else :
if (
if (
@ -256,223 +250,302 @@ class AbstractReportXslx(models.AbstractModel):
and not isinstance ( value , int )
and not isinstance ( value , int )
) :
) :
value = value and value . strftime ( " %d / % m/ % Y " )
value = value and value . strftime ( " %d / % m/ % Y " )
self . sheet . write_string ( self . row_pos , col_pos , value or " " )
report_data [ " sheet " ] . write_string (
report_data [ " row_pos " ] , col_pos , value or " "
)
elif cell_type == " amount " :
elif cell_type == " amount " :
if (
if (
line_dict . get ( " account_group_id " , False )
line_dict . get ( " account_group_id " , False )
and line_dict [ " account_group_id " ]
and line_dict [ " account_group_id " ]
) :
) :
cell_format = self . format_amount_bold
cell_format = report_data [ " formats " ] [ " format_amount_bold " ]
else :
else :
cell_format = self . format_amount
self . sheet . write_number (
self . row_pos , col_pos , float ( value ) , cell_format
cell_format = report_data [ " formats " ] [ " format_amount " ]
report_data [ " sheet " ] . write_number (
report_data [ " row_pos " ] , col_pos , float ( value ) , cell_format
)
)
elif cell_type == " amount_currency " :
elif cell_type == " amount_currency " :
if line_dict . get ( " currency_name " , False ) :
if line_dict . get ( " currency_name " , False ) :
format_amt = self . _get_currency_amt_format_dict ( line_dict )
self . sheet . write_number (
self . row_pos , col_pos , float ( value ) , format_amt
format_amt = self . _get_currency_amt_format_dict (
line_dict , report_data
)
report_data [ " sheet " ] . write_number (
report_data [ " row_pos " ] , col_pos , float ( value ) , format_amt
)
)
elif cell_type == " currency_name " :
elif cell_type == " currency_name " :
self . sheet . write_string (
self . row_pos , col_pos , value or " " , self . format_right
report_data [ " sheet " ] . write_string (
report_data [ " row_pos " ] ,
col_pos ,
value or " " ,
report_data [ " formats " ] [ " format_right " ] ,
)
)
self . row_pos + = 1
report_data [ " row_pos " ] + = 1
def write_initial_balance ( self , my_object , label ) :
def write_initial_balance ( self , my_object , label , report_data ) :
""" Write a specific initial balance line on current line
""" Write a specific initial balance line on current line
using defined columns field_initial_balance name .
using defined columns field_initial_balance name .
Columns are defined with `_get_report_columns` method .
Columns are defined with `_get_report_columns` method .
"""
"""
col_pos_label = self . _get_col_pos_initial_balance_label ( )
col_pos_label = self . _get_col_pos_initial_balance_label ( )
self . sheet . write ( self . row_pos , col_pos_label , label , self . format_right )
for col_pos , column in self . columns . items ( ) :
report_data [ " sheet " ] . write (
report_data [ " row_pos " ] ,
col_pos_label ,
label ,
report_data [ " formats " ] [ " format_right " ] ,
)
for col_pos , column in report_data [ " columns " ] . items ( ) :
if column . get ( " field_initial_balance " ) :
if column . get ( " field_initial_balance " ) :
value = getattr ( my_object , column [ " field_initial_balance " ] )
value = getattr ( my_object , column [ " field_initial_balance " ] )
cell_type = column . get ( " type " , " string " )
cell_type = column . get ( " type " , " string " )
if cell_type == " string " :
if cell_type == " string " :
self . sheet . write_string ( self . row_pos , col_pos , value or " " )
report_data [ " sheet " ] . write_string (
report_data [ " row_pos " ] , col_pos , value or " "
)
elif cell_type == " amount " :
elif cell_type == " amount " :
self . sheet . write_number (
self . row_pos , col_pos , float ( value ) , self . format_amount
report_data [ " sheet " ] . write_number (
report_data [ " row_pos " ] ,
col_pos ,
float ( value ) ,
report_data [ " formats " ] [ " format_amount " ] ,
)
)
elif cell_type == " amount_currency " :
elif cell_type == " amount_currency " :
if my_object . currency_id :
if my_object . currency_id :
format_amt = self . _get_currency_amt_format ( my_object )
self . sheet . write_number (
self . row_pos , col_pos , float ( value ) , format_amt
format_amt = self . _get_currency_amt_format (
my_object , report_data
)
report_data [ " sheet " ] . write_number (
report_data [ " row_pos " ] , col_pos , float ( value ) , format_amt
)
)
elif column . get ( " field_currency_balance " ) :
elif column . get ( " field_currency_balance " ) :
value = getattr ( my_object , column [ " field_currency_balance " ] )
value = getattr ( my_object , column [ " field_currency_balance " ] )
cell_type = column . get ( " type " , " string " )
cell_type = column . get ( " type " , " string " )
if cell_type == " many2one " :
if cell_type == " many2one " :
if my_object . currency_id :
if my_object . currency_id :
self . sheet . write_string (
self . row_pos , col_pos , value . name or " " , self . format_right
report_data [ " sheet " ] . write_string (
report_data [ " row_pos " ] ,
col_pos ,
value . name or " " ,
report_data [ " formats " ] [ " format_right " ] ,
)
)
self . row_pos + = 1
report_data [ " row_pos " ] + = 1
def write_initial_balance_from_dict ( self , my_object , label ) :
def write_initial_balance_from_dict ( self , my_object , label , report_data ) :
""" Write a specific initial balance line on current line
""" Write a specific initial balance line on current line
using defined columns field_initial_balance name .
using defined columns field_initial_balance name .
Columns are defined with `_get_report_columns` method .
Columns are defined with `_get_report_columns` method .
"""
"""
col_pos_label = self . _get_col_pos_initial_balance_label ( )
col_pos_label = self . _get_col_pos_initial_balance_label ( )
self . sheet . write ( self . row_pos , col_pos_label , label , self . format_right )
for col_pos , column in self . columns . items ( ) :
report_data [ " sheet " ] . write (
report_data [ " row_pos " ] ,
col_pos_label ,
label ,
report_data [ " formats " ] [ " format_right " ] ,
)
for col_pos , column in report_data [ " columns " ] . items ( ) :
if column . get ( " field_initial_balance " ) :
if column . get ( " field_initial_balance " ) :
value = my_object . get ( column [ " field_initial_balance " ] , False )
value = my_object . get ( column [ " field_initial_balance " ] , False )
cell_type = column . get ( " type " , " string " )
cell_type = column . get ( " type " , " string " )
if cell_type == " string " :
if cell_type == " string " :
self . sheet . write_string ( self . row_pos , col_pos , value or " " )
report_data [ " sheet " ] . write_string (
report_data [ " row_pos " ] , col_pos , value or " "
)
elif cell_type == " amount " :
elif cell_type == " amount " :
self . sheet . write_number (
self . row_pos , col_pos , float ( value ) , self . format_amount
report_data [ " sheet " ] . write_number (
report_data [ " row_pos " ] ,
col_pos ,
float ( value ) ,
report_data [ " formats " ] [ " format_amount " ] ,
)
)
elif cell_type == " amount_currency " :
elif cell_type == " amount_currency " :
if my_object [ " currency_id " ] :
if my_object [ " currency_id " ] :
format_amt = self . _get_currency_amt_format ( my_object )
self . sheet . write_number (
self . row_pos , col_pos , float ( value ) , format_amt
format_amt = self . _get_currency_amt_format (
my_object , report_data
)
report_data [ " sheet " ] . write_number (
report_data [ " row_pos " ] , col_pos , float ( value ) , format_amt
)
)
elif column . get ( " field_currency_balance " ) :
elif column . get ( " field_currency_balance " ) :
value = my_object . get ( column [ " field_currency_balance " ] , False )
value = my_object . get ( column [ " field_currency_balance " ] , False )
cell_type = column . get ( " type " , " string " )
cell_type = column . get ( " type " , " string " )
if cell_type == " many2one " :
if cell_type == " many2one " :
if my_object [ " currency_id " ] :
if my_object [ " currency_id " ] :
self . sheet . write_string (
self . row_pos , col_pos , value . name or " " , self . format_right
report_data [ " sheet " ] . write_string (
report_data [ " row_pos " ] ,
col_pos ,
value . name or " " ,
report_data [ " formats " ] [ " format_right " ] ,
)
)
self . row_pos + = 1
report_data [ " row_pos " ] + = 1
def write_ending_balance ( self , my_object , name , label ) :
def write_ending_balance ( self , my_object , name , label , report_data ) :
""" Write a specific ending balance line on current line
""" Write a specific ending balance line on current line
using defined columns field_final_balance name .
using defined columns field_final_balance name .
Columns are defined with `_get_report_columns` method .
Columns are defined with `_get_report_columns` method .
"""
"""
for i in range ( 0 , len ( self . columns ) ) :
self . sheet . write ( self . row_pos , i , " " , self . format_header_right )
for i in range ( 0 , len ( report_data [ " columns " ] ) ) :
report_data [ " sheet " ] . write (
report_data [ " row_pos " ] ,
i ,
" " ,
report_data [ " formats " ] [ " format_header_right " ] ,
)
row_count_name = self . _get_col_count_final_balance_name ( )
row_count_name = self . _get_col_count_final_balance_name ( )
col_pos_label = self . _get_col_pos_final_balance_label ( )
col_pos_label = self . _get_col_pos_final_balance_label ( )
self . sheet . merge_range (
self . row_pos ,
report_data [ " sheet " ] . merge_range (
report_data [ " row_pos " ] ,
0 ,
0 ,
self . row_pos ,
report_data [ " row_pos " ] ,
row_count_name - 1 ,
row_count_name - 1 ,
name ,
name ,
self . format_header_left ,
report_data [ " formats " ] [ " format_header_left " ] ,
)
report_data [ " sheet " ] . write (
report_data [ " row_pos " ] ,
col_pos_label ,
label ,
report_data [ " formats " ] [ " format_header_right " ] ,
)
)
self . sheet . write ( self . row_pos , col_pos_label , label , self . format_header_right )
for col_pos , column in self . columns . items ( ) :
for col_pos , column in report_data [ " columns " ] . items ( ) :
if column . get ( " field_final_balance " ) :
if column . get ( " field_final_balance " ) :
value = getattr ( my_object , column [ " field_final_balance " ] )
value = getattr ( my_object , column [ " field_final_balance " ] )
cell_type = column . get ( " type " , " string " )
cell_type = column . get ( " type " , " string " )
if cell_type == " string " :
if cell_type == " string " :
self . sheet . write_string (
self . row_pos , col_pos , value or " " , self . format_header_right
report_data [ " sheet " ] . write_string (
report_data [ " row_pos " ] ,
col_pos ,
value or " " ,
report_data [ " formats " ] [ " format_header_right " ] ,
)
)
elif cell_type == " amount " :
elif cell_type == " amount " :
self . sheet . write_number (
self . row_pos , col_pos , float ( value ) , self . format_header_amount
report_data [ " sheet " ] . write_number (
report_data [ " row_pos " ] ,
col_pos ,
float ( value ) ,
report_data [ " formats " ] [ " format_header_amount " ] ,
)
)
elif cell_type == " amount_currency " :
elif cell_type == " amount_currency " :
if my_object . currency_id :
if my_object . currency_id :
format_amt = self . _get_currency_amt_header_format ( my_object )
self . sheet . write_number (
self . row_pos , col_pos , float ( value ) , format_amt
format_amt = self . _get_currency_amt_header_format (
my_object , report_data
)
report_data [ " sheet " ] . write_number (
report_data [ " row_pos " ] , col_pos , float ( value ) , format_amt
)
)
elif column . get ( " field_currency_balance " ) :
elif column . get ( " field_currency_balance " ) :
value = getattr ( my_object , column [ " field_currency_balance " ] )
value = getattr ( my_object , column [ " field_currency_balance " ] )
cell_type = column . get ( " type " , " string " )
cell_type = column . get ( " type " , " string " )
if cell_type == " many2one " :
if cell_type == " many2one " :
if my_object . currency_id :
if my_object . currency_id :
self . sheet . write_string (
self . row_pos ,
report_data [ " sheet " ] . write_string (
report_data [ " row_pos " ] ,
col_pos ,
col_pos ,
value . name or " " ,
value . name or " " ,
self . format_header_right ,
report_data [ " formats " ] [ " format_header_right " ] ,
)
)
self . row_pos + = 1
report_data [ " row_pos " ] + = 1
def write_ending_balance_from_dict ( self , my_object , name , label ) :
def write_ending_balance_from_dict ( self , my_object , name , label , report_data ) :
""" Write a specific ending balance line on current line
""" Write a specific ending balance line on current line
using defined columns field_final_balance name .
using defined columns field_final_balance name .
Columns are defined with `_get_report_columns` method .
Columns are defined with `_get_report_columns` method .
"""
"""
for i in range ( 0 , len ( self . columns ) ) :
self . sheet . write ( self . row_pos , i , " " , self . format_header_right )
for i in range ( 0 , len ( report_data [ " columns " ] ) ) :
report_data [ " sheet " ] . write (
report_data [ " row_pos " ] ,
i ,
" " ,
report_data [ " formats " ] [ " format_header_right " ] ,
)
row_count_name = self . _get_col_count_final_balance_name ( )
row_count_name = self . _get_col_count_final_balance_name ( )
col_pos_label = self . _get_col_pos_final_balance_label ( )
col_pos_label = self . _get_col_pos_final_balance_label ( )
self . sheet . merge_range (
self . row_pos ,
report_data [ " sheet " ] . merge_range (
report_data [ " row_pos " ] ,
0 ,
0 ,
self . row_pos ,
report_data [ " row_pos " ] ,
row_count_name - 1 ,
row_count_name - 1 ,
name ,
name ,
self . format_header_left ,
report_data [ " formats " ] [ " format_header_left " ] ,
)
report_data [ " sheet " ] . write (
report_data [ " row_pos " ] ,
col_pos_label ,
label ,
report_data [ " formats " ] [ " format_header_right " ] ,
)
)
self . sheet . write ( self . row_pos , col_pos_label , label , self . format_header_right )
for col_pos , column in self . columns . items ( ) :
for col_pos , column in report_data [ " columns " ] . items ( ) :
if column . get ( " field_final_balance " ) :
if column . get ( " field_final_balance " ) :
value = my_object . get ( column [ " field_final_balance " ] , False )
value = my_object . get ( column [ " field_final_balance " ] , False )
cell_type = column . get ( " type " , " string " )
cell_type = column . get ( " type " , " string " )
if cell_type == " string " :
if cell_type == " string " :
self . sheet . write_string (
self . row_pos , col_pos , value or " " , self . format_header_right
report_data [ " sheet " ] . write_string (
report_data [ " row_pos " ] ,
col_pos ,
value or " " ,
report_data [ " formats " ] [ " format_header_right " ] ,
)
)
elif cell_type == " amount " :
elif cell_type == " amount " :
self . sheet . write_number (
self . row_pos , col_pos , float ( value ) , self . format_header_amount
report_data [ " sheet " ] . write_number (
report_data [ " row_pos " ] ,
col_pos ,
float ( value ) ,
report_data [ " formats " ] [ " format_header_amount " ] ,
)
)
elif cell_type == " amount_currency " :
elif cell_type == " amount_currency " :
if my_object [ " currency_id " ] and value :
if my_object [ " currency_id " ] and value :
format_amt = self . _get_currency_amt_format_dict ( my_object )
self . sheet . write_number (
self . row_pos , col_pos , float ( value ) , format_amt
format_amt = self . _get_currency_amt_format_dict (
my_object , report_data
)
report_data [ " sheet " ] . write_number (
report_data [ " row_pos " ] , col_pos , float ( value ) , format_amt
)
)
elif column . get ( " field_currency_balance " ) :
elif column . get ( " field_currency_balance " ) :
value = my_object . get ( column [ " field_currency_balance " ] , False )
value = my_object . get ( column [ " field_currency_balance " ] , False )
cell_type = column . get ( " type " , " string " )
cell_type = column . get ( " type " , " string " )
if cell_type == " many2one " :
if cell_type == " many2one " :
if my_object [ " currency_id " ] :
if my_object [ " currency_id " ] :
self . sheet . write_string (
self . row_pos , col_pos , value or " " , self . format_header_right
report_data [ " sheet " ] . write_string (
report_data [ " row_pos " ] ,
col_pos ,
value or " " ,
report_data [ " formats " ] [ " format_header_right " ] ,
)
)
elif cell_type == " currency_name " :
elif cell_type == " currency_name " :
self . sheet . write_string (
self . row_pos , col_pos , value or " " , self . format_header_right
report_data [ " sheet " ] . write_string (
report_data [ " row_pos " ] ,
col_pos ,
value or " " ,
report_data [ " formats " ] [ " format_header_right " ] ,
)
)
self . row_pos + = 1
report_data [ " row_pos " ] + = 1
def _get_currency_amt_format ( self , line_object ) :
def _get_currency_amt_format ( self , line_object , report_data ) :
""" Return amount format specific for each currency. """
""" Return amount format specific for each currency. """
if " account_group_id " in line_object and line_object [ " account_group_id " ] :
if " account_group_id " in line_object and line_object [ " account_group_id " ] :
format_amt = self . format_amount_bold
format_amt = report_data [ " formats " ] [ " format_amount_bold " ]
field_prefix = " format_amount_bold "
field_prefix = " format_amount_bold "
else :
else :
format_amt = self . format_amount
format_amt = report_data [ " formats " ] [ " format_amount " ]
field_prefix = " format_amount "
field_prefix = " format_amount "
if " currency_id " in line_object and line_object . get ( " currency_id " , False ) :
if " currency_id " in line_object and line_object . get ( " currency_id " , False ) :
field_name = " {}_{} " . format ( field_prefix , line_object [ " currency_id " ] . name )
field_name = " {}_{} " . format ( field_prefix , line_object [ " currency_id " ] . name )
if hasattr ( self , field_name ) :
if hasattr ( self , field_name ) :
format_amt = getattr ( self , field_name )
format_amt = getattr ( self , field_name )
else :
else :
format_amt = self . workbook . add_format ( )
self . field_name = format_amt
format_amt = report_data [ " workbook " ] . add_format ( )
report_data [ " field_name " ] = format_amt
format_amount = " #,##0. " + (
format_amount = " #,##0. " + (
" 0 " * line_object [ " currency_id " ] . decimal_places
" 0 " * line_object [ " currency_id " ] . decimal_places
)
)
format_amt . set_num_format ( format_amount )
format_amt . set_num_format ( format_amount )
return format_amt
return format_amt
def _get_currency_amt_format_dict ( self , line_dict ) :
def _get_currency_amt_format_dict ( self , line_dict , report_data ) :
""" Return amount format specific for each currency. """
""" Return amount format specific for each currency. """
if line_dict . get ( " account_group_id " , False ) and line_dict [ " account_group_id " ] :
if line_dict . get ( " account_group_id " , False ) and line_dict [ " account_group_id " ] :
format_amt = self . format_amount_bold
format_amt = report_data [ " formats " ] [ " format_amount_bold " ]
field_prefix = " format_amount_bold "
field_prefix = " format_amount_bold "
else :
else :
format_amt = self . format_amount
format_amt = report_data [ " formats " ] [ " format_amount " ]
field_prefix = " format_amount "
field_prefix = " format_amount "
if line_dict . get ( " currency_id " , False ) and line_dict [ " currency_id " ] :
if line_dict . get ( " currency_id " , False ) and line_dict [ " currency_id " ] :
if isinstance ( line_dict [ " currency_id " ] , int ) :
if isinstance ( line_dict [ " currency_id " ] , int ) :
@ -483,49 +556,49 @@ class AbstractReportXslx(models.AbstractModel):
if hasattr ( self , field_name ) :
if hasattr ( self , field_name ) :
format_amt = getattr ( self , field_name )
format_amt = getattr ( self , field_name )
else :
else :
format_amt = self . workbook . add_format ( )
self . field_name = format_amt
format_amt = report_data [ " workbook " ] . add_format ( )
report_data [ " field_name " ] = format_amt
format_amount = " #,##0. " + ( " 0 " * currency . decimal_places )
format_amount = " #,##0. " + ( " 0 " * currency . decimal_places )
format_amt . set_num_format ( format_amount )
format_amt . set_num_format ( format_amount )
return format_amt
return format_amt
def _get_currency_amt_header_format ( self , line_object ) :
def _get_currency_amt_header_format ( self , line_object , report_data ) :
""" Return amount header format for each currency. """
""" Return amount header format for each currency. """
format_amt = self . format_header_amount
format_amt = report_data [ " formats " ] [ " format_header_amount " ]
if line_object . currency_id :
if line_object . currency_id :
field_name = " format_header_amount_ %s " % line_object . currency_id . name
field_name = " format_header_amount_ %s " % line_object . currency_id . name
if hasattr ( self , field_name ) :
if hasattr ( self , field_name ) :
format_amt = getattr ( self , field_name )
format_amt = getattr ( self , field_name )
else :
else :
format_amt = self . workbook . add_format (
format_amt = report_data [ " workbook " ] . add_format (
{ " bold " : True , " border " : True , " bg_color " : " #FFFFCC " }
{ " bold " : True , " border " : True , " bg_color " : " #FFFFCC " }
)
)
self . field_name = format_amt
report_data [ " field_name " ] = format_amt
format_amount = " #,##0. " + (
format_amount = " #,##0. " + (
" 0 " * line_object . currency_id . decimal_places
" 0 " * line_object . currency_id . decimal_places
)
)
format_amt . set_num_format ( format_amount )
format_amt . set_num_format ( format_amount )
return format_amt
return format_amt
def _get_currency_amt_header_format_dict ( self , line_object ) :
def _get_currency_amt_header_format_dict ( self , line_object , report_data ) :
""" Return amount header format for each currency. """
""" Return amount header format for each currency. """
format_amt = self . format_header_amount
format_amt = report_data [ " formats " ] [ " format_header_amount " ]
if line_object [ " currency_id " ] :
if line_object [ " currency_id " ] :
field_name = " format_header_amount_ %s " % line_object [ " currency_name " ]
field_name = " format_header_amount_ %s " % line_object [ " currency_name " ]
if hasattr ( self , field_name ) :
if hasattr ( self , field_name ) :
format_amt = getattr ( self , field_name )
format_amt = getattr ( self , field_name )
else :
else :
format_amt = self . workbook . add_format (
format_amt = report_data [ " workbook " ] . add_format (
{ " bold " : True , " border " : True , " bg_color " : " #FFFFCC " }
{ " bold " : True , " border " : True , " bg_color " : " #FFFFCC " }
)
)
self . field_name = format_amt
report_data [ " field_name " ] = format_amt
format_amount = " #,##0. " + (
format_amount = " #,##0. " + (
" 0 " * line_object [ " currency_id " ] . decimal_places
" 0 " * line_object [ " currency_id " ] . decimal_places
)
)
format_amt . set_num_format ( format_amount )
format_amt . set_num_format ( format_amount )
return format_amt
return format_amt
def _generate_report_content ( self , workbook , report , data ) :
def _generate_report_content ( self , workbook , report , data , report_data ) :
"""
"""
Allow to fetch report content to be displayed .
Allow to fetch report content to be displayed .
"""
"""