In the wizard "Post Journal Entries", we can now select multiple journals and multipl...
[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             #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})
56             if journal_id:
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})
59         return default
60
61     def print_check(self, cr, uid, ids, context=None):
62         if not ids:
63             return  {}
64
65         check_layout_report = {
66             'top' : 'account.print.check.top',
67             'middle' : 'account.print.check.middle',
68             'bottom' : 'account.print.check.bottom',
69         }
70
71         check_layout = self.browse(cr, uid, ids[0], context=context).company_id.check_layout
72         return {
73             'type': 'ir.actions.report.xml', 
74             'report_name':check_layout_report[check_layout],
75             'datas': {
76                     'model':'account.voucher',
77                     'id': ids and ids[0] or False,
78                     'ids': ids and ids or [],
79                     'report_type': 'pdf'
80                 },
81             'nodestroy': True
82             }
83
84     def fields_view_get(self, cr, uid, view_id=None, view_type=False, context=None, toolbar=False, submenu=False):
85         """
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
88         """
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) :
94             for node in nodes:
95                 node.set('domain', "[('type', '=', 'bank'), ('allow_check_writing','=',True)]")
96                 node.set('widget', '')
97             res['arch'] = etree.tostring(doc)
98         return res
99
100 account_voucher()