2011-07-04 10:15:44 +00:00
# -*- encoding: utf-8 -*-
2011-03-30 13:25:27 +00:00
##############################################################################
2011-05-12 13:44:20 +00:00
#
2011-03-30 13:25:27 +00:00
# OpenERP, Open Source Management Solution
2011-06-06 10:06:29 +00:00
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
2011-03-30 13:25:27 +00:00
#
# This program is free software: you can redistribute it and/or modify
# 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.
#
# 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
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
2011-05-12 13:44:20 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2011-03-30 13:25:27 +00:00
#
##############################################################################
import time
2011-05-09 14:29:15 +00:00
from datetime import datetime
2011-06-03 09:46:52 +00:00
from dateutil . relativedelta import relativedelta
2012-12-06 14:56:32 +00:00
from openerp . osv import fields , osv
2012-12-17 15:23:03 +00:00
import openerp . addons . decimal_precision as dp
2013-03-14 12:40:46 +00:00
from openerp . tools . translate import _
2011-05-09 14:29:15 +00:00
2011-03-30 13:25:27 +00:00
class account_asset_category ( osv . osv ) :
_name = ' account.asset.category '
_description = ' Asset category '
2011-05-12 12:40:38 +00:00
2011-03-30 13:25:27 +00:00
_columns = {
2014-05-21 09:52:05 +00:00
' name ' : fields . char ( ' Name ' , required = True , select = 1 ) ,
2011-03-30 13:25:27 +00:00
' note ' : fields . text ( ' Note ' ) ,
2011-05-13 10:09:54 +00:00
' account_analytic_id ' : fields . many2one ( ' account.analytic.account ' , ' Analytic account ' ) ,
2014-01-17 10:40:37 +00:00
' account_asset_id ' : fields . many2one ( ' account.account ' , ' Asset Account ' , required = True , domain = [ ( ' type ' , ' = ' , ' other ' ) ] ) ,
' account_depreciation_id ' : fields . many2one ( ' account.account ' , ' Depreciation Account ' , required = True , domain = [ ( ' type ' , ' = ' , ' other ' ) ] ) ,
' account_expense_depreciation_id ' : fields . many2one ( ' account.account ' , ' Depr. Expense Account ' , required = True , domain = [ ( ' type ' , ' = ' , ' other ' ) ] ) ,
2011-03-30 13:25:27 +00:00
' journal_id ' : fields . many2one ( ' account.journal ' , ' Journal ' , required = True ) ,
2011-05-13 10:09:54 +00:00
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' , required = True ) ,
2011-07-09 22:45:35 +00:00
' method ' : fields . selection ( [ ( ' linear ' , ' Linear ' ) , ( ' degressive ' , ' Degressive ' ) ] , ' Computation Method ' , required = True , help = " Choose the method to use to compute the amount of depreciation lines. \n " \
" * Linear: Calculated on basis of: Gross Value / Number of Depreciations \n " \
2012-11-26 10:13:53 +00:00
" * Degressive: Calculated on basis of: Residual Value * Degressive Factor " ) ,
2012-11-26 13:27:58 +00:00
' method_number ' : fields . integer ( ' Number of Depreciations ' , help = " The number of depreciations needed to depreciate your asset " ) ,
2011-07-09 22:45:35 +00:00
' method_period ' : fields . integer ( ' Period Length ' , help = " State here the time between 2 depreciations, in months " , required = True ) ,
' method_progress_factor ' : fields . float ( ' Degressive Factor ' ) ,
2011-07-09 20:32:24 +00:00
' method_time ' : fields . selection ( [ ( ' number ' , ' Number of Depreciations ' ) , ( ' end ' , ' Ending Date ' ) ] , ' Time Method ' , required = True ,
help = " Choose the method to use to compute the dates and number of depreciation lines. \n " \
2011-07-09 22:45:35 +00:00
" * Number of Depreciations: Fix the number of depreciation lines and the time between 2 depreciations. \n " \
" * Ending Date: Choose the time between 2 depreciations and the date the depreciations won ' t go beyond. " ) ,
2011-06-06 05:57:53 +00:00
' method_end ' : fields . date ( ' Ending date ' ) ,
2011-07-09 22:45:35 +00:00
' prorata ' : fields . boolean ( ' Prorata Temporis ' , help = ' Indicates that the first depreciation entry for this asset have to be done from the purchase date instead of the first January ' ) ,
' open_asset ' : fields . boolean ( ' Skip Draft State ' , help = " Check this if you want to automatically confirm the assets of this category when created by invoices. " ) ,
2011-03-30 13:25:27 +00:00
}
2011-05-12 12:40:38 +00:00
_defaults = {
' company_id ' : lambda self , cr , uid , context : self . pool . get ( ' res.company ' ) . _company_default_get ( cr , uid , ' account.asset.category ' , context = context ) ,
2011-05-17 13:18:03 +00:00
' method ' : ' linear ' ,
2011-07-09 20:32:24 +00:00
' method_number ' : 5 ,
' method_time ' : ' number ' ,
2011-05-17 13:18:03 +00:00
' method_period ' : 12 ,
' method_progress_factor ' : 0.3 ,
2011-03-30 13:25:27 +00:00
}
2011-05-12 12:40:38 +00:00
2011-05-17 13:15:18 +00:00
def onchange_account_asset ( self , cr , uid , ids , account_asset_id , context = None ) :
res = { ' value ' : { } }
2011-05-19 05:00:27 +00:00
if account_asset_id :
2011-05-17 13:15:18 +00:00
res [ ' value ' ] = { ' account_depreciation_id ' : account_asset_id }
return res
2011-05-17 06:41:17 +00:00
2011-03-30 13:25:27 +00:00
class account_asset_asset ( osv . osv ) :
_name = ' account.asset.asset '
_description = ' Asset '
2013-03-06 17:05:38 +00:00
def unlink ( self , cr , uid , ids , context = None ) :
for asset in self . browse ( cr , uid , ids , context = context ) :
if asset . account_move_line_ids :
raise osv . except_osv ( _ ( ' Error! ' ) , _ ( ' You cannot delete an asset that contains posted depreciation lines. ' ) )
2013-04-05 05:51:47 +00:00
return super ( account_asset_asset , self ) . unlink ( cr , uid , ids , context = context )
2013-03-06 17:05:38 +00:00
2012-03-05 18:40:03 +00:00
def _get_period ( self , cr , uid , context = None ) :
2013-04-18 10:39:07 +00:00
periods = self . pool . get ( ' account.period ' ) . find ( cr , uid , context = context )
2011-03-30 13:25:27 +00:00
if periods :
return periods [ 0 ]
else :
return False
2011-05-10 12:11:09 +00:00
2011-05-09 14:29:15 +00:00
def _get_last_depreciation_date ( self , cr , uid , ids , context = None ) :
"""
@param id : ids of a account . asset . asset objects
@return : Returns a dictionary of the effective dates of the last depreciation entry made for given asset ids . If there isn ' t any, return the purchase date of this asset
"""
cr . execute ( """
SELECT a . id as id , COALESCE ( MAX ( l . date ) , a . purchase_date ) AS date
FROM account_asset_asset a
LEFT JOIN account_move_line l ON ( l . asset_id = a . id )
2011-05-12 10:12:08 +00:00
WHERE a . id IN % s
2011-05-09 14:29:15 +00:00
GROUP BY a . id , a . purchase_date """ , (tuple(ids),))
return dict ( cr . fetchall ( ) )
2011-07-01 12:55:05 +00:00
def _compute_board_amount ( self , cr , uid , asset , i , residual_amount , amount_to_depr , undone_dotation_number , posted_depreciation_line_ids , total_days , depreciation_date , context = None ) :
2011-07-01 12:21:57 +00:00
#by default amount = 0
amount = 0
if i == undone_dotation_number :
amount = residual_amount
else :
if asset . method == ' linear ' :
amount = amount_to_depr / ( undone_dotation_number - len ( posted_depreciation_line_ids ) )
if asset . prorata :
2011-07-09 20:32:24 +00:00
amount = amount_to_depr / asset . method_number
2011-07-01 12:21:57 +00:00
days = total_days - float ( depreciation_date . strftime ( ' % j ' ) )
if i == 1 :
2011-07-09 20:32:24 +00:00
amount = ( amount_to_depr / asset . method_number ) / total_days * days
2011-07-01 12:21:57 +00:00
elif i == undone_dotation_number :
2011-07-09 20:32:24 +00:00
amount = ( amount_to_depr / asset . method_number ) / total_days * ( total_days - days )
2011-07-01 12:21:57 +00:00
elif asset . method == ' degressive ' :
amount = residual_amount * asset . method_progress_factor
2011-08-08 12:04:28 +00:00
if asset . prorata :
days = total_days - float ( depreciation_date . strftime ( ' % j ' ) )
if i == 1 :
amount = ( residual_amount * asset . method_progress_factor ) / total_days * days
elif i == undone_dotation_number :
amount = ( residual_amount * asset . method_progress_factor ) / total_days * ( total_days - days )
2011-07-01 12:21:57 +00:00
return amount
def _compute_board_undone_dotation_nb ( self , cr , uid , asset , depreciation_date , total_days , context = None ) :
2011-07-09 20:32:24 +00:00
undone_dotation_number = asset . method_number
2011-07-01 12:21:57 +00:00
if asset . method_time == ' end ' :
end_date = datetime . strptime ( asset . method_end , ' % Y- % m- %d ' )
2011-07-06 09:11:36 +00:00
undone_dotation_number = 0
while depreciation_date < = end_date :
depreciation_date = ( datetime ( depreciation_date . year , depreciation_date . month , depreciation_date . day ) + relativedelta ( months = + asset . method_period ) )
2011-07-05 11:50:56 +00:00
undone_dotation_number + = 1
if asset . prorata :
2011-07-01 12:21:57 +00:00
undone_dotation_number + = 1
return undone_dotation_number
2011-12-13 08:34:57 +00:00
2011-07-04 10:15:44 +00:00
def compute_depreciation_board ( self , cr , uid , ids , context = None ) :
2011-05-09 14:29:15 +00:00
depreciation_lin_obj = self . pool . get ( ' account.asset.depreciation.line ' )
2012-08-28 09:22:32 +00:00
currency_obj = self . pool . get ( ' res.currency ' )
2011-03-30 20:21:06 +00:00
for asset in self . browse ( cr , uid , ids , context = context ) :
2011-06-13 07:08:52 +00:00
if asset . value_residual == 0.0 :
2011-06-13 06:44:36 +00:00
continue
2012-11-02 15:27:51 +00:00
posted_depreciation_line_ids = depreciation_lin_obj . search ( cr , uid , [ ( ' asset_id ' , ' = ' , asset . id ) , ( ' move_check ' , ' = ' , True ) ] , order = ' depreciation_date desc ' )
2011-06-08 13:35:53 +00:00
old_depreciation_line_ids = depreciation_lin_obj . search ( cr , uid , [ ( ' asset_id ' , ' = ' , asset . id ) , ( ' move_id ' , ' = ' , False ) ] )
if old_depreciation_line_ids :
depreciation_lin_obj . unlink ( cr , uid , old_depreciation_line_ids , context = context )
2011-12-13 08:34:57 +00:00
2011-06-09 10:25:52 +00:00
amount_to_depr = residual_amount = asset . value_residual
2011-11-09 12:35:44 +00:00
if asset . prorata :
depreciation_date = datetime . strptime ( self . _get_last_depreciation_date ( cr , uid , [ asset . id ] , context ) [ asset . id ] , ' % Y- % m- %d ' )
else :
2011-11-10 07:42:42 +00:00
# depreciation_date = 1st January of purchase year
purchase_date = datetime . strptime ( asset . purchase_date , ' % Y- % m- %d ' )
2012-11-02 15:27:51 +00:00
#if we already have some previous validated entries, starting date isn't 1st January but last entry + method period
if ( len ( posted_depreciation_line_ids ) > 0 ) :
last_depreciation_date = datetime . strptime ( depreciation_lin_obj . browse ( cr , uid , posted_depreciation_line_ids [ 0 ] , context = context ) . depreciation_date , ' % Y- % m- %d ' )
depreciation_date = ( last_depreciation_date + relativedelta ( months = + asset . method_period ) )
else :
depreciation_date = datetime ( purchase_date . year , 1 , 1 )
2011-05-09 14:29:15 +00:00
day = depreciation_date . day
month = depreciation_date . month
year = depreciation_date . year
2011-05-23 12:21:58 +00:00
total_days = ( year % 4 ) and 365 or 366
2011-07-01 12:21:57 +00:00
undone_dotation_number = self . _compute_board_undone_dotation_nb ( cr , uid , asset , depreciation_date , total_days , context = context )
2011-06-23 14:38:32 +00:00
for x in range ( len ( posted_depreciation_line_ids ) , undone_dotation_number ) :
i = x + 1
2011-07-01 12:55:05 +00:00
amount = self . _compute_board_amount ( cr , uid , asset , i , residual_amount , amount_to_depr , undone_dotation_number , posted_depreciation_line_ids , total_days , depreciation_date , context = context )
2011-05-09 14:29:15 +00:00
residual_amount - = amount
vals = {
2011-05-12 13:44:20 +00:00
' amount ' : amount ,
' asset_id ' : asset . id ,
2011-05-24 13:04:45 +00:00
' sequence ' : i ,
2011-06-07 05:38:22 +00:00
' name ' : str ( asset . id ) + ' / ' + str ( i ) ,
2011-05-09 14:29:15 +00:00
' remaining_value ' : residual_amount ,
2011-06-23 14:38:32 +00:00
' depreciated_value ' : ( asset . purchase_value - asset . salvage_value ) - ( residual_amount + amount ) ,
2011-05-09 14:29:15 +00:00
' depreciation_date ' : depreciation_date . strftime ( ' % Y- % m- %d ' ) ,
2011-05-12 13:44:20 +00:00
}
2011-06-08 13:35:53 +00:00
depreciation_lin_obj . create ( cr , uid , vals , context = context )
2011-06-03 09:46:52 +00:00
# Considering Depr. Period as months
depreciation_date = ( datetime ( year , month , day ) + relativedelta ( months = + asset . method_period ) )
day = depreciation_date . day
month = depreciation_date . month
year = depreciation_date . year
2011-05-09 14:29:15 +00:00
return True
2011-05-02 09:51:34 +00:00
2012-03-05 18:40:03 +00:00
def validate ( self , cr , uid , ids , context = None ) :
if context is None :
context = { }
2011-03-30 13:25:27 +00:00
return self . write ( cr , uid , ids , {
2011-05-24 13:14:14 +00:00
' state ' : ' open '
2011-03-30 13:25:27 +00:00
} , context )
2011-06-28 10:13:40 +00:00
def set_to_close ( self , cr , uid , ids , context = None ) :
return self . write ( cr , uid , ids , { ' state ' : ' close ' } , context = context )
2011-12-09 05:59:59 +00:00
def set_to_draft ( self , cr , uid , ids , context = None ) :
2011-12-16 12:22:11 +00:00
return self . write ( cr , uid , ids , { ' state ' : ' draft ' } , context = context )
2011-12-09 05:59:59 +00:00
2011-06-07 05:38:22 +00:00
def _amount_residual ( self , cr , uid , ids , name , args , context = None ) :
2011-05-12 13:44:20 +00:00
cr . execute ( """ SELECT
2012-08-24 05:47:47 +00:00
l . asset_id as id , SUM ( abs ( l . debit - l . credit ) ) AS amount
2011-03-30 20:21:06 +00:00
FROM
2011-05-09 14:29:15 +00:00
account_move_line l
2011-03-30 20:21:06 +00:00
WHERE
2011-05-09 14:29:15 +00:00
l . asset_id IN % s GROUP BY l . asset_id """ , (tuple(ids),))
2011-03-30 20:21:06 +00:00
res = dict ( cr . fetchall ( ) )
2011-05-09 14:29:15 +00:00
for asset in self . browse ( cr , uid , ids , context ) :
2015-03-16 08:49:41 +00:00
company_currency = asset . company_id . currency_id . id
current_currency = asset . currency_id . id
amount = self . pool [ ' res.currency ' ] . compute ( cr , uid , company_currency , current_currency , res . get ( asset . id , 0.0 ) , context = context )
res [ asset . id ] = asset . purchase_value - amount - asset . salvage_value
2011-03-30 20:21:06 +00:00
for id in ids :
res . setdefault ( id , 0.0 )
return res
2011-12-14 05:48:34 +00:00
def onchange_company_id ( self , cr , uid , ids , company_id = False , context = None ) :
2011-12-13 08:34:57 +00:00
val = { }
if company_id :
company = self . pool . get ( ' res.company ' ) . browse ( cr , uid , company_id , context = context )
if company . currency_id . company_id and company . currency_id . company_id . id != company_id :
val [ ' currency_id ' ] = False
else :
val [ ' currency_id ' ] = company . currency_id . id
return { ' value ' : val }
2012-11-26 10:13:53 +00:00
2012-11-28 09:34:48 +00:00
def onchange_purchase_salvage_value ( self , cr , uid , ids , purchase_value , salvage_value , context = None ) :
2012-11-26 10:13:53 +00:00
val = { }
2012-11-26 11:30:56 +00:00
for asset in self . browse ( cr , uid , ids , context = context ) :
2012-11-26 10:13:53 +00:00
if purchase_value :
val [ ' value_residual ' ] = purchase_value - salvage_value
if salvage_value :
val [ ' value_residual ' ] = purchase_value - salvage_value
return { ' value ' : val }
2014-03-25 12:45:31 +00:00
def _entry_count ( self , cr , uid , ids , field_name , arg , context = None ) :
2014-05-07 13:45:44 +00:00
MoveLine = self . pool ( ' account.move.line ' )
return {
asset_id : MoveLine . search_count ( cr , uid , [ ( ' asset_id ' , ' = ' , asset_id ) ] , context = context )
for asset_id in ids
}
2011-03-30 13:25:27 +00:00
_columns = {
2011-05-09 10:27:17 +00:00
' account_move_line_ids ' : fields . one2many ( ' account.move.line ' , ' asset_id ' , ' Entries ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2014-03-25 12:45:31 +00:00
' entry_count ' : fields . function ( _entry_count , string = ' # Asset Entries ' , type = ' integer ' ) ,
2014-05-21 09:52:05 +00:00
' name ' : fields . char ( ' Asset Name ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2011-11-11 19:29:54 +00:00
' code ' : fields . char ( ' Reference ' , size = 32 , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2012-07-14 18:15:25 +00:00
' purchase_value ' : fields . float ( ' Gross Value ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2011-07-09 22:45:35 +00:00
' currency_id ' : fields . many2one ( ' res.currency ' , ' Currency ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2011-05-09 10:27:17 +00:00
' note ' : fields . text ( ' Note ' ) ,
2012-06-25 12:48:04 +00:00
' category_id ' : fields . many2one ( ' account.asset.category ' , ' Asset Category ' , required = True , change_default = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2011-07-09 22:45:35 +00:00
' parent_id ' : fields . many2one ( ' account.asset.asset ' , ' Parent Asset ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2014-07-06 14:44:26 +00:00
' child_ids ' : fields . one2many ( ' account.asset.asset ' , ' parent_id ' , ' Children Assets ' , copy = True ) ,
2011-07-09 22:45:35 +00:00
' purchase_date ' : fields . date ( ' Purchase Date ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2014-07-06 14:44:26 +00:00
' state ' : fields . selection ( [ ( ' draft ' , ' Draft ' ) , ( ' open ' , ' Running ' ) , ( ' close ' , ' Close ' ) ] , ' Status ' , required = True , copy = False ,
2012-10-12 11:42:58 +00:00
help = " When an asset is created, the status is ' Draft ' . \n " \
" If the asset is confirmed, the status goes in ' Running ' and the depreciation lines can be posted in the accounting. \n " \
" You can manually close an asset when the depreciation is over. If the last line of depreciation is posted, the asset automatically goes in that status. " ) ,
2011-07-09 22:45:35 +00:00
' active ' : fields . boolean ( ' Active ' ) ,
' partner_id ' : fields . many2one ( ' res.partner ' , ' Partner ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
' method ' : fields . selection ( [ ( ' linear ' , ' Linear ' ) , ( ' degressive ' , ' Degressive ' ) ] , ' Computation Method ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } , help = " Choose the method to use to compute the amount of depreciation lines. \n " \
" * Linear: Calculated on basis of: Gross Value / Number of Depreciations \n " \
2012-11-26 10:13:53 +00:00
" * Degressive: Calculated on basis of: Residual Value * Degressive Factor " ) ,
2012-11-26 13:27:58 +00:00
' method_number ' : fields . integer ( ' Number of Depreciations ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } , help = " The number of depreciations needed to depreciate your asset " ) ,
2012-11-26 10:13:53 +00:00
' method_period ' : fields . integer ( ' Number of Months in a Period ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } , help = " The amount of time between two depreciations, in months " ) ,
2011-06-27 10:25:54 +00:00
' method_end ' : fields . date ( ' Ending Date ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2011-07-09 22:45:35 +00:00
' method_progress_factor ' : fields . float ( ' Degressive Factor ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2011-05-27 09:37:42 +00:00
' value_residual ' : fields . function ( _amount_residual , method = True , digits_compute = dp . get_precision ( ' Account ' ) , string = ' Residual Value ' ) ,
2011-12-13 08:34:57 +00:00
' method_time ' : fields . selection ( [ ( ' number ' , ' Number of Depreciations ' ) , ( ' end ' , ' Ending Date ' ) ] , ' Time Method ' , required = True , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ,
2011-07-09 20:32:24 +00:00
help = " Choose the method to use to compute the dates and number of depreciation lines. \n " \
2011-07-09 22:45:35 +00:00
" * Number of Depreciations: Fix the number of depreciation lines and the time between 2 depreciations. \n " \
" * Ending Date: Choose the time between 2 depreciations and the date the depreciations won ' t go beyond. " ) ,
' prorata ' : fields . boolean ( ' Prorata Temporis ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } , help = ' Indicates that the first depreciation entry for this asset have to be done from the purchase date instead of the first January ' ) ,
2011-05-10 12:11:09 +00:00
' history_ids ' : fields . one2many ( ' account.asset.history ' , ' asset_id ' , ' History ' , readonly = True ) ,
2011-07-09 19:57:40 +00:00
' depreciation_line_ids ' : fields . one2many ( ' account.asset.depreciation.line ' , ' asset_id ' , ' Depreciation Lines ' , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] , ' open ' : [ ( ' readonly ' , False ) ] } ) ,
2011-07-09 22:45:35 +00:00
' salvage_value ' : fields . float ( ' Salvage Value ' , digits_compute = dp . get_precision ( ' Account ' ) , help = " It is the amount you plan to have that you cannot depreciate. " , readonly = True , states = { ' draft ' : [ ( ' readonly ' , False ) ] } ) ,
2011-03-30 13:25:27 +00:00
}
_defaults = {
' code ' : lambda obj , cr , uid , context : obj . pool . get ( ' ir.sequence ' ) . get ( cr , uid , ' account.asset.code ' ) ,
2011-03-31 23:43:25 +00:00
' purchase_date ' : lambda obj , cr , uid , context : time . strftime ( ' % Y- % m- %d ' ) ,
2011-07-09 22:45:35 +00:00
' active ' : True ,
' state ' : ' draft ' ,
' method ' : ' linear ' ,
' method_number ' : 5 ,
' method_time ' : ' number ' ,
' method_period ' : 12 ,
' method_progress_factor ' : 0.3 ,
2011-05-11 06:47:44 +00:00
' currency_id ' : lambda self , cr , uid , c : self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , c ) . company_id . currency_id . id ,
2011-05-12 12:40:38 +00:00
' company_id ' : lambda self , cr , uid , context : self . pool . get ( ' res.company ' ) . _company_default_get ( cr , uid , ' account.asset.asset ' , context = context ) ,
2011-03-30 13:25:27 +00:00
}
2011-12-13 08:34:57 +00:00
2011-06-27 06:42:52 +00:00
def _check_recursion ( self , cr , uid , ids , context = None , parent = None ) :
return super ( account_asset_asset , self ) . _check_recursion ( cr , uid , ids , context = context , parent = parent )
2011-05-02 09:51:34 +00:00
2011-05-23 06:21:52 +00:00
def _check_prorata ( self , cr , uid , ids , context = None ) :
2011-05-23 12:53:22 +00:00
for asset in self . browse ( cr , uid , ids , context = context ) :
2011-08-08 12:04:28 +00:00
if asset . prorata and asset . method_time != ' number ' :
2011-05-23 06:21:52 +00:00
return False
return True
_constraints = [
2012-07-12 07:06:31 +00:00
( _check_recursion , ' Error ! You cannot create recursive assets. ' , [ ' parent_id ' ] ) ,
2011-08-08 12:04:28 +00:00
( _check_prorata , ' Prorata temporis can be applied only for time method " number of depreciations " . ' , [ ' prorata ' ] ) ,
2011-05-23 06:21:52 +00:00
]
2011-05-02 09:51:34 +00:00
2011-05-17 10:37:53 +00:00
def onchange_category_id ( self , cr , uid , ids , category_id , context = None ) :
res = { ' value ' : { } }
2011-05-17 13:18:03 +00:00
asset_categ_obj = self . pool . get ( ' account.asset.category ' )
2011-05-17 10:37:53 +00:00
if category_id :
2011-05-17 13:18:03 +00:00
category_obj = asset_categ_obj . browse ( cr , uid , category_id , context = context )
res [ ' value ' ] = {
2011-06-07 05:38:22 +00:00
' method ' : category_obj . method ,
2011-07-09 20:32:24 +00:00
' method_number ' : category_obj . method_number ,
2011-05-17 12:44:52 +00:00
' method_time ' : category_obj . method_time ,
' method_period ' : category_obj . method_period ,
' method_progress_factor ' : category_obj . method_progress_factor ,
2011-06-06 05:57:53 +00:00
' method_end ' : category_obj . method_end ,
2011-05-17 12:44:52 +00:00
' prorata ' : category_obj . prorata ,
2011-05-17 13:18:03 +00:00
}
2011-05-17 10:37:53 +00:00
return res
2011-06-07 05:38:22 +00:00
2011-08-08 12:04:28 +00:00
def onchange_method_time ( self , cr , uid , ids , method_time = ' number ' , context = None ) :
2011-06-09 13:25:59 +00:00
res = { ' value ' : { } }
2011-08-08 12:04:28 +00:00
if method_time != ' number ' :
2011-06-09 13:25:59 +00:00
res [ ' value ' ] = { ' prorata ' : False }
return res
2011-06-07 05:38:22 +00:00
2012-03-05 18:40:03 +00:00
def _compute_entries ( self , cr , uid , ids , period_id , context = None ) :
2011-03-30 13:25:27 +00:00
result = [ ]
2011-07-09 23:45:39 +00:00
period_obj = self . pool . get ( ' account.period ' )
depreciation_obj = self . pool . get ( ' account.asset.depreciation.line ' )
2011-12-13 08:34:57 +00:00
period = period_obj . browse ( cr , uid , period_id , context = context )
2012-01-27 09:29:28 +00:00
depreciation_ids = depreciation_obj . search ( cr , uid , [ ( ' asset_id ' , ' in ' , ids ) , ( ' depreciation_date ' , ' <= ' , period . date_stop ) , ( ' depreciation_date ' , ' >= ' , period . date_start ) , ( ' move_check ' , ' = ' , False ) ] , context = context )
2014-07-06 14:44:26 +00:00
context = dict ( context or { } , depreciation_date = period . date_stop )
2011-07-09 23:45:39 +00:00
return depreciation_obj . create_move ( cr , uid , depreciation_ids , context = context )
2011-06-28 06:19:08 +00:00
def create ( self , cr , uid , vals , context = None ) :
2011-06-28 06:50:50 +00:00
asset_id = super ( account_asset_asset , self ) . create ( cr , uid , vals , context = context )
self . compute_depreciation_board ( cr , uid , [ asset_id ] , context = context )
return asset_id
2012-06-25 12:48:04 +00:00
def open_entries ( self , cr , uid , ids , context = None ) :
2014-07-06 14:44:26 +00:00
context = dict ( context or { } , search_default_asset_id = ids , default_asset_id = ids )
2012-06-25 12:48:04 +00:00
return {
2013-06-03 11:45:23 +00:00
' name ' : _ ( ' Journal Items ' ) ,
2012-06-25 12:48:04 +00:00
' view_type ' : ' form ' ,
' view_mode ' : ' tree,form ' ,
' res_model ' : ' account.move.line ' ,
' view_id ' : False ,
' type ' : ' ir.actions.act_window ' ,
2012-07-18 10:17:42 +00:00
' context ' : context ,
2012-06-25 12:48:04 +00:00
}
2011-06-28 06:19:08 +00:00
2011-03-30 13:25:27 +00:00
2011-03-30 20:21:06 +00:00
class account_asset_depreciation_line ( osv . osv ) :
_name = ' account.asset.depreciation.line '
_description = ' Asset depreciation line '
2011-05-12 08:48:45 +00:00
2011-05-12 11:37:46 +00:00
def _get_move_check ( self , cr , uid , ids , name , args , context = None ) :
2011-05-12 08:48:45 +00:00
res = { }
for line in self . browse ( cr , uid , ids , context = context ) :
2011-05-12 17:10:05 +00:00
res [ line . id ] = bool ( line . move_id )
2011-05-12 08:48:45 +00:00
return res
2011-03-30 13:25:27 +00:00
_columns = {
2014-05-21 09:52:05 +00:00
' name ' : fields . char ( ' Depreciation Name ' , required = True , select = 1 ) ,
2012-06-25 12:48:04 +00:00
' sequence ' : fields . integer ( ' Sequence ' , required = True ) ,
2013-03-05 09:21:11 +00:00
' asset_id ' : fields . many2one ( ' account.asset.asset ' , ' Asset ' , required = True , ondelete = ' cascade ' ) ,
2011-07-09 22:45:35 +00:00
' parent_state ' : fields . related ( ' asset_id ' , ' state ' , type = ' char ' , string = ' State of Asset ' ) ,
2012-11-26 10:13:53 +00:00
' amount ' : fields . float ( ' Current Depreciation ' , digits_compute = dp . get_precision ( ' Account ' ) , required = True ) ,
' remaining_value ' : fields . float ( ' Next Period Depreciation ' , digits_compute = dp . get_precision ( ' Account ' ) , required = True ) ,
2011-05-11 06:47:44 +00:00
' depreciated_value ' : fields . float ( ' Amount Already Depreciated ' , required = True ) ,
2012-03-28 09:26:48 +00:00
' depreciation_date ' : fields . date ( ' Depreciation Date ' , select = 1 ) ,
2011-05-12 09:52:37 +00:00
' move_id ' : fields . many2one ( ' account.move ' , ' Depreciation Entry ' ) ,
2011-05-16 13:12:32 +00:00
' move_check ' : fields . function ( _get_move_check , method = True , type = ' boolean ' , string = ' Posted ' , store = True )
2011-03-30 13:25:27 +00:00
}
2011-05-12 09:52:37 +00:00
2011-06-28 10:13:40 +00:00
def create_move ( self , cr , uid , ids , context = None ) :
2014-07-06 14:44:26 +00:00
context = dict ( context or { } )
2011-06-29 06:40:56 +00:00
can_close = False
2011-05-12 09:52:37 +00:00
asset_obj = self . pool . get ( ' account.asset.asset ' )
period_obj = self . pool . get ( ' account.period ' )
move_obj = self . pool . get ( ' account.move ' )
move_line_obj = self . pool . get ( ' account.move.line ' )
2011-05-12 13:44:20 +00:00
currency_obj = self . pool . get ( ' res.currency ' )
2011-07-09 23:45:39 +00:00
created_move_ids = [ ]
2012-11-14 15:01:33 +00:00
asset_ids = [ ]
2011-05-12 09:52:37 +00:00
for line in self . browse ( cr , uid , ids , context = context ) :
2014-11-06 10:47:38 +00:00
depreciation_date = context . get ( ' depreciation_date ' ) or line . depreciation_date or time . strftime ( ' % Y- % m- %d ' )
2011-05-12 09:52:37 +00:00
period_ids = period_obj . find ( cr , uid , depreciation_date , context = context )
2011-05-12 13:44:20 +00:00
company_currency = line . asset_id . company_id . currency_id . id
current_currency = line . asset_id . currency_id . id
context . update ( { ' date ' : depreciation_date } )
amount = currency_obj . compute ( cr , uid , current_currency , company_currency , line . amount , context = context )
2012-11-02 09:14:45 +00:00
sign = ( line . asset_id . category_id . journal_id . type == ' purchase ' and 1 ) or - 1
2015-05-25 16:35:10 +00:00
asset_name = " / "
reference = line . asset_id . name
2011-05-12 09:52:37 +00:00
move_vals = {
2011-07-10 00:45:35 +00:00
' name ' : asset_name ,
2011-05-12 09:52:37 +00:00
' date ' : depreciation_date ,
2011-07-10 00:45:35 +00:00
' ref ' : reference ,
2011-05-12 09:52:37 +00:00
' period_id ' : period_ids and period_ids [ 0 ] or False ,
' journal_id ' : line . asset_id . category_id . journal_id . id ,
}
move_id = move_obj . create ( cr , uid , move_vals , context = context )
2011-06-28 12:43:08 +00:00
journal_id = line . asset_id . category_id . journal_id . id
partner_id = line . asset_id . partner_id . id
2011-05-12 09:52:37 +00:00
move_line_obj . create ( cr , uid , {
2011-06-28 12:43:08 +00:00
' name ' : asset_name ,
' ref ' : reference ,
2011-05-12 09:52:37 +00:00
' move_id ' : move_id ,
2011-05-12 11:23:50 +00:00
' account_id ' : line . asset_id . category_id . account_depreciation_id . id ,
2011-05-12 09:52:37 +00:00
' debit ' : 0.0 ,
2011-05-12 13:44:20 +00:00
' credit ' : amount ,
2011-05-12 09:52:37 +00:00
' period_id ' : period_ids and period_ids [ 0 ] or False ,
2011-06-28 12:43:08 +00:00
' journal_id ' : journal_id ,
' partner_id ' : partner_id ,
2012-11-14 15:01:33 +00:00
' currency_id ' : company_currency != current_currency and current_currency or False ,
' amount_currency ' : company_currency != current_currency and - sign * line . amount or 0.0 ,
2011-05-12 09:52:37 +00:00
' date ' : depreciation_date ,
} )
move_line_obj . create ( cr , uid , {
2011-06-28 12:43:08 +00:00
' name ' : asset_name ,
' ref ' : reference ,
2011-05-12 09:52:37 +00:00
' move_id ' : move_id ,
2011-05-12 11:23:50 +00:00
' account_id ' : line . asset_id . category_id . account_expense_depreciation_id . id ,
2011-05-12 09:52:37 +00:00
' credit ' : 0.0 ,
2011-05-12 13:44:20 +00:00
' debit ' : amount ,
2011-05-12 09:52:37 +00:00
' period_id ' : period_ids and period_ids [ 0 ] or False ,
2011-06-28 12:43:08 +00:00
' journal_id ' : journal_id ,
' partner_id ' : partner_id ,
2012-11-14 15:01:33 +00:00
' currency_id ' : company_currency != current_currency and current_currency or False ,
' amount_currency ' : company_currency != current_currency and sign * line . amount or 0.0 ,
2011-05-12 09:52:37 +00:00
' analytic_account_id ' : line . asset_id . category_id . account_analytic_id . id ,
' date ' : depreciation_date ,
2011-05-24 06:25:30 +00:00
' asset_id ' : line . asset_id . id
2011-05-12 09:52:37 +00:00
} )
self . write ( cr , uid , line . id , { ' move_id ' : move_id } , context = context )
2011-07-09 23:45:39 +00:00
created_move_ids . append ( move_id )
2012-11-14 15:01:33 +00:00
asset_ids . append ( line . asset_id . id )
# we re-evaluate the assets to determine whether we can close them
for asset in asset_obj . browse ( cr , uid , list ( set ( asset_ids ) ) , context = context ) :
if currency_obj . is_zero ( cr , uid , asset . currency_id , asset . value_residual ) :
asset . write ( { ' state ' : ' close ' } )
2011-07-09 23:45:39 +00:00
return created_move_ids
2011-05-12 09:52:37 +00:00
2011-03-30 20:21:06 +00:00
2011-03-30 13:25:27 +00:00
class account_move_line ( osv . osv ) :
_inherit = ' account.move.line '
_columns = {
2013-03-06 17:05:38 +00:00
' asset_id ' : fields . many2one ( ' account.asset.asset ' , ' Asset ' , ondelete = " restrict " ) ,
2011-03-30 13:25:27 +00:00
}
2011-05-10 18:59:04 +00:00
class account_asset_history ( osv . osv ) :
_name = ' account.asset.history '
2011-03-30 13:25:27 +00:00
_description = ' Asset history '
_columns = {
2014-05-21 09:52:05 +00:00
' name ' : fields . char ( ' History name ' , select = 1 ) ,
2011-03-30 13:25:27 +00:00
' user_id ' : fields . many2one ( ' res.users ' , ' User ' , required = True ) ,
' date ' : fields . date ( ' Date ' , required = True ) ,
2011-03-30 20:21:06 +00:00
' asset_id ' : fields . many2one ( ' account.asset.asset ' , ' Asset ' , required = True ) ,
2011-12-13 08:34:57 +00:00
' method_time ' : fields . selection ( [ ( ' number ' , ' Number of Depreciations ' ) , ( ' end ' , ' Ending Date ' ) ] , ' Time Method ' , required = True ,
2011-07-11 11:38:27 +00:00
help = " The method to use to compute the dates and number of depreciation lines. \n " \
2011-07-09 20:32:24 +00:00
" Number of Depreciations: Fix the number of depreciation lines and the time between 2 depreciations. \n " \
" Ending Date: Choose the time between 2 depreciations and the date the depreciations won ' t go beyond. " ) ,
2012-11-26 10:13:53 +00:00
' method_number ' : fields . integer ( ' Number of Depreciations ' , help = " The number of depreciations needed to depreciate your asset " ) ,
2011-07-01 08:42:33 +00:00
' method_period ' : fields . integer ( ' Period Length ' , help = " Time in month between two depreciations " ) ,
2011-03-30 13:25:27 +00:00
' method_end ' : fields . date ( ' Ending date ' ) ,
' note ' : fields . text ( ' Note ' ) ,
}
2011-07-01 08:09:31 +00:00
_order = ' date desc '
2011-03-30 13:25:27 +00:00
_defaults = {
' date ' : lambda * args : time . strftime ( ' % Y- % m- %d ' ) ,
2011-07-01 08:09:31 +00:00
' user_id ' : lambda self , cr , uid , ctx : uid
2011-03-30 13:25:27 +00:00
}
2011-12-13 08:34:57 +00:00
2011-03-30 13:25:27 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: