d915a1cd34bb72c03a4b01a80f7b45fe92a06e4f
[odoo/odoo.git] / addons / account / wizard / account_change_currency.py
1 #!/usr/bin/env python
2 # -*- encoding: utf-8 -*-
3 ##############################################################################
4 #
5 #    OpenERP, Open Source Management Solution
6 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU Affero General Public License as
10 #    published by the Free Software Foundation, either version 3 of the
11 #    License, or (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU Affero General Public License for more details.
17 #
18 #    You should have received a copy of the GNU Affero General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 from osv import osv, fields
24 from tools.translate import _
25
26 class account_change_currency(osv.osv_memory):
27     _name = 'account.change.currency'
28     _description = 'Change Currency'
29     _columns = {
30        'currency_id': fields.many2one('res.currency', 'Change to', required=True, help="Select a currency to apply on the invoice"),
31     }
32
33     def view_init(self, cr , uid , fields_list, context=None):
34         obj_inv = self.pool.get('account.invoice')
35         if context is None:
36             context = {}
37         if context.get('active_id',False):
38             if obj_inv.browse(cr, uid, context['active_id']).state != 'draft':
39                 raise osv.except_osv(_('Error'), _('You can only change currency for Draft Invoice !'))
40             pass
41
42     def change_currency(self, cr, uid, ids, context=None):
43         obj_inv = self.pool.get('account.invoice')
44         obj_inv_line = self.pool.get('account.invoice.line')
45         obj_currency = self.pool.get('res.currency')
46         if context is None:
47             context = {}
48         data = self.read(cr, uid, ids)[0]
49         new_currency = data['currency_id']
50
51         invoice = obj_inv.browse(cr, uid, context['active_id'], context=context)
52         if invoice.currency_id.id == new_currency:
53             return {}
54         rate = obj_currency.browse(cr, uid, new_currency, context=context).rate
55         for line in invoice.invoice_line:
56             new_price = 0
57             if invoice.company_id.currency_id.id == invoice.currency_id.id:
58                 new_price = line.price_unit * rate
59                 if new_price <= 0:
60                     raise osv.except_osv(_('Error'), _('New currency is not confirured properly !'))
61
62             if invoice.company_id.currency_id.id != invoice.currency_id.id and invoice.company_id.currency_id.id == new_currency:
63                 old_rate = invoice.currency_id.rate
64                 if old_rate <= 0:
65                     raise osv.except_osv(_('Error'), _('Currnt currency is not confirured properly !'))
66                 new_price = line.price_unit / old_rate
67
68             if invoice.company_id.currency_id.id != invoice.currency_id.id and invoice.company_id.currency_id.id != new_currency:
69                 old_rate = invoice.currency_id.rate
70                 if old_rate <= 0:
71                     raise osv.except_osv(_('Error'), _('Current currency is not confirured properly !'))
72                 new_price = (line.price_unit / old_rate ) * rate
73             obj_inv_line.write(cr, uid, [line.id], {'price_unit': new_price})
74         obj_inv.write(cr, uid, [invoice.id], {'currency_id': new_currency}, context=context)
75         return {'type': 'ir.actions.act_window_close'}
76
77 account_change_currency()
78
79 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: