<?php 
    require_once "db.php";

	function GetUserData ($login,$pass=null){
		global $con;
		$params = [$login,$pass];
		$sql = "Select * from Apiusers
		Where  Login = ? and Password = ? and Status='1' ";
		$query = sqlsrv_query($con,$sql,$params) or die (print_r(sqlsrv_errors()));
		$arr = [];
		if(sqlsrv_has_rows($query)){
			$row = sqlsrv_fetch_array($query);
			$arr [] = $row;
		}
		else{
			$arr = ["code"=>"100","sql"=>$sql];
		}
		return $arr;
	}
	
	
	function GetFines ($plate){
    global $con;
	$params = [$plate];
    $sql = "Select 
			orderID
			,Account
			,summa
			,dateFine
			,tf.Name
			,i.systemName
			,tf.SystemId
			from AMI.dbo.Invoices i 
			left join TypeFine tf on tf.tip = i.typeFine 
			Where i.Account = ?
			and tf.SystemId = i.systemName ";
    $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;
		}
        
    }
    else{
        $arr = ["code"=>"100","sql"=>$sql];
    }
    return $arr;
}

	function CheckFines ($plate,$ruz)
	{
		global $con;
		$params = [$plate,$ruz];
		$sql = "SELECT count(invoiceID) as kol from Invoices with (nolock)  
				where Account = ? AND  status = '0' 
				AND  Inputdate<=? ";

		$query = sqlsrv_query($con,$sql,$params) or die (print_r(sqlsrv_errors()));
		$arr = [];
		if(sqlsrv_has_rows($query))
		{
			$row = sqlsrv_fetch_array($query);
			$arr = ["code"=>"200","kol"=>$row['kol']];
		}
		else{
			$arr = ["code"=>"100","kol"=>0];
		}
		return $arr;
	}





?>