<?php 


$db=$GLOBALS['db'];
//,'7e2d6029-cfd3-ca7b-587f-59ce2429601f'

$query="select id from ecmsales where deleted=0 and id in ('7e2d6029-cfd3-ca7b-587f-59ce2429601f')";


$zap=$db->query($query);

while($dane=$db->fetchByAssoc($zap)){
	
	$query2="select id from ecmstockdocouts where so_id='".$dane['id']."' and deleted=0";
	echo $query2;
	$zap2=$db->query($query2);
	$dane2=$db->fetchByAssoc($zap2);
	
	if($dane2['id']!=""){
		$cenaz=$db->query("select * from ecmstockoperations where parent_id='".$dane2['id']."'");
		
		echo "select * from ecmstockoperations where parent_id='".$dane2['id']."'";
		$ceny=[];
		while($cenyz=$db->fetchByAssoc($cenaz)){
			$ceny[]=$cenyz['price'];
		}
		var_dump($ceny);
		
		$query3="select id from ecminvoiceouts where so_id='".$dane['id']."' and deleted=0";
		
		$zap3=$db->query($query3);
		$dane3=$db->fetchByAssoc($zap3);
		
		if($dane3['id']!=""){
			$query44="select id from ecminvoiceoutitems where deleted=0 and ecminvoiceout_id='".$dane3['id']."'";
			
			
			$zap222=$db->query($query44);
			$pos=0;
			while($xx=$db->fetchByAssoc($zap222)){
				$upate_q="update ecminvoiceoutitems set price_purchase='".$ceny[$pos]."' where id='".$xx['id']."'";
				
				$db->query($upate_q);
				$pos++;
			}
			
			$razem="update ecminvoiceouts set purchase_price=(select sum(quantity*price) as total from ecmstockoperations where parent_id='".$dane2['id']."' and deleted=0) where id='".$dane3['id']."'";
			
			$db->query($razem);
			
		}
	}
}

?>