[FIX] account_asset: Changed order by id for history changes. Improved indentations...
[odoo/odoo.git] / addons / account_asset / account_asset.py
index e4a0431..3484f6e 100644 (file)
@@ -1,6 +1,6 @@
 # -*- encoding: utf-8 -*-
 ##############################################################################
-#    
+#
 #    OpenERP, Open Source Management Solution
 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
 #
 #    GNU Affero General Public License for more details.
 #
 #    You should have received a copy of the GNU Affero General Public License
-#    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
 
-from osv import osv, fields
 import time
 from datetime import datetime
+from dateutil.relativedelta import relativedelta
+
+from osv import osv, fields
+import decimal_precision as dp
 
 class account_asset_category(osv.osv):
     _name = 'account.asset.category'
     _description = 'Asset category'
+
     _columns = {
-        'name': fields.char('Asset category', size=64, required=True, select=1),
+        'name': fields.char('Name', size=64, required=True, select=1),
         'note': fields.text('Note'),
-        'journal_analytic_id': fields.many2one('account.analytic.journal', 'Analytic journal'), #FIXME:add in the form view  with group = analytic 
-        'account_analytic_id': fields.many2one('account.analytic.account', 'Analytic account'), #FIXME:add in the form view  with group = analytic
+        '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',),#FIXME: required=True + add in the form view 
+        'account_expense_depreciation_id': fields.many2one('account.account', 'Depr. Expense Account', required=True),
         'journal_id': fields.many2one('account.journal', 'Journal', required=True),
-        'company_id': fields.many2one('res.company', 'Company'),
+        '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 Depreciation'),
+        '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_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."),
+    }
+
+    _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_period': 12,
+        'method_progress_factor': 0.3,
     }
+
+    def onchange_account_asset(self, cr, uid, ids, account_asset_id, context=None):
+        res = {'value':{}}
+        if account_asset_id:
+           res['value'] = {'account_depreciation_id': account_asset_id}
+        return res
+
 account_asset_category()
 
 #class one2many_mod_asset(fields.one2many):
@@ -52,7 +79,7 @@ account_asset_category()
 #        #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 
+#            #write values on asset
 #            obj.pool.get(self._obj).write(cr, user, key, {'depreciation_line_ids': [6,0,value]})
 #        return depreciation_line_ids
 
@@ -76,72 +103,82 @@ class account_asset_asset(osv.osv):
             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)
-            WHERE a.id IN %s 
+            WHERE a.id IN %s
             GROUP BY a.id, a.purchase_date """, (tuple(ids),))
         return dict(cr.fetchall())
 
     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):
-            old_depreciation_line_ids = depreciation_lin_obj.search(cr, uid, [('asset_id', '=', asset.id), ('move_line_id', '=', False)])
+            if asset.value_residual == 0.0:
+                continue
+            posted_depreciation_line_ids = depreciation_lin_obj.search(cr, uid, [('asset_id', '=', asset.id), ('move_check', '=', True)])
+            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
 
-            undone_dotation_number = asset.method_delay - len(asset.account_move_line_ids)
-            residual_amount = asset.value_residual
             depreciation_date = datetime.strptime(self._get_last_depreciation_date(cr, uid, [asset.id], context)[asset.id], '%Y-%m-%d')
             day = depreciation_date.day
             month = depreciation_date.month
             year = depreciation_date.year
-            for i in range(1,undone_dotation_number+1):
-                if i == undone_dotation_number + 1:
+            total_days = (year % 4) and 365 or 366
+            undone_dotation_number = asset.method_delay
+            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 += 1
+            for x in range(len(posted_depreciation_line_ids), undone_dotation_number):
+                i = x + 1
+                if i == undone_dotation_number:
                     amount = residual_amount
                 else:
                     if asset.method == 'linear':
-                        amount = asset.purchase_value / undone_dotation_number
+                        amount = amount_to_depr / (undone_dotation_number - len(posted_depreciation_line_ids))
+                        if asset.prorata:
+                            amount = amount_to_depr / asset.method_delay
+                            if i == 1:
+                                days = total_days - float(depreciation_date.strftime('%j'))
+                                amount = (amount_to_depr / asset.method_delay) / total_days * days
+                            elif i == undone_dotation_number:
+                                amount = (amount_to_depr / asset.method_delay) / total_days * (total_days - days)
                     else:
                         amount = residual_amount * asset.method_progress_factor
                 residual_amount -= amount
                 vals = {
-                     'amount': amount, 
-                     'asset_id': asset.id, 
-                     'sequence':i, 
-                     'name': str(asset.id) +'/'+ str(i),
+                     'amount': amount,
+                     'asset_id': asset.id,
+                     'sequence': i,
+                     'name': str(asset.id) +'/' + str(i),
                      'remaining_value': residual_amount,
-                     'depreciated_value': asset.purchase_value - residual_amount,
+                     'depreciated_value': (asset.purchase_value - asset.salvage_value) - (residual_amount + amount),
                      'depreciation_date': depreciation_date.strftime('%Y-%m-%d'),
-                } 
-                self.pool.get('account.asset.depreciation.line').create(cr, uid, vals)
-                month += asset.method_period
-                depreciation_date = datetime(year + (month / 12), month % 12, day)
+                }
+                depreciation_lin_obj.create(cr, uid, vals, context=context)
+                # 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
         return True
 
     def validate(self, cr, uid, ids, context={}):
         return self.write(cr, uid, ids, {
-            'state':'normal'
+            'state':'open'
         }, context)
 
-    def _amount_total(self, cr, uid, ids, name, args, context={}):
-        #FIXME: function not working²
-        id_set=",".join(map(str,ids))
-        cr.execute("""SELECT l.asset_id,abs(SUM(l.debit-l.credit)) AS amount FROM 
-                account_move_line l
-            WHERE l.asset_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 _amount_residual(self, cr, uid, ids, name, args, context={}):
-        cr.execute("""SELECT 
-                l.asset_id as id, SUM(abs(l.debit-l.credit)) AS amount
+    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
             FROM
                 account_move_line l
             WHERE
                 l.asset_id IN %s GROUP BY l.asset_id """, (tuple(ids),))
         res=dict(cr.fetchall())
         for asset in self.browse(cr, uid, ids, context):
-            res[asset.id] = asset.purchase_value - res.get(asset.id, 0.0)
+            res[asset.id] = asset.purchase_value - res.get(asset.id, 0.0) - asset.salvage_value
         for id in ids:
             res.setdefault(id, 0.0)
         return res
@@ -160,22 +197,22 @@ class account_asset_asset(osv.osv):
         'localisation': fields.char('Localisation', size=32, select=2),
         'parent_id': fields.many2one('account.asset.asset', 'Parent Asset'),
         'child_ids': fields.one2many('account.asset.asset', 'parent_id', 'Children Assets'),
-        'purchase_date': fields.date('Purchase Date', required=True), 
-        'state': fields.selection([('view','View'),('draft','Draft'),('normal','Normal'),('close','Close')], 'state', required=True),
+        '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'),('progressif','Progressive')], 'Computation method', required=True, readonly=True, states={'draft':[('readonly',False)]}),
-        'method_delay': fields.integer('During (interval)', readonly=True, states={'draft':[('readonly',False)]}),
-        'method_period': fields.integer('Depre. all (period)', readonly=True, states={'draft':[('readonly',False)]}),
-        'method_end': fields.date('Ending date'),
-        'value_total': fields.function(_amount_total, method=True, digits=(16,2),string='Gross Value'),
+        'method': fields.selection([('linear','Linear'),('progressif','Progressive')], 'Computation Method', required=True, readonly=True, states={'draft':[('readonly',False)]}, help="Linear: Calculated on basis of Gross Value/During (interval) \
+         \nProgressive: Calculated on basis of Gross Value * Progressif Factor"),
+        'method_delay': fields.integer('Number of Depreciation', 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"),
+        '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)]}),
-        'value_residual': fields.function(_amount_residual, method=True, digits=(16,2), string='Residual Value'),
-        'method_time': fields.selection([('delay','Delay'),('end','Ending Period')], 'Time Method', required=True, 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'),
         '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', 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."),
     }
     _defaults = {
         'code': lambda obj, cr, uid, context: obj.pool.get('ir.sequence').get(cr, uid, 'account.asset.code'),
@@ -189,8 +226,48 @@ class account_asset_asset(osv.osv):
         'method_period': lambda obj, cr, uid, context: 12,
         'method_progress_factor': lambda obj, cr, uid, context: 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_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'):
+                return False
+        return True
+
+    _constraints = [
+        (_check_prorata, '\nProrata temporis can be applied only for computation method linear and time method delay.', ['prorata']),
+    ]
+
+    def onchange_category_id(self, cr, uid, ids, category_id, context=None):
+        res = {'value':{}}
+        asset_categ_obj = self.pool.get('account.asset.category')
+        if category_id:
+            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_time': category_obj.method_time,
+                            'method_period': category_obj.method_period,
+                            'method_progress_factor': category_obj.method_progress_factor,
+                            'method_end': category_obj.method_end,
+                            'prorata': category_obj.prorata,
+            }
+        return res
+
+    def onchange_method_time(self, cr, uid, ids, method='linear', method_time='delay', context=None):
+        res = {'value': {}}
+        if method != 'linear' or method_time != 'delay':
+            res['value'] = {'prorata': False}
+        return res
+
+    def copy(self, cr, uid, id, default=None, context=None):
+        if default is None:
+            default = {}
+        if context is None:
+            context = {}
+        default.update({'depreciation_line_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:
@@ -264,7 +341,7 @@ class account_asset_asset(osv.osv):
         return result
 
     def _compute_entries(self, cr, uid, asset, period_id, context={}):
-        #FIXME: function not working CHECK all res 
+        #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:
@@ -282,7 +359,7 @@ class account_asset_depreciation_line(osv.osv):
     def _get_move_check(self, cr, uid, ids, name, args, context=None):
         res = {}
         for line in self.browse(cr, uid, ids, context=context):
-            res[line.id] = line.move_id and True or False
+            res[line.id] = bool(line.move_id)
         return res
 
     _columns = {
@@ -293,16 +370,73 @@ class account_asset_depreciation_line(osv.osv):
         'remaining_value': fields.float('Amount to Depreciate', required=True),
         'depreciated_value': fields.float('Amount Already Depreciated', required=True),
         'depreciation_date': fields.char('Depreciation Date', size=64, select=1),
-        'move_line_id': fields.many2one('account.move.line', 'Depreciation Entry'),
-        'move_check': fields.function(_get_move_check, method=True, type='boolean', string='Move Included', store=True)
+        '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):
+        if context is None:
+            context = {}
+        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')
+        currency_obj = self.pool.get('res.currency')
+        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)
+            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
+            move_vals = {
+                'name': line.name,
+                'date': depreciation_date,
+                'ref': 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)
+            move_line_obj.create(cr, uid, {
+                'name': line.name,
+                'ref': line.name,
+                '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,
+                'date': depreciation_date,
+            })
+            move_line_obj.create(cr, uid, {
+                'name': line.name,
+                'ref': line.name,
+                '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,
+                '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
+
 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 
+#        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)
@@ -372,7 +506,7 @@ class account_move_line(osv.osv):
     _inherit = 'account.move.line'
     _columns = {
         'asset_id': fields.many2one('account.asset.asset', 'Asset'),
-        'entry_ids': fields.one2many('account.move.line', 'asset_id', 'Entries', readonly=True, states={'draft':[('readonly',False)]}),    
+        'entry_ids': fields.one2many('account.move.line', 'asset_id', 'Entries', readonly=True, states={'draft':[('readonly',False)]}),
 
     }
 account_move_line()
@@ -390,41 +524,12 @@ class account_asset_history(osv.osv):
         'method_end': fields.date('Ending date'),
         'note': fields.text('Note'),
     }
+    _order = 'id desc'
     _defaults = {
         'date': lambda *args: time.strftime('%Y-%m-%d'),
         'user_id': lambda self,cr, uid,ctx: uid
     }
+    
 account_asset_history()
 
-class account_asset_board(osv.osv):
-    _name = 'account.asset.board'
-    _description = 'Asset board'
-    _columns = {
-        'name': fields.char('Asset name', size=64, required=True, select=1),
-        'asset_id': fields.many2one('account.asset.asset', 'Asset', required=True, select=1),
-        'value_gross': fields.float('Gross value', required=True, select=1),
-        'value_asset': fields.float('Asset Value', required=True, select=1),
-        'value_asset_cumul': fields.float('Cumul. value', required=True, select=1),
-        'value_net': fields.float('Net value', required=True, select=1),
-
-    }
-    _auto = False
-    def init(self, cr):
-        cr.execute("""
-            create or replace view account_asset_board as (
-                select
-                    min(l.id) as id,
-                    min(l.id) as asset_id,
-                    0.0 as value_gross,
-                    0.0 as value_asset,
-                    0.0 as value_asset_cumul,
-                    0.0 as value_net
-                from
-                    account_move_line l
-                where
-                    l.state <> 'draft' and
-                    l.asset_id=3
-            )""")
-account_asset_board()
-
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: