2 # -*- encoding: utf-8 -*-
3 ##############################################################################
5 # OpenERP, Open Source Management Solution
6 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
21 ##############################################################################
23 from osv import osv, fields
24 from tools.translate import _
26 class account_change_currency(osv.osv_memory):
27 _name = 'account.change.currency'
28 _description = 'Change Currency'
30 'currency_id': fields.many2one('res.currency', 'Change to', required=True, help="Select a currency to apply on the invoice"),
33 def view_init(self, cr , uid , fields_list, context=None):
34 obj_inv = self.pool.get('account.invoice')
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 !'))
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')
48 data = self.read(cr, uid, ids)[0]
49 new_currency = data['currency_id']
51 invoice = obj_inv.browse(cr, uid, context['active_id'], context=context)
52 if invoice.currency_id.id == new_currency:
54 rate = obj_currency.browse(cr, uid, new_currency, context=context).rate
55 for line in invoice.invoice_line:
57 if invoice.company_id.currency_id.id == invoice.currency_id.id:
58 new_price = line.price_unit * rate
60 raise osv.except_osv(_('Error'), _('New currency is not confirured properly !'))
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
65 raise osv.except_osv(_('Error'), _('Currnt currency is not confirured properly !'))
66 new_price = line.price_unit / old_rate
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
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'}
77 account_change_currency()
79 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: