<?php
/*+***********************************************************************************
 * The contents of this file are subject to the vtiger CRM Public License Version 1.0
 * ("License"); You may not use this file except in compliance with the License
 * The Original Code is:  vtiger CRM Open Source
 * The Initial Developer of the Original Code is vtiger.
 * Portions created by vtiger are Copyright (C) vtiger.
 * All Rights Reserved.
 *************************************************************************************/

class Accounts_getSentinelData_Action extends Vtiger_IndexAjax_View {

	public function process(Vtiger_Request $request) {
		global $adb, $site_URL, $PORTAL_URL,$current_user;

		//Obtenemos el ruc
		$rutcliente  = $request->get('ruc');
		$sql = '';
		if(!empty($request->get('record')))
		{
			$sql = " AND e.crmid <> ".$request->get('record');
		}
		$rs_cuenta = $adb->pquery("SELECT *FROM vtiger_account a
										INNER JOIN vtiger_crmentity e ON e.crmid = a.accountid AND e.deleted = 0
										WHERE a.siccode = ?".$sql,array($rutcliente));
		//Obtenemos al usuarios 
        $result_ = '';
		$response = new Vtiger_Response();
		//Obtenemos el modelo 
		if($adb->num_rows($rs_cuenta) > 0){
			$response->setResult(array('success' => true, 'duplicado'=>true, 'data' => ''));
		}
		else {
			if( !empty($rutcliente) ){
				#if(strpos('creantiscrm',$site_URL) !== false)
				$result = '{"result":{"actividades_economicas":[{"ciiu":"","descripcion":"","tipo":"Principal"}],"contribuyente_condicion":"HABIDO","contribuyente_estado":"ACTIVO","departamento":"","distrito":"","domicilio_fiscal":"CALLE SANTANDER  NRO 186 URBANIZACION CHACARILLA SANTA CRUZ EL - MIRAFLORES - LIMA - LIMA (ENTRE CUADRAS 39 Y 40 DE AV. AREQUIPA)","fecha_actividad":"2019-10-01","fecha_inscripcion":"2019-09-24","nombre_comercial":"-","ple":"","provincia":"","razon_social":"CREANTIS DIGITAL E.I.R.L.","representante_legal":{"0":{"cargo":"-","fecha_desde":"2019-09-24","nombre":"SANTA CRUZ ODAR OSCAR DAVID","nro_documento":"08317955","tipo_documento":"D"}},"ruc":"20605309403","tipo":"EMPRESA INDIVIDUAL DE RESP. LTDA","tipo_documento":"R","trabajadores":0},"success":true}';
				#$result = file_get_contents("http://172.21.0.5:5011/ruc/".$rutcliente);
				/*TEST */

				#
				$result = json_decode($result,true);
				$result_ = !empty($result['success']) ? json_encode($result) : NULL;
			}
			$response->setResult(array('success' => true, 'duplicado'=>false, 'data' => $result_));
		}
		
		$response->emit();



	}
}
