[IMP] removed unused netsvc imports.
[odoo/odoo.git] / addons / account / wizard / account_change_currency.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 from openerp.tools.translate import _
24
25 class account_change_currency(osv.osv_memory):
26     _name = 'account.change.currency'
27     _description = 'Change Currency'
28     _columns = {
29        'currency_id': fields.many2one('res.currency', 'Change to', required=True, help="Select a currency to apply on the invoice"),
30     }
31
32     def view_init(self, cr , uid , fields_list, context=None):
33         obj_inv = self.pool.get('account.invoice')
34         if context is None:
35             context = {}
36         if context.get('active_id',False):
37             if obj_inv.browse(cr, uid, context['active_id']).state != 'draft':
38                 raise osv.except_osv(_('Error!'), _('You can only change currency for Draft Invoice.'))
39             pass
40
41     def change_currency(self, cr, uid, ids, context=None):
42         obj_inv = self.pool.get('account.invoice')
43         obj_inv_line = self.pool.get('account.invoice.line')
44         obj_currency = self.pool.get('res.currency')
45         if context is None:
46             context = {}
47         data = self.browse(cr, uid, ids, context=context)[0]
48         new_currency = data.currency_id.id
49
50         invoice = obj_inv.browse(cr, uid, context['active_id'], context=context)
51         if invoice.currency_id.id == new_currency:
52             return {}
53         rate = obj_currency.browse(cr, uid, new_currency, context=context).rate
54         for line in invoice.invoice_line:
55             new_price = 0
56             if invoice.company_id.currency_id.id == invoice.currency_id.id:
57                 new_price = line.price_unit * rate
58                 if new_price <= 0:
59                     raise osv.except_osv(_('Error!'), _('New currency is not configured properly.'))
60
61             if invoice.company_id.currency_id.id != invoice.currency_id.id and invoice.company_id.currency_id.id == new_currency:
62                 old_rate = invoice.currency_id.rate
63                 if old_rate <= 0:
64                     raise osv.except_osv(_('Error!'), _('Current currency is not configured properly.'))
65                 new_price = line.price_unit / old_rate
66
67             if invoice.company_id.currency_id.id != invoice.currency_id.id and invoice.company_id.currency_id.id != new_currency:
68                 old_rate = invoice.currency_id.rate
69                 if old_rate <= 0:
70                     raise osv.except_osv(_('Error!'), _('Current currency is not configured properly.'))
71                 new_price = (line.price_unit / old_rate ) * rate
72             obj_inv_line.write(cr, uid, [line.id], {'price_unit': new_price})
73         obj_inv.write(cr, uid, [invoice.id], {'currency_id': new_currency}, context=context)
74         return {'type': 'ir.actions.act_window_close'}
75
76 account_change_currency()
77
78 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: