<?php
	include '../../autoload.php';

	$answer="<root>";
	$audit_id=$_REQUEST['audit_id'];
	$point_id=$_REQUEST['point_id'];
	$bquaadtag_id=$_REQUEST['tag_id'];
	$scope_id=$_REQUEST['scope_id'];
	$scope2_id=$_REQUEST['scope2_id'];
	$req_doc_id=$_REQUEST['doc_id'];
	
	if($audit_id)
	{
	
	
	$force_status = false;
	if(isset($_REQUEST['forcestatus']))
	{
		$force_status=$_REQUEST['forcestatus'];
	}
	$remark="";
	if(isset($_REQUEST['remark']))
	{
		$remark=$_REQUEST['remark'];
	}
	$record_id=-1;
	$conn = get_dbconnection();
	$sql = "with e as(INSERT INTO audit_record(audit_id,quaad_point_id) VALUES($1,$2) ON CONFLICT ON CONSTRAINT audit_record_un DO NOTHING returning id) select * from e union select id from audit_record where audit_id=$3 and quaad_point_id=$4";
	
	//error_log($sql);
	$result = pg_prepare($conn, "query_ar", $sql);
	
	$query = pg_execute($conn, "query_ar", array($audit_id,$point_id,$audit_id,$point_id));
	if($query)
	{
		//error_log("QUERY OK");
		if ($row = pg_fetch_row($query))
		{
			$record_id=$row[0];
		}
		else error_log("NOT SAVED");
	}
	if($record_id!=-1)
	{
		error_log("record_id = ".$record_id);
		
		$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
		$arr = explode("?", $actual_link, 2);
		$current_url = $arr[0];
		$url=str_replace("pointsaver.php","checkrules.php",$current_url);
		/*$point_id=7;
		$bquaadtag_id=4;
		$scope2_id=-1;
		$scope_id=1;*/
		$params= array(
			'id' => $point_id,
			'tag_id' => $bquaadtag_id,
			'scope_id' => $scope_id,
			'scope2_id' => $scope2_id,
			'audit_id' => $audit_id
		);

		$strCookie = 'eoQUAL=' . $_COOKIE['eoQUAL'] . '; path=/';
		$useragent = $_SERVER['HTTP_USER_AGENT'];
		session_write_close();
		$ch = curl_init($url);


		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		//curl_setopt($ch, CURLOPT_HEADER, 1);

		curl_setopt($ch,CURLOPT_USERAGENT, $useragent);
		curl_setopt( $ch, CURLOPT_COOKIE, $strCookie );

		$resultt = curl_exec($ch);
//$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
//$header = substr($response, 0, $header_size);
//echo $header;
		curl_close($ch);
		
		session_start();
		
		
		$xml = new SimpleXMLElement($resultt);
		
		if($bquaadtag_id==-1 && $scope_id==-1)
		{
			$sql_proc = "delete from audit_recordprocedure where audit_record_id=$1;";
			$re_proc = pg_prepare($conn,"query_proc",$sql_proc);
			$query=pg_execute($conn,"query_proc",array($record_id));
			
		//	error_log($sql_proc."  ".$record_id);
			$result_proc= $xml->xpath('//procedure');
			$doc_id=-1;
			$fname="";
			$proc_id=-1;
			error_log($xml->asXML());
			foreach ($result_proc as $node)
			{
				$attrs=$node->attributes();
				foreach($attrs as $attr => $val_attr)
				{
					if($attr=="docid")
							$doc_id = $val_attr;
					if($attr=="fname")
							$fname=$val_attr;
					if($attr=="procid")
							$proc_id=$val_attr;
				}
				break;
			}
			error_log("record fname docid procid".$fname." ".$docid." ".$proc_id);
			$sql_procinsert="insert into audit_recordprocedure(audit_record_id,procedure_id,doc_id,remark,status) values ($1,$2,$3,$4,$5);";
			$in_proc=pg_prepare($conn,"query_procin",$sql_procinsert);
			$query=pg_execute($conn,"query_procin",array($record_id,$proc_id,$doc_id,$remark,true));
			error_log($sql_procinsert);
		
		}
		
		
		$sql2="delete from audit_subrecord where audit_record_id=$1 and quaadpoint_tag_id=$2 and scope_id=$3 and scope2_id=$4 and doc_id=$5;";
		error_log("DELETE subrecoreds");
		$re = pg_prepare($conn, "query_de", $sql2);
		$query = pg_execute($conn, "query_de", array($record_id,$bquaadtag_id,$scope_id,$scope2_id,$req_doc_id));
	
		if($req_doc_id==-1)
		{
			error_log("INSERT subrecord reqdocid=-1");
			$sqlred="insert into audit_subrecord(audit_record_id,quaadpoint_tag_id,scope_id,scope2_id,remark,doc_id,forcedstatus,status) values ($1,$2,$3,$4,$5,$6,$7,$8);";
			error_log($sqlred);
			$remarkred = preg_replace( "/\r|\n/", "&#13;&#10;", $remark );
			$status = "false";
			$forcedstatus = $force_status;
			$resultred = pg_prepare($conn, "query_red", $sqlred);
			$queryred = pg_execute($conn, "query_red", array($record_id,$bquaadtag_id,$scope_id,$scope2_id,$remarkred,$req_doc_id,$forcedstatus,$status));
			
		}
		error_log($xml->asXML());
		
		$result = $xml->xpath('//entity');
		
		foreach ($result as $node)
		{
						
			$attrs=$node->attributes();
			
			foreach($attrs as $attr => $val) {
				error_log( $attr."=".$val);
			}
		
			$docs=$node->xpath('document');
			
			error_log("ENTITY");
			foreach($docs as $doc)
			{							
				$doc_attrs=$doc->attributes();
				$doc_id=-1;
					$realfname="";
					$filename="";
					
					$force_doctext="";
					$force_docid=-1;
					$status="true";
					$forcedstatus="false";
					
					error_log("DOCUMENT");
				foreach($doc_attrs as $docattr => $val_doc)
				{
					
					//echo $docattr.="=".$val_doc;
					
					if($docattr=="docid")
						$doc_id = $val_doc;
					if($docattr=="realfname")
						$realfname=$val_doc;
					if($docattr=="file")
						$filename=$val_doc;
					$status="true";
					$forcedstatus="false";
					
					
				}
				
				if($req_doc_id==$doc_id)
				{
					$sql2="insert into audit_subrecord(audit_record_id,quaadpoint_tag_id,scope_id,scope2_id,force_doctext,force_docid,remark,doc_id,forcedstatus,status) values ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10);";
					
					$remark2=preg_replace( "/\r|\n/", "&#13;&#10;", $remark );

					$result = pg_prepare($conn, "query_in", $sql2);
					$query = pg_execute($conn, "query_in", array($record_id,$bquaadtag_id,$scope_id,$scope2_id,$force_doctext,$force_docid,$remark2,$doc_id,$forcedstatus,$status));
				}
			}
			$subs = $node->xpath("subentity");
			foreach($subs as $sub)
			{
			
				$subattrs=$sub->attributes();
				$docs=$sub->xpath('document');
				foreach($docs as $doc)
				{				
					
					$doc_attrs=$doc->attributes();
					$doc_id=-1;
					$realfname="";
					$filename="";
					
					$force_doctext=$doc;
					$force_docid=-1;
					$status="true";
					$forcedstatus="false";
					$forceresult="";
					
					foreach($doc_attrs as $docattr => $val_doc)
					{
						//echo $docattr.="=".$val_doc;
						if($docattr=="docid")
							$doc_id = $val_doc;
						if($docattr=="realfname")
							$realfname=$val_doc;
						if($docattr=="file")
							$filename=$val_doc;
						if($docattr=="force_result")
							$forceresult=$val_doc;
						$status="true";
						$forcedstatus="false";
					}
					if($req_doc_id==$doc_id)
					{
						$sql2="insert into audit_subrecord(audit_record_id,quaadpoint_tag_id,scope_id,scope2_id,force_doctext,force_docid,remark,doc_id,forcedstatus,status,force_result) values ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11);";
						error_log($sql2);
						$remark2=preg_replace( "/\r|\n/", "&#13;&#10;", $remark );
						$result = pg_prepare($conn, "query_in", $sql2);
						$query = pg_execute($conn, "query_in", array($record_id,$bquaadtag_id,$scope_id,$scope2_id,$force_doctext,$force_docid,$remark2,$doc_id,$forcedstatus,$status,$forceresult));
					}
				}
			}
		}
		/*$force_doctext="";
		$force_docid=-1;*/
	}
	else error_log("record_id=-1");
	}
	
	$answer.="</root>";
	header('Content-Type: text/xml');
	echo $answer;
?>