2009-10-13 05:58:37 +00:00
# -*- coding: utf-8 -*-
2006-12-07 13:41:40 +00:00
##############################################################################
2009-10-14 11:15:34 +00:00
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
2006-12-07 13:41:40 +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
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2006-12-07 13:41:40 +00:00
#
##############################################################################
from osv import fields , osv
2008-07-08 08:13:12 +00:00
from tools . translate import _
2007-09-11 14:14:18 +00:00
2006-12-07 13:41:40 +00:00
class hr_timesheet_invoice_factor ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_name = " hr_timesheet_invoice.factor "
_description = " Invoice rate "
_columns = {
' name ' : fields . char ( ' Internal name ' , size = 128 , required = True ) ,
' customer_name ' : fields . char ( ' Visible name ' , size = 128 ) ,
' factor ' : fields . float ( ' Discount ( % ) ' , required = True ) ,
}
_defaults = {
' factor ' : lambda * a : 0.0 ,
}
2007-09-11 14:14:18 +00:00
2006-12-07 13:41:40 +00:00
hr_timesheet_invoice_factor ( )
2007-09-11 14:14:18 +00:00
2006-12-07 13:41:40 +00:00
class account_analytic_account ( osv . osv ) :
2008-07-22 15:11:28 +00:00
def _invoiced_calc ( self , cr , uid , ids , name , arg , context = { } ) :
res = { }
for account in self . browse ( cr , uid , ids ) :
invoiced = { }
2009-09-15 15:39:38 +00:00
cr . execute ( ' select distinct(l.invoice_id) from hr_analytic_timesheet h left join account_analytic_line l on (h.line_id=l.id) where account_id= %s ' , ( account . id , ) )
2008-07-22 15:11:28 +00:00
invoice_ids = filter ( None , map ( lambda x : x [ 0 ] , cr . fetchall ( ) ) )
for invoice in self . pool . get ( ' account.invoice ' ) . browse ( cr , uid , invoice_ids , context ) :
res . setdefault ( account . id , 0.0 )
res [ account . id ] + = invoice . amount_untaxed
for id in ids :
res [ id ] = round ( res . get ( id , 0.0 ) , 2 )
return res
2009-12-17 14:13:32 +00:00
def _get_account_currency ( self , cr , uid , ids , field_name , arg , context = { } ) :
result = super ( account_analytic_account , self ) . _get_account_currency ( cr , uid , ids , field_name , arg , context )
for rec in self . browse ( cr , uid , ids , context ) :
result [ rec . id ] = rec . pricelist_id and ( rec . pricelist_id . currency_id . id , rec . pricelist_id . currency_id . code ) or result [ rec . id ]
return result
2008-07-22 15:11:28 +00:00
_inherit = " account.analytic.account "
_columns = {
2009-12-17 14:13:32 +00:00
' currency_id ' : fields . function ( _get_account_currency , method = True , type = ' many2one ' , relation = ' res.currency ' , string = ' Account currency ' , store = True ) ,
2008-07-22 15:11:28 +00:00
' pricelist_id ' : fields . many2one ( ' product.pricelist ' , ' Sale Pricelist ' ) ,
' amount_max ' : fields . float ( ' Max. Invoice Price ' ) ,
2008-10-27 15:29:13 +00:00
' amount_invoiced ' : fields . function ( _invoiced_calc , method = True , string = ' Invoiced Amount ' ,
help = " Total invoiced " ) ,
' to_invoice ' : fields . many2one ( ' hr_timesheet_invoice.factor ' , ' Reinvoice Costs ' ,
help = " Check this field if you plan to automatically generate invoices based " \
2008-12-20 04:57:05 +00:00
" on the costs in this analytic account: timesheets, expenses, ... " \
" You can configure an automatic invoice rate on analytic accounts. " ) ,
2008-10-27 15:29:13 +00:00
}
_defaults = {
' pricelist_id ' : lambda self , cr , uid , ctx : ctx . get ( ' pricelist_id ' , False ) ,
2008-07-22 15:11:28 +00:00
}
2006-12-07 13:41:40 +00:00
account_analytic_account ( )
2007-09-11 14:14:18 +00:00
2006-12-07 13:41:40 +00:00
class account_analytic_line ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_inherit = ' account.analytic.line '
_columns = {
' invoice_id ' : fields . many2one ( ' account.invoice ' , ' Invoice ' ) ,
' to_invoice ' : fields . many2one ( ' hr_timesheet_invoice.factor ' , ' Invoicing ' ) ,
}
def unlink ( self , cursor , user , ids , context = None ) :
2009-12-17 14:13:32 +00:00
# self._check(cursor, user, ids)
2008-07-22 15:11:28 +00:00
return super ( account_analytic_line , self ) . unlink ( cursor , user , ids ,
context = context )
def write ( self , cr , uid , ids , vals , context = None ) :
2009-11-26 13:54:00 +00:00
self . _check_inv ( cr , uid , ids , vals )
2008-07-22 15:11:28 +00:00
return super ( account_analytic_line , self ) . write ( cr , uid , ids , vals ,
context = context )
2009-11-26 13:54:00 +00:00
def _check_inv ( self , cr , uid , ids , vals ) :
2008-07-22 15:11:28 +00:00
select = ids
if isinstance ( select , ( int , long ) ) :
select = [ ids ]
2009-12-17 14:13:32 +00:00
if ( not vals . has_key ( ' invoice_id ' ) ) or vals [ ' invoice_id ' ] == False :
for line in self . browse ( cr , uid , select ) :
if line . invoice_id :
raise osv . except_osv ( _ ( ' Error ! ' ) ,
_ ( ' You can not modify an invoiced analytic line! ' ) )
2008-07-22 15:11:28 +00:00
return True
def copy ( self , cursor , user , obj_id , default = None , context = None ) :
if default is None :
default = { }
default = default . copy ( )
default . update ( { ' invoice_id ' : False } )
return super ( account_analytic_line , self ) . copy ( cursor , user , obj_id ,
default , context )
2007-12-11 08:14:37 +00:00
2006-12-07 13:41:40 +00:00
account_analytic_line ( )
2007-09-11 14:14:18 +00:00
2007-06-25 14:36:49 +00:00
class hr_analytic_timesheet ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_inherit = " hr.analytic.timesheet "
def on_change_account_id ( self , cr , uid , ids , account_id ) :
res = { }
if not account_id :
return res
res . setdefault ( ' value ' , { } )
2008-11-21 11:04:55 +00:00
acc = self . pool . get ( ' account.analytic.account ' ) . browse ( cr , uid , account_id )
st = acc . to_invoice . id
2008-07-22 15:11:28 +00:00
res [ ' value ' ] [ ' to_invoice ' ] = st or False
2008-11-21 11:04:55 +00:00
if acc . state == ' pending ' :
res [ ' warning ' ] = {
' title ' : ' Warning ' ,
' message ' : ' The analytic account is in pending state. \n You should not work on this account ! '
}
2008-07-22 15:11:28 +00:00
return res
def copy ( self , cursor , user , obj_id , default = None , context = None ) :
if default is None :
default = { }
default = default . copy ( )
default . update ( { ' invoice_id ' : False } )
return super ( hr_analytic_timesheet , self ) . copy ( cursor , user , obj_id ,
default , context )
2007-12-11 08:14:37 +00:00
2007-06-25 14:36:49 +00:00
hr_analytic_timesheet ( )
class account_invoice ( osv . osv ) :
2008-07-22 15:11:28 +00:00
_inherit = " account.invoice "
2008-12-02 18:08:12 +00:00
def _get_analytic_lines ( self , cr , uid , id ) :
iml = super ( account_invoice , self ) . _get_analytic_lines ( cr , uid , id )
2008-07-22 15:11:28 +00:00
inv = self . browse ( cr , uid , [ id ] ) [ 0 ]
if inv . type == ' in_invoice ' :
for il in iml :
if il [ ' account_analytic_id ' ] :
to_invoice = self . pool . get ( ' account.analytic.account ' ) . read ( cr , uid , [ il [ ' account_analytic_id ' ] ] , [ ' to_invoice ' ] ) [ 0 ] [ ' to_invoice ' ]
if to_invoice :
il [ ' analytic_lines ' ] [ 0 ] [ 2 ] [ ' to_invoice ' ] = to_invoice [ 0 ]
return iml
2007-09-11 14:14:18 +00:00
2007-06-25 14:36:49 +00:00
account_invoice ( )
2008-07-23 14:41:47 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: