/
account_move_change_rate.py
45 lines (38 loc) · 1.66 KB
/
account_move_change_rate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
##############################################################################
# For copyright and license notices, see __manifest__.py file in module root
# directory
##############################################################################
from odoo import fields, models, api, _
from odoo.exceptions import ValidationError
class AccountMoveChangeRate(models.TransientModel):
_name = 'account.move.change.rate'
_description = 'account.move.change.rate'
@api.model
def get_move(self):
move = self.env['account.move'].browse(
self._context.get('active_id', False))
return move
currency_rate = fields.Float(
'Currency Rate',
required=True,
digits=(16, 6),
help="Select a rate to apply on the invoice"
)
move_id = fields.Many2one(
'account.move',
default=get_move
)
@api.constrains('currency_rate')
def _prevent_change_currency(self):
"""prevent currency change when the invoice is not draft
"""
if self.move_id.state != 'draft':
raise ValidationError(_('This invoice is not draft, reset it to draft to change currency rate'))
@api.onchange('move_id')
def _onchange_move(self):
self.currency_rate = self.move_id.l10n_ar_currency_rate or self.move_id.computed_currency_rate
def confirm(self):
message = _("Currency rate changed from %s to %s") % (self.move_id.l10n_ar_currency_rate or self.move_id.computed_currency_rate, self.currency_rate)
self.move_id.message_post(body=message)
self.move_id.l10n_ar_currency_rate = self.currency_rate
return {'type': 'ir.actions.act_window_close'}