<?php
session_start();
include 'myfunctions.php'; 
$IDc=1;
$Username='';$Password='';$AFM= "";$myafm='';

$afm=(isset($_REQUEST['afm'])?$_REQUEST['afm']:'');
 
if($conn == false){
	 echo 'Δεν υπαρχει συνδεση...<br>';
	  dbConnect();
}
 
 $query = "SELECT * FROM mitroo_parametroi WHERE IDc=1"   ;
$result = mysqli_query($conn, $query);
    $rs = mysqli_fetch_assoc($result);

$Username = $rs['Username'];
$Password = $rs['Password'] ;
$myafm = $rs['AFM']; 
 
//$username=(isset($_REQUEST['u'])?$_REQUEST['u']:GSISUSER);
//$password=(isset($_REQUEST['p'])?$_REQUEST['p']:GSISPASS);
//$myafm= (isset($_REQUEST['myafm'])?$_REQUEST['myafm']:MYAFM);

if($myafm == ''){
    return 'Must set MYAFM';
    exit;
}

if($afm == ''){
    return 'Must request afm';
    exit;
}
 
$result = checkVATGR($Username,$Password,$myafm,$afm);

// print results
// echo 'PHP version: ' . phpversion(). "<br>";

//echo "======================================== <br>";
//echo "     Greek AFM <br>";
//echo "======================================== <br>";
//	print_r($result); echo "<br>";
//echo "1. ======================================== <br>";
//	print_r(array_keys($result)); echo "<br>";
//echo "2. ========================================<br>";
 
//echo "3. ========================================<br>";
#echo "getKeysMultidimensional ------> " ; print_r(getKeysMultidimensional($result)); echo "<br><br>";
$array = json_decode(json_encode($result), true);
# print_r( $array) ;
 
$onomasia = array_column($result, 'onomasia')[0];
$dnsi = array_column($result, 'postalAddress')[0];
$dnsiNo = array_column($result, 'postalAddressNo')[0];
$tk = array_column($result, 'postalZipCode')[0];
$poli = array_column($result, 'postalAreaDescription')[0];
$typosepaggelmatos = array_column($result, 'firmFlagDescr')[0];
//$epaggelma = array_column($array, 'firmActDescr')[0] ;
$doy = array_column($result, 'doyDescr')[0];
$doy_code = array_column($result, 'doy')[0];  
$afm = array_column($result, 'afm')[0];
$afm_Katastasi = array_column($result, 'deactivationFlag')[0]; 
$isDimosiaYpiresia= array_column($result, 'legalStatusDescr')[0];
 
$epaggelma = getMDArrayValueByKey($array, 'firmActDescr');
$arrLength = count($epaggelma);
 
if(!$isDimosiaYpiresia==""){
	$typosepaggelmatos=$isDimosiaYpiresia;
}

echo "Eponymia=".$onomasia."\n";
echo "Dnsi=".$dnsi." ".$dnsiNo ."\n";
echo "Poli=".$poli."\n";
echo "TK=".$tk."\n";
echo "Epaggelma_Typos=".$typosepaggelmatos ."\n";
if ($arrLength>0) { 
	echo "Epaggelma=".$epaggelma[0]."\n";
	for($i = 0; $i < $arrLength; $i++) {
		echo "Epaggelma$i=". $epaggelma[$i]."\n";
	}
}
echo "AFM_Katastasi=". $afm_Katastasi."\n";
echo "AFM=".$afm."\n"; 
echo "Doy=".$doy."\n";
echo "Doy Kodikos=".$doy_code."\n";
echo "DOY=".$doy."\n";

 
//echo "<br><br>";	
//echo "4. ======================================== <br>";
//echo "5. ======================================== <br>";
//echo "\nVIES Results:\n";
//Make call to VIES

// ======================================== ;
//       EU
// ======================================== ;
# $result_eu=checkVATEU($afm);
# print_r($result_eu);
// ======================================== ;


/* 
 * $username:  username & password από εδώ: http://gsis.gr/gsis/info/gsis_site/PublicIssue/wnsp/wnsp_pages/wnsp.html
 * $password: 
 * $AFMCalledby: ΑΦΜ εξουσιοδοτούντος ή κενό (οπότε εννοείται το ΑΦΜ του username)
 * $AFMCalledfor: Το ΑΦΜ που αναζητούμε
 */
function checkVATGR($username, $password, $AFMcalledby="", $AFMcalledfor)
{
    $client = new SoapClient( "https://www1.gsis.gr/webtax2/wsgsis/RgWsPublic/RgWsPublicPort?WSDL",array('trace' => true) );
    $version = $client->rgWsPublicVersionInfo();
		
    $authHeader = new stdClass();
 #   $authHeader->UsernameToken->Username = "$username";
 #   $authHeader->UsernameToken->Password = "$password";
 #	 $authHeader = new stdClass();
     $authHeader->UsernameToken=new stdClass();
	# echo "aaaaaa  " .  $version;
    $authHeader->UsernameToken->Username = "$username";
    $authHeader->UsernameToken->Password = "$password";
 
    $Headers[] = new SoapHeader('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'Security', $authHeader,TRUE);
	
	
    $client->__setSoapHeaders($Headers);
    $result = $client->rgWsPublicAfmMethod(
        array(
            'afmCalledBy'=>"$AFMcalledby",
            'afmCalledFor'=>"$AFMcalledfor",
            )
        );
    return $result;
}    
function checkVATEU($vatNo, $CountryCode='EL' )
{
	# $CountryCode='EL';
    $client = new SoapClient( "http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl");
    //$client = new SoapClient( "checkVatService.wsdl");
    $result = $client->checkVat(array('countryCode'=>$CountryCode,'vatNumber'=>$vatNo));
    return $result;
}
 
function getKeysMultidimensional(array $array) 
{
    $keys = array();
    foreach($array as $key => $value)
    {
        $keys[] = $key;
        if( is_array($value) ) { 
            $keys = array_merge($keys, getKeysMultidimensional($value));
        }
    }

    return $keys;

}

function object_to_array($data)
{
    if (is_array($data) || is_object($data))
    {
        $result = [];
        foreach ($data as $key => $value)
        {
            $result[$key] = (is_array($value) || is_object($value)) ? object_to_array($value) : $value;
        }
        return $result;
    }
    return $data;
}

function object_to_array2($data) {
    if ((! is_array($data)) and (! is_object($data)))
        return 'xxx'; // $data;

    $result = array();

    $data = (array) $data;
    foreach ($data as $key => $value) {
        if (is_object($value))
            $value = (array) $value;
        if (is_array($value))
            $result[$key] = object_to_array($value);
        else
            $result[$key] = $value;
    }
    return $result;
}

function get_values_for_keys($mapping, $keys) {
    foreach($keys as $key) {
        $output_arr[] = $mapping[$key];
    }
    return $output_arr;
} 

function getMDArrayValueByKey($a, $k) {
    $r = [];
    array_walk_recursive ($a, 
                          function ($item, $key) use ($k, &$r) {if ($key == $k) $r[] = $item;}
                          );
    return $r;
}
?>
