<?php
$db=$GLOBALS['db'];

$query="select * from ecmstockoperations where deleted=0 and type=0";

$res=$db->query($query);
$items=array();
while($dane=$db->fetchByAssoc($res)){
	$query2="select sum(quantity) as a from ecmstockoperations where deleted=0 and type=1 and in_id='".$dane['id']."'";
	$res2=$db->query($query2);
	$d2=$db->fetchByAssoc($res2);
	
	$dane['q2']=$d2['a'];
	$dane['quantity']=$dane['quantity']-$dane['q2'];
	$items[]=$dane;
}

echo "<pre>";
$text="";
foreach ($items as $k=>$item){
	if($item['quantity']>0){
	
				
				    $text.='"=""'.$item['product_name'].'""";';
				    $text.='"=""'.$item['product_code'].'""";';
				    $text.='"=""'.$item['stock_name'].'""";';
				    $text.='"=""'.str_replace(".",",",number_format($item['quantity'], 2, '.', '')).'""";';
				    $text.='"=""'.str_replace(".",",",number_format($item['price'], 2, '.', '')).'""";';
				    $text.='"=""'.$item['part_no'].'""";';
				  	$text.="".PHP_EOL;
   
             }
             
	}
	$text=iconv('UTF-8//IGNORE','windows-1250',$text);

	file_put_contents("stan.csv",$text );
?>