2016-12-28 52 views
0

我想計算account.bank.statement上「Montantcrédit」列的總和。我試圖使用_get_sum_entry_encoding,但它不起作用。獲取odoo8中列的總和

看到的景象:

sum of column

+1

我們可以看看你迄今爲止所嘗試過的嗎?你是否以代碼的形式嘗試? – halfer

回答

0

大量試驗後,並與Kbir這裏的幫助是解決方案: 的.py:

from openerp.osv import fields, orm, osv 
class sale_order_line(orm.Model): 
_inherit = 'account.bank.statement' 
_name = 'account.bank.statement' 

def _get_tot(self, cr, uid, ids, name, arg, context=None): 
res = {} 
cr.execute("SELECT sum(amount) FROM account_bank_statement_line WHERE statement_id ="+str(ids[0])+"") 
data = cr.fetchone() 
for re in self.browse(cr, uid, ids, context=context): 
res[re.id] = data[0] 
return res 


def _get_statement_from_line(self, cr, uid, ids, context=None): 
    result = {} 
    for line in self.pool.get('account.bank.statement.line').browse(cr, uid, ids, context=context): 
     result[line.statement_id.id] = True 
    return result.keys() 

_columns = { 

'totm': fields.function(_get_tot, 'totm',type='float', 
     store = { 
      'account.bank.statement': (lambda self, cr, uid, ids, context=None: ids, ['line_ids','move_line_ids'], 10), 
      'account.bank.statement.line': (_get_statement_from_line, ['amount'], 10), 
     }), 
     } 

的.xml:

<?xml version="1.0" encoding="utf-8"?> 
<openerp> 
    <data> 
    <record model="ir.ui.view" id="view_order_form_ftz"> 
     <field name="name">view.order.form.ftz</field> 
     <field name="model">account.bank.statement</field> 
     <field name="inherit_id" ref="account.view_bank_statement_formfz" /> 
     <field name="arch" type="xml"> 
      <xpath expr="//field[@name='balance_start']" position="after"> 
      <field name="totm" string="Montant total"/> 
      </xpath> 
     </field> 
    </record> 
    </data> 
</openerp> 

我再想想你卡比爾。

0

試試下面的代碼:

def _get_tot(self, cr, uid, ids, name, arg, context=None): 
    res = {} 
    cr.execute("SELECT sum(amount) FROM account_bank_statement_line WHERE statement_id ="+str(ids)+"") 
    data = cr.fetchall() 
    for re in self.browse(cr, uid, ids, context=context): 
     res[re.id] = data 

    return res 
+0

您好,我想用Python代碼來嘗試,因爲我想打電話給在其他功能 –

+0

總值嘗試SQL查詢... – KbiR

+0

是的,我嘗試..plz看到答案..我得到這個錯誤:我得到這個錯誤:KeyError:'statement_id' –

0

謝謝kbiR的回答我嘗試在這裏使用SQL查詢我的代碼:

from openerp.osv import fields, orm, osv 
class sale_order_line(orm.Model): 
_inherit = 'account.bank.statement' 
_name = 'account.bank.statement' 

def _get_tot(self,cr,uid,values,context): 
idstate = self.id 
cr.execute("SELECT sum(amount) FROM account_bank_statement_line WHERE statement_id ="+str(idstate)+"") 
return cr.fetchall() 

_columns = { 

'totm': fields.selection(_get_tot, 'totm'), 
     } 

我的xml字段:

<?xml version="1.0" encoding="utf-8"?> 
<openerp> 
    <data> 
    <record model="ir.ui.view" id="view_order_form_ftz"> 
     <field name="name">view.order.form.ftz</field> 
     <field name="model">account.bank.statement</field> 
     <field name="inherit_id" ref="account.view_bank_statement_formfz" /> 
     <field name="arch" type="xml"> 
      <xpath expr="//field[@name='balance_start']" position="after"> 
      <field name="totm"/> 
      </xpath> 
     </field> 
    </record> 
    </data> 
</openerp> 

我得到這個錯誤:_get_tot()到底需要5個參數(給出4)

+0

我不能看到id'的'的聲明,在你的代碼,沒有價值的id這就是爲什麼你得到'keyError'。更新我以前的答案現在就試試。 – KbiR

+0

嘗試''totm':dields.function(_get_tot,type ='selection','totm) – KbiR

+0

hi thnx kbir我試試它('totm':fields。功能(_get_tot, 'TOTM',類型= '浮動'))我得到這個錯誤:ProgrammingError:ERREUR:ERREUR德syntaxe河畔OUPrès區德«[» LINE 1:...噸)FROM account_bank_statement_line WHERE statement_id = [16 ] –