<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class caract_terreno extends Admin_Controller {

	public function __construct()
	{
		parent::__construct();

		$this->load->model('ficha_catastral/caract_terreno_model');
		$this->lang->load('ficha_catastral/caract_terreno_lang');

		/* Title Page :: Common */
		$this->page_title->push(lang('menu_caract_terreno'));
		$this->data['pagetitle'] = $this->page_title->show();

		$_SESSION['id_modulo_ant'] = $_SESSION['id_modulo'];

		//echo '<pre>';
		//print_r($_SESSION['id_modulo_ant']);
		//echo '</pre>';
		//show_error(1);

		//if ($_SESSION['id_modulo'] != 'ENCUESTAS' AND $_SESSION['id_modulo'] != 'CONSULTAS_REG') {

		if ($_SESSION['id_modulo'] != 'caract_terreno') {
			//show_error($_SESSION['id_modulo']);
			if ($_SESSION['id_modulo'] != 'consultas') {
				
				$_SESSION['arraywhere_cuest'] = NULL;
				$_SESSION['arraywhere_cuest_busq'] = NULL;	
			}
			
			$_SESSION['id_modulo'] = 'caract_terreno';

			$_SESSION['errores_ficha_catastral'] = NULL;
			is_array($_SESSION['errores_ficha_catastral']);
		}

		/* Breadcrumbs :: Common */
		$this->breadcrumbs->unshift(1, lang('menu_lotes'), 'ficha_catastral/lotes/lotes/index');
	}
	
	
	public function lotes_caract_terreno($id_lote, $modo_lote)
	{
		if ($_SESSION['id_modulo_ant'] != 'caract_terreno') {
			
			if (isset($_SERVER["HTTP_REFERER"])) {
				$_SESSION['url_anterior'] = $_SERVER["HTTP_REFERER"];
			}
		}

		if ( ! $this->ion_auth->logged_in() OR ! $this->ion_auth->is_admin())
		{
			redirect('auth', 'refresh');
		}

		/* Breadcrumbs */
		$this->breadcrumbs->unshift(2, lang('lotes_caract_terreno'), 'caract_terreno/create');
		$this->data['breadcrumb'] = $this->breadcrumbs->show();


		$_SESSION['identificador'] 	= $id_lote;

		$lotes 	= $this->caract_terreno_model->view($_SESSION['identificador'])->row();

		$this->data['lotes_info'] 			= $lotes;


		$this->data['v_factores_lotes'] = $this->caract_terreno_model->viewlist_abm('v_lotes_factores', $_SESSION['identificador'])->result();

		$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('//message')));

		/* Load Template */
		$this->template->admin_render('ficha_catastral/lotes/caract_terreno', $this->data);
		
	}
	
	
	public function caract_terreno_edit($id_lote)
	{
		if ( ! $this->ion_auth->logged_in() OR ! $this->ion_auth->is_admin())
		{
			redirect('auth', 'refresh');
		}

		/* Breadcrumbs */
		$this->breadcrumbs->unshift(3, lang('lotes_lotes_factores'), 'ficha_catastral/lotes_caract_terreno');
		$this->data['breadcrumb'] = $this->breadcrumbs->show();

		$_SESSION['identificador'] = $id_lote;

		$v_factores_lotes = $this->caract_terreno_model->view_edit_f_lote('lotes_factores', array('id_lote' => $_SESSION['identificador'], 'id_lote' => $id_lote))->row();

		/* Validate form input */
		//$this->form_validation->set_rules('p01_a', 'lang:geocolonia_col_cod' , 'required');
		//$this->form_validation->set_rules('p01_b','lang:geocolonia_col_desc', 'required');

		//if ($this->form_validation->run() == TRUE)
		if ($_POST)
		{
			if ($_POST['id_lote']) {
				$arraycampos['id_lote']         	= $_POST['id_lote'];
			}

			if ($_POST['id_avaluo']) {
				$arraycampos['id_avaluo']  	= $_POST['id_avaluo'];
			}

			if ($_POST['dom_dominio']) {
				$arraycampos['dom_dominio']         	= $_POST['dom_dominio'];
			}

			if ($_POST['dom_valor']) {
				$arraycampos['dom_valor']         	= $_POST['dom_valor'];
			}

			if ($_POST['coeficiente']) {
				$arraycampos['coeficiente']      	= $_POST['coeficiente'];
			}



			$update_lotes_factores	= $this->caract_terreno_model->actualizar_cuest(array('id_lote' => $_SESSION['identificador'], 'id_lote' => $id_lote), 'caract_terreno', $arraycampos);

			/****** Actualizar tabla MONITOREO Y ASIGNACIONLOTES *********************/
			/**
			Se actualiza el campo PROCESO pasa a ser A03
			**/
			/********$monitoreoasignacion = $this->reg01_model->view('monitoreo_verificador', array('id_registro_lote' => $_SESSION['identificador']))->row();
			$reg02 = $this->reg01_model->view('reg02', array('id_registro_lote' => $_SESSION['identificador']))->row();
			if ($monitoreoasignacion->proceso == 'A04' || $monitoreoasignacion->proceso == 'A07') 
			{   
				$arraycampos1 = array('registro_analizado' => 0
					,'proceso'            => 'A03' 
					//,'mapear'             => $reg02->mapear
					);
						//show_error($reg02->mapear. ' RECH ');
				$update_monitoreo = $this->reg01_model->actualizar_cuest(array('id_registro_lote' => $_SESSION['identificador']),
					'monitoreo',
					$arraycampos1
					);

				$cantidad_reg_analizado = $monitoreoasignacion->cantidad_reg_analizado - 1;

				$arraycampos2 = array('cantidad_reg_analizado' => $cantidad_reg_analizado);
				$update_asignacion = $this->reg01_model->actualizar_cuest(array('lote' => $monitoreoasignacion->lote),
					'asignacionlotes',
					$arraycampos2
					);
			}elseif ($monitoreoasignacion->proceso == 'A02' || $monitoreoasignacion->proceso == 'A03') 
			{
				if ((string)$monitoreoasignacion->fecha_procesado_4 == '0001-01-01' || 
					(string)$monitoreoasignacion->fecha_procesado_4 == '1970-01-01' || 
					$monitoreoasignacion->fecha_procesado_4 == null || 
					$monitoreoasignacion->fecha_procesado_4 == "" )  {
					$str = date_default_timezone_get();
					$fecha_procesado_4 =date("d/m/Y", strtotime($str));
					
				$arraycampos3 = array('proceso' 		   => 'A03'
					//,'mapear' 			   => $reg02->mapear
					,'procesado_4'		   => 1
					,'fecha_procesado_4'	   => $fecha_procesado_4
					
					);
				}
				else {

				$str = date_default_timezone_get();
				$fecha_procesado_4 = date("d/m/Y", strtotime($str));
				$arraycampos3 = array('proceso' 		   => 'A03'
					//,'mapear' 			   => $reg02->mapear
					,'procesado_4'		   => 1
					
					);
				}

			//	$arraycampos3 = array('proceso'            => 'A03',
			//		'mapear'             => $reg02->mapear
			//		);
						//show_error($reg02->mapear. ' ASIG ');
				$update_monitoreo = $this->reg01_model->actualizar_cuest(array('id_registro_lote' => $_SESSION['identificador']),
					'monitoreo',
					$arraycampos3
					);
				
				$str = date_default_timezone_get();
				$fecha = date("d/m/Y", strtotime($str));
				$arraycampos4 = array('fecha_inicio' => $fecha);

				$update_monitoreo = $this->reg01_model->actualizar_cuest(array('lote' => $monitoreoasignacion->lote),
					'asignacionlotes',
					$arraycampos4
					);
			}*/
			/*****************************************************************/
			
			redirect('cuest/reg01/reg10_poblacion_censo');

		}
		else
		{
			$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

			$this->data['id_lote'] = array(
				'name'  => 'id_lote',
				'id'    => 'id_lote',
				'type'  => 'text',
				'class' => 'form-control',
				'value' => $this->form_validation->set_value('id_lote', $lotes_factores->id_lote),
				'maxlength' => 40
				);
			$this->data['id_avaluo'] = array(
				'name'  => 'id_avaluo',
				'id'    => 'id_avaluo',
				'type'  => 'text',
				'class' => 'form-control',
				'value' => $this->form_validation->set_value('id_avaluo', $lotes_factores->id_avaluo),
				'maxlength' => 12
				);
			$this->data['dom_dominio'] = array(
				'name'  => 'dom_dominio',
				'id'    => 'dom_dominio',
				'type'  => 'text',
				'class' => 'form-control',
				'value' => $this->form_validation->set_value('dom_dominio', $lotes_factores->dom_dominio),
				'maxlength' => 20
				);
			$this->data['dom_valor'] = array(
				'name'  => 'dom_valor',
				'id'    => 'dom_valor',
				'type'  => 'text',
				'class' => 'form-control',
				'value' => $this->form_validation->set_value('dom_valor', $lotes_factores->dom_valor),
				'maxlength' => 20
				);
			$this->data['coeficiente'] = array(
				'name'  => 'coeficiente',
				'id'    => 'coeficiente',
				'type'  => 'text',
				'class' => 'form-control',
				'value' => $this->form_validation->set_value('coeficiente', $reg10_poblacion_censo->p06),
				'maxlength' => 7
				);
			$this->data['p08'] = array(
				'name'  => 'p08',
				'id'    => 'p08',
				'type'  => 'number',
				'max'   => 110,
				'class' => 'form-control',
				'value' => $this->form_validation->set_value('p08', $reg10_poblacion_censo->p08),
				'maxlength' => 3
				);
			$this->data['p09_esp'] = array(
				'name'  => 'p09_esp',
				'id'    => 'p09_esp',
				'type'  => 'text',
				'class' => 'form-control',
				'value' => $this->form_validation->set_value('p09_esp', $reg10_poblacion_censo->p09_esp),
				'maxlength' => 20
				);
			$this->data['p10_esp'] = array(
				'name'  => 'p10_esp',
				'id'    => 'p10_esp',
				'type'  => 'text',
				'class' => 'form-control',
				'value' => $this->form_validation->set_value('p10_esp', $reg10_poblacion_censo->p10_esp),
				'maxlength' => 20
				);
			$this->data['p11_esp'] = array(
				'name'  => 'p11_esp',
				'id'    => 'p11_esp',
				'type'  => 'text',
				'class' => 'form-control',
				'value' => $this->form_validation->set_value('p11_esp', $reg10_poblacion_censo->p11_esp),
				'maxlength' => 20
				);
			

			
			$p02_filtro = $this->reg01_model->concepto('P02_FILTRO',false,'CONCEPTO')->result_array();
			//inserta en un array asociativo para poder utilizarlo en el form_dropdown
			foreach ($p02_filtro as $p02f) {
				$p02_fs[$p02f['concepto']] = $p02f['concepto'].' - '.$p02f['nombre'];
			}
			$this->data['p02_filtro'] = $p02_fs;
			$this->data['p02_filtro_sel'] = $reg10_poblacion_censo->p02_filtro;

			$p03 = $this->reg01_model->concepto('P03',false,'CONCEPTO')->result_array();
			//inserta en un array asociativo para poder utilizarlo en el form_dropdown
			foreach ($p03 as $p03_) {
				$p03s[$p03_['concepto']] = $p03_['concepto'].' - '.$p03_['nombre'];
			}
			$this->data['p03'] = $p03s;
			$this->data['p03_sel'] = $reg10_poblacion_censo->p03;

			$p04 = $this->reg01_model->concepto('P04',false,'CONCEPTO')->result_array();
			//inserta en un array asociativo para poder utilizarlo en el form_dropdown
			foreach ($p04 as $p04_) {
				$p04s[$p04_['concepto']] = $p04_['concepto'].' - '.$p04_['nombre'];
			}
			$this->data['p04'] = $p04s;
			$this->data['p04_sel'] = $reg10_poblacion_censo->p04;

			$p05 = $this->reg01_model->concepto('P05',false,'CONCEPTO')->result_array();
			//inserta en un array asociativo para poder utilizarlo en el form_dropdown
			foreach ($p05 as $p05_) {
				$p05s[$p05_['concepto']] = $p05_['concepto'].' - '.$p05_['nombre'];
			}
			$this->data['p05'] = $p05s;
			$this->data['p05_sel'] = $reg10_poblacion_censo->p05;

			$p07 = $this->reg01_model->concepto('P07',false,'CONCEPTO')->result_array();
			//inserta en un array asociativo para poder utilizarlo en el form_dropdown
			foreach ($p07 as $p07_) {
				$p07s[$p07_['concepto']] = $p07_['concepto'].' - '.$p07_['nombre'];
			}
			$this->data['p07'] = $p07s;
			$this->data['p07_sel'] = $reg10_poblacion_censo->p07;

			$p09 = $this->reg01_model->concepto('P09',false,'CONCEPTO')->result_array();
			//inserta en un array asociativo para poder utilizarlo en el form_dropdown
			foreach ($p09 as $p09_) {
				$p09s[$p09_['concepto']] = $p09_['concepto'].' - '.$p09_['nombre'];
			}
			$this->data['p09'] = $p09s;
			$this->data['p09_sel'] = $reg10_poblacion_censo->p09;

			$p10 = $this->reg01_model->concepto('P10',false,'CONCEPTO')->result_array();
			//inserta en un array asociativo para poder utilizarlo en el form_dropdown
			foreach ($p10 as $p10_) {
				$p10s[$p10_['concepto']] = $p10_['concepto'].' - '.$p10_['nombre'];
			}
			$this->data['p10'] = $p10s;
			$this->data['p10_sel'] = $reg10_poblacion_censo->p10;

			$p11 = $this->reg01_model->concepto('P11',false,'CONCEPTO')->result_array();
			//inserta en un array asociativo para poder utilizarlo en el form_dropdown
			foreach ($p11 as $p11_) {
				$p11s[$p11_['concepto']] = $p11_['concepto'].' - '.$p11_['nombre'];
			}
			$this->data['p11'] = $p11s;
			$this->data['p11_sel'] = $reg10_poblacion_censo->p11;

			$p12ab = $this->reg01_model->concepto('P12AB',false,'CONCEPTO')->result_array();
			//inserta en un array asociativo para poder utilizarlo en el form_dropdown
			foreach ($p12ab as $p12ab_) {
				$p12abs[$p12ab_['concepto']] = $p12ab_['concepto'].' - '.$p12ab_['nombre'];
			}
			$this->data['p12ab'] = $p12abs;
			$this->data['p12ab_sel'] = $reg10_poblacion_censo->p12ab;

			/* Load Template */
			$this->template->admin_render('cuest/reg01/reg10_poblacion_censo_edit', $this->data);
		}
	}

	public function reg10_poblacion_censo_delete($id)
	{
		$reg_delete = $this->reg01_model->eliminar_registro('reg10',array('id_registro_lote' => $_SESSION['identificador'], 'p01a' => $id));

		if ($reg_delete)
		{
			redirect('cuest/reg01/reg10_poblacion_censo');
		}
		else
		{
			$this->session->set_flashdata('message', $this->ion_auth->errors());
		}
	}

	public function reg11_ocup_v()
	{
				if ( ! $this->ion_auth->logged_in() OR ! $this->ion_auth->is_admin())
				{
					redirect('auth', 'refresh');
				}

				/* Breadcrumbs */
				$this->breadcrumbs->unshift(2, lang('reg02_datos_de_la_ocupacion'), 'reg01/create');
				$this->data['breadcrumb'] = $this->breadcrumbs->show();

				$this->data['otras_personas'] = $this->reg01_model->viewlist_abm('reg11', $_SESSION['identificador'])->result();

				$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('//message')));

				/* Load Template */
				$this->template->admin_render('cuest/reg01/reg11_ocup_v', $this->data);

	}

}