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
#
##############################################################################
2012-12-06 14:56:32 +00:00
from openerp . osv import fields
from openerp . osv import osv
from openerp . tools . translate import _
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 = {
2012-04-25 07:08:25 +00:00
' product_uom_id ' : fields . many2one ( ' product.uom ' , ' Unit of Measure ' ) ,
2010-10-11 06:03:40 +00:00
' product_id ' : fields . many2one ( ' product.product ' , ' Product ' ) ,
2010-12-16 11:52:33 +00:00
' general_account_id ' : fields . many2one ( ' account.account ' , ' General Account ' , required = True , ondelete = ' restrict ' ) ,
2011-04-25 06:11:14 +00:00
' move_id ' : fields . many2one ( ' account.move.line ' , ' Move Line ' , ondelete = ' cascade ' , select = True ) ,
2010-12-16 11:52:33 +00:00
' journal_id ' : fields . many2one ( ' account.analytic.journal ' , ' Analytic Journal ' , required = True , ondelete = ' restrict ' , select = True ) ,
2010-10-11 06:03:40 +00:00
' code ' : fields . char ( ' Code ' , size = 8 ) ,
2014-05-21 09:52:05 +00:00
' ref ' : fields . char ( ' Ref. ' ) ,
2012-07-06 07:42:15 +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 ) ,
2012-07-06 03:45:53 +00:00
' 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 = {
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 ) :
2010-12-27 07:31:56 +00:00
args . append ( [ ' date ' , ' >= ' , context [ ' from_date ' ] ] )
2009-04-08 14:35:50 +00:00
if context . get ( ' to_date ' , False ) :
2010-12-27 07:31:56 +00:00
args . append ( [ ' date ' , ' <= ' , context [ ' to_date ' ] ] )
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
2010-11-19 13:48:01 +00:00
def _check_company ( self , cr , uid , ids , context = None ) :
lines = self . browse ( cr , uid , ids , context = context )
2008-07-22 15:11:28 +00:00
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
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 ' ) ] )
2010-11-01 09:37:14 +00:00
journal_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 ' )
2010-10-17 17:30:00 +00:00
product_price_type_obj = self . pool . get ( ' product.price.type ' )
2014-07-31 16:05:17 +00:00
product_uom_obj = self . pool . get ( ' product.uom ' )
2010-09-21 12:19:33 +00:00
j_id = analytic_journal_obj . browse ( cr , uid , journal_id , context = context )
2010-11-19 13:48:01 +00:00
prod = product_obj . browse ( cr , uid , prod_id , context = context )
2010-09-21 12:19:33 +00:00
result = 0.0
2011-02-21 12:50:34 +00:00
if prod_id :
2014-07-31 16:15:41 +00:00
unit_obj = False
if unit :
unit_obj = product_uom_obj . browse ( cr , uid , unit , context = context )
if not unit_obj or prod . uom_id . category_id . id != unit_obj . category_id . id :
2014-07-31 16:05:17 +00:00
unit = prod . uom_id . id
2011-02-21 09:10:35 +00:00
if j_id . type == ' purchase ' :
2014-07-31 16:15:41 +00:00
if not unit_obj or prod . uom_po_id . category_id . id != unit_obj . category_id . id :
2014-07-31 16:05:17 +00:00
unit = prod . uom_po_id . id
2010-10-04 23:23:08 +00:00
if j_id . type < > ' sale ' :
2012-07-27 01:52:02 +00:00
a = prod . property_account_expense . id
2008-07-22 15:11:28 +00:00
if not a :
a = prod . categ_id . property_account_expense_categ . id
if not a :
2012-08-07 11:31:37 +00:00
raise osv . except_osv ( _ ( ' Error! ' ) ,
2012-08-06 15:44:10 +00:00
_ ( ' There is no expense account defined ' \
2012-07-25 07:33:57 +00:00
' for this product: " %s " (id: %d ). ' ) % \
2008-07-22 15:11:28 +00:00
( prod . name , prod . id , ) )
2010-09-21 12:19:33 +00:00
else :
2012-07-27 01:52:02 +00:00
a = prod . property_account_income . id
2010-09-21 12:19:33 +00:00
if not a :
a = prod . categ_id . property_account_income_categ . id
if not a :
2012-08-07 11:31:37 +00:00
raise osv . except_osv ( _ ( ' Error! ' ) ,
2012-08-06 15:44:10 +00:00
_ ( ' There is no income account defined ' \
2012-07-25 07:33:57 +00:00
' for this product: " %s " (id: %d ). ' ) % \
2010-09-21 12:19:33 +00:00
( prod . name , prod_id , ) )
2010-11-10 11:46:09 +00:00
flag = False
# Compute based on pricetype
product_price_type_ids = product_price_type_obj . search ( cr , uid , [ ( ' field ' , ' = ' , ' standard_price ' ) ] , context = context )
2010-11-19 13:48:01 +00:00
pricetype = product_price_type_obj . browse ( cr , uid , product_price_type_ids , context = context ) [ 0 ]
2010-11-10 11:46:09 +00:00
if journal_id :
2010-11-19 13:48:01 +00:00
journal = analytic_journal_obj . browse ( cr , uid , journal_id , context = context )
2010-11-10 11:46:09 +00:00
if journal . type == ' sale ' :
2012-10-31 10:51:22 +00:00
product_price_type_ids = product_price_type_obj . search ( cr , uid , [ ( ' field ' , ' = ' , ' list_price ' ) ] , context = context )
2010-11-10 11:46:09 +00:00
if product_price_type_ids :
2010-11-19 13:48:01 +00:00
pricetype = product_price_type_obj . browse ( cr , uid , product_price_type_ids , context = context ) [ 0 ]
2010-11-10 11:46:09 +00:00
# Take the company currency as the reference one
if pricetype . field == ' list_price ' :
flag = True
ctx = context . copy ( )
if unit :
# price_get() will respect a 'uom' in its context, in order
# to return a default price for those units
ctx [ ' uom ' ] = unit
amount_unit = prod . price_get ( pricetype . field , context = ctx ) [ prod . id ]
prec = self . pool . get ( ' decimal.precision ' ) . precision_get ( cr , uid , ' Account ' )
2011-09-30 09:59:42 +00:00
amount = amount_unit * quantity or 0.0
2010-11-10 11:46:09 +00:00
result = round ( amount , prec )
if not flag :
result * = - 1
2010-10-19 12:44:35 +00:00
return { ' value ' : {
' amount ' : result ,
' general_account_id ' : a ,
2011-02-25 14:11:14 +00:00
' product_uom_id ' : unit
2010-09-21 12:19:33 +00:00
}
}
2007-01-04 09:45:50 +00:00
2010-11-19 13:48:01 +00:00
def view_header_get ( self , cr , user , view_id , view_type , context = None ) :
2010-11-23 11:31:52 +00:00
if context is None :
context = { }
2008-09-23 06:00:56 +00:00
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
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 = {
2010-10-28 09:46:09 +00:00
' contract_ids ' : fields . one2many ( ' account.analytic.account ' , \
2010-05-26 04:52:15 +00:00
' partner_id ' , ' Contracts ' , readonly = True ) ,
2010-10-28 09:46:09 +00:00
}
2010-05-13 14:15:36 +00:00
2011-02-25 14:11:14 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: