Moved base report designer in extra_addons
[odoo/odoo.git] / addons / account_payment / account_move_line.py
index 85dc6d7..38d38ec 100644 (file)
 #
 ##############################################################################
 
-from osv import fields
-from osv import osv
+from osv import fields, osv
+
 
 class account_move_line(osv.osv):
        _inherit = "account.move.line"
 
        def amount_to_pay(self, cr, uid, ids, name, arg={}, context={}):
-               """ Return the amount still to pay regarding all the payemnt orders (excepting cancelled orders)"""
+               """ Return the amount still to pay regarding all the payemnt orders
+               (excepting cancelled orders)"""
                if not ids:
                        return {}
-               cr.execute("SELECT ml.id,ml.credit - (select coalesce(sum(amount),0) from payment_line pl inner join payment_order po on (pl.order_id = po.id)where move_line_id = ml.id and po.state != 'cancel') as amount from account_move_line ml where credit > 0 and id in (%s)"% (",".join(map(str,ids))))
+               cr.execute("""SELECT ml.id,
+                                       CASE WHEN ml.amount_currency < 0
+                                               THEN - ml.amount_currency
+                                               ELSE ml.credit
+                                       END -
+                                       (SELECT coalesce(sum(amount_currency),0)
+                                               FROM payment_line pl
+                                                       INNER JOIN payment_order po
+                                                               ON (pl.order_id = po.id)
+                                               WHERE move_line_id = ml.id
+                                               AND po.state != 'cancel') as amount
+                                       FROM account_move_line ml
+                                       WHERE id in (%s)""" % (",".join(map(str, ids))))
                r=dict(cr.fetchall())
                return r
 
-
        def _to_pay_search(self, cr, uid, obj, name, args):
                if not len(args):
                        return []
-               query = self.pool.get('account.move.line')._query_get(cr, uid, context={})
-               where = ' and '.join(map(lambda x: '''(select l.credit - coalesce(sum(amount),0)
-                                                       from payment_line pl
-                                                         inner join payment_order po on (pl.order_id = po.id)
-                                                       where move_line_id = l.id and po.state != 'cancel') '''+x[1]+str(x[2])+' ',args))
+               line_obj = self.pool.get('account.move.line')
+               query = line_obj._query_get(cr, uid, context={})
+               where = ' and '.join(map(lambda x: '''(SELECT
+               CASE WHEN l.amount_currency < 0
+                       THEN - l.amount_currency
+                       ELSE l.credit
+               END - coalesce(sum(pl.amount_currency), 0)
+               FROM payment_line pl
+               INNER JOIN payment_order po ON (pl.order_id = po.id)
+               WHERE move_line_id = l.id AND po.state != 'cancel')''' \
+               + x[1] + str(x[2])+' ',args))
 
                cr.execute(('''select id
-                                          from account_move_line l
-                                          where account_id in (select id from account_account where type=%s and active)
-                                          and reconcile_id is null
-                                          and credit > 0
-                                          and '''+where+' and '+query), ('payable',) )
+                       from account_move_line l
+                       where account_id in (select id
+                               from account_account
+                               where type=%s and active)
+                       and reconcile_id is null
+                       and credit > 0
+                       and ''' + where + ' and ' + query), ('payable',) )
 
                res = cr.fetchall()
                if not len(res):
                        return [('id','=','0')]
                return [('id','in',map(lambda x:x[0], res))]
 
-
-       def line2bank(self,cr,uid,ids,payment_mode= 'manual',context=None):
+       def line2bank(self, cr, uid, ids, payment_type='manual', context=None):
                """
                Try to return for each account move line a corresponding bank
-               account according to the payment mode.  This work using one of
+               account according to the payment type.  This work using one of
                the bank of the partner defined on the invoice eventually
                associated to the line.
-               Return the first suitable bank for the corresponding partner.  
-
+               Return the first suitable bank for the corresponding partner.
                """
-               if not ids: return {}
-               bank_type= self.pool.get('payment.mode').suitable_bank_types(cr,uid,payment_mode,context=context)
-               cr.execute('''select DISTINCT l.id,b.id,b.state
-                                 from account_invoice i
-                                   join account_move m on (i.move_id = m.id)
-                                   join account_move_line l on (m.id = l.move_id)
-                                   join res_partner p on (p.id = i.partner_id)
-                                   join res_partner_bank b on (p.id = b.partner_id)
-                                 where l.id in (%s)
-                                 ''' % ",".join(map(str,ids)) )
-
-               r= cr.fetchall()
-               type_ok=[]
-               line2bank={}.fromkeys(ids)
-               for line,bank,t in r:
-                       if not line2bank[line]:
-                               line2bank[line]= bank
-                               if t in bank_type:
-                                       type_ok.append(line)
-                       elif (line not in  type_ok) and (t in bank_type) :
-                               line2bank[line]= bank
-                               type_ok.append(line)
-
+               payment_mode_obj = self.pool.get('payment.mode')
+               line2bank = {}
+               if not ids:
+                       return {}
+               bank_type = payment_mode_obj.suitable_bank_types(cr, uid, payment_type,
+                               context=context)
+               for line in self.browse(cr, uid, ids, context=context):
+                       line2bank[line.id] = False
+                       if line.invoice and line.invoice.partner_bank:
+                               line2bank[line.id] = line.invoice.partner_bank.id
+                       elif line.partner_id:
+                               for bank in line.partner_id.bank_ids:
+                                       if bank.state in bank_type:
+                                               line2bank[line.id] = bank.id
+                                               break
+                               if line.id not in line2bank and line.partner_id.bank_ids:
+                                       line2bank[line.id] = line.partner_id.bank_ids[0].id
                return line2bank
 
        _columns = {
-               'amount_to_pay' : fields.function(amount_to_pay, method=True, type='float', string='Amount to pay', fnct_search=_to_pay_search),
-                               }
+               'amount_to_pay' : fields.function(amount_to_pay, method=True,
+                       type='float', string='Amount to pay', fnct_search=_to_pay_search),
+       }
+
 account_move_line()