[fix] some problems in o2m
[odoo/odoo.git] / addons / account_asset / account_asset.py
index 142fc89..97aec1d 100644 (file)
@@ -1,4 +1,4 @@
-       # -*- encoding: utf-8 -*-
+# -*- encoding: utf-8 -*-
 ##############################################################################
 #
 #    OpenERP, Open Source Management Solution
@@ -112,18 +112,27 @@ class account_asset_asset(osv.osv):
                         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_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')
         for asset in self.browse(cr, uid, ids, context=context):
             if asset.value_residual == 0.0:
@@ -132,10 +141,14 @@ class account_asset_asset(osv.osv):
             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')
+                depreciation_date = datetime(purchase_date.year, 1, 1)
             day = depreciation_date.day
             month = depreciation_date.month
             year = depreciation_date.year
@@ -171,6 +184,9 @@ class account_asset_asset(osv.osv):
     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
@@ -185,10 +201,20 @@ class account_asset_asset(osv.osv):
             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}
+
     _columns = {
         '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, readonly=True, states={'draft':[('readonly',False)]}),
-        'code': fields.char('Reference ', size=16, 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)]}),
@@ -211,7 +237,7 @@ class account_asset_asset(osv.osv):
         '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'),
-        'method_time': fields.selection([('number','Number of Depreciations'),('end','Ending Date')], 'Time Method', required=True, readonly=True, states={'draft':[('readonly',False)]}, 
+        '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."),
@@ -233,19 +259,19 @@ class account_asset_asset(osv.osv):
         '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 != 'number'):
+            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, 'Prorata temporis can be applied only for computation method "linear" and time method "number of depreciations".', ['prorata']),
+        (_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):
@@ -264,9 +290,9 @@ class account_asset_asset(osv.osv):
             }
         return res
 
-    def onchange_method_time(self, cr, uid, ids, method='linear', method_time='number', context=None):
+    def onchange_method_time(self, cr, uid, ids, method_time='number', context=None):
         res = {'value': {}}
-        if method != 'linear' or method_time != 'number':
+        if method_time != 'number':
             res['value'] = {'prorata': False}
         return res
 
@@ -282,8 +308,8 @@ class account_asset_asset(osv.osv):
         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)], context=context)
+        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)
         return depreciation_obj.create_move(cr, uid, depreciation_ids, context=context)
 
     def create(self, cr, uid, vals, context=None):
@@ -336,16 +362,16 @@ class account_asset_depreciation_line(osv.osv):
             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
+            asset_name = line.asset_id.name
+            reference = line.name
             move_vals = {
-                'name': line.name,
+                'name': asset_name,
                 'date': depreciation_date,
-                'ref': line.name,
+                'ref': reference,
                 '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)
-            asset_name = line.asset_id.name
-            reference = line.name
             journal_id = line.asset_id.category_id.journal_id.id
             partner_id = line.asset_id.partner_id.id
             move_line_obj.create(cr, uid, {
@@ -381,7 +407,7 @@ class account_asset_depreciation_line(osv.osv):
             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_obj.write(cr, uid, [line.asset_id.id], {'state': 'close'}, context=context)
         return created_move_ids
 
 account_asset_depreciation_line()
@@ -403,8 +429,8 @@ class account_asset_history(osv.osv):
         '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([('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"\
+        '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'),
@@ -417,7 +443,7 @@ class account_asset_history(osv.osv):
         '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: