
 <?php

	
	 //$cedula=$_GET["cedula"];
	//$cedtip=$_GET["cedtip"];
	
 //$cedula=112320379;
		include('conexion.php');
		
			
			  
	  $cliente="UPDATE web_0301 SET  cdgact=''    ";
					$mysqli->query($cliente);
					
					
$consulta ="SELECT * FROM web_0301 WHERE clitributa = 1 ";

	$count=0;
	

					
					
if ($resultado = $mysqli->query($consulta)) 
	$count=0;


    while ($fila2 = $resultado->fetch_assoc()) {                                          ////  abre el ford
		
		$cedula  = $fila2["CEDCLI"];
		$DSCCLI  = $fila2["DSCCLI"];
		
		

$curl = curl_init();
 
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.hacienda.go.cr/fe/ae?identificacion=".$cedula,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => false,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
));
 
$response = curl_exec($curl);
$err = curl_error($curl);
 
curl_close($curl);
 
if ($err) {                                                       /// si ahi error  ////////////////
  echo "cURL Error #:" . $err;
} else {                                                               //  si no ahi error ///////////////////////
  //echo $response;
$data = json_decode($response, true);


	  $query = "delete from actividades_comercial_clientes where cedcli = '$cedula' "; 
	  $mysqli->query($query);

	  
	  
 foreach ($data['actividades'] as $actividad) {                                  // entra el ford //////////////////////////
            echo "Código: " . $actividad['codigo'] . "\n";
			echo "<BR>";
            echo "Descripción: " . $actividad['descripcion'] . "\n";
			echo "<BR>";
            echo "Estado: " . $actividad['estado'] . "\n";
			echo "<BR>";
            echo "--------------------------\n";
			echo "<BR>";
			
			$cdgactcom =  $actividad['codigo'];
			$dscactcom =  $actividad['descripcion'];
		

			 
			$query_operacion="INSERT INTO actividades_comercial_clientes ( idact, cedcli,cdgactcom, dscactcom , cdgprin ) VALUES ('','$cedula', '$cdgactcom', '$dscactcom' , 1 )";					
	        $mysqli->query($query_operacion);
			
			
				$nuevo="UPDATE web_0301 SET  cdgact='$cdgactcom' WHERE cedcli = '$cedula' AND   cdgact = ''    ";
					$mysqli->query($nuevo);
		


}              // cierra el ford 



} 	  // cierra q no ahi eror 		



$count++;


}   // fin while 
	
	
	


?>









