<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: juan
 * Date: 12/2/2021
 * Time: 19:43
 */

class General extends MY_Controller
{
    function __construct() {
        parent::__construct();

        if (!$this->loggedIn) {
            redirect('login');
        }

    }

    function get_data_by_ruc($ruc) {
        try{
            $postData["sNroDocumento"] = $ruc;

            $context = stream_context_create(array(
                'http' => array(
                    'method' => 'GET',
                    'header' => "Authorization: application/json\r\n" .
                        "Content-Type: application/json\r\n",
                    'content' => json_encode($postData),
                    'ignore_errors' => true
                )
            ));

            $urlRuc = "ruc";
            $url_NC = $this->config->item('api_general');
            $response = file_get_contents($url_NC . $urlRuc, false, $context);

        }
        catch (Exception $e) {
            echo $e->getMessage();
        }

        echo $response;
    }
    function get_data_by_dni($dni) {
        try{
            $postData["sNroDocumento"] = $dni;

            $context = stream_context_create(array(
                'http' => array(
                    'method' => 'GET',
                    'header' => "Authorization: application/json\r\n" .
                        "Content-Type: application/json\r\n",
                    'content' => json_encode($postData),
                    'ignore_errors' => true
                )
            ));
           
            $url = $this->config->item('api_general')."dni";
            $response = file_get_contents($url, false, $context);            

        }
        catch (Exception $e) {
            echo $e->getMessage();
        }

        echo $response;
    }
    function tipoAfectacionIGV(){
    
        // try{
            $request = array(
                'flagGratuito' => (string)$this->session->userdata('gratuito')
            );
            $url = $this->config->item('api_general').'catalogos_sunat/tipo_afectacion';
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'GET');
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
            curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($request)); // Envía el array como JSON
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            
            $response = curl_exec($ch);
            curl_close($ch);
            echo $response; 
    }
}