<?
if($_GET['stock_id'])$stock=" and stock_id='".$_GET['stock_id']."'";
else $stock="";
/*$z="select * from ecmstockoperations where deleted='0' and product_id='".$_GET['id']."'".$stock;
$w=$GLOBALS['db']->query($z);
$total=0;
while($r=$GLOBALS['db']->fetchByAssoc($w))
{
	if($r['type']=="0" || $r['type']=="2")$total+=$r['quantity'];
	elseif($r['type']=="1") $total-=$r['quantity'];

}*/
$q1=0;
$w=$GLOBALS['db']->query("select quantity as qty,id,price,product_id from ecmstockoperations where deleted='0' and type='0' and in_id IS NULL".$stock." and product_id='".$_GET['id']."'");
while($rrr=$GLOBALS['db']->fetchByAssoc($w)){
	print $rrr['product_id'].'<br>';
	$ww=$GLOBALS['db']->query("select quantity from ecmstockoperations where in_id='".$rrr['id']."' and type='1' and in_id IS NOT NULL".$stock." and product_id='".$_GET['id']."' and deleted='0'");
	$rqty=0;
	while($rrp=$GLOBALS['db']->fetchByAssoc($ww)){
		$rqty+=$rrp['quantity'];
	}
	$qty=$rrr['qty']-$rqty;
	if($qty>0){
		$arr[]=array("qty"=>$qty,"id"=>$rrr['id'],"price"=>$rrr['price']);
	}
}
echo '<pre>';
print_r($arr);
echo '</pre>';
if(count($arr)>0){
	foreach($arr as $v){
		$q1+=$v['qty'];
		$pp1+=$v['qty']*$v['price'];
	}
}

print $q1;
?>
