<?php

namespace App\Http\Controllers;

class Execute_file_sql
{
	
	private $mysqli;
	private $host;
	private $user;
	private $pass;
	private $name_db;
	
	function __construct($host, $user, $pass, $name_db)
    {
        $this->host=$host;
		$this->user=$user;
		$this->pass=$pass;
		$this->name_db=$name_db;
    }
	
	public function crear_conexion(){
		
		//mysqli_report(MYSQLI_REPORT_STRICT | MYSQLI_REPORT_ALL);
		
			if($this->mysqli = @mysqli_connect($this->host, $this->user, $this->pass, $this->name_db)){//;
			    return 1;
			}else{
			    return 0;
			}
		
	}
	
	public function execute_sql($ubicacion_file, $username, $password, $email, $first_name, $last_name, $phone, $nombre_empresa, $ruc, $files_pse, $direccion, $region, $pais){
		
		if($fileSQL = @file_get_contents($ubicacion_file)){//;
		
		$fileSQL = $fileSQL . " " . $this->insert_user($username, $password, $email, $first_name, $last_name, $phone);
		
		$fileSQL = $fileSQL . " " . $this->update_empresa($nombre_empresa, $ruc, $files_pse, $phone, $direccion, $region, $pais);
		
			if($this->mysqli->multi_query($fileSQL)){//;
				return 1;
			}else{
				return 0;
			}
		}else{
			return 0;
		}
	}
	
	public function insert_user($username, $password, $email, $first_name, $last_name, $phone){
		
		$fileSQL = "INSERT INTO tec_users
		(id, 
		last_ip_address, ip_address, username, 
		password, email,  created_on, 
		last_login, active, first_name, 
		last_name, phone, gender, 
		group_id) VALUES ( 2,
		 '::1', '::1', '". $username ."',
		 '". $password ."', '". $email ."', ". time() .",
		 ". time() .", 1, '". $first_name ."',
		 '". $last_name ."', '". $phone ."', 'male',
		 1
		);";
		
		return $fileSQL;
			/*if($this->mysqli->query($fileSQL)){//;
				return 1;
			}else{
				return $this->mysqli->error;
			}*/
	}
	
	public function update_empresa($nombre_empresa, $ruc, $files_pse, $phone, $direccion, $region, $pais){
		
		$fileSQL = "UPDATE tec_settings 
		SET tel ='". $phone ."', site_name = '". $nombre_empresa ."', 
		razon_social = '". $nombre_empresa ."', ruc='". $ruc /*."', 
		certificado = '". $files_pse */."', ubicacion_XML = '". $files_pse ."',
		direccion='". $direccion ."', region='". $region ."', pais='". $pais ."'
		WHERE tec_settings.setting_id = 1";
		
		return $fileSQL;

	}
	
	public function cerrar_conexion(){
		mysqli_close($this->mysqli);
	}
	
}