[REF] removed explicit model instanciations.
[odoo/odoo.git] / addons / account_asset / account_asset_invoice.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
22 from openerp.osv import fields, osv
23
24 class account_invoice(osv.osv):
25
26     _inherit = 'account.invoice'
27     def action_number(self, cr, uid, ids, *args):
28         result = super(account_invoice, self).action_number(cr, uid, ids, *args)
29         for inv in self.browse(cr, uid, ids):
30             self.pool.get('account.invoice.line').asset_create(cr, uid, inv.invoice_line)
31         return result
32
33     def line_get_convert(self, cr, uid, x, part, date, context=None):
34         res = super(account_invoice, self).line_get_convert(cr, uid, x, part, date, context=context)
35         res['asset_id'] = x.get('asset_id', False)
36         return res
37
38
39 class account_invoice_line(osv.osv):
40
41     _inherit = 'account.invoice.line'
42     _columns = {
43         'asset_category_id': fields.many2one('account.asset.category', 'Asset Category'),
44     }
45     def asset_create(self, cr, uid, lines, context=None):
46         context = context or {}
47         asset_obj = self.pool.get('account.asset.asset')
48         for line in lines:
49             if line.asset_category_id:
50                 vals = {
51                     'name': line.name,
52                     'code': line.invoice_id.number or False,
53                     'category_id': line.asset_category_id.id,
54                     'purchase_value': line.price_subtotal,
55                     'period_id': line.invoice_id.period_id.id,
56                     'partner_id': line.invoice_id.partner_id.id,
57                     'company_id': line.invoice_id.company_id.id,
58                     'currency_id': line.invoice_id.currency_id.id,
59                     'purchase_date' : line.invoice_id.date_invoice,
60                 }
61                 changed_vals = asset_obj.onchange_category_id(cr, uid, [], vals['category_id'], context=context)
62                 vals.update(changed_vals['value'])
63                 asset_id = asset_obj.create(cr, uid, vals, context=context)
64                 if line.asset_category_id.open_asset:
65                     asset_obj.validate(cr, uid, [asset_id], context=context)
66         return True
67
68
69 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: