2009-10-13 05:58:37 +00:00
# -*- coding: utf-8 -*-
2006-12-07 13:41:40 +00:00
##############################################################################
2010-04-07 06:41:53 +00:00
#
2009-10-14 11:15:34 +00:00
# OpenERP, Open Source Management Solution
2010-01-12 09:18:39 +00:00
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
2008-06-16 11:00:21 +00:00
#
2008-11-03 19:18:56 +00:00
# This program is free software: you can redistribute it and/or modify
2009-10-14 11:15:34 +00:00
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
2006-12-07 13:41:40 +00:00
#
2008-11-03 19:18:56 +00:00
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2009-10-14 11:15:34 +00:00
# GNU Affero General Public License for more details.
2006-12-07 13:41:40 +00:00
#
2009-10-14 11:15:34 +00:00
# You should have received a copy of the GNU Affero General Public License
2010-04-07 06:41:53 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2006-12-07 13:41:40 +00:00
#
##############################################################################
import time
2010-07-14 12:54:18 +00:00
from common_report_header import common_report_header
2012-12-06 14:56:32 +00:00
from openerp . report import report_sxw
2013-04-05 07:14:10 +00:00
from openerp . tools . translate import _
2010-08-03 04:25:54 +00:00
2010-07-14 12:54:18 +00:00
class journal_print ( report_sxw . rml_parse , common_report_header ) :
2010-06-28 14:52:36 +00:00
2010-07-09 04:17:43 +00:00
def __init__ ( self , cr , uid , name , context = None ) :
if context is None :
context = { }
2009-10-09 11:49:00 +00:00
super ( journal_print , self ) . __init__ ( cr , uid , name , context = context )
2010-07-09 13:06:52 +00:00
self . period_ids = [ ]
2012-02-03 14:53:20 +00:00
self . last_move_id = False
2010-07-09 13:06:52 +00:00
self . journal_ids = [ ]
2012-02-03 14:53:20 +00:00
self . sort_selection = ' am.name '
2010-08-02 05:30:51 +00:00
self . localcontext . update ( {
2008-07-22 15:11:28 +00:00
' time ' : time ,
' lines ' : self . lines ,
' sum_debit ' : self . _sum_debit ,
2010-06-29 13:48:46 +00:00
' sum_credit ' : self . _sum_credit ,
' get_start_period ' : self . get_start_period ,
' get_end_period ' : self . get_end_period ,
2010-07-12 05:42:20 +00:00
' get_account ' : self . _get_account ,
2010-07-12 11:00:39 +00:00
' get_filter ' : self . _get_filter ,
' get_start_date ' : self . _get_start_date ,
2010-07-15 06:24:11 +00:00
' get_end_date ' : self . _get_end_date ,
2010-07-15 10:50:38 +00:00
' get_fiscalyear ' : self . _get_fiscalyear ,
2010-07-28 07:07:05 +00:00
' display_currency ' : self . _display_currency ,
2010-07-21 09:47:57 +00:00
' get_sortby ' : self . _get_sortby ,
2010-10-04 11:31:09 +00:00
' get_target_move ' : self . _get_target_move ,
2012-02-03 14:53:20 +00:00
' check_last_move_id ' : self . check_last_move_id ,
' set_last_move_id ' : self . set_last_move_id ,
' tax_codes ' : self . tax_codes ,
' sum_vat ' : self . _sum_vat ,
2010-08-02 05:30:51 +00:00
} )
2010-07-20 06:21:28 +00:00
2010-07-26 08:16:58 +00:00
def set_context ( self , objects , data , ids , report_type = None ) :
2010-10-01 10:19:36 +00:00
obj_move = self . pool . get ( ' account.move.line ' )
2010-07-13 03:39:27 +00:00
new_ids = ids
self . query_get_clause = ' '
2010-09-16 09:13:12 +00:00
self . target_move = data [ ' form ' ] . get ( ' target_move ' , ' all ' )
2010-07-13 03:39:27 +00:00
if ( data [ ' model ' ] == ' ir.ui.menu ' ) :
2012-02-06 10:27:00 +00:00
self . period_ids = tuple ( data [ ' form ' ] [ ' periods ' ] )
self . journal_ids = tuple ( data [ ' form ' ] [ ' journal_ids ' ] )
2010-08-02 05:30:51 +00:00
new_ids = data [ ' form ' ] . get ( ' active_ids ' , [ ] )
2010-07-13 03:39:27 +00:00
self . query_get_clause = ' AND '
2010-10-01 10:19:36 +00:00
self . query_get_clause + = obj_move . _query_get ( self . cr , self . uid , obj = ' l ' , context = data [ ' form ' ] . get ( ' used_context ' , { } ) )
2010-08-02 11:19:11 +00:00
self . sort_selection = data [ ' form ' ] . get ( ' sort_selection ' , ' date ' )
2010-07-13 03:39:27 +00:00
objects = self . pool . get ( ' account.journal.period ' ) . browse ( self . cr , self . uid , new_ids )
2012-02-06 10:27:00 +00:00
elif new_ids :
#in case of direct access from account.journal.period object, we need to set the journal_ids and periods_ids
2010-07-13 03:39:27 +00:00
self . cr . execute ( ' SELECT period_id, journal_id FROM account_journal_period WHERE id IN %s ' , ( tuple ( new_ids ) , ) )
res = self . cr . fetchall ( )
self . period_ids , self . journal_ids = zip ( * res )
2010-08-02 05:30:51 +00:00
return super ( journal_print , self ) . set_context ( objects , data , ids , report_type = report_type )
2010-07-13 03:39:27 +00:00
2012-02-03 14:53:20 +00:00
def set_last_move_id ( self , move_id ) :
self . last_move_id = move_id
def check_last_move_id ( self , move_id ) :
'''
return True if we need to draw a gray line above this line , used to separate moves
'''
if self . last_move_id :
return not ( self . last_move_id == move_id )
return False
def tax_codes ( self , period_id , journal_id ) :
ids_journal_period = self . pool . get ( ' account.journal.period ' ) . search ( self . cr , self . uid ,
[ ( ' journal_id ' , ' = ' , journal_id ) , ( ' period_id ' , ' = ' , period_id ) ] )
self . cr . execute (
' select distinct tax_code_id from account_move_line ' \
' where period_id= %s and journal_id= %s and tax_code_id is not null and state<> \' draft \' ' ,
( period_id , journal_id )
)
ids = map ( lambda x : x [ 0 ] , self . cr . fetchall ( ) )
tax_code_ids = [ ]
if ids :
self . cr . execute ( ' select id from account_tax_code where id in %s order by code ' , ( tuple ( ids ) , ) )
tax_code_ids = map ( lambda x : x [ 0 ] , self . cr . fetchall ( ) )
tax_codes = self . pool . get ( ' account.tax.code ' ) . browse ( self . cr , self . uid , tax_code_ids )
return tax_codes
def _sum_vat ( self , period_id , journal_id , tax_code_id ) :
self . cr . execute ( ' select sum(tax_amount) from account_move_line where ' \
' period_id= %s and journal_id= %s and tax_code_id= %s and state<> \' draft \' ' ,
( period_id , journal_id , tax_code_id ) )
return self . cr . fetchone ( ) [ 0 ] or 0.0
2010-09-16 09:13:12 +00:00
def _sum_debit ( self , period_id = False , journal_id = False ) :
if journal_id and isinstance ( journal_id , int ) :
journal_id = [ journal_id ]
if period_id and isinstance ( period_id , int ) :
period_id = [ period_id ]
if not journal_id :
journal_id = self . journal_ids
if not period_id :
period_id = self . period_ids
if not ( period_id and journal_id ) :
return 0.0
move_state = [ ' draft ' , ' posted ' ]
if self . target_move == ' posted ' :
move_state = [ ' posted ' ]
2010-10-11 05:51:53 +00:00
self . cr . execute ( ' SELECT SUM(debit) FROM account_move_line l, account_move am '
2010-09-16 09:13:12 +00:00
' WHERE l.move_id=am.id AND am.state IN %s AND l.period_id IN %s AND l.journal_id IN %s ' + self . query_get_clause + ' ' ,
( tuple ( move_state ) , tuple ( period_id ) , tuple ( journal_id ) ) )
return self . cr . fetchone ( ) [ 0 ] or 0.0
def _sum_credit ( self , period_id = False , journal_id = False ) :
if journal_id and isinstance ( journal_id , int ) :
journal_id = [ journal_id ]
if period_id and isinstance ( period_id , int ) :
period_id = [ period_id ]
if not journal_id :
journal_id = self . journal_ids
if not period_id :
period_id = self . period_ids
if not ( period_id and journal_id ) :
return 0.0
move_state = [ ' draft ' , ' posted ' ]
if self . target_move == ' posted ' :
move_state = [ ' posted ' ]
self . cr . execute ( ' SELECT SUM(l.credit) FROM account_move_line l, account_move am '
' WHERE l.move_id=am.id AND am.state IN %s AND l.period_id IN %s AND l.journal_id IN %s ' + self . query_get_clause + ' ' ,
( tuple ( move_state ) , tuple ( period_id ) , tuple ( journal_id ) ) )
return self . cr . fetchone ( ) [ 0 ] or 0.0
2010-08-03 09:16:55 +00:00
def lines ( self , period_id , journal_id = False ) :
if not journal_id :
journal_id = self . journal_ids
else :
journal_id = [ journal_id ]
2010-04-07 06:41:53 +00:00
obj_mline = self . pool . get ( ' account.move.line ' )
2010-07-09 04:17:43 +00:00
self . cr . execute ( ' update account_journal_period set state= %s where journal_id IN %s and period_id= %s and state= %s ' , ( ' printed ' , self . journal_ids , period_id , ' draft ' ) )
2010-09-22 06:15:07 +00:00
2010-09-16 09:13:12 +00:00
move_state = [ ' draft ' , ' posted ' ]
if self . target_move == ' posted ' :
move_state = [ ' posted ' ]
2012-02-03 14:53:20 +00:00
self . cr . execute ( ' SELECT l.id FROM account_move_line l, account_move am WHERE l.move_id=am.id AND am.state IN %s AND l.period_id= %s AND l.journal_id IN %s ' + self . query_get_clause + ' ORDER BY ' + self . sort_selection + ' , l.move_id ' , ( tuple ( move_state ) , period_id , tuple ( journal_id ) ) )
2008-07-22 15:11:28 +00:00
ids = map ( lambda x : x [ 0 ] , self . cr . fetchall ( ) )
2010-04-07 06:41:53 +00:00
return obj_mline . browse ( self . cr , self . uid , ids )
2006-12-07 13:41:40 +00:00
2010-07-15 06:24:11 +00:00
def _set_get_account_currency_code ( self , account_id ) :
2010-10-15 10:57:57 +00:00
self . cr . execute ( " SELECT c.symbol AS code " \
2010-08-03 04:25:54 +00:00
" FROM res_currency c,account_account AS ac " \
" WHERE ac.id = %s AND ac.currency_id = c.id " % ( account_id ) )
2010-07-15 06:24:11 +00:00
result = self . cr . fetchone ( )
if result :
self . account_currency = result [ 0 ]
else :
self . account_currency = False
2010-07-26 08:16:58 +00:00
2010-07-20 09:08:11 +00:00
def _get_fiscalyear ( self , data ) :
2010-08-02 05:30:51 +00:00
if data [ ' model ' ] == ' account.journal.period ' :
2010-07-20 09:08:11 +00:00
return self . pool . get ( ' account.journal.period ' ) . browse ( self . cr , self . uid , data [ ' id ' ] ) . fiscalyear_id . name
2010-10-11 05:51:53 +00:00
return super ( journal_print , self ) . _get_fiscalyear ( data )
2010-07-26 08:16:58 +00:00
2010-07-20 05:43:11 +00:00
def _get_account ( self , data ) :
2010-07-20 06:21:28 +00:00
if data [ ' model ' ] == ' account.journal.period ' :
2010-07-20 05:43:11 +00:00
return self . pool . get ( ' account.journal.period ' ) . browse ( self . cr , self . uid , data [ ' id ' ] ) . company_id . name
2010-10-11 05:51:53 +00:00
return super ( journal_print , self ) . _get_account ( data )
2010-07-20 05:43:11 +00:00
2010-07-28 07:07:05 +00:00
def _display_currency ( self , data ) :
2010-07-26 08:16:58 +00:00
if data [ ' model ' ] == ' account.journal.period ' :
2010-07-26 10:35:08 +00:00
return True
2010-07-21 09:47:57 +00:00
return data [ ' form ' ] [ ' amount_currency ' ]
2010-07-26 08:16:58 +00:00
2010-07-21 09:47:57 +00:00
def _get_sortby ( self , data ) :
2010-07-26 08:16:58 +00:00
if self . sort_selection == ' date ' :
2013-04-05 07:14:10 +00:00
return _ ( ' Date ' )
2010-07-26 08:16:58 +00:00
elif self . sort_selection == ' ref ' :
2013-04-05 07:14:10 +00:00
return _ ( ' Reference Number ' )
return _ ( ' Date ' )
2010-07-26 08:16:58 +00:00
2012-11-20 07:22:36 +00:00
report_sxw . report_sxw ( ' report.account.journal.period.print ' , ' account.journal.period ' , ' addons/account/report/account_journal.rml ' , parser = journal_print , header = ' external ' )
report_sxw . report_sxw ( ' report.account.journal.period.print.sale.purchase ' , ' account.journal.period ' , ' addons/account/report/account_journal_sale_purchase.rml ' , parser = journal_print , header = ' external ' )
2008-07-23 14:41:47 +00:00
2010-09-22 06:15:07 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: