[IMP] account_check_writing, review: code improvements and cleaning
authorQuentin (OpenERP) <qdp-launchpad@openerp.com>
Mon, 23 Jan 2012 12:44:29 +0000 (13:44 +0100)
committerQuentin (OpenERP) <qdp-launchpad@openerp.com>
Mon, 23 Jan 2012 12:44:29 +0000 (13:44 +0100)
bzr revid: qdp-launchpad@openerp.com-20120123124429-0656ahqedqe6f05q

1  2 
addons/account_check_writing/account_voucher.py
addons/account_check_writing/report/check_print.py
addons/account_voucher/account_voucher.py

index 0000000,5d1d080..ea0e7c7
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,121 +1,100 @@@
+ # -*- coding: utf-8 -*-
+ ##############################################################################
+ #
+ #    OpenERP, Open Source Management Solution
+ #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
+ #
+ #    This program is free software: you can redistribute it and/or modify
+ #    it under the terms of the GNU Affero General Public License as
+ #    published by the Free Software Foundation, either version 3 of the
+ #    License, or (at your option) any later version.
+ #
+ #    This program is distributed in the hope that it will be useful,
+ #    but WITHOUT ANY WARRANTY; without even the implied warranty of
+ #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ #    GNU Affero General Public License for more details.
+ #
+ #    You should have received a copy of the GNU Affero General Public License
+ #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ #
+ ##############################################################################
+ from osv import osv,fields
+ from tools.translate import _
+ from tools.amount_to_text_en import amount_to_text
+ from lxml import etree
 -check_layout_report = {
 -    'top' : 'account.print.check.top',
 -    'middle' : 'account.print.check.middle',
 -    'bottom' : 'account.print.check.bottom',
 -}
 -
+ class account_voucher(osv.osv):
+     _inherit = 'account.voucher'
 -    def _get_journal(self, cr, uid, context=None):
 -    
 -        if context is None: context = {}
 -        
++    def _make_journal_search(self, cr, uid, ttype, context=None):
++        if context is None: 
++            context = {}
+         journal_pool = self.pool.get('account.journal')
 -        invoice_pool = self.pool.get('account.invoice')
 -        
 -        if context.get('invoice_id', False):
 -            currency_id = invoice_pool.browse(cr, uid, context['invoice_id'], context=context).currency_id.id
 -            journal_id = journal_pool.search(cr, uid, [('currency', '=', currency_id)], limit=1)
 -            return journal_id and journal_id[0] or False
 -        
 -        if context.get('journal_id', False):
 -            return context.get('journal_id')
 -            
 -        if not context.get('journal_id', False) and context.get('search_default_journal_id', False):
 -            return context.get('search_default_journal_id')
 -
 -        ttype = context.get('type', 'bank')
++        if context.get('write_check',False) :
++            return journal_pool.search(cr, uid, [('allow_check_writing', '=', True)], limit=1)
++        return journal_pool.search(cr, uid, [('type', '=', ttype)], limit=1)
 -        if ttype in ('payment', 'receipt'):
 -            ttype = 'bank'
 -        if context.get('write_check',False) :           
 -            res = journal_pool.search(cr, uid, [('allow_check_writing', '=', True)], limit=1)
 -        else :
 -            res = journal_pool.search(cr, uid, [('type', '=', ttype)], limit=1)
 -        return res and res[0] or False
 -
 -    _columns = {    
 -        'amount_in_word' : fields.char("Amount in word" , size=128, readonly=True, states={'draft':[('readonly',False)]}),
 -        'allow_check' : fields.boolean('Allow Check Writing'),
++    _columns = {
++        'amount_in_word' : fields.char("Amount in Word" , size=128, readonly=True, states={'draft':[('readonly',False)]}),
++        'allow_check' : fields.related('journal_id', 'allow_check_writing', type='boolean', string='Allow Check Writing'),
+         'number': fields.char('Number', size=32),
+     }
 -    
 -    _defaults = {
 -        'journal_id':_get_journal,
 -        }
 -    def onchange_partner_id(self, cr, uid, ids, partner_id, journal_id, price, currency_id, ttype, date, context=None):
 -        """ Inherited - add amount_in_word in return value dictionary """
 -        
++    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):
++        """ Inherited - add amount_in_word and allow_check_writting in returned value dictionary """
+         if not context:
+             context = {}
 -        default = super(account_voucher, self).onchange_partner_id(cr, uid, ids, partner_id, journal_id, price, currency_id, ttype, date, context=context)
++        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)
+         if 'value' in default:
 -            amount = 'amount' in default['value'] and default['value']['amount'] or price
++            amount = 'amount' in default['value'] and default['value']['amount'] or amount
+             #TODO : generic amount_to_text is not ready yet, otherwise language and currency can be passed
 -            
++            ##??
++            #amount_in_word = amount_to_text(amount, context=context)
+             amount_in_word = amount_to_text(amount)
+             default['value'].update({'amount_in_word':amount_in_word})
 -            
+             if journal_id:
 -                allow_check_writing = self.pool.get('account.journal').browse(cr, uid, journal_id).allow_check_writing
++                allow_check_writing = self.pool.get('account.journal').browse(cr, uid, journal_id, context=context).allow_check_writing
+                 default['value'].update({'allow_check':allow_check_writing})
 -                
+         return default
+     def print_check(self, cr, uid, ids, context=None):
 -        if not ids: return []
 -        
++        if not ids:
++            return  {}
++
++        check_layout_report = {
++            'top' : 'account.print.check.top',
++            'middle' : 'account.print.check.middle',
++            'bottom' : 'account.print.check.bottom',
++        }
++
+         check_layout = self.browse(cr, uid, ids[0], context=context).company_id.check_layout
 -        
+         return {
+             'type': 'ir.actions.report.xml', 
+             'report_name':check_layout_report[check_layout],
+             'datas': {
+                     'model':'account.voucher',
+                     'id': ids and ids[0] or False,
+                     'ids': ids and ids or [],
+                     'report_type': 'pdf'
+                 },
+             'nodestroy': True
+             }
 -            
++
+     def fields_view_get(self, cr, uid, view_id=None, view_type=False, context=None, toolbar=False, submenu=False):
++        """
++            Add domain 'allow_check_writting = True' on journal_id field and remove 'widget = selection' on the same
++            field because the dynamic domain is not allowed on such widget
++        """
+         res = super(account_voucher, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
+         doc = etree.XML(res['arch'])
+         nodes = doc.xpath("//field[@name='journal_id']")
+         if context.get('write_check', False) :
+             for node in nodes:
 -                node.set('domain', "[('type', '=', 'bank')]")
++                node.set('domain', "[('type', '=', 'bank'), ('allow_check_writing','=',True)]")
++                node.set('widget', '')
+             res['arch'] = etree.tostring(doc)
+         return res
 -        
++
+ account_voucher()
index 0000000,e046096..f8e048b
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,108 +1,107 @@@
+ # -*- coding: utf-8 -*-
+ ##############################################################################
+ #
+ #    OpenERP, Open Source Management Solution
+ #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
+ #
+ #    This program is free software: you can redistribute it and/or modify
+ #    it under the terms of the GNU Affero General Public License as
+ #    published by the Free Software Foundation, either version 3 of the
+ #    License, or (at your option) any later version.
+ #
+ #    This program is distributed in the hope that it will be useful,
+ #    but WITHOUT ANY WARRANTY; without even the implied warranty of
+ #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ #    GNU Affero General Public License for more details.
+ #
+ #    You should have received a copy of the GNU Affero General Public License
+ #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ #
+ ##############################################################################
+ import time
+ from report import report_sxw
+ from tools import amount_to_text_en
+ class report_print_check(report_sxw.rml_parse):
+     def __init__(self, cr, uid, name, context):
+         super(report_print_check, self).__init__(cr, uid, name, context)
+         self.number_lines = 0
+         self.number_add = 0
+         self.localcontext.update({
+             'time': time,
+             'get_lines': self.get_lines,
+             'fill_stars' : self.fill_stars,
+             'get_zip_line': self.get_zip_line,
+         })
+     def fill_stars(self, amount):
+         amount = amount.replace('Dollars','')
 -        if len(amount) < 100: #TODO
++        if len(amount) < 100:
+             stars = 100 - len(amount)
+             return ' '.join([amount,'*'*stars])
+         else: return amount
 -    
++
+     def get_zip_line(self, address):
+         '''
+         Get the address line
+         '''
+         ret = ''
+         if address:
+             if address.city:
+                 ret += address.city
+             if address.state_id:
+                 if address.state_id.name:
+                     if ret:
+                         ret += ', '
+                     ret += address.state_id.name
+             if address.zip:
+                 if ret:
+                     ret += ' '
+                 ret += address.zip
+         return ret
+     
+     def get_lines(self, voucher_lines):
+         result = []
+         self.number_lines = len(voucher_lines)
 -        print "==============",voucher_lines
+         for i in range(0, min(10,self.number_lines)):
+             if i < self.number_lines:
+                 res = {
+                     'date_due' : voucher_lines[i].date_due,
+                     'name' : voucher_lines[i].name,
+                     'amount_original' : voucher_lines[i].amount_original and voucher_lines[i].amount_original or False,
+                     'amount_unreconciled' : voucher_lines[i].amount_unreconciled and voucher_lines[i].amount_unreconciled or False,
+                     'amount' : voucher_lines[i].amount and voucher_lines[i].amount or False,
+                 }
+             else :
+                 res = {
+                     'date_due' : False,
+                     'name' : False,
+                     'amount_original' : False,
+                     'amount_due' : False,
+                     'amount' : False,
+                 }
+             result.append(res)
+         return result
 -        
++
+ report_sxw.report_sxw(
+     'report.account.print.check.top',
+     'account.voucher',
+     'addons/account_check_writing/report/check_print_top.rml',
+     parser=report_print_check,header=False
+ )
+ report_sxw.report_sxw(
+     'report.account.print.check.middle',
+     'account.voucher',
+     'addons/account_check_writing/report/check_print_middle.rml',
+     parser=report_print_check,header=False
+ )
+ report_sxw.report_sxw(
+     'report.account.print.check.bottom',
+     'account.voucher',
+     'addons/account_check_writing/report/check_print_bottom.rml',
+     parser=report_print_check,header=False
+ )
+ # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
@@@ -51,10 -53,10 +51,14 @@@ class account_voucher(osv.osv)
          periods = self.pool.get('account.period').find(cr, uid)
          return periods and periods[0] or False
  
++    def _make_journal_search(self, cr, uid, ttype, context=None):
++        journal_pool = self.pool.get('account.journal')
++        return journal_pool.search(cr, uid, [('type', '=', ttype)], limit=1)
++
      def _get_journal(self, cr, uid, context=None):
          if context is None: context = {}
--        journal_pool = self.pool.get('account.journal')
          invoice_pool = self.pool.get('account.invoice')
++        journal_pool = self.pool.get('account.journal')
          if context.get('invoice_id', False):
              currency_id = invoice_pool.browse(cr, uid, context['invoice_id'], context=context).currency_id.id
              journal_id = journal_pool.search(cr, uid, [('currency', '=', currency_id)], limit=1)
@@@ -67,7 -69,7 +71,7 @@@
          ttype = context.get('type', 'bank')
          if ttype in ('payment', 'receipt'):
              ttype = 'bank'
--        res = journal_pool.search(cr, uid, [('type', '=', ttype)], limit=1)
++        res = self._make_journal_search(cr, uid, ttype, context=context)
          return res and res[0] or False
  
      def _get_tax(self, cr, uid, context=None):