[REL] OpenERP 6.1.1
[odoo/odoo.git] / addons / account_asset / wizard / account_asset_change_duration.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
19 #
20 ##############################################################################
21 import time
22 from lxml import etree
23
24 from osv import osv, fields
25
26 class asset_modify(osv.osv_memory):
27     _name = 'asset.modify'
28     _description = 'Modify Asset'
29
30     _columns = {
31         'name': fields.char('Reason', size=64, required=True),
32         'method_number': fields.integer('Number of Depreciations', required=True),
33         'method_period': fields.integer('Period Length'),
34         'method_end': fields.date('Ending date'),
35         'note': fields.text('Notes'),
36     }
37
38     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
39         """ Returns views and fields for current model.
40         @param cr: A database cursor
41         @param user: ID of the user currently logged in
42         @param view_id: list of fields, which required to read signatures
43         @param view_type: defines a view type. it can be one of (form, tree, graph, calender, gantt, search, mdx)
44         @param context: context arguments, like lang, time zone
45         @param toolbar: contains a list of reports, wizards, and links related to current model
46
47         @return: Returns a dictionary that contains definition for fields, views, and toolbars
48         """
49         if not context:
50             context = {}
51         asset_obj = self.pool.get('account.asset.asset')
52         result = super(asset_modify, self).fields_view_get(cr, uid, view_id, view_type, context=context, toolbar=toolbar, submenu=submenu)
53         asset_id = context.get('active_id', False)
54         active_model = context.get('active_model', '')
55         if active_model == 'account.asset.asset' and asset_id:
56             asset = asset_obj.browse(cr, uid, asset_id, context=context)
57             doc = etree.XML(result['arch'])
58             if asset.method_time == 'number':
59                 node = doc.xpath("//field[@name='method_end']")[0]
60                 node.set('invisible', '1')
61             elif asset.method_time == 'end':
62                 node = doc.xpath("//field[@name='method_number']")[0]
63                 node.set('invisible', '1')
64             result['arch'] = etree.tostring(doc)
65         return result
66
67     def default_get(self, cr, uid, fields, context=None):
68         """ To get default values for the object.
69         @param self: The object pointer.
70         @param cr: A database cursor
71         @param uid: ID of the user currently logged in
72         @param fields: List of fields for which we want default values 
73         @param context: A standard dictionary 
74         @return: A dictionary which of fields with values. 
75         """ 
76         if not context:
77             context = {}
78         asset_obj = self.pool.get('account.asset.asset')
79         res = super(asset_modify, self).default_get(cr, uid, fields, context=context)
80         asset_id = context.get('active_id', False)
81         asset = asset_obj.browse(cr, uid, asset_id, context=context)
82         if 'name' in fields:
83             res.update({'name': asset.name})
84         if 'method_number' in fields and asset.method_time == 'number':
85             res.update({'method_number': asset.method_number})
86         if 'method_period' in fields:
87             res.update({'method_period': asset.method_period})
88         if 'method_end' in fields and asset.method_time == 'end':
89             res.update({'method_end': asset.method_end})
90         return res
91     
92     def modify(self, cr, uid, ids, context=None):
93         """ Modifies the duration of asset for calculating depreciation
94         and maintains the history of old values.
95         @param self: The object pointer.
96         @param cr: A database cursor
97         @param uid: ID of the user currently logged in
98         @param ids: List of Ids 
99         @param context: A standard dictionary 
100         @return: Close the wizard. 
101         """ 
102         if not context:
103             context = {}
104         asset_obj = self.pool.get('account.asset.asset')
105         history_obj = self.pool.get('account.asset.history')
106         asset_id = context.get('active_id', False)
107         asset = asset_obj.browse(cr, uid, asset_id, context=context)
108         data = self.browse(cr, uid, ids[0], context=context)
109         history_vals = {
110             'asset_id': asset_id,
111             'name': data.name,
112             'method_time': asset.method_time,
113             'method_number': asset.method_number,
114             'method_period': asset.method_period,
115             'method_end': asset.method_end,
116             'user_id': uid,
117             'date': time.strftime('%Y-%m-%d'),
118             'note': data.note,
119         }
120         history_obj.create(cr, uid, history_vals, context=context)
121         asset_vals = {
122             'method_number': data.method_number,
123             'method_period': data.method_period,
124             'method_end': data.method_end,
125         }
126         asset_obj.write(cr, uid, [asset_id], asset_vals, context=context)
127         asset_obj.compute_depreciation_board(cr, uid, [asset_id], context=context)
128         return {'type': 'ir.actions.act_window_close'}
129
130 asset_modify()
131
132 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: