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

class Reports extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('mo_responden');
		$config = array('module' => 'responden/reports','roleid'=> $this->session->userdata('sRoleId'));
		$this->load->library('rolemodule',$config);
    }
    
	public function index()
    {
		$this->rolemodule->view_page();
		$customer = $this->input->get_post('f_name_id');
		$last_event = $this->mo_responden->last_event($customer,$this->input->get_post('f_event_id'));
		//$event = ($this->input->get_post('f_event_id')) ? $this->input->get_post('f_event'): $last_event;
		
		$data = array(
			'app_title'=>'Data Reports',
			'app_desc' =>'it all Reports'
		);
		
		$data['result'] = false;
		if(!empty($customer) && !empty($last_event))
		{
			$data['kategori'] 		= $this->mo_responden->comboDataKategori();
			$data['subkategori']	= $this->mo_responden->comboDataSubKategori();
			$data['event']			= $last_event;
			$get_data = $this->mo_responden->getDataResponden($customer,$last_event);
			$data['result'] 		= $get_data;
			$data['bisnis'] 		= $this->bisnis_om($get_data);
		}
		
        $data['content']	= 'vw_reports';
        $this->load->view('webadmin/main', $data);
    }

	public function fnListCustomersEvent()
	{
		$this->db->select('f_event_id, f_event_no',false);
		$this->db->where('f_customer_id',$this->input->get_post('group'));
		$this->db->like('f_event_no',$this->input->get_post('query'), 'both');
		$this->db->limit(15);
		$sql = $this->db->get('t_event');
		$data = array();
		if($sql->num_rows()>0)
		{
			foreach($sql->result() as $a)
			{
				$data[] = array(
					'id' => $a->f_event_id,
					'label' => $a->f_event_no,
				);
			}
			echo json_encode($data);
		}
		else
		{
			$data = array(
					'id' => 0,
					'label' => 'Harap pilih Customers',
				);
			echo json_encode(array($data));
		}
	}
	
	function bisnis_om($data)
	{
		$final = array();$bisnis_on = array();$bisnis_minat = array();
		foreach($data as $r)
		{
			if($r['f_bisnis_on']) {
				foreach($r['f_bisnis_on'] as $a => $b)
				{
					$bisnis_on[$b] +=1;
				}
			}
			if($r['f_bisnis_minat']) {
				foreach($r['f_bisnis_minat'] as $a => $b)
				{
					$bisnis_minat[$b] +=1;
				}
			}
		}
		$final = array('on' => $bisnis_on,'minat' => $bisnis_minat);
		return $final;
	}
	
	public function cetakExcel($id = null,$periode = null)
	{
		$kategori = $this->mo_responden->comboDataKategori();
		$subkategori = $this->mo_responden->comboDataSubKategori();


		$last_event = $this->mo_responden->last_event($id,$periode);
		$customer = $this->mo_responden->getDataCustomer($id,$last_event);
		//echo "<pre>";echo print_r($customer)."</pre><br/>";
		$get_data = $this->mo_responden->getDataResponden($id,$last_event);
		$_bisnis 		= $this->bisnis_om($get_data);

		$date = date('Ymd_His');
        $namaFile = "report_".str_replace(' ','',$customer['f_customer_name'])."_".$customer['f_event_no']."_".$date.".xls";

		//echo "<td>".$namaFile."</td>";
		header("Content-Disposition: attachment; filename=" . $namaFile . "");
		header("Content-type: application/download");
		header("Pragma: no-cache");
		header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
		header("Pragma: public");
		
		$html = "<table border=\"1\" width=\"100%\">
				<tr>
					<th rowspan=\"2\">No</th>
					<th rowspan=\"2\">Nama</th>
					<th rowspan=\"2\">Tgl Lahir</th>
					<th rowspan=\"2\">Jenis Kelamin</th>
					<th rowspan=\"2\">Bagian/Jabatan</th>
					<th rowspan=\"2\">Email</th>
					<th rowspan=\"2\">HP</th>
					<th rowspan=\"2\">Alamat</th>
					<th rowspan=\"2\">Bisnis</th>
					<th rowspan=\"2\">Bisnis Now</th>
					<th rowspan=\"2\">Minat Bisnis</th>
					<th rowspan=\"2\">Sehat 1</th>
					<th rowspan=\"2\">Sehat 2</th>
					<th rowspan=\"2\">Uk. Kaos</th>
					<th colspan=\"10\">".$kategori[1]['desc']."</th>
					<th rowspan=\"2\">Total</th>
					<th rowspan=\"2\">Kesiapan Pensiun</th>
					<th rowspan=\"2\">Modus</th>
					<th rowspan=\"2\">Tipe Pensiun</th>
				</tr>
				<tr>";
		foreach($subkategori[1] as $a)
		{
			$html .= "<th colspan=\"2\">".$a['name']."</th>";
		}
		$html .= "</tr>";
		// body
		$i=1; $data_pensiun = array(); $data_pensiun1 = array();
		foreach($get_data as $a)
		{
			$fs = ($a['f_sehat1'] == 1) ? 'Ya':'Tidak';
			$html .= "<tr>";
			$html .= "<td>".$i."</td>
					<td>".$a['f_name']."</td>
					<td>".$a['f_age']."</td>
					<td>".$a['f_gender']."</td>
					<td>".$a['f_jabatan']."</td>
					<td>".$a['f_email']."</td>
					<td>".$a['f_hp']."</td>
					<td>".$a['f_alamat']."</td>
					<td>".$a['f_bisnis']."</td>
					<td>".join(', ',$a['f_bisnis_on'])."</td>
					<td>".join(', ',$a['f_bisnis_minat'])."</td>
					<td>".$fs."</td>
					<td>".join(', ',$a['f_sehat2'])."</td>
					<td>".$a['f_survey']['ukuran_kaos']."</td>";
			$h=0;$j=0; $modus = array();
			foreach($subkategori[1] as $b => $c)
			{
				$d = $a['f_survey']['subkategori'][$b];
				foreach($a['f_survey']['subkategori'][$b] as $e => $f)
				{
					if($e == 6 && $f['urut'] == $d[1]['urut'] || $e == 7 && $f['urut'] == $d[2]['urut'] || $e == 8 && $f['urut'] == $d[3]['urut'] || $e == 9 && $f['urut'] == $d[4]['urut'] || $e == 10 && $f['urut'] == $d[5]['urut'])
					{
						$hasil_akhir = 1;
					}
					else
					{
						$hasil_akhir = $f['jawab'];
					}
					
					$html .= "<td>".$hasil_akhir."</td>";
					$modus[$h] = $hasil_akhir;
					$j += $hasil_akhir;
					$h++;
				}
			}
			// Total Kesiapan dan Hasilnya
			$html .= "<td>".$j."</td>";
			$html .= "<td>".kesiapan($j)."</td>";
			
			$data_pensiun[kesiapan($j)] += 1;
			// Modus dan Hasilnya
			$values = array_count_values($modus);
			//asort($values);
			$hmodus = array_search(max($values), $values); 
			$html .= "<td>".$hmodus."</td>";
			$html .= "<td>".tipe_pensiun($hmodus)."</td>";
			$data_pensiun1[tipe_pensiun($hmodus)] += 1;
			$html .= "</tr>";
			$i++;
		}		
		$html .= "</table>";

		$html .= '<br style="mso-data-placement:same-cell;" /> ';
		$html .= '<br style="mso-data-placement:same-cell;" /> ';

		$tipe_pensiun = array(
			1 => 'Berlibur',
			2 => 'Istirahat',
			3 => 'Aktif',
			4 => 'Terencana',
		);
		krsort($tipe_pensiun);
		

		$html .= "<table  border=\"1\" width=\"100%\">
					<tr>
						<th>Kode</th>
						<th>Tipe Pensiun</th>
						<th>Tallies</th>
					</tr>";
					$total = 0;
					foreach($tipe_pensiun as $a => $b)
					{
						$html .= "<tr>";
						$html .= "<td>".$a."</td>";
						$html .= "<td>".$b."</td>";
						$st = ($data_pensiun1[$b]) ? $data_pensiun1[$b]:0;
						$html .= "<td style=\"text-align:center;\">".$st."</td>";
						$html .= "</tr>";
						$total += $st;
					}
					
		$html .= "<tr>
					<th colspan=\"2\">Total Peserta</th>
					<th>".$total."</th>
				</tr>
			</table>";
		
		//echo $html;	
			
		$html .= '<br style="mso-data-placement:same-cell;" /> ';
		$html .= '<br style="mso-data-placement:same-cell;" /> ';

		$html .=  "<table border=\"1\" width=\"100%\">
			<tr>
				<th rowspan=\"2\">No</th>
				<th rowspan=\"2\">Nama</th>
				<th rowspan=\"2\">Tgl Lahir</th>
				<th rowspan=\"2\">Jenis Kelamin</th>
				<th rowspan=\"2\">Bagian/Jabatan</th>
				<th rowspan=\"2\">Email</th>
				<th rowspan=\"2\">HP</th>
				<th rowspan=\"2\">Alamat</th>
				<th rowspan=\"2\">Bisnis</th>
				<th rowspan=\"2\">Bisnis Now</th>
				<th rowspan=\"2\">Minat Bisnis</th>
				<th rowspan=\"2\">Uk. Kaos</th>
				<th colspan=\"10\">".$kategori[2]['desc']."</th>
				<th rowspan=\"2\">Total</th>
				<th rowspan=\"2\">Tingkat Kecemasan</th>
				<th rowspan=\"2\">Tipe Kecemasan karena kehilangan</th>
			</tr>
			<tr>";
		foreach($subkategori[2] as $a)
		{
			$html .= "<th colspan=\"2\">".$a['name']."</th>";
		}
		$html .= "</tr>";
		// body
		$i=1; $data_cemas = array(); $data_cemas1 = array();
		foreach($get_data as $a)
		{
			$html .= "<tr>
						<td>".$i."</td>
						<td>".$a['f_name']."</td>
						<td>".$a['f_age']."</td>
						<td>".$a['f_gender']."</td>
						<td>".$a['f_jabatan']."</td>
						<td>".$a['f_email']."</td>
						<td>".$a['f_hp']."</td>
						<td>".$a['f_alamat']."</td>
						<td>".$a['f_bisnis']."</td>
						<td>".join(', ',$a['f_bisnis_on'])."</td>
						<td>".join(', ',$a['f_bisnis_minat'])."</td>
						<td>".$a['f_survey']['ukuran_kaos']."</td>";
			$h=0;$j=0; $modus = array();
			foreach($subkategori[2] as $b => $c)
			{
				$d = $a['f_survey']['subkategori'][$b];
				foreach($a['f_survey']['subkategori'][$b] as $e => $f)
				{
					if($e == 16 && $f['urut'] == $d[11]['urut'] || $e == 17 && $f['urut'] == $d[12]['urut'] || $e == 18 && $f['urut'] == $d[13]['urut'] || $e == 19 && $f['urut'] == $d[14]['urut'] || $e == 20 && $f['urut'] == $d[15]['urut'])
					{
						$hasil_akhir = 1;
					}
					else
					{
						$hasil_akhir = $f['jawab'];
					}
					
					$cemas[$i][$hasil_akhir] .= $c['desc'].',';
					$html .= "<td>".$hasil_akhir."</td>";
					$modus[$h] = $hasil_akhir;
					$j += $hasil_akhir;
					$h++;
				}
			}
			// Total Kesiapan dan Hasilnya
			$html .= "<td>".$j."</td>";
			$html .= "<td>".kecemasan($j)."</td>";
			
			$data_cemas[kecemasan($j)] += 1;

			// Modus dan Hasilnya
			$values = array_count_values($modus);
			ksort($values);
			$hmodus = array_search(key($values), $values); 
			$trim = trim($cemas[$i][key($values)],',');
			$html .= "<td>".$trim."</td>";
			$html .= "</tr>";
			$explode = explode(',',$trim);
			foreach($explode as $x => $y)
			{
				$data_cemas1[$y] +=1;
			}
			$i++;
		}		
		
		$html .= "</table>";		
		$html .= '<br style="mso-data-placement:same-cell;" /> ';
		$html .= '<br style="mso-data-placement:same-cell;" /> ';

		$cemas = array(
			0 => array("range" => "< 20","name" => "Panik"),
			1 => array("range" => "21 - 23","name" => "Agak Panik"),
			2 => array("range" => "24 - 27","name" => "Kecemasan Sedang"),
			3 => array("range" => "28 - 31","name" => "Cukup Cemas"),
			4 => array("range" => "> 32","name" => "Kecemasan Ringan"),
		);
		
		$cemas1 = array(
			0 => array('val'=>'Hilang Pekerjaan','name'=>'Kehilangan Pekerajaan'),
			1 => array('val'=>'Hilang Keuangan','name'=>'Kehilangan Sumber Keuangan'),
			2 => array('val'=>'Hilang Rekan','name'=>'Kehilangan Rekan Kerja'),
			3 => array('val'=>'Hilang Kesehatan','name'=>'Menurunnya Tingkar Kesehatan'),
			4 => array('val'=>'Hilang Jabatan','name'=>'Kehilangan Jabatan'),
		);

		$html .= "<table border=\"1\" width=\"100%\">
				<thead>
					<tr>
						<th>Range Skor</th>
						<th>Tallies</th>
						<th>Tipe Kecemasan Pensiun</th>
					</tr>
				</thead>
				<tbody>";
			$total1 = 0;
			foreach($cemas as $a => $b)
			{
				$html .= "<tr>";
				$html .= "<td>".$b['range']."</td>";
				$st = ($data_cemas[$b['name']]) ? $data_cemas[$b['name']]:0;
				$html .= "<td style=\"text-align:center;\">".$st."</td>";
				$html .= "<td>".$b['name']."</td>";
				$html .= "</tr>";
				$total1 += $st;
			}
		$html .= "<tr>
					<th>Total</th>
					<th>".$total1."</th>
					<th></th>
				</tr>
			</table>";
			
		$html .= '<br style="mso-data-placement:same-cell;" /> ';
		$html .= '<br style="mso-data-placement:same-cell;" /> ';

		$html .= "<table border=\"1\" width=\"100%\">
					<tr>
						<th>Penyebab Kecemasan Utama</th>
						<th>Tallies</th>
					</tr>";
				$total1 = 0;
				foreach($cemas1 as $a => $b)
				{
					$html .= "<tr>";
					$html .= "<td>".$b['name']."</td>";
					$st = ($data_cemas1[$b['val']]) ? $data_cemas1[$b['val']]:0;
					$html .= "<td style=\"text-align:center;\">".$st."</td>";
					$html .= "</tr>";
					$total1 += $st;
				}
		$html .= "<tr>
					<th>Total Penyebab Kecemasan</th>
					<th>".$total1."</th>
				</tr>
			</table>";

		$html .= '<br style="mso-data-placement:same-cell;" /> ';
		$html .= '<br style="mso-data-placement:same-cell;" /> ';

		$html .= "<table  border=\"1\" width=\"100%\">
					<tr>
						<th>Usaha / Aktivitas yang sudah dijalani</th>
						<th>Total</th>
					</tr>";
					$total = 0;
					foreach($_bisnis['on'] as $a => $b)
					{
						$html .= "<tr>";
						$html .= "<td>".$a."</td>";
						$html .= "<td>".$b."</td>";
						$html .= "</tr>";
						$total++;
					}
					
		$html .= "<tr>
					<th>Total</th>
					<th>".$total."</th>
				</tr>
			</table>";

		$html .= '<br style="mso-data-placement:same-cell;" /> ';
		$html .= '<br style="mso-data-placement:same-cell;" /> ';

		$html .= "<table  border=\"1\" width=\"100%\">
					<tr>
						<th>Minat / Rencana usaha</th>
						<th>Total</th>
					</tr>";
					$total = 0;
					foreach($_bisnis['minat'] as $a => $b)
					{
						$html .= "<tr>";
						$html .= "<td>".$a."</td>";
						$html .= "<td>".$b."</td>";
						$html .= "</tr>";
						$total++;
					}
					
		$html .= "<tr>
					<th>Total</th>
					<th>".$total."</th>
				</tr>
			</table>";

		echo $html;
	} 

	public function cetakJawabExcel($id = null,$periode = null)
	{
		$kategori = $this->mo_responden->comboDataKategori();
		$subkategori = $this->mo_responden->comboDataSubKategori();
		$soal = $this->mo_responden->comboDataSoal();
		//echo "<pre>";echo print_r($soal)."</pre><br/>";

		$customer = $this->mo_responden->get_event_no($id,$periode);
		$f_event_id = $customer['f_event_id'];
		//echo "<pre>";echo print_r($customer)."</pre><br/>";
		//die();
		//$this->db->limit(1);
		$get_data = $this->mo_responden->getDataRespondenEvent($id,$f_event_id);
		$_bisnis 		= $this->bisnis_om($get_data);
		//echo "<pre>";echo print_r($get_data)."</pre><br/>";
		//die();

		$date = date('Ymd_His');
        $namaFile = "jawab_peserta_".str_replace(' ','',$customer['f_customer_name'])."_".$customer['f_event_no']."_".$date.".xls";

		//echo "<td>".$namaFile."</td>";
		header("Content-Disposition: attachment; filename=" . $namaFile . "");
		header("Content-type: application/download");
		header("Pragma: no-cache");
		header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
		header("Pragma: public");
		
		$html = "<table border=\"1\" width=\"100%\">
				<tr>
					<th rowspan=\"2\">No</th>
					<th rowspan=\"2\">Nama</th>
					<th rowspan=\"2\">Tgl Lahir</th>
					<th rowspan=\"2\">Jenis Kelamin</th>
					<th rowspan=\"2\">Bagian/Jabatan</th>
					<th rowspan=\"2\">Email</th>
					<th rowspan=\"2\">HP</th>
					<th rowspan=\"2\">Alamat</th>
					<th rowspan=\"2\">Bisnis</th>
					<th rowspan=\"2\">Bisnis Now</th>
					<th rowspan=\"2\">Minat Bisnis</th>
					<th rowspan=\"2\">Sehat 1</th>
					<th rowspan=\"2\">Sehat 2</th>
					<th rowspan=\"2\">Uk. Kaos</th>
					<th rowspan=\"2\">Uk. Kaos Pasangan</th>
					<th colspan=\"".count($soal)."\">Soal</th>
				</tr>
				<tr>";
		foreach($soal as $a => $b)
		{
			$html .= "<th colspan=\"\">".$b."</th>";
		}
		$html .= "</tr>";
		// body
		$i=1; $data_pensiun = array(); $data_pensiun1 = array();
		foreach($get_data as $a)
		{
			$fs = ($a['f_sehat1'] == 1) ? 'Ya':'Tidak';
			$jawaban = array();
			foreach($a['f_survey']['kategori'] as $e => $f)
			{
				//echo "<pre>f : ";echo print_r($f)."</pre><br/>";
				foreach($f as $g => $h)
				{
					$jawaban[$g] = $h['jawab'];
				}
			}
			$fsurvey = json_decode($a['f_survey'],true);
			//echo "<pre>";echo print_r($jawaban)."</pre><br/>";

			$html .= "<tr>";
			$html .= "<td>".$i."</td>
					<td>".$a['f_name']."</td>
					<td>".$a['f_age']."</td>
					<td>".$a['f_gender']."</td>
					<td>".$a['f_jabatan']."</td>
					<td>".$a['f_email']."</td>
					<td>".$a['f_hp']."</td>
					<td>".$a['f_alamat']."</td>
					<td>".$a['f_bisnis']."</td>
					<td>".join(', ',$a['f_bisnis_on'])."</td>
					<td>".join(', ',$a['f_bisnis_minat'])."</td>
					<td>".$fs."</td>
					<td>".join(', ',$a['f_sehat2'])."</td>
					<td>".$a['f_survey']['ukuran_kaos']."</td>
					<td>".$a['f_survey']['ukuran_kaos_pasangan']."</td>";
			foreach($soal as $b => $c)
			{
				$html .= "<td>".$jawaban[$b]."</td>";
			}
			$html .= "</tr>";
			$i++;
		}		
		$html .= "</table>";

		echo $html;
	} 

}

/* End of file Reports.php */
/* Location: ./application/modules/Responden/controllers/Reports.php */ 
