Merge remote-tracking branch 'odoo/7.0' into 7.0
[odoo/odoo.git] / addons / account_asset / account_asset.py
index b682806..6dc050b 100644 (file)
@@ -23,8 +23,9 @@ import time
 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'
@@ -34,15 +35,15 @@ class account_asset_category(osv.osv):
         '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'),('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: Remaining Value * Degressive Factor"),
-        'method_number': fields.integer('Number of Depreciations'),
+            "  * 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,
@@ -75,8 +76,15 @@ 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:
@@ -134,10 +142,11 @@ class account_asset_asset(osv.osv):
 
     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)
@@ -148,7 +157,12 @@ class account_asset_asset(osv.osv):
             else:
                 # depreciation_date = 1st January of purchase year
                 purchase_date = datetime.strptime(asset.purchase_date, '%Y-%m-%d')
-                depreciation_date = datetime(purchase_date.year, 1, 1)
+                #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
@@ -158,12 +172,16 @@ class account_asset_asset(osv.osv):
             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'),
@@ -176,7 +194,9 @@ class account_asset_asset(osv.osv):
                 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)
@@ -189,7 +209,7 @@ class account_asset_asset(osv.osv):
 
     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
@@ -210,6 +230,15 @@ class account_asset_asset(osv.osv):
             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 = {
         'account_move_line_ids': fields.one2many('account.move.line', 'asset_id', 'Entries', readonly=True, states={'draft':[('readonly',False)]}),
@@ -224,16 +253,16 @@ class account_asset_asset(osv.osv):
         'child_ids': fields.one2many('account.asset.asset', 'parent_id', 'Children Assets'),
         '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 state is 'Draft'.\n" \
-                                       "If the asset is confirmed, the state 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 state."),
+                                  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: Remaining Value * Degressive Factor"),
-        'method_number': fields.integer('Number of Depreciations', readonly=True, states={'draft':[('readonly',False)]}, help="Calculates Depreciation within specified interval"),
-        'method_period': fields.integer('Number of Months in a Period', required=True, readonly=True, states={'draft':[('readonly',False)]}, help="State here the time during 2 depreciations, in months"),
+            "  * 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('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'),
@@ -301,15 +330,18 @@ class account_asset_asset(osv.osv):
             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_entries(self, cr, uid, ids, period_id, context={}):
+    def _compute_entries(self, cr, uid, ids, period_id, context=None):
         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):
@@ -345,12 +377,12 @@ class account_asset_depreciation_line(osv.osv):
     _columns = {
         'name': fields.char('Depreciation Name', size=64, required=True, select=1),
         'sequence': fields.integer('Sequence', required=True),
-        'asset_id': fields.many2one('account.asset.asset', 'Asset', 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('Depreciation Amount', required=True),
-        'remaining_value': fields.float('Amount to Depreciate', required=True),
+        '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)
     }
@@ -365,22 +397,21 @@ class account_asset_depreciation_line(osv.osv):
         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):
-            if currency_obj.is_zero(cr, uid, line.asset_id.currency_id, line.remaining_value):
-                can_close = True
-            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': asset_name,
                 'date': depreciation_date,
-                'ref': reference,
+                '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,
                 }
@@ -397,8 +428,8 @@ class account_asset_depreciation_line(osv.osv):
                 'period_id': period_ids and period_ids[0] or False,
                 '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,
+                '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, {
@@ -411,16 +442,19 @@ class account_asset_depreciation_line(osv.osv):
                 'period_id': period_ids and period_ids[0] or False,
                 '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,
+                '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)
             created_move_ids.append(move_id)
-            if can_close:
-                asset_obj.write(cr, uid, [line.asset_id.id], {'state': 'close'}, context=context)
+            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()
@@ -428,7 +462,7 @@ account_asset_depreciation_line()
 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)]}),
 
     }
@@ -446,7 +480,7 @@ class account_asset_history(osv.osv):
                                   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'),
+        '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'),