1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from openerp.osv import fields, osv
23 from openerp.tools.translate import _
25 class account_change_currency(osv.osv_memory):
26 _name = 'account.change.currency'
27 _description = 'Change Currency'
29 'currency_id': fields.many2one('res.currency', 'Change to', required=True, help="Select a currency to apply on the invoice"),
32 def view_init(self, cr , uid , fields_list, context=None):
33 obj_inv = self.pool.get('account.invoice')
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.'))
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')
47 data = self.browse(cr, uid, ids, context=context)[0]
48 new_currency = data.currency_id.id
50 invoice = obj_inv.browse(cr, uid, context['active_id'], context=context)
51 if invoice.currency_id.id == new_currency:
53 rate = obj_currency.browse(cr, uid, new_currency, context=context).rate
54 for line in invoice.invoice_line:
56 if invoice.company_id.currency_id.id == invoice.currency_id.id:
57 new_price = line.price_unit * rate
59 raise osv.except_osv(_('Error!'), _('New currency is not configured properly.'))
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
64 raise osv.except_osv(_('Error!'), _('Current currency is not configured properly.'))
65 new_price = line.price_unit / old_rate
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
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'}
76 account_change_currency()
78 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: