@ -1,8 +1,9 @@
import time
from odoo import fields , models , api
from odoo import fields , models , api , _
import io
import json
from odoo . exceptions import AccessError , UserError , AccessDenied
try :
from odoo . tools . misc import xlsxwriter
@ -12,36 +13,33 @@ except ImportError:
class BalanceSheetView ( models . TransientModel ) :
_name = ' dynamic.balance.sheet.report '
_inherit = " account.common.report "
company_id = fields . Many2one ( ' res.company ' , required = True ,
default = lambda self : self . env . company )
journal_ids = fields . Many2many ( ' account.journal ' ,
string = ' Journals ' , required = True ,
default = [ ] )
account_ids = fields . Many2many (
" account.account " ,
string = " Accounts " ,
)
account_ids = fields . Many2many ( " account.account " , string = " Accounts " )
account_tag_ids = fields . Many2many ( " account.account.tag " ,
string = " Account Tags " )
analytic_ids = fields . Many2many (
" account.analytic.account " , string = " Analytic Accounts "
)
" account.analytic.account " , string = " Analytic Accounts " )
analytic_tag_ids = fields . Many2many ( " account.analytic.tag " ,
string = " Analytic Tags " )
display_account = fields . Selection (
[ ( ' all ' , ' All ' ) , ( ' movement ' , ' With movements ' ) ,
( ' not_zero ' , ' With balance is not equal to 0 ' ) ] ,
string = ' Display Accounts ' , required = True , default = ' movement ' )
target_move = fields . Selection (
[ ( ' all ' , ' All ' ) , ( ' posted ' , ' Posted ' ) ] ,
string = ' Target Move ' , required = True , default = ' posted ' )
date_from = fields . Date ( string = " Start date " )
date_to = fields . Date ( string = " End date " )
@api . model
def view_report ( self , option , tag ) :
r = self . env [ ' dynamic.balance.sheet.report ' ] . search ( [ ( ' id ' , ' = ' , option [ 0 ] ) ] )
r = self . env [ ' dynamic.balance.sheet.report ' ] . search (
[ ( ' id ' , ' = ' , option [ 0 ] ) ] )
data = {
' display_account ' : r . display_account ,
' model ' : self ,
@ -51,7 +49,6 @@ class BalanceSheetView(models.TransientModel):
' account_tags ' : r . account_tag_ids ,
' analytics ' : r . analytic_ids ,
' analytic_tags ' : r . analytic_tag_ids ,
}
if r . date_from :
data . update ( {
@ -62,7 +59,7 @@ class BalanceSheetView(models.TransientModel):
' date_to ' : r . date_to ,
} )
company_id = self . env . user . company_id
company_id = self . env . company
company_domain = [ ( ' company_id ' , ' = ' , company_id . id ) ]
if r . account_tag_ids :
company_domain . append (
@ -71,10 +68,7 @@ class BalanceSheetView(models.TransientModel):
company_domain . append ( ( ' id ' , ' in ' , r . account_ids . ids ) )
new_account_ids = self . env [ ' account.account ' ] . search ( company_domain )
data . update ( {
' accounts ' : new_account_ids ,
} )
data . update ( { ' accounts ' : new_account_ids , } )
filters = self . get_filter ( option )
records = self . _get_report_values ( data )
@ -101,7 +95,7 @@ class BalanceSheetView(models.TransientModel):
' view_format ' : ' vertical ' ,
' company_id ' : self . company_id ,
' used_context ' : { ' journal_ids ' : False ,
' state ' : filters [ ' target_move ' ] ,
' state ' : filters [ ' target_move ' ] . lower ( ) ,
' date_from ' : filters [ ' date_from ' ] ,
' date_to ' : filters [ ' date_to ' ] ,
' strict_range ' : False ,
@ -146,7 +140,6 @@ class BalanceSheetView(models.TransientModel):
rec [ ' balance ' ] = move_lines_dict [ rec [ ' code ' ] ] [ ' balance ' ]
parent_list = list ( set ( parent_list ) )
max_level = 0
for rep in report_lines_move :
if rep [ ' level ' ] > max_level :
@ -155,21 +148,18 @@ class BalanceSheetView(models.TransientModel):
def get_parents ( obj ) :
for item in report_lines_move :
for each in obj :
if item [ ' report_type ' ] != ' account_type ' and each in item [
' c_ids ' ] :
if item [ ' report_type ' ] != ' account_type ' and \
each in item [ ' c_ids ' ] :
obj . append ( item [ ' r_id ' ] )
if item [ ' report_type ' ] == ' account_report ' :
obj . append ( item [ ' r_id ' ] )
break
get_parents ( parent_list )
for i in range ( max_level ) :
get_parents ( parent_list )
parent_list = list ( set ( parent_list ) )
final_report_lines = [ ]
for rec in report_lines_move :
@ -196,8 +186,8 @@ class BalanceSheetView(models.TransientModel):
def assign_sum ( obj ) :
for each in obj :
if each [ ' r_id ' ] in parent_list and each [
' report_type ' ] != ' account_report ' :
if each [ ' r_id ' ] in parent_list and \
each [ ' report_type ' ] != ' account_report ' :
each [ ' debit ' ] = sum_list_new [ each [ ' r_id ' ] ] [ ' s_debit ' ]
each [ ' credit ' ] = sum_list_new [ each [ ' r_id ' ] ] [ ' s_credit ' ]
@ -205,7 +195,7 @@ class BalanceSheetView(models.TransientModel):
sum_list_new = filter_sum ( final_report_lines )
assign_sum ( final_report_lines )
company_id = self . env . user . company_id
company_id = self . env . company
currency = company_id . currency_id
symbol = currency . symbol
rounding = currency . rounding
@ -216,15 +206,20 @@ class BalanceSheetView(models.TransientModel):
rec [ ' credit ' ] = round ( rec [ ' credit ' ] , 2 )
rec [ ' balance ' ] = rec [ ' debit ' ] - rec [ ' credit ' ]
rec [ ' balance ' ] = round ( rec [ ' balance ' ] , 2 )
if position == " before " :
rec [ ' m_debit ' ] = symbol + " " + str ( rec [ ' debit ' ] )
rec [ ' m_credit ' ] = symbol + " " + str ( rec [ ' credit ' ] )
rec [ ' m_balance ' ] = symbol + " " + str ( rec [ ' balance ' ] )
if ( rec [ ' balance_cmp ' ] < 0 and rec [ ' balance ' ] > 0 ) or (
rec [ ' balance_cmp ' ] > 0 and rec [ ' balance ' ] < 0 ) :
rec [ ' balance ' ] = rec [ ' balance ' ] * - 1
if position == " before " :
rec [ ' m_debit ' ] = symbol + " " + " {:,.2f} " . format ( rec [ ' debit ' ] )
rec [ ' m_credit ' ] = symbol + " " + " {:,.2f} " . format ( rec [ ' credit ' ] )
rec [ ' m_balance ' ] = symbol + " " + " {:,.2f} " . format (
rec [ ' balance ' ] )
else :
rec [ ' m_debit ' ] = str ( rec [ ' debit ' ] ) + " " + symbol
rec [ ' m_credit ' ] = str ( rec [ ' credit ' ] ) + " " + symbol
rec [ ' m_balance ' ] = str ( rec [ ' balance ' ] ) + " " + symbol
rec [ ' m_debit ' ] = " {:,.2f} " . format ( rec [ ' debit ' ] ) + " " + symbol
rec [ ' m_credit ' ] = " {:,.2f} " . format ( rec [ ' credit ' ] ) + " " + symbol
rec [ ' m_balance ' ] = " {:,.2f} " . format (
rec [ ' balance ' ] ) + " " + symbol
return {
' name ' : tag ,
@ -289,19 +284,17 @@ class BalanceSheetView(models.TransientModel):
filters [ ' account_tag_list ' ] = data . get ( ' account_tag_list ' )
filters [ ' analytic_tag_list ' ] = data . get ( ' analytic_tag_list ' )
filters [ ' company_name ' ] = data . get ( ' company_name ' )
filters [ ' target_move ' ] = data . get ( ' target_move ' ) . capitalize ( )
return filters
def get_filter_data ( self , option ) :
r = self . env [ ' dynamic.balance.sheet.report ' ] . search ( [ ( ' id ' , ' = ' , option [ 0 ] ) ] )
r = self . env [ ' dynamic.balance.sheet.report ' ] . search (
[ ( ' id ' , ' = ' , option [ 0 ] ) ] )
default_filters = { }
company_id = r . env . user . company_id
company_id = self . env . company
company_domain = [ ( ' company_id ' , ' = ' , company_id . id ) ]
journals = r . journal_ids if r . journal_ids else self . env [
' account.journal ' ] . search ( company_domain )
analytics = self . analytic_ids if self . analytic_ids else self . env [
' account.analytic.account ' ] . search (
company_domain )
@ -320,12 +313,10 @@ class BalanceSheetView(models.TransientModel):
accounts = self . account_ids if self . account_ids else self . env [
' account.account ' ] . search ( company_domain )
filter_dict = {
' journal_ids ' : r . journal_ids . ids ,
' account_ids ' : r . account_ids . ids ,
' analytic_ids ' : r . analytic_ids . ids ,
' company_id ' : company_id . id ,
' date_from ' : r . date_from ,
' date_to ' : r . date_to ,
@ -349,6 +340,8 @@ class BalanceSheetView(models.TransientModel):
init_balance = True
journals = data [ ' journals ' ]
accounts = self . env [ ' account.account ' ] . search ( [ ] )
if not accounts :
raise UserError ( _ ( " No Accounts Found! Please Add One " ) )
account_res = self . _get_accounts ( accounts , init_balance ,
display_account , data )
debit_total = 0
@ -367,49 +360,39 @@ class BalanceSheetView(models.TransientModel):
@api . model
def create ( self , vals ) :
vals [ ' target_move ' ] = ' all '
vals [ ' target_move ' ] = ' posted '
res = super ( BalanceSheetView , self ) . create ( vals )
return res
def write ( self , vals ) :
if vals . get ( ' target_move ' ) :
vals . update ( { ' target_move ' : vals . get ( ' target_move ' ) . lower ( ) } )
if vals . get ( ' journal_ids ' ) :
vals . update ( { ' journal_ids ' : [ ( 6 , 0 , vals . get ( ' journal_ids ' ) ) ] } )
if vals . get ( ' journal_ids ' ) == [ ] :
if not vals . get ( ' journal_ids ' ) :
vals . update ( { ' journal_ids ' : [ ( 5 , ) ] } )
if vals . get ( ' account_ids ' ) :
vals . update (
{ ' account_ids ' : [ ( 4 , j ) for j in vals . get ( ' account_ids ' ) ] } )
if vals . get ( ' account_ids ' ) == [ ] :
if not vals . get ( ' account_ids ' ) :
vals . update ( { ' account_ids ' : [ ( 5 , ) ] } )
if vals . get ( ' analytic_ids ' ) :
vals . update (
{ ' analytic_ids ' : [ ( 4 , j ) for j in vals . get ( ' analytic_ids ' ) ] } )
if vals . get ( ' analytic_ids ' ) == [ ] :
if not vals . get ( ' analytic_ids ' ) :
vals . update ( { ' analytic_ids ' : [ ( 5 , ) ] } )
if vals . get ( ' analytic_tags ' ) :
vals . update (
{ ' analytic_tags ' : [ ( 4 , j ) for j in vals . get ( ' analytic_tags ' ) ] } )
if vals . get ( ' analytic_tags ' ) == [ ] :
vals . update ( { ' analytic_tags ' : [ ( 5 , ) ] } )
if vals . get ( ' account_tags ' ) :
vals . update (
{ ' account_tags ' : [ ( 4 , j ) for j in vals . get ( ' account_tags ' ) ] } )
if vals . get ( ' account_tags ' ) == [ ] :
vals . update ( { ' account_tags ' : [ ( 5 , ) ] } )
if vals . get ( ' account_tag_ids ' ) :
vals . update ( { ' account_tag_ids ' : [ ( 4 , j ) for j in
vals . get ( ' account_tag_ids ' ) ] } )
if vals . get ( ' account_tag_ids ' ) == [ ] :
if not vals . get ( ' account_tag_ids ' ) :
vals . update ( { ' account_tag_ids ' : [ ( 5 , ) ] } )
if vals . get ( ' analytic_tag_ids ' ) :
vals . update ( { ' analytic_tag_ids ' : [ ( 4 , j ) for j in
vals . get ( ' analytic_tag_ids ' ) ] } )
if vals . get ( ' analytic_tag_ids ' ) == [ ] :
if not vals . get ( ' analytic_tag_ids ' ) :
vals . update ( { ' analytic_tag_ids ' : [ ( 5 , ) ] } )
res = super ( BalanceSheetView , self ) . write ( vals )
@ -436,6 +419,7 @@ class BalanceSheetView(models.TransientModel):
new_final_filter + = " AND m.state = ' posted ' "
else :
new_final_filter + = " AND m.state in ( ' draft ' , ' posted ' ) "
if data . get ( ' date_from ' ) :
new_final_filter + = " AND l.date >= ' %s ' " % data . get ( ' date_from ' )
if data . get ( ' date_to ' ) :
@ -516,12 +500,11 @@ class BalanceSheetView(models.TransientModel):
self . env . context . get ( ' default_journal_id ' , False ) )
if journal . currency_id :
return journal . currency_id . id
currency_array = [ self . env . user . company_id . currency_id . symbol ,
self . env . user . company_id . currency_id . position ]
currency_array = [ self . env . company . currency_id . symbol ,
self . env . company . currency_id . position ]
return currency_array
def get_dynamic_xlsx_report ( self , options , response , report_data , dfr_data ) :
i_data = str ( report_data )
filters = json . loads ( options )
j_data = dfr_data
@ -557,7 +540,7 @@ class BalanceSheetView(models.TransientModel):
txt = workbook . add_format ( { ' font_size ' : ' 10px ' , ' border ' : 1 } )
sheet . merge_range ( ' A2:D3 ' ,
self . env . user . company_id . name + ' : ' + i_data ,
filters . get ( ' company_name ' ) + ' : ' + i_data ,
head )
date_head = workbook . add_format ( { ' align ' : ' center ' , ' bold ' : True ,
' font_size ' : ' 10px ' } )