[IMP] account, account_voucher: Improved find method and Context passing scenario
authorpso (OpenERP) <pso@tinyerp.com>
Tue, 2 Aug 2011 12:32:40 +0000 (18:02 +0530)
committerpso (OpenERP) <pso@tinyerp.com>
Tue, 2 Aug 2011 12:32:40 +0000 (18:02 +0530)
bzr revid: pso@tinyerp.com-20110802123240-rph1k1znsbp1e38r

1  2 
addons/account/account.py
addons/account_voucher/account_voucher.py

@@@ -910,7 -911,10 +911,13 @@@ class account_period(osv.osv)
          if not dt:
              dt = time.strftime('%Y-%m-%d')
  #CHECKME: shouldn't we check the state of the period?
-         ids = self.search(cr, uid, [('date_start','<=',dt),('date_stop','>=',dt)])
+         args = [('date_start', '<=' ,dt), ('date_stop', '>=', dt)]
+         if context.get('company_id', False):
+             args.append(('company_id', '=', context['company_id']))
++        else:
++            company_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id
++            args.append(('company_id', '=', company_id))
+         ids = self.search(cr, uid, args, context=context)
          if not ids:
              raise osv.except_osv(_('Error !'), _('No period defined for this date: %s !\nPlease create a fiscal year.')%dt)
          return ids
@@@ -1374,7 -1378,7 +1381,7 @@@ class account_move(osv.osv)
          cr.execute('SELECT SUM(%s) FROM account_move_line WHERE move_id=%%s AND id!=%%s' % (mode,), (move.id, line_id2))
          result = cr.fetchone()[0] or 0.0
          cr.execute('update account_move_line set '+mode2+'=%s where id=%s', (result, line_id))
--        
++
          #adjust also the amount in currency if needed
          cr.execute("select currency_id, sum(amount_currency) as amount_currency from account_move_line where move_id = %s and currency_id is not null group by currency_id", (move.id,))
          for row in cr.dictfetchall():
@@@ -39,7 -39,11 +39,10 @@@ class account_voucher(osv.osv)
          if context is None: context = {}
          if context.get('period_id', False):
              return context.get('period_id')
-         periods = self.pool.get('account.period').find(cr, uid)
 -        company_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id
+         if context.get('invoice_id', False):
+             company_id = self.pool.get('account.invoice').browse(cr, uid, context['invoice_id'], context=context).company_id.id
 -        context.update({'company_id': company_id})
++            context.update({'company_id': company_id})
+         periods = self.pool.get('account.period').find(cr, uid, context=context)
          return periods and periods[0] or False
  
      def _get_journal(self, cr, uid, context=None):
      def fields_view_get(self, cr, uid, view_id=None, view_type=False, context=None, toolbar=False, submenu=False):
          mod_obj = self.pool.get('ir.model.data')
          if context is None: context = {}
--        
++
          def get_res_id(view_type, condition):
              result = False
              if view_type == 'tree':
              domain = [('state','=','valid'), ('account_id.type', '=', account_type), ('reconcile_id', '=', False), ('partner_id', '=', partner_id)]
              if context.get('invoice_id', False):
                    domain.append(('invoice', '=', context['invoice_id']))
--            ids = move_line_pool.search(cr, uid, domain, context=context)    
++            ids = move_line_pool.search(cr, uid, domain, context=context)
          else:
              ids = context['move_line_ids']
          ids.reverse()
          """
          period_pool = self.pool.get('account.period')
          res = self.onchange_partner_id(cr, uid, ids, partner_id, journal_id, price, currency_id, ttype, date, context=context)
-         pids = period_pool.search(cr, uid, [('date_start', '<=', date), ('date_stop', '>=', date)])
 -        company_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id
+         if context.get('invoice_id', False):
+             company_id = self.pool.get('account.invoice').browse(cr, uid, context['invoice_id'], context=context).company_id.id
 -        context.update({'company_id': company_id})
++            context.update({'company_id': company_id})
+         pids = period_pool.find(cr, uid, date, context=context)
          if pids:
              if not 'value' in res:
                  res['value'] = {}