哈哈 我也重构过这块的代码!!我还是放在循环里面的 我_make
_production_consume_line 方法被我返回成数组了
<br />class mrp_fresh(osv.osv):<br /> _inherit='mrp.production'<br /><br /> def _make_production_consume_line(self, cr, uid, production_line, parent_move_id, source_location_id=False, context=None):<br /> stock_move = self.pool.get('stock.move')<br /> production = production_line.production_id<br /> # Internal shipment is created for Stockable and Consumer Products<br /> if production_line.product_id.type not in ('product', 'consu'):<br /> return False<br /> destination_location_id = production.product_id.property_stock_production.id<br /> if not source_location_id:<br /> source_location_id = production.location_src_id.id<br /> <br /> #TODO 自动选择批次号 先入先出<br /> production_lot = self.pool.get('stock.production.lot')<br /> <br /> #查询该商品所有的批次号信息,是根据批次号的排序查询<br /> ids = production_lot.search(cr,uid,[('product_id','=',production_line.product_id.id)])<br /> objects = production_lot.browse(cr,uid,ids)<br /> temp_new = []<br /> move_state = ['assigned','waiting']<br /> #需求要的数量<br /> use_quantity = production_line.product_qty<br /> #下面查找符合条件的批次号<br /> for obj in objects:<br /> prodlot_id = obj.id<br /> #查询该批次号 的商品 所有的待生产 未投料的 数量<br /> cr.execute('select sum(product_qty) from stock_move where (state IN %s) and product_id =%s and prodlot_id =%s',(tuple(move_state),production_line.product_id.id,prodlot_id))<br /> res = cr.fetchone()<br /> # 已有的生产单的在本批次号中 商品数量 和 本次要的数量 总和 <br /> #等待生产的数量 <br /> user_quantity = (res and res[0] and int(res[0])) or 0<br /> #需求总量<br /> def_quantity = obj.stock_available - user_quantity <br /> if def_quantity >=use_quantity:#如果需求的数量直接就被该批次满足了情况<br /> temp_new.append({'prodlot_id':prodlot_id,'quantity':use_quantity})<br /> break<br /> else :<br /> temp_new.append({'prodlot_id':prodlot_id,'quantity':def_quantity})<br /> use_quantity -= def_quantity<br /> move_ids = []<br /> all_quantity = 0<br /> for temp in temp_new:<br /> all_quantity+=int(temp['quantity'])<br /> <br /> if int(production_line.product_qty - all_quantity)>0:<br /> temp_new.append({'prodlot_id':None,'quantity':int(production_line.product_qty - all_quantity)})<br /><br /> for new in temp_new:<br /> move_id = stock_move.create(cr, uid, {<br /> 'name': production.name,<br /> 'date': production.date_planned,<br /> 'product_id': production_line.product_id.id,<br /> #'product_qty': production_line.product_qty,<br /> 'product_qty': new['quantity'],<br /> 'product_uom': production_line.product_uom.id,<br /> 'product_uos_qty': production_line.product_uos and production_line.product_uos_qty or False,<br /> 'product_uos': production_line.product_uos and production_line.product_uos.id or False,<br /> 'location_id': source_location_id,<br /> 'location_dest_id': destination_location_id,<br /> 'move_dest_id': parent_move_id,<br /> 'state': 'waiting',<br /> #'prodlot_id':prodlot_id,<br /> 'prodlot_id':new['prodlot_id'],<br /> 'company_id': production.company_id.id,<br /> })<br /> production.write({'move_lines': [(4, move_id)]}, context=context)<br /> move_ids.append(move_id)<br /> return move_ids<br /> <br /> <br /> def action_confirm(self, cr, uid, ids, context=None):<br /> """ Confirms production order.<br /> @return: Newly generated Shipment Id.<br /> """<br /> shipment_id = False<br /> wf_service = netsvc.LocalService("workflow")<br /> uncompute_ids = filter(lambda x:x, [not x.product_lines and x.id or False for x in self.browse(cr, uid, ids, context=context)])<br /> self.action_compute(cr, uid, uncompute_ids, context=context)<br /> for production in self.browse(cr, uid, ids, context=context):<br /> shipment_id = self._make_production_internal_shipment(cr, uid, production, context=context)<br /> produce_move_id = self._make_production_produce_line(cr, uid, production, context=context)<br /><br /> # Take routing location as a Source Location.<br /> source_location_id = production.location_src_id.id<br /> if production.bom_id.routing_id and production.bom_id.routing_id.location_id:<br /> source_location_id = production.bom_id.routing_id.location_id.id<br /><br /> for line in production.product_lines:<br /> consume_move_ids = self._make_production_consume_line(cr, uid, line, produce_move_id, source_location_id=source_location_id, context=context)<br /> if shipment_id:<br /> for consume_move_id in consume_move_ids:<br /> shipment_move_id = self._make_production_internal_shipment_line(cr, uid, line, shipment_id, consume_move_id,\<br /> destination_location_id=source_location_id, context=context)<br /> self._make_production_line_procurement(cr, uid, line, shipment_move_id, context=context)<br /><br /> if shipment_id:<br /> wf_service.trg_validate(uid, 'stock.picking', shipment_id, 'button_confirm', cr)<br /> production.write({'state':'confirmed'}, context=context)<br /> return shipment_id<br /><br />