##############################################################################
#
# OpenERP, Open Source Management Solution
-# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
+# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
from datetime import datetime
from dateutil.relativedelta import relativedelta
-from osv import osv, fields
-import decimal_precision as dp
+from openerp.osv import fields, osv
+import openerp.addons.decimal_precision as dp
+from tools.translate import _
class account_asset_category(osv.osv):
_name = 'account.asset.category'
'name': fields.char('Name', size=64, required=True, select=1),
'note': fields.text('Note'),
'account_analytic_id': fields.many2one('account.analytic.account', 'Analytic account'),
- 'account_asset_id': fields.many2one('account.account', 'Asset Account', required=True),
- 'account_depreciation_id': fields.many2one('account.account', 'Depreciation Account', required=True),
- 'account_expense_depreciation_id': fields.many2one('account.account', 'Depr. Expense Account', required=True),
+ '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')]),
'journal_id': fields.many2one('account.journal', 'Journal', required=True),
'company_id': fields.many2one('res.company', 'Company', required=True),
- 'method': fields.selection([('linear','Linear'),('progressif','Progressive')], 'Computation method', required=True),
- 'method_delay': fields.integer('Number of Depreciations'),
- 'method_period': fields.integer('Period Length', help="State here the time between 2 depreciations, in months"),
- 'method_progress_factor': fields.float('Progressif Factor'),
- 'method_time': fields.selection([('delay','Delay'),('end','Ending Period')], 'Time Method', required=True),
+ '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" \
+ " * Degressive: Calculated on basis of: Residual Value * Degressive Factor"),
+ 'method_number': fields.integer('Number of Depreciations', help="The number of depreciations needed to depreciate your asset"),
+ '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'),
+ '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"\
+ " * 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."),
'method_end': fields.date('Ending date'),
- 'prorata':fields.boolean('Prorata Temporis', help='Indicates that the accounting entries 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 invoice."),
+ '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."),
}
_defaults = {
'company_id': lambda self, cr, uid, context: self.pool.get('res.company')._company_default_get(cr, uid, 'account.asset.category', context=context),
'method': 'linear',
- 'method_delay': 5,
- 'method_time': 'delay',
+ 'method_number': 5,
+ 'method_time': 'number',
'method_period': 12,
'method_progress_factor': 0.3,
}
account_asset_category()
-#class one2many_mod_asset(fields.one2many):
-#
-# def get(self, cr, obj, ids, name, user=None, offset=0, context=None, values=None):
-# prinasset_property_id if context is None:
-# context = {}
-# if not values:
-# values = {}
-# res = {}
-# for id in ids:
-# res[id] = []
-# #compute depreciation board
-# depreciation_line_ids = obj.pool.get('account.asset.asset').compute_depreciation_board(cr, user, ids, context=context)
-# for key, value in depreciation_line_ids.items():
-# #write values on asset
-# obj.pool.get(self._obj).write(cr, user, key, {'depreciation_line_ids': [6,0,value]})
-# return depreciation_line_ids
-
class account_asset_asset(osv.osv):
_name = 'account.asset.asset'
_description = 'Asset'
- def _get_period(self, cr, uid, context={}):
- periods = self.pool.get('account.period').find(cr, uid)
+ 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.'))
+ return super(account_asset_asset, self).unlink(cr, uid, ids, context=context)
+
+ def _get_period(self, cr, uid, context=None):
+ ctx = dict(context or {}, account_period_prefer_normal=True)
+ periods = self.pool.get('account.period').find(cr, uid, context=ctx)
if periods:
return periods[0]
else:
if asset.method == 'linear':
amount = amount_to_depr / (undone_dotation_number - len(posted_depreciation_line_ids))
if asset.prorata:
- amount = amount_to_depr / asset.method_delay
+ amount = amount_to_depr / asset.method_number
days = total_days - float(depreciation_date.strftime('%j'))
if i == 1:
- amount = (amount_to_depr / asset.method_delay) / total_days * days
+ amount = (amount_to_depr / asset.method_number) / total_days * days
elif i == undone_dotation_number:
- amount = (amount_to_depr / asset.method_delay) / total_days * (total_days - days)
+ amount = (amount_to_depr / asset.method_number) / total_days * (total_days - days)
elif asset.method == 'degressive':
amount = residual_amount * asset.method_progress_factor
+ 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)
return amount
def _compute_board_undone_dotation_nb(self, cr, uid, asset, depreciation_date, total_days, context=None):
- undone_dotation_number = asset.method_delay
+ undone_dotation_number = asset.method_number
if asset.method_time == 'end':
end_date = datetime.strptime(asset.method_end, '%Y-%m-%d')
- undone_dotation_number = (end_date - depreciation_date).days / total_days
- if asset.prorata or asset.method_time == 'end':
+ 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))
+ undone_dotation_number += 1
+ if asset.prorata:
undone_dotation_number += 1
return undone_dotation_number
- def compute_depreciation_board(self, cr, uid,ids, context=None):
+ def compute_depreciation_board(self, cr, uid, ids, context=None):
depreciation_lin_obj = self.pool.get('account.asset.depreciation.line')
+ currency_obj = self.pool.get('res.currency')
for asset in self.browse(cr, uid, ids, context=context):
if asset.value_residual == 0.0:
continue
- posted_depreciation_line_ids = depreciation_lin_obj.search(cr, uid, [('asset_id', '=', asset.id), ('move_check', '=', True)])
+ posted_depreciation_line_ids = depreciation_lin_obj.search(cr, uid, [('asset_id', '=', asset.id), ('move_check', '=', True)],order='depreciation_date desc')
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)
-
- amount_to_depr = residual_amount = asset.value_residual
- depreciation_date = datetime.strptime(self._get_last_depreciation_date(cr, uid, [asset.id], context)[asset.id], '%Y-%m-%d')
+ amount_to_depr = residual_amount = asset.value_residual
+ if asset.prorata:
+ depreciation_date = datetime.strptime(self._get_last_depreciation_date(cr, uid, [asset.id], context)[asset.id], '%Y-%m-%d')
+ else:
+ # depreciation_date = 1st January of purchase year
+ purchase_date = datetime.strptime(asset.purchase_date, '%Y-%m-%d')
+ #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)
day = depreciation_date.day
month = depreciation_date.month
year = depreciation_date.year
for x in range(len(posted_depreciation_line_ids), undone_dotation_number):
i = x + 1
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)
+ company_currency = asset.company_id.currency_id.id
+ current_currency = asset.currency_id.id
+ # compute amount into company currency
+ amount = currency_obj.compute(cr, uid, current_currency, company_currency, amount, context=context)
residual_amount -= amount
vals = {
'amount': amount,
'asset_id': asset.id,
'sequence': i,
- 'name': str(asset.id) +'/' + str(i),
+ 'name': "%s/%s" %(i, undone_dotation_number),
'remaining_value': residual_amount,
'depreciated_value': (asset.purchase_value - asset.salvage_value) - (residual_amount + amount),
'depreciation_date': depreciation_date.strftime('%Y-%m-%d'),
year = depreciation_date.year
return True
- def validate(self, cr, uid, ids, context={}):
+ def validate(self, cr, uid, ids, context=None):
+ if context is None:
+ context = {}
return self.write(cr, uid, ids, {
'state':'open'
}, context)
+ def set_to_close(self, cr, uid, ids, context=None):
+ return self.write(cr, uid, ids, {'state': 'close'}, context=context)
+
+ def set_to_draft(self, cr, uid, ids, context=None):
+ return self.write(cr, uid, ids, {'state': 'draft'}, context=context)
+
def _amount_residual(self, cr, uid, ids, name, args, context=None):
cr.execute("""SELECT
- l.asset_id as id, round(SUM(abs(l.debit-l.credit))) AS amount
+ l.asset_id as id, SUM(abs(l.debit-l.credit)) AS amount
FROM
account_move_line l
WHERE
res.setdefault(id, 0.0)
return res
+ def onchange_company_id(self, cr, uid, ids, company_id=False, context=None):
+ 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}
+
+ def onchange_purchase_salvage_value(self, cr, uid, ids, purchase_value, salvage_value, context=None):
+ val = {}
+ for asset in self.browse(cr, uid, ids, context=context):
+ if purchase_value:
+ val['value_residual'] = purchase_value - salvage_value
+ if salvage_value:
+ val['value_residual'] = purchase_value - salvage_value
+ return {'value': val}
+
_columns = {
- 'period_id': fields.many2one('account.period', 'First Period', required=True, readonly=True, states={'draft':[('readonly',False)]}),
'account_move_line_ids': fields.one2many('account.move.line', 'asset_id', 'Entries', readonly=True, states={'draft':[('readonly',False)]}),
-
- 'name': fields.char('Asset', size=64, required=True, select=1),
- 'code': fields.char('Reference ', size=16, select=1),
- 'purchase_value': fields.float('Gross Value ', required=True, size=16, select=1),
- 'currency_id': fields.many2one('res.currency','Currency',required=True,size=5,select=1),
- 'company_id': fields.many2one('res.company', 'Company', required=True),
+ 'name': fields.char('Asset Name', size=64, required=True, readonly=True, states={'draft':[('readonly',False)]}),
+ 'code': fields.char('Reference', size=32, readonly=True, states={'draft':[('readonly',False)]}),
+ 'purchase_value': fields.float('Gross Value', required=True, readonly=True, states={'draft':[('readonly',False)]}),
+ '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)]}),
'note': fields.text('Note'),
- 'category_id': fields.many2one('account.asset.category', 'Asset Category',required=True, change_default=True),
- 'localisation': fields.char('Localisation', size=32, select=2),
- 'parent_id': fields.many2one('account.asset.asset', 'Parent Asset'),
+ 'category_id': fields.many2one('account.asset.category', 'Asset Category', required=True, change_default=True, readonly=True, states={'draft':[('readonly',False)]}),
+ 'parent_id': fields.many2one('account.asset.asset', 'Parent Asset', readonly=True, states={'draft':[('readonly',False)]}),
'child_ids': fields.one2many('account.asset.asset', 'parent_id', 'Children Assets'),
- 'purchase_date': fields.date('Purchase Date', required=True),
- 'state': fields.selection([('draft','Draft'),('open','Running'),('close','Close')], 'State', required=True),
- 'active': fields.boolean('Active', select=2),
- 'partner_id': fields.many2one('res.partner', 'Partner'),
- 'method': fields.selection([('linear','Linear'),('degressive','Degressive')], 'Computation Method', required=True, readonly=True, states={'draft':[('readonly',False)]}, help="Linear: Calculated on basis of Gross Value/During (interval) \
- \nDegressive: Calculated on basis of Gross Value * Degressive Factor"),
- 'method_delay': fields.integer('Number of Depreciations', readonly=True, states={'draft':[('readonly',False)]}, help="Calculates Depreciation within specified interval"),
- 'method_period': fields.integer('Period Length', readonly=True, states={'draft':[('readonly',False)]}, help="State here the time during 2 depreciations, in months"),
+ 'purchase_date': fields.date('Purchase Date', required=True, readonly=True, states={'draft':[('readonly',False)]}),
+ 'state': fields.selection([('draft','Draft'),('open','Running'),('close','Close')], 'Status', required=True,
+ 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."),
+ '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" \
+ " * Degressive: Calculated on basis of: Residual Value * Degressive Factor"),
+ 'method_number': fields.integer('Number of Depreciations', readonly=True, states={'draft':[('readonly',False)]}, help="The number of depreciations needed to depreciate your asset"),
+ '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"),
'method_end': fields.date('Ending Date', readonly=True, states={'draft':[('readonly',False)]}),
- 'method_progress_factor': fields.float('Progressif Factor', readonly=True, states={'draft':[('readonly',False)]}),
+ 'method_progress_factor': fields.float('Degressive Factor', readonly=True, states={'draft':[('readonly',False)]}),
'value_residual': fields.function(_amount_residual, method=True, digits_compute=dp.get_precision('Account'), string='Residual Value'),
- 'method_time': fields.selection([('delay','Delay'),('end','Ending Period')], 'Time Method', required=True, readonly=True, states={'draft':[('readonly',False)]}, help="Delay: Allow users to enter number of periods to generate depreciation lines \n Ending Period: Calculates depreciation lines on the basis of Ending Period Date and Number of Depreciation"),
- 'prorata':fields.boolean('Prorata Temporis', Readonly="True", help='Indicates that the accounting entries for this asset have to be done from the purchase date instead of the first January'),
+ 'method_time': fields.selection([('number','Number of Depreciations'),('end','Ending Date')], 'Time Method', required=True, readonly=True, states={'draft':[('readonly',False)]},
+ help="Choose the method to use to compute the dates and number of depreciation lines.\n"\
+ " * 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'),
'history_ids': fields.one2many('account.asset.history', 'asset_id', 'History', readonly=True),
- 'depreciation_line_ids': fields.one2many('account.asset.depreciation.line', 'asset_id', 'Depreciation Lines'),
- '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."),
+ 'depreciation_line_ids': fields.one2many('account.asset.depreciation.line', 'asset_id', 'Depreciation Lines', readonly=True, states={'draft':[('readonly',False)],'open':[('readonly',False)]}),
+ '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)]}),
}
_defaults = {
'code': lambda obj, cr, uid, context: obj.pool.get('ir.sequence').get(cr, uid, 'account.asset.code'),
'purchase_date': lambda obj, cr, uid, context: time.strftime('%Y-%m-%d'),
- 'active': lambda obj, cr, uid, context: True,
- 'state': lambda obj, cr, uid, context: 'draft',
- 'period_id': _get_period,
- 'method': lambda obj, cr, uid, context: 'linear',
- 'method_delay': lambda obj, cr, uid, context: 5,
- 'method_time': lambda obj, cr, uid, context: 'delay',
- 'method_period': lambda obj, cr, uid, context: 12,
- 'method_progress_factor': lambda obj, cr, uid, context: 0.3,
+ 'active': True,
+ 'state': 'draft',
+ 'method': 'linear',
+ 'method_number': 5,
+ 'method_time': 'number',
+ 'method_period': 12,
+ 'method_progress_factor': 0.3,
'currency_id': lambda self,cr,uid,c: self.pool.get('res.users').browse(cr, uid, uid, c).company_id.currency_id.id,
'company_id': lambda self, cr, uid, context: self.pool.get('res.company')._company_default_get(cr, uid, 'account.asset.asset',context=context),
}
-
+
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)
def _check_prorata(self, cr, uid, ids, context=None):
for asset in self.browse(cr, uid, ids, context=context):
- if asset.prorata and (asset.method != 'linear' or asset.method_time != 'delay'):
+ if asset.prorata and asset.method_time != 'number':
return False
return True
_constraints = [
- (_check_recursion, 'Error ! You can not create recursive assets.', ['parent_id']),
- (_check_prorata, '\nProrata temporis can be applied only for computation method linear and time method delay.', ['prorata']),
+ (_check_recursion, 'Error ! You cannot create recursive assets.', ['parent_id']),
+ (_check_prorata, 'Prorata temporis can be applied only for time method "number of depreciations".', ['prorata']),
]
def onchange_category_id(self, cr, uid, ids, category_id, context=None):
category_obj = asset_categ_obj.browse(cr, uid, category_id, context=context)
res['value'] = {
'method': category_obj.method,
- 'method_delay': category_obj.method_delay,
+ 'method_number': category_obj.method_number,
'method_time': category_obj.method_time,
'method_period': category_obj.method_period,
'method_progress_factor': category_obj.method_progress_factor,
}
return res
- def onchange_method_time(self, cr, uid, ids, method='linear', method_time='delay', context=None):
+ def onchange_method_time(self, cr, uid, ids, method_time='number', context=None):
res = {'value': {}}
- if method != 'linear' or method_time != 'delay':
+ if method_time != 'number':
res['value'] = {'prorata': False}
return res
default = {}
if context is None:
context = {}
- default.update({'depreciation_line_ids': [], 'state': 'draft'})
+ default.update({'depreciation_line_ids': [], 'account_move_line_ids': [], 'history_ids': [], 'state': 'draft'})
return super(account_asset_asset, self).copy(cr, uid, id, default, context=context)
- def _compute_period(self, cr, uid, property, context={}):
- if (len(property.entry_asset_ids or [])/2)>=property.method_delay:
- return False
- if len(property.entry_asset_ids):
- cp = property.entry_asset_ids[-1].period_id
- cpid = self.pool.get('account.period').next(cr, uid, cp, property.method_period, context)
- current_period = self.pool.get('account.period').browse(cr, uid, cpid, context)
- else:
- current_period = property.asset_id.period_id
- return current_period
-
- def _compute_move(self, cr, uid, property, period, context={}):
- #FIXME: fucntion not working OK
+ def _compute_entries(self, cr, uid, ids, period_id, context=None):
result = []
- total = 0.0
- for move in property.asset_id.entry_ids:
- total += move.debit-move.credit
- for move in property.entry_asset_ids:
- if move.account_id == property.account_asset_ids:
- total += move.debit
- total += -move.credit
- periods = (len(property.entry_asset_ids)/2) - property.method_delay
-
- if periods==1:
- amount = total
- else:
- if property.method == 'linear':
- amount = total / periods
- else:
- amount = total * property.method_progress_factor
-
- move_id = self.pool.get('account.move').create(cr, uid, {
- 'journal_id': property.journal_id.id,
- 'period_id': period.id,
- 'name': property.name or property.asset_id.name,
- 'ref': property.asset_id.code
- })
- result = [move_id]
- id = self.pool.get('account.move.line').create(cr, uid, {
- 'name': property.name or property.asset_id.name,
- 'move_id': move_id,
- 'account_id': property.account_asset_id.id,
- 'debit': amount>0 and amount or 0.0,
- 'credit': amount<0 and -amount or 0.0,
- 'ref': property.asset_id.code,
- 'period_id': period.id,
- 'journal_id': property.journal_id.id,
- 'partner_id': property.asset_id.partner_id.id,
- 'date': time.strftime('%Y-%m-%d'),
- })
- id2 = self.pool.get('account.move.line').create(cr, uid, {
- 'name': property.name or property.asset_id.name,
- 'move_id': move_id,
- 'account_id': property.account_actif_id.id,
- 'credit': amount>0 and amount or 0.0,
- 'debit': amount<0 and -amount or 0.0,
- 'ref': property.asset_id.code,
- 'period_id': period.id,
- 'journal_id': property.journal_id.id,
- 'partner_id': property.asset_id.partner_id.id,
- 'date': time.strftime('%Y-%m-%d'),
- })
- #
- self.pool.get('account.asset.asset').write(cr, uid, [property.id], {
- 'entry_asset_ids': [(4, id2, False),(4,id,False)]
- })
- if property.method_delay - (len(property.entry_asset_ids)/2)<=1:
- #self.pool.get('account.asset.property')._close(cr, uid, property, context)
- return result
- return result
-
- def _compute_entries(self, cr, uid, asset, period_id, context={}):
- #FIXME: function not working CHECK all res
- result = []
- date_start = self.pool.get('account.period').browse(cr, uid, period_id, context).date_start
- for property in asset.property_ids:
- if property.state=='open':
- period = self._compute_period(cr, uid, property, context)
- if period and (period.date_start<=date_start):
- result += self._compute_move(cr, uid, property, period, context)
- return result
+ period_obj = self.pool.get('account.period')
+ depreciation_obj = self.pool.get('account.asset.depreciation.line')
+ period = period_obj.browse(cr, uid, period_id, context=context)
+ 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)
+ if context is None:
+ context = {}
+ context.update({'depreciation_date':period.date_stop})
+ return depreciation_obj.create_move(cr, uid, depreciation_ids, context=context)
+
+ def create(self, cr, uid, vals, context=None):
+ 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
+
+ def open_entries(self, cr, uid, ids, context=None):
+ if context is None:
+ context = {}
+ context.update({'search_default_asset_id': ids, 'default_asset_id': ids})
+ return {
+ 'view_type': 'form',
+ 'view_mode': 'tree,form',
+ 'res_model': 'account.move.line',
+ 'view_id': False,
+ 'type': 'ir.actions.act_window',
+ 'context': context,
+ }
+
account_asset_asset()
class account_asset_depreciation_line(osv.osv):
_columns = {
'name': fields.char('Depreciation Name', size=64, required=True, select=1),
- 'sequence': fields.integer('Sequence of the depreciation', required=True),
- 'asset_id': fields.many2one('account.asset.asset', 'Asset', required=True),
- 'amount': fields.float('Depreciation Amount', required=True),
- 'remaining_value': fields.float('Amount to Depreciate', required=True),
+ 'sequence': fields.integer('Sequence', required=True),
+ 'asset_id': fields.many2one('account.asset.asset', 'Asset', required=True, ondelete='cascade'),
+ 'parent_state': fields.related('asset_id', 'state', type='char', string='State of Asset'),
+ '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),
'depreciated_value': fields.float('Amount Already Depreciated', required=True),
- 'depreciation_date': fields.char('Depreciation Date', size=64, select=1),
+ 'depreciation_date': fields.date('Depreciation Date', select=1),
'move_id': fields.many2one('account.move', 'Depreciation Entry'),
'move_check': fields.function(_get_move_check, method=True, type='boolean', string='Posted', store=True)
}
- def create_move(self, cr, uid,ids, context=None):
+ def create_move(self, cr, uid, ids, context=None):
+ can_close = False
if context is None:
context = {}
asset_obj = self.pool.get('account.asset.asset')
move_obj = self.pool.get('account.move')
move_line_obj = self.pool.get('account.move.line')
currency_obj = self.pool.get('res.currency')
+ created_move_ids = []
+ asset_ids = []
for line in self.browse(cr, uid, ids, context=context):
- depreciation_date = line.asset_id.prorata and line.asset_id.purchase_date or time.strftime('%Y-%m-%d')
- period_ids = period_obj.find(cr, uid, depreciation_date, context=context)
+ depreciation_date = context.get('depreciation_date') or time.strftime('%Y-%m-%d')
+ ctx = dict(context, account_period_prefer_normal=True)
+ period_ids = period_obj.find(cr, uid, depreciation_date, context=ctx)
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)
- sign = line.asset_id.category_id.journal_id.type = 'purchase' and 1 or -1
+ sign = (line.asset_id.category_id.journal_id.type == 'purchase' and 1) or -1
+ asset_name = line.asset_id.name
+ reference = line.name
move_vals = {
- 'name': line.name,
'date': depreciation_date,
- 'ref': line.name,
+ 'ref': "%s %s" %(line.asset_id.code or line.asset_id.name, line.name),
'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)
+ journal_id = line.asset_id.category_id.journal_id.id
+ partner_id = line.asset_id.partner_id.id
move_line_obj.create(cr, uid, {
- 'name': line.name,
- 'ref': line.name,
+ 'name': asset_name,
+ 'ref': reference,
'move_id': move_id,
'account_id': line.asset_id.category_id.account_depreciation_id.id,
'debit': 0.0,
'credit': amount,
'period_id': period_ids and period_ids[0] or False,
- 'journal_id': line.asset_id.category_id.journal_id.id,
- 'partner_id': line.asset_id.partner_id.id,
- 'currency_id': company_currency <> current_currency and current_currency or False,
- 'amount_currency': company_currency <> current_currency and - sign * line.amount or 0.0,
+ 'journal_id': journal_id,
+ 'partner_id': partner_id,
+ 'currency_id': company_currency != current_currency and current_currency or False,
+ 'amount_currency': company_currency != current_currency and - sign * line.amount or 0.0,
'date': depreciation_date,
})
move_line_obj.create(cr, uid, {
- 'name': line.name,
- 'ref': line.name,
+ 'name': asset_name,
+ 'ref': reference,
'move_id': move_id,
'account_id': line.asset_id.category_id.account_expense_depreciation_id.id,
'credit': 0.0,
'debit': amount,
'period_id': period_ids and period_ids[0] or False,
- 'journal_id': line.asset_id.category_id.journal_id.id,
- 'partner_id': line.asset_id.partner_id.id,
- 'currency_id': company_currency <> current_currency and current_currency or False,
- 'amount_currency': company_currency <> current_currency and sign * line.amount or 0.0,
+ 'journal_id': journal_id,
+ 'partner_id': partner_id,
+ 'currency_id': company_currency != current_currency and current_currency or False,
+ 'amount_currency': company_currency != current_currency and sign * line.amount or 0.0,
'analytic_account_id': line.asset_id.category_id.account_analytic_id.id,
'date': depreciation_date,
'asset_id': line.asset_id.id
})
self.write(cr, uid, line.id, {'move_id': move_id}, context=context)
- return True
+ created_move_ids.append(move_id)
+ 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'})
+ return created_move_ids
account_asset_depreciation_line()
-#class account_asset_property(osv.osv):
-# def _amount_total(self, cr, uid, ids, name, args, context={}):
-# id_set=",".join(map(str,ids))
-# cr.execute("""SELECT l.asset_id,abs(SUM(l.debit-l.credit)) AS amount FROM
-# account_asset_property p
-# left join
-# account_move_line l on (p.asset_id=l.asset_id)
-# WHERE p.id IN ("""+id_set+") GROUP BY l.asset_id ")
-# res=dict(cr.fetchall())
-# for id in ids:
-# res.setdefault(id, 0.0)
-# return res
-#
-# def _close(self, cr, uid, property, context={}):
-# if property.state<>'close':
-# self.pool.get('account.asset.property').write(cr, uid, [property.id], {
-# 'state': 'close'
-# })
-# property.state='close'
-# ok = property.asset_id.state=='open'
-# for prop in property.asset_id.property_ids:
-# ok = ok and prop.state=='close'
-# self.pool.get('account.asset.asset').write(cr, uid, [property.asset_id.id], {
-# 'state': 'close'
-# }, context)
-# return True
-#
-# _name = 'account.asset.property'
-# _description = 'Asset property'
-# _columns = {
-# 'name': fields.char('Method name', size=64, select=1),
-# 'type': fields.selection([('direct','Direct'),('indirect','Indirect')], 'Depr. method type', select=2, required=True),
-# 'asset_id': fields.many2one('account.asset.asset', 'Asset', required=True),
-# 'account_asset_id': fields.many2one('account.account', 'Asset account', required=True),
-# 'account_actif_id': fields.many2one('account.account', 'Depreciation account', required=True),
-# 'journal_id': fields.many2one('account.journal', 'Journal', required=True),
-# 'journal_analytic_id': fields.many2one('account.analytic.journal', 'Analytic journal'),
-# 'account_analytic_id': fields.many2one('account.analytic.account', 'Analytic account'),
-#
-# 'method': fields.selection([('linear','Linear'),('progressif','Progressive')], 'Computation method', required=True, readonly=True, states={'draft':[('readonly',False)]}),
-# 'method_delay': fields.integer('During', readonly=True, states={'draft':[('readonly',False)]}),
-# 'method_period': fields.integer('Depre. all', readonly=True, states={'draft':[('readonly',False)]}),
-# 'method_end': fields.date('Ending date'),
-#
-# 'date': fields.date('Date created'),
-# #'test': fields.one2many('account.pre', 'asset_id', readonly=True, states={'draft':[('readonly',False)]}),
-# 'entry_asset_ids': fields.many2many('account.move.line', 'account_move_asset_entry_rel', 'asset_property_id', 'move_id', 'Asset Entries'),
-# 'board_ids': fields.one2many('account.asset.board', 'asset_id', 'Asset board'),
-#
-# 'value_total': fields.function(_amount_total, method=True, digits=(16,2),string='Gross value'),
-# 'state': fields.selection([('draft','Draft'), ('open','Open'), ('close','Close')], 'State', required=True),
-# 'history_ids': fields.one2many('account.asset.property.history', 'asset_property_id', 'History', readonly=True)
-## 'parent_id': fields.many2one('account.asset.asset', 'Parent asset'),
-## 'partner_id': fields.many2one('res.partner', 'Partner'),
-## 'note': fields.text('Note'),
-#
-# }
-# _defaults = {
-# 'type': lambda obj, cr, uid, context: 'direct',
-# 'state': lambda obj, cr, uid, context: 'draft',
-# 'method': lambda obj, cr, uid, context: 'linear',
-# 'method_time': lambda obj, cr, uid, context: 'delay',
-# 'method_progress_factor': lambda obj, cr, uid, context: 0.3,
-# 'method_delay': lambda obj, cr, uid, context: 5,
-# 'method_period': lambda obj, cr, uid, context: 12,
-# 'date': lambda obj, cr, uid, context: time.strftime('%Y-%m-%d')
-# }
-#account_asset_property()
-
class account_move_line(osv.osv):
_inherit = 'account.move.line'
_columns = {
- 'asset_id': fields.many2one('account.asset.asset', 'Asset'),
+ 'asset_id': fields.many2one('account.asset.asset', 'Asset', ondelete="restrict"),
'entry_ids': fields.one2many('account.move.line', 'asset_id', 'Entries', readonly=True, states={'draft':[('readonly',False)]}),
}
'user_id': fields.many2one('res.users', 'User', required=True),
'date': fields.date('Date', required=True),
'asset_id': fields.many2one('account.asset.asset', 'Asset', required=True),
- 'method_time': fields.selection([('delay','Delay'),('end','Ending Period')], 'Time Method', required=True, help="Delay: Allow users to enter number of periods to generate depreciation lines \n Ending Period: Calculates depreciation lines on the basis of Ending Period Date and Number of Depreciations"),
- 'method_delay': fields.integer('Number of Depreciations'),
+ 'method_time': fields.selection([('number','Number of Depreciations'),('end','Ending Date')], 'Time Method', required=True,
+ help="The method to use to compute the dates and number of depreciation lines.\n"\
+ "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."),
+ 'method_number': fields.integer('Number of Depreciations', help="The number of depreciations needed to depreciate your asset"),
'method_period': fields.integer('Period Length', help="Time in month between two depreciations"),
'method_end': fields.date('Ending date'),
'note': fields.text('Note'),
'date': lambda *args: time.strftime('%Y-%m-%d'),
'user_id': lambda self, cr, uid, ctx: uid
}
-
+
account_asset_history()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: