def action_pro_mate(self,cr,uid,ids,context=None):
result={}assert len(ids)==1id=ids[0] #生产单有保存销售单id
pro_mateobj=self.pool.get('sale.product.material')
mrpobj=self.pool.get('mrp.production')
mrpobj_ids=mrpobj.search(cr,uid,[('sale_origin_id','=',id)],context=context) #查询有关的生产单
mrpobjres=mrpobj.browse(cr,uid,mrpobj_ids,context=context)
seq=1vals={}
sql="select feeding_name,partner_id,COALESCE(SUM(feeding_number),0) product_qty,COALESCE (price_unit,0) price_unit,(COALESCE(SUM(feeding_number),0)*COALESCE( price_unit,0)) as je from feeding_order where production_id in (select id from mrp_production where sale_origin_id=%d) GROUP BY feeding_name,partner_id,price_unit ORDER BY partner_id"%(id)
cr.execute(sql)
dict=cr.dictfetchall()
num=len(dict)
pro_mate_ids=[]
i=0
#将查询好的数据,添加到表中,通过sql语句查询分组汇总for i inrange(num):printi
pro_mate_ids.append((0,0,{'sale_origin_id':id,'sequence':i+1,'product_id':dict[i]['feeding_name'],'partner_id':dict[i]['partner_id'],'price':dict[i]['price_unit'],'qty':dict[i]['product_qty'],'je':dict[i]['je'],
}))
i+=1vals={'pro_mate_id':pro_mate_ids}
最后添加进去
self.write(cr,uid,[id],vals,context=context)return True
#排序record= sorted(record, key=lambda record : record[2]['product_id'])