@ -18,7 +18,7 @@ class CustomerActivityStatement(models.AbstractModel):
date = datetime . strptime ( str_date , DEFAULT_SERVER_DATE_FORMAT ) . date ( )
date = datetime . strptime ( str_date , DEFAULT_SERVER_DATE_FORMAT ) . date ( )
return date . strftime ( lang . date_format )
return date . strftime ( lang . date_format )
def _initial_balance_sql_q1 ( self , partners , date_start ) :
def _initial_balance_sql_q1 ( self , partners , date_start , account_type ) :
return """
return """
SELECT l . partner_id , l . currency_id , l . company_id ,
SELECT l . partner_id , l . currency_id , l . company_id ,
CASE WHEN l . currency_id is not null AND l . amount_currency > 0.0
CASE WHEN l . currency_id is not null AND l . amount_currency > 0.0
@ -32,11 +32,11 @@ class CustomerActivityStatement(models.AbstractModel):
FROM account_move_line l
FROM account_move_line l
JOIN account_account_type at ON ( at . id = l . user_type_id )
JOIN account_account_type at ON ( at . id = l . user_type_id )
JOIN account_move m ON ( l . move_id = m . id )
JOIN account_move m ON ( l . move_id = m . id )
WHERE l . partner_id IN ( % s ) AND at . type = ' receivable '
WHERE l . partner_id IN ( % s ) AND at . type = ' %s '
AND l . date < ' %s ' AND not l . blocked
AND l . date < ' %s ' AND not l . blocked
GROUP BY l . partner_id , l . currency_id , l . amount_currency ,
GROUP BY l . partner_id , l . currency_id , l . amount_currency ,
l . company_id
l . company_id
""" % (partners, date_start)
""" % (partners, account_type, date_start)
def _initial_balance_sql_q2 ( self , company_id ) :
def _initial_balance_sql_q2 ( self , company_id ) :
return """
return """
@ -48,7 +48,7 @@ class CustomerActivityStatement(models.AbstractModel):
""" % c ompany_id
""" % c ompany_id
def _get_account_initial_balance ( self , company_id , partner_ids ,
def _get_account_initial_balance ( self , company_id , partner_ids ,
date_start ) :
date_start , account_type ) :
res = dict ( map ( lambda x : ( x , [ ] ) , partner_ids ) )
res = dict ( map ( lambda x : ( x , [ ] ) , partner_ids ) )
partners = ' , ' . join ( [ str ( i ) for i in partner_ids ] )
partners = ' , ' . join ( [ str ( i ) for i in partner_ids ] )
date_start = datetime . strptime (
date_start = datetime . strptime (
@ -56,13 +56,15 @@ class CustomerActivityStatement(models.AbstractModel):
# pylint: disable=E8103
# pylint: disable=E8103
self . env . cr . execute ( """ WITH Q1 AS ( %s ), Q2 AS ( %s )
self . env . cr . execute ( """ WITH Q1 AS ( %s ), Q2 AS ( %s )
SELECT partner_id , currency_id , balance
SELECT partner_id , currency_id , balance
FROM Q2 """ % (self._initial_balance_sql_q1(partners, date_start),
FROM Q2 """ % (self._initial_balance_sql_q1(partners, date_start,
account_type ) ,
self . _initial_balance_sql_q2 ( company_id ) ) )
self . _initial_balance_sql_q2 ( company_id ) ) )
for row in self . env . cr . dictfetchall ( ) :
for row in self . env . cr . dictfetchall ( ) :
res [ row . pop ( ' partner_id ' ) ] . append ( row )
res [ row . pop ( ' partner_id ' ) ] . append ( row )
return res
return res
def _display_lines_sql_q1 ( self , partners , date_start , date_end ) :
def _display_lines_sql_q1 ( self , partners , date_start , date_end ,
account_type ) :
return """
return """
SELECT m . name AS move_id , l . partner_id , l . date , l . name ,
SELECT m . name AS move_id , l . partner_id , l . date , l . name ,
l . ref , l . blocked , l . currency_id , l . company_id ,
l . ref , l . blocked , l . currency_id , l . company_id ,
@ -81,12 +83,12 @@ class CustomerActivityStatement(models.AbstractModel):
FROM account_move_line l
FROM account_move_line l
JOIN account_account_type at ON ( at . id = l . user_type_id )
JOIN account_account_type at ON ( at . id = l . user_type_id )
JOIN account_move m ON ( l . move_id = m . id )
JOIN account_move m ON ( l . move_id = m . id )
WHERE l . partner_id IN ( % s ) AND at . type = ' receivable '
WHERE l . partner_id IN ( % s ) AND at . type = ' %s '
AND ' %s ' < = l . date AND l . date < = ' %s '
AND ' %s ' < = l . date AND l . date < = ' %s '
GROUP BY l . partner_id , m . name , l . date , l . date_maturity , l . name ,
GROUP BY l . partner_id , m . name , l . date , l . date_maturity , l . name ,
l . ref , l . blocked , l . currency_id ,
l . ref , l . blocked , l . currency_id ,
l . amount_currency , l . company_id
l . amount_currency , l . company_id
""" % (partners, date_start, date_end)
""" % (partners, account_type, date_start, date_end)
def _display_lines_sql_q2 ( self , company_id ) :
def _display_lines_sql_q2 ( self , company_id ) :
return """
return """
@ -99,7 +101,7 @@ class CustomerActivityStatement(models.AbstractModel):
""" % c ompany_id
""" % c ompany_id
def _get_account_display_lines ( self , company_id , partner_ids , date_start ,
def _get_account_display_lines ( self , company_id , partner_ids , date_start ,
date_end ) :
date_end , account_type ) :
res = dict ( map ( lambda x : ( x , [ ] ) , partner_ids ) )
res = dict ( map ( lambda x : ( x , [ ] ) , partner_ids ) )
partners = ' , ' . join ( [ str ( i ) for i in partner_ids ] )
partners = ' , ' . join ( [ str ( i ) for i in partner_ids ] )
date_start = datetime . strptime (
date_start = datetime . strptime (
@ -112,7 +114,8 @@ class CustomerActivityStatement(models.AbstractModel):
credit , amount , blocked , currency_id
credit , amount , blocked , currency_id
FROM Q2
FROM Q2
ORDER BY date , date_maturity , move_id """ % (
ORDER BY date , date_maturity , move_id """ % (
self . _display_lines_sql_q1 ( partners , date_start , date_end ) ,
self . _display_lines_sql_q1 ( partners , date_start , date_end ,
account_type ) ,
self . _display_lines_sql_q2 ( company_id ) ) )
self . _display_lines_sql_q2 ( company_id ) ) )
for row in self . env . cr . dictfetchall ( ) :
for row in self . env . cr . dictfetchall ( ) :
res [ row . pop ( ' partner_id ' ) ] . append ( row )
res [ row . pop ( ' partner_id ' ) ] . append ( row )
@ -143,7 +146,7 @@ class CustomerActivityStatement(models.AbstractModel):
GROUP BY l1 . id
GROUP BY l1 . id
""" % (date_end, date_end)
""" % (date_end, date_end)
def _show_buckets_sql_q1 ( self , partners , date_end ) :
def _show_buckets_sql_q1 ( self , partners , date_end , account_type ) :
return """
return """
SELECT l . partner_id , l . currency_id , l . company_id , l . move_id ,
SELECT l . partner_id , l . currency_id , l . company_id , l . move_id ,
CASE WHEN l . balance > 0.0
CASE WHEN l . balance > 0.0
@ -174,14 +177,14 @@ class CustomerActivityStatement(models.AbstractModel):
ON pr . debit_move_id = l2 . id
ON pr . debit_move_id = l2 . id
WHERE l2 . date < = ' %s '
WHERE l2 . date < = ' %s '
) as pc ON pc . credit_move_id = l . id
) as pc ON pc . credit_move_id = l . id
WHERE l . partner_id IN ( % s ) AND at . type = ' receivable '
WHERE l . partner_id IN ( % s ) AND at . type = ' %s '
AND ( Q0 . reconciled_date is null or
AND ( Q0 . reconciled_date is null or
Q0 . reconciled_date > ' %s ' )
Q0 . reconciled_date > ' %s ' )
AND l . date < = ' %s ' AND not l . blocked
AND l . date < = ' %s ' AND not l . blocked
GROUP BY l . partner_id , l . currency_id , l . date , l . date_maturity ,
GROUP BY l . partner_id , l . currency_id , l . date , l . date_maturity ,
l . amount_currency , l . balance , l . move_id ,
l . amount_currency , l . balance , l . move_id ,
l . company_id
l . company_id
""" % (date_end, date_end, partners, date_end, date_end)
""" % (date_end, date_end, partners, account_type, date_end, date_end)
def _show_buckets_sql_q2 ( self , date_end , minus_30 , minus_60 , minus_90 ,
def _show_buckets_sql_q2 ( self , date_end , minus_30 , minus_60 , minus_90 ,
minus_120 ) :
minus_120 ) :
@ -273,7 +276,8 @@ class CustomerActivityStatement(models.AbstractModel):
' minus_120 ' : date_end - timedelta ( days = 120 ) ,
' minus_120 ' : date_end - timedelta ( days = 120 ) ,
}
}
def _get_account_show_buckets ( self , company_id , partner_ids , date_end ) :
def _get_account_show_buckets ( self , company_id , partner_ids , date_end ,
account_type ) :
res = dict ( map ( lambda x : ( x , [ ] ) , partner_ids ) )
res = dict ( map ( lambda x : ( x , [ ] ) , partner_ids ) )
partners = ' , ' . join ( [ str ( i ) for i in partner_ids ] )
partners = ' , ' . join ( [ str ( i ) for i in partner_ids ] )
date_end = datetime . strptime (
date_end = datetime . strptime (
@ -290,7 +294,7 @@ class CustomerActivityStatement(models.AbstractModel):
GROUP BY partner_id , currency_id , current , b_1_30 , b_30_60 , b_60_90 ,
GROUP BY partner_id , currency_id , current , b_1_30 , b_30_60 , b_60_90 ,
b_90_120 , b_over_120 """ % (
b_90_120 , b_over_120 """ % (
self . _show_buckets_sql_q0 ( date_end ) ,
self . _show_buckets_sql_q0 ( date_end ) ,
self . _show_buckets_sql_q1 ( partners , date_end ) ,
self . _show_buckets_sql_q1 ( partners , date_end , account_type ) ,
self . _show_buckets_sql_q2 (
self . _show_buckets_sql_q2 (
full_dates [ ' date_end ' ] ,
full_dates [ ' date_end ' ] ,
full_dates [ ' minus_30 ' ] ,
full_dates [ ' minus_30 ' ] ,
@ -309,6 +313,7 @@ class CustomerActivityStatement(models.AbstractModel):
partner_ids = data [ ' partner_ids ' ]
partner_ids = data [ ' partner_ids ' ]
date_start = data [ ' date_start ' ]
date_start = data [ ' date_start ' ]
date_end = data [ ' date_end ' ]
date_end = data [ ' date_end ' ]
account_type = data [ ' account_type ' ]
today = fields . Date . today ( )
today = fields . Date . today ( )
balance_start_to_display , buckets_to_display = { } , { }
balance_start_to_display , buckets_to_display = { } , { }
@ -317,7 +322,7 @@ class CustomerActivityStatement(models.AbstractModel):
today_display , date_start_display , date_end_display = { } , { } , { }
today_display , date_start_display , date_end_display = { } , { } , { }
balance_start = self . _get_account_initial_balance (
balance_start = self . _get_account_initial_balance (
company_id , partner_ids , date_start )
company_id , partner_ids , date_start , account_type )
for partner_id in partner_ids :
for partner_id in partner_ids :
balance_start_to_display [ partner_id ] = { }
balance_start_to_display [ partner_id ] = { }
@ -329,7 +334,7 @@ class CustomerActivityStatement(models.AbstractModel):
line [ ' balance ' ]
line [ ' balance ' ]
lines = self . _get_account_display_lines (
lines = self . _get_account_display_lines (
company_id , partner_ids , date_start , date_end )
company_id , partner_ids , date_start , date_end , account_type )
for partner_id in partner_ids :
for partner_id in partner_ids :
lines_to_display [ partner_id ] , amount_due [ partner_id ] = { } , { }
lines_to_display [ partner_id ] , amount_due [ partner_id ] = { } , { }
@ -361,7 +366,7 @@ class CustomerActivityStatement(models.AbstractModel):
if data [ ' show_aging_buckets ' ] :
if data [ ' show_aging_buckets ' ] :
buckets = self . _get_account_show_buckets (
buckets = self . _get_account_show_buckets (
company_id , partner_ids , date_end )
company_id , partner_ids , date_end , account_type )
for partner_id in partner_ids :
for partner_id in partner_ids :
buckets_to_display [ partner_id ] = { }
buckets_to_display [ partner_id ] = { }
for line in buckets [ partner_id ] :
for line in buckets [ partner_id ] :
@ -385,4 +390,5 @@ class CustomerActivityStatement(models.AbstractModel):
' Date_start ' : date_start_display ,
' Date_start ' : date_start_display ,
' Date_end ' : date_end_display ,
' Date_end ' : date_end_display ,
' Date ' : today_display ,
' Date ' : today_display ,
' account_type ' : account_type ,
}
}