我使用jasper在openerp
创建了2个报告。但是我对2个报告使用相同的解析器,第一个报告是“详细报告”,第二个报告是“摘要”。在附加到结果列表的字典(命名数据)中,有密钥日期,发票,借方和贷方。对于详细报告,我只需要从account.move.line
获取这些值。但是对于摘要报告,只有一行包含相同发票号的帐户移动行。那就是我需要添加具有相同发票号的帐户移动行的借方和贷方,并将其显示为一行,我该如何在Python中执行。例如,结果列表是
[{'invoice':1,'debit':10,'credit':10},
{'name':'b','invoice':2,'debit':10,'credit':18},
{'invoice':1,'debit':10,'credit':10}]
我需要的是
[{'invoice':1,'debit':20,'credit':20},
{'invoice':2,'debit':10,'credit':18}]
修改 代码是
move_line_ids = move_line_pool.search(cr, uid, domain, context=context)
print 'move_line_ids', len(move_line_ids)
for move_line in move_line_pool.browse(cr, uid, move_line_ids, context=context):
group_by = grp = grp_acc = ''
debit = credit = bal = 0.0
if move_line.debit > 0.0:
debit = move_line.amount_residual
elif move_line.credit > 0.0:
credit = move_line.amount_residual
bal = debit - credit
if form_group_by == 'group_account_code':
group_by = 'ACCOUNT CODE : '
grp = move_line.account_id.code or ''
grp_acc = move_line.account_id.name or ''
elif form_group_by == 'group_order_no':
group_by = 'ORDER : '
grp = move_line.sale_id.name or ''
else:
group_by = 'INVOICE : '
grp = move_line.invoice.number or ''
date = dt.strptime(str(move_line.date), '%Y-%m-%d').strftime('%d-%m-%Y')
data = {
'period': period,
'date': date,
'company': company or '',
'so': move_line.sale_id.name or '',
'invoice': move_line.invoice.number or '',
'po': '',
'status': '',
'ref': move_line.move_id.name or '',
'debit': debit,
'credit': credit,
'bal': bal,
'fc': '',
'f_amt': '',
'trx': move_line.journal_id.code or '',
'account': move_line.account_id.name or '',
'account_code': move_line.account_id.code or '',
'acc_range': acc_range,
'group_by': group_by,
'grp': grp,
'grp_acc': grp_acc,
}
result.append(data)
if form_group_by == 'group_account_code':
result = sorted(result, key=lambda k: k['account_code'])
elif form_group_by == 'group_order_no':
result = sorted(result, key=lambda k: k['so'])
else:
result = sorted(result, key=lambda k: k['invoice'])
return result
只考虑字典中的密钥invoice,debit and credit
,如果发票相同,则不需要更新其他密钥。
答案 0 :(得分:2)
假设我理解你的问题,这样的事情可以解决问题:
sum_dict = {} for e in source_list: if e["invoice"] in sum_dict.keys(): sum_dict[e["invoice"]]["debit"]+=e["debit"] sum_dict[e["invoice"]]["credit"]+=e["credit"] else: sum_dict[e["invoice"] = e result_list = [sum_dict[k] for k in sum_dict.keys()]
<强> ETA:强>
在添加代码之前,我回答了基本问题。将更新。
ETA 2:
好的,我认为这是一个简单的修复,可以在for循环和if / else块之间运行。
但是,正如评论者指出的那样,最好的修复方法可能是对db调用的逻辑进行一些更改。但是我不知道那里有什么好的改变。
答案 1 :(得分:1)
可能不是优化的答案......
li_dict =[{'invoice':1,'debit':10,'credit':10},{'name':'b','invoice':2,'debit':10,'credit':18},
{'invoice':1,'debit':10,'credit':10}, {'name':'b','invoice':2,'debit':10,'credit':18}]
final_result = []
for dic in li_dict :
temp_dict = {}
flag = False
for dic_f in final_result:
if 'invoice' in dic and 'invoice' in dic_f:
if dic['invoice'] == dic_f['invoice']:
dic_f['debit'] = dic['debit'] + dic_f['debit']
dic_f['credit'] = dic['credit'] + dic_f['credit']
flag = True
break
if not flag:
temp_dict['invoice'] = dic.get('invoice', 0)
temp_dict['debit'] = dic.get('debit', 0)
temp_dict['credit'] = dic.get('credit', 0)
final_result.append(temp_dict)
print final_result
结果:
[{'credit': 20, 'invoice': 1, 'debit': 20}, {'credit': 18, 'invoice': 2, 'debit': 10}]