<?php
ini_set("display_errors", 1);
if (!defined('BASEPATH'))
	exit('No direct script access allowed');


class Md_pepsi extends CI_Model {

	public function getProductos($id) {

		$otherdb = $this->load->database('pepsi', TRUE);
		$otherdb->select('*');
		$otherdb->from('productos');
		$otherdb->where('bots_keys_buttons_id', $id);
		$otherdb->where('user_id', 74);

		return $otherdb->get()->result_array();
	}

	public function getProductosAll($type) {

		$otherdb = $this->load->database('pepsi', TRUE);
		$otherdb->select('*');
		$otherdb->from('productos');
		$otherdb->where('tipo', $type);
		$otherdb->where('user_id', 74);

		return $otherdb->get()->result_array();
	}

	public function putPrecio($buttonId, $price, $token, $lat, $long, $lugar, $user) {
		$otherdb = $this->load->database('pepsi', TRUE);
		$otherdb->select('colaboradores_id');
		$otherdb->from('colaboradores_token');
		$otherdb->where('token', $token);
		$test_usr = $otherdb->get()->result_array();
		$return = FALSE;

		if( isset($test_usr[0]['colaboradores_id'] )) {
				$data2 = array(
					'productos_buttons_id' => $buttonId,
					'fecha' => date('Y-m-d H:i:s'),
					'colaboradores_id' => $test_usr[0]['colaboradores_id'],
					'precio' => $price,
					'latitud' => $lat,
					'longitud' => $long,
					'user_id' => $user,
					'lugar' => $lugar,
				);
				$return = $otherdb->insert('precios_nuevos', $data2);
			}
		return $return;
	}


	public function getCuentas($id) {

		$otherdb = $this->load->database('pepsi', TRUE);
		$otherdb->select('*');
		$otherdb->from('colaboradores');
		$otherdb->where('codigo', $id);

		return $otherdb->get()->result_array();
	}

	public function getDocumentos($token) {


		$otherdb = $this->load->database('pepsi', TRUE);
		$otherdb->select('colaboradores_id');
		$otherdb->from('colaboradores_token');
		$otherdb->where('token', $token);
		$test_usr = $otherdb->get()->result_array();
		$return = false;

		if( isset($test_usr[0]['colaboradores_id'] )) {

			$otherdb->select('departamentos_id');
			$otherdb->from('colaboradores');
			$otherdb->where('id', $test_usr[0]['colaboradores_id']);
			$usr = $otherdb->get()->result_array();

		$otherdb->select('*');
		$otherdb->from('documentos');
		$otherdb->where('departamentos_id', $usr[0]['departamentos_id']);

		return $otherdb->get()->result_array();
		} else {
			return FALSE;
		}
	}

	public function getLogin($pin,$token) {

		$otherdb = $this->load->database('pepsi', TRUE);
		$otherdb->select('colaboradores_id');
		$otherdb->from('colaboradores_token');
		$otherdb->where('token', $token);
		$test_usr = $otherdb->get()->result_array();
		$return = false;

		if( empty($test_usr) ) {

			$return = FALSE;

		} else if(isset($test_usr[0]['colaboradores_id'])) {

			$otherdb->select('pin');
			$otherdb->from('colaboradores');
			$otherdb->where('id', $test_usr[0]['colaboradores_id']);
			$usr = $otherdb->get()->result_array();

			if(isset($usr[0]['pin'])){
				if($usr[0]['pin'] == $pin) $return = TRUE;
				else $return = FALSE;
			} else $return = FALSE;

		}

		return $return;
	}

	public function putLogin($token, $userWeb, $user) {

		$otherdb = $this->load->database('pepsi', TRUE);
		$otherdb->select('colaboradores_id');
		$otherdb->from('colaboradores_token');
		$otherdb->where('token', $token);
		$test_usr = $otherdb->get()->result_array();
		$return = false;

		if(isset($test_usr[0]['colaboradores_id'])) {
			$data2 = array(
				'colaboradores_id' => $test_usr[0]['colaboradores_id'],
				'fecha' => date('Y-m-d H:i:s'),
				'usuario' => $userWeb,
				'user_id' => $user,
			);
			$return = $otherdb->insert('colaboradores_access', $data2);
		}


		return $return;
	}

	public function putLoging($token, $userWeb, $user, $latitud, $longitud, $lugar) {
		$otherdb = $this->load->database('pepsi', TRUE);
		$otherdb->select('colaboradores_id');
		$otherdb->from('colaboradores_token');
		$otherdb->where('token', $token);
		$test_usr = $otherdb->get()->result_array();
		$return = false;
		if(isset($test_usr[0]['colaboradores_id'])) {
			$data2 = array(
				'colaboradores_id' => $test_usr[0]['colaboradores_id'],
				'fecha' => date('Y-m-d H:i:s'),
				'usuario' => $userWeb,
				'user_id' => $user,
				'latitud' => $latitud,
				'longitud' => $longitud,
				'lugar' => $lugar,
			);
			$return = $otherdb->insert('colaboradores_login', $data2);
		}
		return $return;
	}

	public function putLogoutg($token, $userWeb, $user, $latitud, $longitud, $lugar) {
		$otherdb = $this->load->database('pepsi', TRUE);
		$otherdb->select('colaboradores_id');
		$otherdb->from('colaboradores_token');
		$otherdb->where('token', $token);
		$test_usr = $otherdb->get()->result_array();
		$return = false;
		if(isset($test_usr[0]['colaboradores_id'])) {
			$data2 = array(
				'colaboradores_id' => $test_usr[0]['colaboradores_id'],
				'fecha' => date('Y-m-d H:i:s'),
				'usuario' => $userWeb,
				'user_id' => $user,
				'latitud' => $latitud,
				'longitud' => $longitud,
				'lugar' => $lugar,
			);
			$return = $otherdb->insert('colaboradores_logout', $data2);
		}
		return $return;
	}

	public function putImage($token, $image, $user) {

		$otherdb = $this->load->database('pepsi', TRUE);
		$otherdb->select('colaboradores_id');
		$otherdb->from('colaboradores_token');
		$otherdb->where('token', $token);
		$otherdb->order_by("id", "DESC");
		$test_usr = $otherdb->get()->result_array();
		$return = false;

		if(isset($test_usr[0]['colaboradores_id'])) {
			$data2 = array(
				'colaboradores_id' => $test_usr[0]['colaboradores_id'],
				'fecha' => date('Y-m-d H:i:s'),
				'imagen' => $image,
				'user_id' => $user,
			);
			$otherdb->insert('colaboradores_fotos', $data2);
			$return = $otherdb->insert_id();

			$otherdb->select('usuario');
			$otherdb->from('colaboradores_access');
			$otherdb->where('colaboradores_id', $test_usr[0]['colaboradores_id']);
			$otherdb->order_by("id", "DESC");
			$otherdb->limit(1);
			$chat = $otherdb->get()->result_array();


			$data3 = array(
				'user' => $chat[0]['usuario'],
				'time' => time(),
				'msg' => $image,
				'type' => 5,
				'account_id' => 214,
				'bots_key_id' => 0,
				'keyenc_id' => 0,
				'message' => "'".$image."'",
				'live' => 0,
			);
			$this->db->insert('chatbot_web', $data3);

		}


		return $return;
	}

	public function putImagea($token, $image, $lat, $long, $user, $tipo) {

		$otherdb = $this->load->database('pepsi', TRUE);
		$otherdb->select('colaboradores_id');
		$otherdb->from('colaboradores_token');
		$otherdb->where('token', $token);
		$otherdb->order_by("id", "DESC");
		$test_usr = $otherdb->get()->result_array();
		$return = false;

		if(isset($test_usr[0]['colaboradores_id'])) {
			$data2 = array(
				'colaboradores_id' => $test_usr[0]['colaboradores_id'],
				'fecha' => date('Y-m-d H:i:s'),
				'imagen' => $image,
				'latitud' => $lat,
				'longitud' => $long,
				'user_id' => $user,
				'tipo' => $tipo,
			);
			$otherdb->insert('colaboradores_fotos', $data2);
			$return = $otherdb->insert_id();

			$otherdb->select('usuario');
			$otherdb->from('colaboradores_login');
			$otherdb->where('colaboradores_id', $test_usr[0]['colaboradores_id']);
			$otherdb->order_by("id", "DESC");
			$otherdb->limit(1);
			$chat = $otherdb->get()->result_array();


			$data3 = array(
				'user' => $chat[0]['usuario'],
				'time' => time(),
				'msg' => $image,
				'type' => 5,
				'account_id' => 214,
				'bots_key_id' => 0,
				'keyenc_id' => 0,
				'message' => "'".$image."'",
				'live' => 0,
			);
			$this->db->insert('chatbot_web', $data3);

		}


		return $return;
	}

	public function updateImage($id, $lat, $long) {

			$return = FALSE;
			$otherdb = $this->load->database('pepsi', TRUE);
			$data2 = array(
							'latitud' => $lat,
							'longitud' => $long,
			);
			$otherdb->where('id', $id);
			$return = $otherdb->update('colaboradores_fotos', $data2);

			return $return;
	}

	public function putToken($id) {

		$token = $id.md5(microtime(true).mt_Rand());
		$otherdb = $this->load->database('pepsi', TRUE);
		$otherdb->select('*');
		$otherdb->from('colaboradores_token');
		$otherdb->where('colaboradores_id', $id);
		$test_usr = $otherdb->get()->result_array();
		$return = false;

		if( empty($test_usr) ) {
			$data2 = array(
							'colaboradores_id' => $id,
							'fecha' => date('Y-m-d H:i:s'),
							'token' => $token,
			);
			$return = $otherdb->insert('colaboradores_token', $data2);

		} else if(isset($test_usr[0]['id'])) {
			$data2 = array(
					'fecha' => date('Y-m-d H:i:s'),
					'token' => $token,
			);
			$otherdb->where('id', $test_usr[0]['id']);
			$return = $otherdb->update('colaboradores_token', $data2);
		}


		return $token;
	}


	public function getAll() {

	/*	$otherdb = $this->load->database('pepsi', TRUE);
		$otherdb->select('*');
		$otherdb->from('clientes');
		$mante = $otherdb->get()->result_array();
$return = array();
$count = 0;

		foreach ($mante as $value) {

				$otherdb->select('*');
				$otherdb->from('colaboradores');
				$otherdb->where('codigo', $value['colaboradores_codigo']);

				$mante2 = $otherdb->get()->result_array();

				if($mante2 == FALSE){
					$return['colaboradores'][] = $value['colaboradores_codigo'];
					$count++;
				}

		}
		$return['count'] = $count;
		*/
		$otherdb = $this->load->database('pepsi', TRUE);
		$otherdb->select('*');
		$otherdb->from('clientes');
		$mante = $otherdb->get()->result_array();

		$return = array();
		$count = 0;
		$test = FALSE;
		foreach ($mante as $value) {


				if($value['supervisor_codigo'] == 0){
					$data2 = array(
									'supervisor_codigo' => NULL,
									//'supervisor_codigo' => $value['super'],
					);
					$otherdb->where('id', $value['id']);
					$test = $otherdb->update('clientes', $data2);
				} else {
					$test = FALSE;
				}



			if($test){
				$return['clientes'][] = $value['id'];
				$count++;
			}

		}
		$return['count'] = $count;

		return $return;
	}

}
