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

$doc = $db -> query("select * from documents where category_id IN ('invoice_costs', 'invoice_goods') AND deleted ='0'");

while ($d = $db->fetchByAssoc($doc)) {
	$document = new Document();
	$document->retrieve($d['id']);
	
	if ($document->parent_name != '' && $document->parent_id != '' && $document->left_paid > 0 && $document->payment_date != '') {
		
		echo $document->id.'<br>';
	
		$res_count = $db->query ( "select id from ecmtransactions where record_id='" . $return_id . "' and deleted='0'" );
		$count = $res_count->num_rows;
		if ($count == 0) {
			$GLOBALS ['db']->query ( "INSERT INTO ecmtransactions(id,name,date_entered,date_modified,modified_user_id,created_by,deleted,assigned_user_id,value,parent_name,parent_id,payment_date,type,paid,register_date,record_id,record_type,currency_id,currency_value)VALUES
		            ('" . create_guid () . "','" . $document->document_name . "','" . date ( "Y-m-d H:i:s" ) . "','" . date ( "Y-m-d H:i:s" ) . "','" . $_SESSION ['authenticated_user_id'] . "','" . $_SESSION ['authenticated_user_id'] . "','" . $document->deleted . "','" . $document->created_by . "','" . $document->value . "','" . $document->parent_name . "','" . $document->parent_id . "','" . $d['payment_date']. "','0','0','" . $d['payment_date'] . "','" . $document->id . "','Documents','" . $document->currency_id . "', '".$document->currency_value."')" );
		} else {
			$db->query ( "update ecmtransactions set value='" . $document->value . "' where record_id='" . $docuent->id . "'" );
		}
	}
	unset($document);
}
?>