[FIX] website_roller: remove useless on_change
[odoo/odoo.git] / addons / account_check_writing / account_voucher.py
1 # -*- coding: 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 osv,fields
23 from openerp.tools.translate import _
24 from openerp.tools.amount_to_text_en import amount_to_text
25 from lxml import etree
26
27 class account_voucher(osv.osv):
28     _inherit = 'account.voucher'
29
30     def _make_journal_search(self, cr, uid, ttype, context=None):
31         if context is None: 
32             context = {}
33         journal_pool = self.pool.get('account.journal')
34         if context.get('write_check',False) :
35             return journal_pool.search(cr, uid, [('allow_check_writing', '=', True)], limit=1)
36         return journal_pool.search(cr, uid, [('type', '=', ttype)], limit=1)
37
38     _columns = {
39         'amount_in_word' : fields.char("Amount in Word" , size=128, readonly=True, states={'draft':[('readonly',False)]}),
40         'allow_check' : fields.related('journal_id', 'allow_check_writing', type='boolean', string='Allow Check Writing'),
41         'number': fields.char('Number', size=32),
42     }
43
44     def onchange_amount(self, cr, uid, ids, amount, rate, partner_id, journal_id, currency_id, ttype, date, payment_rate_currency_id, company_id, context=None):
45         """ Inherited - add amount_in_word and allow_check_writting in returned value dictionary """
46         if not context:
47             context = {}
48         default = super(account_voucher, self).onchange_amount(cr, uid, ids, amount, rate, partner_id, journal_id, currency_id, ttype, date, payment_rate_currency_id, company_id, context=context)
49         if 'value' in default:
50             amount = 'amount' in default['value'] and default['value']['amount'] or amount
51
52             # Currency complete name is not available in res.currency model
53             # Exceptions done here (EUR, USD, BRL) cover 75% of cases
54             # For other currencies, display the currency code
55             currency = self.pool['res.currency'].browse(cr, uid, currency_id, context=context)
56             if currency.name.upper() == 'EUR':
57                 currency_name = 'Euro'
58             elif currency.name.upper() == 'USD':
59                 currency_name = 'Dollars'
60             elif currency.name.upper() == 'BRL':
61                 currency_name = 'reais'
62             else:
63                 currency_name = currency.name
64             #TODO : generic amount_to_text is not ready yet, otherwise language (and country) and currency can be passed
65             #amount_in_word = amount_to_text(amount, context=context)
66             amount_in_word = amount_to_text(amount, currency=currency_name)
67             default['value'].update({'amount_in_word':amount_in_word})
68             if journal_id:
69                 allow_check_writing = self.pool.get('account.journal').browse(cr, uid, journal_id, context=context).allow_check_writing
70                 default['value'].update({'allow_check':allow_check_writing})
71         return default
72
73     def print_check(self, cr, uid, ids, context=None):
74         if not ids:
75             return  {}
76
77         check_layout_report = {
78             'top' : 'account.print.check.top',
79             'middle' : 'account.print.check.middle',
80             'bottom' : 'account.print.check.bottom',
81         }
82
83         check_layout = self.browse(cr, uid, ids[0], context=context).company_id.check_layout
84         return {
85             'type': 'ir.actions.report.xml', 
86             'report_name':check_layout_report[check_layout],
87             'datas': {
88                     'model':'account.voucher',
89                     'id': ids and ids[0] or False,
90                     'ids': ids and ids or [],
91                     'report_type': 'pdf'
92                 },
93             'nodestroy': True
94             }
95
96     def fields_view_get(self, cr, uid, view_id=None, view_type=False, context=None, toolbar=False, submenu=False):
97         """
98             Add domain 'allow_check_writting = True' on journal_id field and remove 'widget = selection' on the same
99             field because the dynamic domain is not allowed on such widget
100         """
101         if not context: context = {}
102         res = super(account_voucher, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
103         doc = etree.XML(res['arch'])
104         nodes = doc.xpath("//field[@name='journal_id']")
105         if context.get('write_check', False) :
106             for node in nodes:
107                 node.set('domain', "[('type', '=', 'bank'), ('allow_check_writing','=',True)]")
108                 node.set('widget', '')
109             res['arch'] = etree.tostring(doc)
110         return res
111