query("select id,product_code,quantity,price,parent_name from ecmstockoperations where (used=0 or used is null) and type=0 and deleted='0'"); while($r=$GLOBALS['db']->fetchByAssoc($w)){; $qty=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select sum(quantity) as qty from ecmstockoperations where type=1 and in_id='".$r['id']."' and deleted='0'")); if($qty['qty']>=$r['quantity']){ echo $r['product_code']." ".$r['parent_name']." ".$r['quantity']." ".$qty['qty']."\n"; $GLOBALS['db']->query("update ecmstockoperations set used=1 where id='".$r['id']."'"); } } mysql_close($sql); ?>