<?php
function SporAudit($action,$inspector,$inputdate,$sign,$secret,$type){
    		// Проверка хеша
		$hash = md5($action.$inputdate.$secret);
		if($sign!=$hash){
			$arr ['code'] = 103;
			$arr ['message']='Error sign';
			$arr ['data'] = $hash;
			$result = json_encode($arr);
			echo $result;
			return;
		}
        $inspector=trim($inspector," ");        
        if($inspector==""){
			$arr ['code'] = 103;
			$arr ['message']='Error parameter';
			//$arr ['data'] = $hash;
			$result = json_encode($arr);
			echo $result;
			return;
		}


        $arr = Auditdata ($type,$inspector);
		foreach ($arr as $item){
        $data_array [] = [
            "fineid"=> $item['fineid'],
			"inputdate"=> $item['inputdate'],
			"reason"=> $item['reason'],
			"approved_by" => $item['approved_by'],
			"approved_date"=> $item['approved_date'],
			"Comment"=> $item['Comment'],
			"video"=> $item['video'],
			"photo" => $item['photo'],
			"additionalPhoto" => str_replace('http://217.11.177.108:9907','https://bda.dc.tj',$item['additionalPhoto']),
            "metaPlate" => str_replace('http://217.11.177.108:9907','https://bda.dc.tj',$item['metaPlate']),
            "Inputdate" =>$item['Inputdate'],
            "P2" =>$item['P2']
        ];
		//https://bda.dc.tj/Fines/invoiceImg.php?login=DC&path=c:/Shohin/2024/07/T-22198479_PhotoPlate.jpg&sign=a2baf76d399c8da91d2c9ed7636a5879
		}	
		$responce = ["code"=>"200","comment"=>"Success","result"=>$data_array];
        $result = json_encode($responce);
        echo $result;
        return;

}


function Auditdata($type,$login = ""){
    global $con;
	if($type='approved'){
		$sql = "Select 
		s.fineid
		,s.inputdate
		,s.reason
		,s.approved_by
		,s.approved_date
		,s.Comment
		,i.datefine
		,i.metalinks video
		,i.photo
		,i.additionalPhoto
		,i.metaPlate
		,i.Inputdate
		,i.P2

		from spor s
		left join Invoices i on i.orderID = s.fineid
		Where isnull(s.status,'') in ('10') and isnull(audit_by,'')=''
		";
	}
	else if($type='declined'){
		$sql = "Select 
		s.fineid
		,s.inputdate
		,s.reason
		,s.approved_by
		,s.approved_date
		,s.Comment
		,i.datefine
		,i.metalinks video
		,i.photo
		,i.additionalPhoto
		,i.metaPlate
		,i.Inputdate
		,i.P2

		from spor s
		left join Invoices i on i.orderID = s.fineid
		Where isnull(s.status,'') in ('3') and isnull(audit_by,'')=''
		";
	}
	
	else if($type='audit'){
		$sql = "Select 
		s.fineid
		,s.inputdate
		,s.reason
		,s.approved_by
		,s.approved_date
		,s.Comment
		,i.datefine
		,i.metalinks video
		,i.photo
		,i.additionalPhoto
		,i.metaPlate
		,i.Inputdate
		,i.P2
		,s.audit_status
		,s.audit_by
		,s.audit_date
		,s.audit_comment
		from spor s
		left join Invoices i on i.orderID = s.fineid
		Where isnull(s.audit_status,'') in ('10') and isnull(audit_by,'')= ?
		";
	}
    
	$params = [$login];
    $query = sqlsrv_query($con,$sql,$params) or die (print_r(sqlsrv_errors()));
    $arr = [];
    if(sqlsrv_has_rows($query)){
        while($row = sqlsrv_fetch_array($query)){
			$arr [] = $row;
		}
        
    }
    return $arr;
}

?>