<?php
function SporConfirm($action,$inspector,$fineid,$inputdate,$comment,$sign,$secret){
    		// Проверка хеша
		$hash = md5($action.$inspector.$fineid.$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," ");
        $comment=str_replace("\'","",$comment);
        if($inspector==""){
			$arr ['code'] = 103;
			$arr ['message']='Error parameter';
			//$arr ['data'] = $hash;
			$result = json_encode($arr);
			echo $result;
			return;
		}


        $arr = SporConfirmDB ($inspector,$fineid,$inputdate, $comment);
        $result = json_encode($arr);
        echo $result;
        return;

}


function SporConfirmDB ($inspector,$fineid,$inputdate, $comment)
{
    
    global $con;

    $dateoper=date("ymd");
    $timeoper=date("His");
    $shtampUser="fromDB";
    $shtamp="fromDB";
    $account="fromdb";
    $summa="fromdb";
    $tranid=$dateoper . $timeoper .$inspector;
    $schet = "51-E0006";
    $FinOper="10700";
    $coment="";

    try {
		$params = [$fineid];
        $sql = "Select  top 1 account,summa,systemID,orderID from invoices where orderID= ? ";
	    $query = sqlsrv_query($con,$sql,$params);
		$arr = [];
		if(sqlsrv_has_rows($query))
		{
			$row = sqlsrv_fetch_array($query,SQLSRV_FETCH_ASSOC);
            $P0=$row['orderID'];
            $P1 = $row['account'];
            $P2 = $row['summa'];
            $P3 = $row['systemID'];
            $shtampUser = $P3.'r';
        } else{
            $arr = ["code"=>"202","message"=>"Fine not found"];
            return $arr;
        }

    $sql1="Insert into operJurnal (
			Users,ShtampUsers,DateOper,
			TimeOper,Status,FinOper,Coment,P0,P1,P2,P3) 
		  Values(
          '".$inspector."',
          '".$shtampUser."',
          '".$dateoper."',
          '".$timeoper."',
          '1',
          '".$FinOper."',
          '".$coment."',
          '".$P0."',
          '".$P1."',
          '".$P2."', 
           '".$P3."'
		)";

    $sql2="Update spor set [billingtransaction] = '".$P3."',status='10', approved_by='".$inspector."', approved_date='".$dateoper.$timeoper."', comment='".$comment."', biling_status='10',biling_message='Inserted' where fineid = '".$fineid."'";

    
    if ( sqlsrv_begin_transaction( $con) === false ) {
        $arr = ["code"=>"202","message"=>"Error transaction"];
        return $arr;
   }

    
        // Execute the first query
        $query = sqlsrv_query($con, $sql1);
        if ($query === false) {
            throw new Exception("Error executing the first query.");
        }
    
        // Execute the second query
        $query = sqlsrv_query($con, $sql2);
        if ($query === false) {
            throw new Exception("Error executing the second query.".$sql2);
        }

        sqlsrv_commit( $con);
        $arr = ["code"=>"200","message"=>"Success confirm"];
        return $arr;
    } catch (Exception $e) {

        $arr = ["code"=>"202","message"=>"Error confirm" . $e->getMessage()];
        sqlsrv_rollback( $con);
        return $arr;
    }

}

?>