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

$cor_res = $db->query("select * from ecminvoiceouts where wz_id is not null and date_entered>= '2018-01-01'");

while ($cor_row = $db->fetchByAssoc($cor_res)) {

	$pos = $db->query("select  * from ecminvoiceoutitems where ecminvoiceout_id='".$cor_row['id']."'");
	
	while ($pos_row = $db->fetchByAssoc($pos)) {

		 $q3 = $db->query("select * from ecmstockdocoutitems where ecmstockdocout_id='".$cor_row['wz_id']."' and ecmproduct_id='".$pos_row['ecmproduct_id']."' and quantity=".$pos_row['quantity']."");
		 $pos_row2 = $db->fetchByAssoc($q3);
		
		if($pos_row!==null && $pos_row2['id']!=""){
			$qu = "update ecminvoiceoutitems set parent_doc_id='".$cor_row['wz_id']."', parent_doc_type='EcmStockDocOut',parent_doc_item_id='".$pos_row2['id']."' where id='".$pos_row['id']."'";
			echo $qu.";<br>";
	}
	}
	
}
?> 