1 # -*- coding: 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 osv import osv,fields
23 from tools.translate import _
24 from tools.amount_to_text_en import amount_to_text
25 from lxml import etree
27 class account_voucher(osv.osv):
28 _inherit = 'account.voucher'
30 def _make_journal_search(self, cr, uid, ttype, context=None):
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)
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),
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 """
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
52 #TODO : generic amount_to_text is not ready yet, otherwise language (and country) and currency can be passed
53 #amount_in_word = amount_to_text(amount, context=context)
54 amount_in_word = amount_to_text(amount)
55 default['value'].update({'amount_in_word':amount_in_word})
57 allow_check_writing = self.pool.get('account.journal').browse(cr, uid, journal_id, context=context).allow_check_writing
58 default['value'].update({'allow_check':allow_check_writing})
61 def print_check(self, cr, uid, ids, context=None):
65 check_layout_report = {
66 'top' : 'account.print.check.top',
67 'middle' : 'account.print.check.middle',
68 'bottom' : 'account.print.check.bottom',
71 check_layout = self.browse(cr, uid, ids[0], context=context).company_id.check_layout
73 'type': 'ir.actions.report.xml',
74 'report_name':check_layout_report[check_layout],
76 'model':'account.voucher',
77 'id': ids and ids[0] or False,
78 'ids': ids and ids or [],
84 def fields_view_get(self, cr, uid, view_id=None, view_type=False, context=None, toolbar=False, submenu=False):
86 Add domain 'allow_check_writting = True' on journal_id field and remove 'widget = selection' on the same
87 field because the dynamic domain is not allowed on such widget
89 if not context: context = {}
90 res = super(account_voucher, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
91 doc = etree.XML(res['arch'])
92 nodes = doc.xpath("//field[@name='journal_id']")
93 if context.get('write_check', False) :
95 node.set('domain', "[('type', '=', 'bank'), ('allow_check_writing','=',True)]")
96 node.set('widget', '')
97 res['arch'] = etree.tostring(doc)