[MERGE] Forward-port of latest 7.0 bugfixes, up to rev. 9929 revid:dle@openerp.com...
[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 _amount_to_text(self, cr, uid, amount, currency_id, context=None):
45         # Currency complete name is not available in res.currency model
46         # Exceptions done here (EUR, USD, BRL) cover 75% of cases
47         # For other currencies, display the currency code
48         currency = self.pool['res.currency'].browse(cr, uid, currency_id, context=context)
49         if currency.name.upper() == 'EUR':
50             currency_name = 'Euro'
51         elif currency.name.upper() == 'USD':
52             currency_name = 'Dollars'
53         elif currency.name.upper() == 'BRL':
54             currency_name = 'reais'
55         else:
56             currency_name = currency.name
57         #TODO : generic amount_to_text is not ready yet, otherwise language (and country) and currency can be passed
58         #amount_in_word = amount_to_text(amount, context=context)
59         return amount_to_text(amount, currency=currency_name)
60
61     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):
62         """ Inherited - add amount_in_word and allow_check_writting in returned value dictionary """
63         if not context:
64             context = {}
65         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)
66         if 'value' in default:
67             amount = 'amount' in default['value'] and default['value']['amount'] or amount
68             amount_in_word = self._amount_to_text(cr, uid, amount, currency_id, context=context)
69             default['value'].update({'amount_in_word':amount_in_word})
70             if journal_id:
71                 allow_check_writing = self.pool.get('account.journal').browse(cr, uid, journal_id, context=context).allow_check_writing
72                 default['value'].update({'allow_check':allow_check_writing})
73         return default
74
75     def print_check(self, cr, uid, ids, context=None):
76         if not ids:
77             return  {}
78
79         check_layout_report = {
80             'top' : 'account.print.check.top',
81             'middle' : 'account.print.check.middle',
82             'bottom' : 'account.print.check.bottom',
83         }
84
85         check_layout = self.browse(cr, uid, ids[0], context=context).company_id.check_layout
86         return {
87             'type': 'ir.actions.report.xml', 
88             'report_name':check_layout_report[check_layout],
89             'datas': {
90                     'model':'account.voucher',
91                     'id': ids and ids[0] or False,
92                     'ids': ids and ids or [],
93                     'report_type': 'pdf'
94                 },
95             'nodestroy': True
96             }
97     def create(self, cr, uid, vals, context=None):
98         if vals.get('amount') and vals.get('journal_id') and 'amount_in_word' not in vals:
99             vals['amount_in_word'] = self._amount_to_text(cr, uid, vals['amount'], vals.get('currency_id') or \
100                 self.pool['account.journal'].browse(cr, uid, vals['journal_id'], context=context).currency.id or \
101                 self.pool['res.company'].browse(cr, uid, vals['company_id']).currency_id.id, context=context)
102         return super(account_voucher, self).create(cr, uid, vals, context=context)
103
104     def write(self, cr, uid, ids, vals, context=None):
105         if vals.get('amount') and vals.get('journal_id') and 'amount_in_word' not in vals:
106             vals['amount_in_word'] = self._amount_to_text(cr, uid, vals['amount'], vals.get('currency_id') or \
107                 self.pool['account.journal'].browse(cr, uid, vals['journal_id'], context=context).currency.id or \
108                 self.pool['res.company'].browse(cr, uid, vals['company_id']).currency_id.id, context=context)
109         return super(account_voucher, self).write(cr, uid, ids, vals, context=context)
110
111     def fields_view_get(self, cr, uid, view_id=None, view_type=False, context=None, toolbar=False, submenu=False):
112         """
113             Add domain 'allow_check_writting = True' on journal_id field and remove 'widget = selection' on the same
114             field because the dynamic domain is not allowed on such widget
115         """
116         if not context: context = {}
117         res = super(account_voucher, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
118         doc = etree.XML(res['arch'])
119         nodes = doc.xpath("//field[@name='journal_id']")
120         if context.get('write_check', False) :
121             for node in nodes:
122                 node.set('domain', "[('type', '=', 'bank'), ('allow_check_writing','=',True)]")
123                 node.set('widget', '')
124             res['arch'] = etree.tostring(doc)
125         return res
126