#2018-04-16 def action_create_purc(self,cr,uid,ids,context=None): mrp_origin_obj=self.browse(cr,uid,ids,context) mod_obj = self.pool.get('ir.model.data') purchaseobj=self.pool('purchase.origin') date_now=mrp_origin_obj.date origin=mrp_origin_obj.name #变量初始化 record=[] var_part_name=0 i=1 for pro_line_id in mrp_origin_obj.pro_mate_id: # 变量赋值,供应商的id part_name=pro_line_id.partner_id.id #第一条数据判断 如果是第一条 if var_part_name==0: record.append((0,0,{ 'sequence':i,'product_id':pro_line_id.product_id.id,'product_qty':pro_line_id.qty,'product_uom':pro_line_id.uom_id.id,'date_planned':date_now, 'price_unit':pro_line_id.price, })) #明细加数据,供应商重新赋值 var_part_name=part_name #第2、3、4.。。。。N条数据判断,如果与第一条相同,数据就直接加入明细中 elif part_name==var_part_name : #当供应商相等时,直接添加明细+1 record.append((0,0,{ 'sequence':i,'product_id':pro_line_id.product_id.id,'product_qty':pro_line_id.qty,'product_uom':pro_line_id.uom_id.id,'date_planned':date_now, 'price_unit':pro_line_id.price, })) else: #part_name!=firstvar_part_name :当他不相等时 # 1、首先把之前的record创建成采购订单 ctx={ 'partner_id':var_part_name,'ref_sale_name':origin,'order_type':'rawmaterial','location_id':19,'date_planned':date_now,'pricelist_id':1,'picking_type_id':8,'line_id':record} purchase_new_id= purchaseobj.create(cr,uid,ctx,context=context) #2、把record变成空,重新添加一条 record=[] ctx={} record.append((0,0,{ 'product_id':pro_line_id.product_id.id,'product_qty':pro_line_id.qty,'product_uom':pro_line_id.uom_id.id,'date_planned':date_now, 'price_unit':pro_line_id.price })) var_part_name=part_name #当最后一条,相等或不等时都要执行这一句。 ctx={ 'partner_id':var_part_name,'ref_sale_name':origin,'order_type':'rawmaterial','location_id':19,'date_planned':date_now,'pricelist_id':1,'picking_type_id':8,'line_id':record} purchase_new_id= purchaseobj.create(cr,uid,ctx,context=context) return purchase_new_id
def action_pro_mate(self,cr,uid,ids,context=None): result={} assert len(ids)==1 id=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=1 vals={} 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 in range(num): print i 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+=1 vals={ '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'])