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 check_layout_report = {
28 'top' : 'account.print.check.top',
29 'middle' : 'account.print.check.middle',
30 'bottom' : 'account.print.check.bottom',
33 class account_voucher(osv.osv):
34 _inherit = 'account.voucher'
36 def _get_journal(self, cr, uid, context=None):
38 if context is None: context = {}
40 journal_pool = self.pool.get('account.journal')
41 invoice_pool = self.pool.get('account.invoice')
43 if context.get('invoice_id', False):
44 currency_id = invoice_pool.browse(cr, uid, context['invoice_id'], context=context).currency_id.id
45 journal_id = journal_pool.search(cr, uid, [('currency', '=', currency_id)], limit=1)
46 return journal_id and journal_id[0] or False
48 if context.get('journal_id', False):
49 return context.get('journal_id')
51 if not context.get('journal_id', False) and context.get('search_default_journal_id', False):
52 return context.get('search_default_journal_id')
54 ttype = context.get('type', 'bank')
56 if ttype in ('payment', 'receipt'):
58 if context.get('write_check',False) :
59 res = journal_pool.search(cr, uid, [('allow_check_writing', '=', True)], limit=1)
61 res = journal_pool.search(cr, uid, [('type', '=', ttype)], limit=1)
62 return res and res[0] or False
65 'amount_in_word' : fields.char("Amount in word" , size=128, readonly=True, states={'draft':[('readonly',False)]}),
66 'allow_check' : fields.boolean('Allow Check Writing'),
67 'number': fields.char('Number', size=32),
71 'journal_id':_get_journal,
74 def onchange_partner_id(self, cr, uid, ids, partner_id, journal_id, price, currency_id, ttype, date, context=None):
75 """ Inherited - add amount_in_word in return value dictionary """
79 default = super(account_voucher, self).onchange_partner_id(cr, uid, ids, partner_id, journal_id, price, currency_id, ttype, date, context=context)
80 if 'value' in default:
81 amount = 'amount' in default['value'] and default['value']['amount'] or price
83 #TODO : generic amount_to_text is not ready yet, otherwise language and currency can be passed
85 amount_in_word = amount_to_text(amount)
86 default['value'].update({'amount_in_word':amount_in_word})
89 allow_check_writing = self.pool.get('account.journal').browse(cr, uid, journal_id).allow_check_writing
90 default['value'].update({'allow_check':allow_check_writing})
94 def print_check(self, cr, uid, ids, context=None):
97 check_layout = self.browse(cr, uid, ids[0], context=context).company_id.check_layout
100 'type': 'ir.actions.report.xml',
101 'report_name':check_layout_report[check_layout],
103 'model':'account.voucher',
104 'id': ids and ids[0] or False,
105 'ids': ids and ids or [],
111 def fields_view_get(self, cr, uid, view_id=None, view_type=False, context=None, toolbar=False, submenu=False):
112 res = super(account_voucher, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
113 doc = etree.XML(res['arch'])
114 nodes = doc.xpath("//field[@name='journal_id']")
115 if context.get('write_check', False) :
117 node.set('domain', "[('type', '=', 'bank')]")
118 res['arch'] = etree.tostring(doc)