[IMP] account_asset: usability changes for asset history
[odoo/odoo.git] / addons / account_asset / wizard / wizard_asset_compute.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 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
22 import wizard
23 import pooler
24 from tools.translate import _
25
26
27 asset_end_arch = '''<?xml version="1.0"?>
28 <form string="Compute assets">
29     <separator string="Generated entries" colspan="4"/>
30     <field name="move_ids" readonly="1" nolabel="1"/>
31 </form>'''
32
33 asset_end_fields = {
34     'move_ids': {'string':'Entries', 'type': 'one2many', 'relation':'account.move'},
35 }
36
37
38 asset_ask_form = '''<?xml version="1.0"?>
39 <form string="Compute assets">
40     <field name="period_id"/>
41 </form>'''
42
43 asset_ask_fields = {
44     'period_id': {'string': 'Period', 'type': 'many2one', 'relation':'account.period', 'required':True},
45 }
46
47 def _asset_compute(self, cr, uid, data, context):
48     pool = pooler.get_pool(cr.dbname)
49     ass_obj = pool.get('account.asset.asset')
50     ids = ass_obj.search(cr, uid, [('state','=','normal')], context=context)
51     ids_create = []
52     for asset in ass_obj.browse(cr, uid, ids, context):
53         ids_create += ass_obj._compute_entries(cr, uid, asset, data['form']['period_id'], context)
54     self.move_ids = ids_create
55     return {'move_ids': ids_create}
56
57 def _asset_open(self, cr, uid, data, context):
58     value = {
59         'name': 'Created moves',
60         'view_type': 'form',
61         'view_mode': 'tree,form',
62         'res_model': 'account.move',
63         'view_id': False,
64         'type': 'ir.actions.act_window'
65     }
66     if data['form']['move_ids']:
67         value['domain']= "[('id','in',["+','.join(map(str,self.move_ids))+"])]"
68     else:
69         value['domain']= "[('id','=', False)]"
70     return value
71
72 def _get_period(self, cr, uid, data, context={}):
73     pool = pooler.get_pool(cr.dbname)
74     ids = pool.get('account.period').find(cr, uid, context=context)
75     period_id = False
76     if len(ids):
77         period_id = ids[0]
78     return {'period_id': period_id}
79
80 class wizard_asset_compute(wizard.interface):
81     states = {
82         'init': {
83             'actions': [_get_period],
84             'result': {'type':'form', 'arch':asset_ask_form, 'fields':asset_ask_fields, 'state':[
85                 ('end','Cancel'),
86                 ('asset_compute','Compute assets')
87             ]}
88         },
89         'asset_compute': {
90             'actions': [_asset_compute],
91             'result': {'type' : 'form', 'arch': asset_end_arch, 'fields':asset_end_fields, 'state':[
92                 ('end','Close'),
93                 ('asset_open','Open entries')
94             ]}
95         },
96         'asset_open': {
97             'actions': [],
98             'result': {'type':'action', 'action': _asset_open,  'state':'end'}
99         }
100     }
101 wizard_asset_compute('account.asset.compute')
102
103
104 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
105