[NEW] Account Check Writing-1
[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 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
26
27 check_layout_report = {
28     'top' : 'account.print.check.top',
29     'middle' : 'account.print.check.middle',
30     'bottom' : 'account.print.check.bottom',
31 }
32
33 class account_voucher(osv.osv):
34     _inherit = 'account.voucher'
35
36     def _get_journal(self, cr, uid, context=None):
37     
38         if context is None: context = {}
39         
40         journal_pool = self.pool.get('account.journal')
41         invoice_pool = self.pool.get('account.invoice')
42         
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
47         
48         if context.get('journal_id', False):
49             return context.get('journal_id')
50             
51         if not context.get('journal_id', False) and context.get('search_default_journal_id', False):
52             return context.get('search_default_journal_id')
53
54         ttype = context.get('type', 'bank')
55
56         if ttype in ('payment', 'receipt'):
57             ttype = 'bank'
58         if context.get('write_check',False) :           
59             res = journal_pool.search(cr, uid, [('allow_check_writing', '=', True)], limit=1)
60         else :
61             res = journal_pool.search(cr, uid, [('type', '=', ttype)], limit=1)
62         return res and res[0] or False
63
64     _columns = {    
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),
68     }
69     
70     _defaults = {
71         'journal_id':_get_journal,
72         }
73
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 """
76         
77         if not context:
78             context = {}
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
82
83             #TODO : generic amount_to_text is not ready yet, otherwise language and currency can be passed
84             
85             amount_in_word = amount_to_text(amount)
86             default['value'].update({'amount_in_word':amount_in_word})
87             
88             if journal_id:
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})
91                 
92         return default
93
94     def print_check(self, cr, uid, ids, context=None):
95         if not ids: return []
96         
97         check_layout = self.browse(cr, uid, ids[0], context=context).company_id.check_layout
98         
99         return {
100             'type': 'ir.actions.report.xml', 
101             'report_name':check_layout_report[check_layout],
102             'datas': {
103                     'model':'account.voucher',
104                     'id': ids and ids[0] or False,
105                     'ids': ids and ids or [],
106                     'report_type': 'pdf'
107                 },
108             'nodestroy': True
109             }
110             
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) :
116             for node in nodes:
117                 node.set('domain', "[('type', '=', 'bank')]")
118             res['arch'] = etree.tostring(doc)
119         return res
120         
121 account_voucher()