2009-10-13 05:58:37 +00:00
# -*- coding: utf-8 -*-
2007-01-04 09:45:50 +00:00
##############################################################################
2010-05-19 13:07:03 +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>).
2007-01-04 09:45:50 +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.
2007-01-04 09:45:50 +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.
2007-01-04 09:45:50 +00:00
#
2009-10-14 11:15:34 +00:00
# You should have received a copy of the GNU Affero General Public License
2010-05-19 13:07:03 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2007-01-04 09:45:50 +00:00
#
##############################################################################
import time
from osv import fields
from osv import osv
2008-07-08 08:13:12 +00:00
from tools . translate import _
2009-12-17 14:13:32 +00:00
import tools
from tools import config
2007-01-04 09:45:50 +00:00
class account_analytic_line ( osv . osv ) :
2010-04-29 05:46:14 +00:00
_inherit = ' account.analytic.line '
2010-05-19 18:32:32 +00:00
_description = ' Analytic Line '
2008-07-22 15:11:28 +00:00
_columns = {
2010-10-11 06:03:40 +00:00
' product_uom_id ' : fields . many2one ( ' product.uom ' , ' UoM ' ) ,
' product_id ' : fields . many2one ( ' product.product ' , ' Product ' ) ,
' general_account_id ' : fields . many2one ( ' account.account ' , ' General Account ' , required = True , ondelete = ' cascade ' ) ,
' move_id ' : fields . many2one ( ' account.move.line ' , ' Move Line ' , ondelete = ' cascade ' , select = True ) ,
' journal_id ' : fields . many2one ( ' account.analytic.journal ' , ' Analytic Journal ' , required = True , ondelete = ' cascade ' , select = True ) ,
' code ' : fields . char ( ' Code ' , size = 8 ) ,
2010-02-08 17:17:26 +00:00
' ref ' : fields . char ( ' Ref. ' , size = 64 ) ,
2010-09-06 14:07:01 +00:00
' currency_id ' : fields . related ( ' move_id ' , ' currency_id ' , type = ' many2one ' , relation = ' res.currency ' , string = ' Account currency ' , store = True , help = " The related account currency if not equal to the company one. " , readonly = True ) ,
' amount_currency ' : fields . related ( ' move_id ' , ' amount_currency ' , type = ' float ' , string = ' Amount currency ' , store = True , help = " The amount expressed in the related account currency if not equal to the company one. " , readonly = True ) ,
2008-07-22 15:11:28 +00:00
}
2010-09-06 10:33:34 +00:00
2008-07-22 15:11:28 +00:00
_defaults = {
' date ' : lambda * a : time . strftime ( ' % Y- % m- %d ' ) ,
2010-05-20 05:41:30 +00:00
' company_id ' : lambda self , cr , uid , c : self . pool . get ( ' res.company ' ) . _company_default_get ( cr , uid , ' account.analytic.line ' , context = c ) ,
2010-08-19 11:51:57 +00:00
}
2010-09-07 11:57:28 +00:00
_order = ' date desc '
2010-05-19 13:07:03 +00:00
2009-06-15 07:18:29 +00:00
def search ( self , cr , uid , args , offset = 0 , limit = None , order = None , context = None , count = False ) :
2009-04-08 14:35:50 +00:00
if context is None :
context = { }
if context . get ( ' from_date ' , False ) :
args . append ( [ ' date ' , ' >= ' , context [ ' from_date ' ] ] )
2010-05-19 13:07:03 +00:00
2009-04-08 14:35:50 +00:00
if context . get ( ' to_date ' , False ) :
args . append ( [ ' date ' , ' <= ' , context [ ' to_date ' ] ] )
2010-05-19 13:07:03 +00:00
2009-04-08 14:35:50 +00:00
return super ( account_analytic_line , self ) . search ( cr , uid , args , offset , limit ,
2009-06-15 07:18:29 +00:00
order , context = context , count = count )
2010-05-19 13:07:03 +00:00
2008-07-22 15:11:28 +00:00
def _check_company ( self , cr , uid , ids ) :
lines = self . browse ( cr , uid , ids )
for l in lines :
if l . move_id and not l . account_id . company_id . id == l . move_id . account_id . company_id . id :
return False
return True
_constraints = [
]
2010-05-19 13:07:03 +00:00
2009-12-23 15:28:06 +00:00
# Compute the cost based on the price type define into company
# property_valuation_price_type property
2010-09-21 12:19:33 +00:00
def on_change_unit_amount ( self , cr , uid , id , prod_id , quantity , company_id ,
2010-09-07 11:57:28 +00:00
unit = False , journal_id = False , context = None ) :
2009-12-23 15:28:06 +00:00
if context == None :
context = { }
2010-10-04 23:23:08 +00:00
if not journal_id :
j_ids = self . pool . get ( ' account.analytic.journal ' ) . search ( cr , uid , [ ( ' type ' , ' = ' , ' purchase ' ) ] )
j_id = j_ids and j_ids [ 0 ] or False
2010-09-21 12:19:33 +00:00
if not journal_id or not prod_id :
return { }
2008-07-22 15:11:28 +00:00
product_obj = self . pool . get ( ' product.product ' )
2010-09-21 12:19:33 +00:00
analytic_journal_obj = self . pool . get ( ' account.analytic.journal ' )
j_id = analytic_journal_obj . browse ( cr , uid , journal_id , context = context )
prod = product_obj . browse ( cr , uid , prod_id )
if not company_id :
company_id = j_id . company_id . id
result = 0.0
is_purchase = False
2010-10-11 06:03:40 +00:00
2010-10-04 23:23:08 +00:00
if j_id . type < > ' sale ' :
2008-07-22 15:11:28 +00:00
a = prod . product_tmpl_id . property_account_expense . id
if not a :
a = prod . categ_id . property_account_expense_categ . id
if not a :
raise osv . except_osv ( _ ( ' Error ! ' ) ,
2009-04-09 10:02:19 +00:00
_ ( ' There is no expense account defined ' \
2008-07-22 15:11:28 +00:00
' for this product: " %s " (id: %d ) ' ) % \
( prod . name , prod . id , ) )
2010-09-21 12:19:33 +00:00
amount_unit = prod . price_get ( ' standard_price ' , context ) [ prod . id ]
is_purchase = True
else :
a = prod . product_tmpl_id . property_account_income . id
if not a :
a = prod . categ_id . property_account_income_categ . id
if not a :
raise osv . except_osv ( _ ( ' Error ! ' ) ,
_ ( ' There is no income account defined ' \
' for this product: " %s " (id: %d ) ' ) % \
( prod . name , prod_id , ) )
amount_unit = prod . price_get ( ' list_price ' , context ) [ prod_id ]
2010-09-28 10:45:49 +00:00
if not company_id :
company_id = company_obj . _company_default_get ( cr , uid , ' account.analytic.line ' , context )
2010-09-07 11:57:28 +00:00
flag = False
2009-12-23 15:28:06 +00:00
# Compute based on pricetype
2010-09-21 10:21:42 +00:00
product_price_type_ids = product_price_type_obj . search ( cr , uid , [ ( ' field ' , ' = ' , ' standard_price ' ) ] , context )
pricetype = product_price_type_obj . browse ( cr , uid , product_price_type_ids , context ) [ 0 ]
2010-09-07 11:57:28 +00:00
if journal_id :
journal = analytic_journal_obj . browse ( cr , uid , journal_id )
if journal . type == ' sale ' :
product_price_type_ids = product_price_type_obj . search ( cr , uid , [ ( ' field ' , ' = ' , ' list_price ' ) ] , context )
if product_price_type_ids :
pricetype = product_price_type_obj . browse ( cr , uid , product_price_type_ids , context ) [ 0 ]
2010-03-25 10:59:36 +00:00
# Take the company currency as the reference one
2010-09-07 11:57:28 +00:00
if pricetype . field == ' list_price ' :
flag = True
2010-08-04 12:11:24 +00:00
amount_unit = prod . price_get ( pricetype . field , context ) [ prod . id ]
amount = amount_unit * unit_amount or 1.0
prec = self . pool . get ( ' decimal.precision ' ) . precision_get ( cr , uid , ' Account ' )
2010-08-02 09:06:56 +00:00
amount = amount_unit * unit_amount or 1.0
2010-09-07 11:57:28 +00:00
result = round ( amount , prec )
if not flag :
result * = - 1
2008-07-22 15:11:28 +00:00
return { ' value ' : {
2010-09-07 11:57:28 +00:00
' amount ' : result ,
2008-07-22 15:11:28 +00:00
' general_account_id ' : a ,
2010-09-21 12:19:33 +00:00
}
}
2007-01-04 09:45:50 +00:00
2008-09-23 06:00:56 +00:00
def view_header_get ( self , cr , user , view_id , view_type , context ) :
if context . get ( ' account_id ' , False ) :
2009-11-26 13:54:00 +00:00
# account_id in context may also be pointing to an account.account.id
2008-12-10 14:29:55 +00:00
cr . execute ( ' select name from account_analytic_account where id= %s ' , ( context [ ' account_id ' ] , ) )
2008-09-23 06:00:56 +00:00
res = cr . fetchone ( )
2009-11-26 13:54:00 +00:00
if res :
res = _ ( ' Entries: ' ) + ( res [ 0 ] or ' ' )
2008-09-23 06:00:56 +00:00
return res
return False
2007-01-04 09:45:50 +00:00
account_analytic_line ( )
2010-05-13 14:15:36 +00:00
class res_partner ( osv . osv ) :
""" Inherits partner and adds contract information in the partner form """
_inherit = ' res.partner '
2010-05-19 13:07:03 +00:00
2010-05-13 14:15:36 +00:00
_columns = {
' contract_ids ' : fields . one2many ( ' account.analytic.account ' , \
2010-05-26 04:52:15 +00:00
' partner_id ' , ' Contracts ' , readonly = True ) ,
2010-05-13 14:15:36 +00:00
}
res_partner ( )
2008-07-23 14:41:47 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: