<?php 
    require_once "db.php";




function Getallfines ($account,$type=1){
    global $con;
	// Type 1 - order,Type 2 - plate
	if($type=='2'){
		$account = GetPlateByorder($account);
	}
     $sql = "Select i.orderID
			,i.account
			,i.summa
			,i.metaPlate
			,i.photo
			,i.additionalPhoto
			,i.Inputdate
			,i.Value
			,t.Name
			,i.metalinks
			,sum(i.summa) over() totalsumma
			,count(i.summa) over() totalkol
			,i.dateFine
			,i.OrderID
			from Invoices i 
			left join TypeFine t on t.tip = i.typeFine and  t.SystemId = i.systemName
			Where (i.account = '".$account."' or i.orderid = '".$account."' ) and i.status = '0'";
			
    $query = sqlsrv_query($con,$sql) or die (print_r(sqlsrv_errors()));
    if(sqlsrv_has_rows($query)){
		$arr = [];
        while($row = sqlsrv_fetch_array($query)){
			$arr [] = $row;	
		}
    }
    else{
        $arr = ["code"=>"100","sql"=>$sql];
    }
    return $arr;
}

function GetallfinesbyPhone ($phone){
    global $con;

     $sql = "Select i.orderID
			,i.account
			,i.summa
			,i.metaPlate
			,i.photo
			,i.additionalPhoto
			,i.Inputdate
			,i.Value
			,t.Name
			,i.metalinks
			,sum(i.summa) over() totalsumma
			,count(i.summa) over() totalkol
			,i.dateFine
			,i.OrderID
			,i.Address
			,isnull(s.status,'2') spostat
			,i.systemName
			from Invoices i 
			left join TypeFine t on t.tip = i.typeFine and  t.SystemId = i.systemName
			left join Account a on a.Account = i.account
			left join parkingdb.dbo.cars c on c.car1 = i.account
			left join spor s on s.fineid = i.orderID
			Where isnull(c.phone,a.Telephone) = '".$phone."'  and i.status = '0'
			and c.status='10'";
			
    $query = sqlsrv_query($con,$sql) or die (print_r(sqlsrv_errors()));
    if(sqlsrv_has_rows($query)){
		$arr = [];
        while($row = sqlsrv_fetch_array($query)){
			$arr [] = $row;	
		}
    }
    else{
        $arr = ["code"=>"100","sql"=>$sql];
    }
    return $arr;
}


function History($account){
    global $con;
	// Type 1 - order,Type 2 - plate
	if($type=='2'){
		$account = GetPlateByorder($account);
	}
    $sql =  "with history as (select i.summa,i.orderID,i.typeFine,i.Inputdate,i.dateFine,'nach' as tip,tf.Name from Invoices i
			 left join TypeFine tf on tf.tip = i.typeFine and i.systemName= tf.SystemId
			 Where i.account = '".$account."'
			 union all 
			 select i.summa,i.orderID,i.typeFine,i.Paydata,i.dateFine,'pay' as tip,tf.Name from Invoices i
			 left join TypeFine tf on tf.tip = i.typeFine and i.systemName= tf.SystemId
			 Where i.account = '".$account."' and i.status = '10')
			 select * from history 
			 order by Inputdate desc";			
    $query = sqlsrv_query($con,$sql) or die (print_r(sqlsrv_errors()));
    if(sqlsrv_has_rows($query)){
		$arr = [];
        while($row = sqlsrv_fetch_array($query)){
			$arr [] = $row;	
		}
    }
    else{
        $arr = ["code"=>"100","sql"=>$sql];
    }
    return $arr;
}



?>