<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
ini_set("memory_limit","3024M");
class Hitung_panjar extends CI_Controller {
	function __construct() {
		parent::__construct();
		
	}
	
	public function index() {
		$this->session->sess_destroy();
		$a['jenis'] = $this->model_hitung_panjar->list_();
		$this->load->view('admin/panjar', $a);
	}
	public function pilih_alamat_p() {
		//$a['jenisperkara'] = $this->input->post('a');
		$cek = $this->db->query("SELECT * FROM kewenangan_satker where id_kewenangan ='".$this->input->post('a')."'");
		foreach ($cek->result_array() as $row){
			$a['sebutan_p'] = $row['sebutan_p'];
			$a['jenisperkara'] = $row['nama_kewenangan'];
			$a['id_kewenangan'] = $row['id_kewenangan'];
		 }
		$a['jenis'] = $this->model_hitung_panjar->list_();
		$this->load->view('admin/pihak', $a);
	}
	public function simpan_pihak1() {
		$kodeunik = date('Y-m-d');
		$id_kel = $this->input->post('id');
		$nama_pihak = $this->input->post('nama_pihak');
		// Kirim session
		$data = array(
					'username' => $nama_pihak,
                   	'IDkewenangan' => $this->input->post('idkewenangan'),
					'kode_unik' => $kodeunik."-".$nama_pihak,
                    'admin_valid' => true
                    );
				$this->session->set_userdata($data);
		// session
		$cek = $this->db->query("SELECT MAX(id) AS idnext FROM temp_panjar");
		foreach ($cek->result_array() as $row){
			$a['id'] = $row['idnext'];
		}
		
		
		//$no_agenda	= glinext("tb_mahasiswa", "stambuk", 3);
		//$idstambuk =substr($year,2,2).'.1.03.4.1.'.$no_agenda ;
			$idNext	= glinext("temp_panjar", "id", 1);
		//echo "Kode Unik :".$kodeunik."-".$nama_pihak;exit;
		$cek = $this->db->query("SELECT * FROM kewenangan_satker where id_kewenangan ='".$this->session->userdata('IDkewenangan')."'");
		foreach ($cek->result_array() as $row){
			$radius_p = $row['panggilan_p'];
			$radius_t = $row['panggilan_t'];
			$sebutan_p = $row['sebutan_p'];
			$sebutan_t = $row['sebutan_t'];
			
		}
		
		//btasss
		$radius = $this->input->post('nilai');
		$namasatker = $this->input->post('satker_name');
		$ceksatker = $this->db->query("SELECT * FROM identitas");
		foreach ($ceksatker->result_array() as $row){
			$ceknamasatker = $row['nama_satker'];
			$rowbiayapos = $row['biaya_pos'];
		 }
		 if ($ceknamasatker == $namasatker){
			 $biayapos = 0;
		 }else { $biayapos = $rowbiayapos; }
		if ($this->input->post('e') == '1'){ 
			$biaya = $radius;
			$biayatotal = $radius_p * $radius;
			$sebutanpihak = $sebutan_p;
			$jumlah_panggilan = $radius_p;
			$biayapos = $biayapos;
			$biayatotalpos = $radius_p * $biayapos;
			$sebutanpihakpos = $sebutan_p;
			$jumlah_panggilanpos = $radius_p;
		} else { 
				$biaya = $radius;
				$biayatotal = $radius_t * $radius;
				$sebutanpihak = $sebutan_t;
				$jumlah_panggilan = $radius_t;
				$biayapos = $biayapos;
				$biayatotalpos = $radius_t * $biayapos;
				$sebutanpihakpos = $sebutan_t;
				$jumlah_panggilanpos = $radius_t;
		}
	
		if (isset($_POST['submit'])){
			
			$cek = $this->db->query("SELECT * FROM temp_panjar where nama_pihak='".$this->input->post('nama_pihak')."'");
		    $row = $cek->row_array();
		    $total = $cek->num_rows();
			if ($total > 0){
				$this->session->set_flashdata("k", "<div class=\"alert alert-warning\">Ups, Nama Pihak Sudah terdaftar [".$this->input->post('nama_pihak')."],ulangi lagi/hub. Petugas.</div>");
				redirect('hitung_panjar/pilih_pihak_next');
			}else{
			
				$datadb = array('id_kewenangan'=>$this->db->escape_str($this->input->post('idkewenangan')),
						'id_kel'=>$this->input->post('id'),
						'nama_pihak'=>$this->db->escape_str($this->input->post('nama_pihak')),
						'pemohon_termohon'=>$sebutanpihak,
						'jumlah_panggilan'=>$jumlah_panggilan,
						'jumlah_panggilan_x_biaya'=>$biayatotal,
						'biaya'=>$biaya,
						'kode_panjar'=>$kodeunik."-".$this->session->userdata('username'),
						'tanggal_input'=>date('Y-m-d H:i:s'),
						'username_input'=>$this->session->userdata('username'));
				
				$datadbpos = array('id_kewenangan'=>$this->db->escape_str($this->input->post('idkewenangan')),
						'id_kel'=>$this->input->post('id'),
						'nama_pihak'=>$this->db->escape_str($this->input->post('nama_pihak')),
						'pemohon_termohon'=>$sebutanpihakpos,
						'jumlah_panggilan'=>$jumlah_panggilanpos,
						'jumlah_panggilan_x_biaya'=>$biayatotalpos,
						'biaya'=>$biayapos,
						'kode_panjar'=>$kodeunik."-".$this->session->userdata('username'),
						'tanggal_input'=>date('Y-m-d H:i:s'),
						'username_input'=>$this->session->userdata('username'));
				$this->model_hitung_panjar->tambah_pihak1($datadb);
				if ($ceknamasatker != $namasatker){
				 $this->model_hitung_panjar->tambah_pos($datadbpos);
				}
				///batas
				$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data berhasil ditambah,silahkan Tambah Lagi atau Klik Tombol Cetak.</div>");
				redirect('hitung_panjar/pilih_pihak_next');
			}
			
		}else {
			redirect('hitung_panjar');
		}
	}	
	function pilih_pihak_next() {
		$kodeunik = $this->session->userdata('kode_unik');
		//echo "Kode Unik :".$kodeunik;exit;
		$cek = $this->db->query("SELECT * FROM kewenangan_satker where id_kewenangan ='".$this->session->userdata('IDkewenangan')."'");
		foreach ($cek->result_array() as $row){
			$a['sebutan_p'] = $row['sebutan_p'];
			$a['jenisperkara'] = $row['nama_kewenangan'];
			$a['id_kewenangan'] = $row['id_kewenangan'];
	
		}
		$countpihak 	= $this->db->query("SELECT COUNT(kode_panjar) AS jumlahpihak						
							FROM temp_panjar
							WHERE kode_panjar = '$kodeunik'");
		foreach ($countpihak->result_array() as $row){
			$a['jumlahpihaktemp'] = $row['jumlahpihak'];
		}
		$countdatabiayax 	= $this->db->query("SELECT SUM(jumlah_panggilan_x_biaya) AS totalradius						
							FROM temp_panjar
							WHERE kode_panjar = '$kodeunik'");
		foreach ($countdatabiayax->result_array() as $row){
			$totalbiayaradius = $row['totalradius'];
		}
		$countdatabiayapos 	= $this->db->query("SELECT SUM(jumlah_panggilan_x_biaya) AS totalradius						
							FROM temp_pos
							WHERE kode_panjar = '$kodeunik'");
		foreach ($countdatabiayapos->result_array() as $rowpos){
			$totalbiayaradiuspos = $rowpos['totalradius'];
		}
		$countkomponen = $this->db->query("SELECT SUM(jumlah) AS total FROM komponen_biaya_satker");
		foreach ($countkomponen->result_array() as $row){
			$totalbiaya = $row['total'];
		}
		$a['totalbiaya'] = $totalbiayaradius + $totalbiaya + $totalbiayaradiuspos; 
		$a['databiaya'] 	= $this->db->query("SELECT A.*,B.nama_kewenangan						
						FROM temp_panjar A
							LEFT JOIN kewenangan_satker B ON B.id_kewenangan=A.id_kewenangan
							WHERE A.kode_panjar = '$kodeunik' ORDER BY A.id ASC")->result();
		$a['countdatabiaya'] 	= $this->db->query("SELECT SUM(jumlah_panggilan_x_biaya) AS totalradius						
							FROM temp_panjar
							WHERE kode_panjar = '$kodeunik'")->result();
		$a['komponen'] = $this->db->query("SELECT * FROM komponen_biaya_satker")->result();
		$a['databiayapos'] 	= $this->db->query("SELECT A.*,B.nama_kewenangan					
						FROM temp_pos A
							LEFT JOIN kewenangan_satker B ON B.id_kewenangan=A.id_kewenangan
							WHERE A.kode_panjar = '$kodeunik' ORDER BY A.id ASC")->result();
		$a['countdatabiayapos'] 	= $this->db->query("SELECT SUM(jumlah_panggilan_x_biaya) AS totalpos					
							FROM temp_pos
							WHERE kode_panjar = '$kodeunik'")->result();
		$a['countkomponen'] = $this->db->query("SELECT SUM(jumlah) AS total FROM komponen_biaya_satker")->result();
		$this->load->view('admin/pihak_next', $a);
	}
	public function simpan_pihak2() {
		$kodeunik = $this->session->userdata('kode_unik');
		$kodeunik = date('Y-m-d');
		$id_kel = $this->input->post('id');
		$nama_pihak = $this->input->post('nama_pihak');
		$idNext	= glinext("temp_panjar", "id", 1);
		//echo "Kode Unik :".$kodeunik."-".$nama_pihak;exit;
		$cek = $this->db->query("SELECT * FROM kewenangan_satker where id_kewenangan ='".$this->session->userdata('IDkewenangan')."'");
		foreach ($cek->result_array() as $row){
			$radius_p = $row['panggilan_p'];
			$radius_t = $row['panggilan_t'];
			$sebutan_p = $row['sebutan_p'];
			$sebutan_t = $row['sebutan_t'];
			
		}
		
		$radius = $this->input->post('nilai');
		$namasatker = $this->input->post('satker_name');
		$ceksatker = $this->db->query("SELECT * FROM identitas");
		foreach ($ceksatker->result_array() as $row){
			$ceknamasatker = $row['nama_satker'];
			$rowbiayapos = $row['biaya_pos'];
		 }
		 if ($ceknamasatker == $namasatker){
			 $biayapos = 0;
		 }else { $biayapos = $rowbiayapos; }
		if ($this->input->post('e') == '1'){ 
			$biaya = $radius;
			$biayatotal = $radius_p * $radius;
			$sebutanpihak = $sebutan_p;
			$jumlah_panggilan = $radius_p;
			$biayapos = $biayapos;
			$biayatotalpos = $radius_p * $biayapos;
			$sebutanpihakpos = $sebutan_p;
			$jumlah_panggilanpos = $radius_p;
		} else { 
				$biaya = $radius;
				$biayatotal = $radius_t * $radius;
				$sebutanpihak = $sebutan_t;
				$jumlah_panggilan = $radius_t;
				$biayapos = $biayapos;
				$biayatotalpos = $radius_t * $biayapos;
				$sebutanpihakpos = $sebutan_t;
				$jumlah_panggilanpos = $radius_t;
		}
		$cekcout = $this->db->query("SELECT COUNT(kode_panjar) AS jumlah FROM temp_panjar where kode_panjar ='".$this->session->userdata('kode_unik')."'");
		foreach ($cekcout->result_array() as $row){
			$a['$jumlahpihak'] = $row['jumlah'];
					
		}
		if (isset($_POST['submit'])){
			
			$cek = $this->db->query("SELECT * FROM temp_panjar where nama_pihak='".$this->input->post('nama_pihak')."'");
		    $row = $cek->row_array();
		    $total = $cek->num_rows();
			if ($total > 0){
				$this->session->set_flashdata("k", "<div class=\"alert alert-warning\">Ups, Nama Pihak Sudah terdaftar [".$this->input->post('nama_pihak')."],ulangi lagi/hub. Petugas.</div>");
				redirect('hitung_panjar/pilih_pihak_next');
			}else{
				$datadb = array('id_kewenangan'=>$this->db->escape_str($this->input->post('idkewenangan')),
						'id_kel'=>$this->input->post('id'),
						'nama_pihak'=>$this->db->escape_str($this->input->post('nama_pihak')),
						'pemohon_termohon'=>$sebutanpihak,
						'jumlah_panggilan'=>$jumlah_panggilan,
						'jumlah_panggilan_x_biaya'=>$biayatotal,
						'biaya'=>$biaya,
						'kode_panjar'=>$kodeunik."-".$this->session->userdata('username'),
						'tanggal_input'=>date('Y-m-d H:i:s'),
						'username_input'=>$this->session->userdata('username'));
				
				$datadbpos = array('id_kewenangan'=>$this->db->escape_str($this->input->post('idkewenangan')),
						'id_kel'=>$this->input->post('id'),
						'nama_pihak'=>$this->db->escape_str($this->input->post('nama_pihak')),
						'pemohon_termohon'=>$sebutanpihakpos,
						'jumlah_panggilan'=>$jumlah_panggilanpos,
						'jumlah_panggilan_x_biaya'=>$biayatotalpos,
						'biaya'=>$biayapos,
						'kode_panjar'=>$kodeunik."-".$this->session->userdata('username'),
						'tanggal_input'=>date('Y-m-d H:i:s'),
						'username_input'=>$this->session->userdata('username'));
				$this->model_hitung_panjar->tambah_pihak1($datadb);
				if ($ceknamasatker != $namasatker){
				 $this->model_hitung_panjar->tambah_pos($datadbpos);
				}
				
				$this->session->set_flashdata("k", "<div class=\"alert alert-success\" id=\"alert\">Data berhasil ditambah,silahkan Tambah Lagi atau Klik Tombol Cetak.</div>");
				redirect('hitung_panjar/pilih_pihak_next');
			}
			
		}else {
			redirect('hitung_panjar');
		}
	}
	function mulai_hitung() {
		//$kodeunik = date('Y-m-d');
		$kodeunik = $this->session->userdata('kode_unik');
		$a['username'] = $this->session->userdata('username');
		//echo "Kode Unik :".$kodeunik;exit;
		$cek = $this->db->query("SELECT * FROM kewenangan_satker where id_kewenangan ='".$this->session->userdata('IDkewenangan')."'");
		foreach ($cek->result_array() as $row){
			$a['sebutan_p'] = $row['sebutan_p'];
			$a['jenisperkara'] = $row['nama_kewenangan'];
			$a['id_kewenangan'] = $row['id_kewenangan'];
	
		}
		$a['databiaya'] 	= $this->db->query("SELECT A.*,B.nama_kewenangan,C.kel						
						FROM temp_panjar A
							LEFT JOIN kewenangan_satker B ON B.id_kewenangan=A.id_kewenangan
							LEFT JOIN tb_kelurahan_komdanas C ON C.id=A.id_kel
							WHERE A.kode_panjar = '$kodeunik' ORDER BY A.id ASC")->result();
		$a['databiayapos'] 	= $this->db->query("SELECT A.*,B.nama_kewenangan,C.kel						
						FROM temp_pos A
							LEFT JOIN kewenangan_satker B ON B.id_kewenangan=A.id_kewenangan
							LEFT JOIN tb_kelurahan_komdanas C ON C.id=A.id_kel
							WHERE A.kode_panjar = '$kodeunik' ORDER BY A.id ASC")->result();
		$a['countdatabiaya'] 	= $this->db->query("SELECT SUM(jumlah_panggilan_x_biaya) AS totalradius						
							FROM temp_panjar
							WHERE kode_panjar = '$kodeunik'")->result();
		$a['countdatabiayapos'] 	= $this->db->query("SELECT SUM(jumlah_panggilan_x_biaya) AS totalradius						
							FROM temp_pos
							WHERE kode_panjar = '$kodeunik'")->result();
		$a['dataidentitas'] = $this->db->query("SELECT * FROM identitas")->result();
		$a['komponen'] = $this->db->query("SELECT * FROM komponen_biaya_satker")->result();
		$a['countkomponen'] = $this->db->query("SELECT SUM(jumlah) AS total FROM komponen_biaya_satker")->result();
		$a['namasatker'] = 'Pengadilan Agama Palu';
		$this->load->view('admin/cetak_skum', $a);
	}	
	function json_radius() {
		$this->db->query("DELETE FROM tb_kelurahan_komdanas");
		
		$cek = $this->db->query("SELECT * FROM satker WHERE id_satker='1'");
		foreach ($cek->result_array() as $row){
			$eselon		= $row['eselon'];
			$satkerCode	= $row['kode'];
			
		}
		//https://komdanas.mahkamahagung.go.id/jsons/radius04.json
		//$urljason =base_URL().'jsons/radius04.json';
		$urljason ='http://komdanas.mahkamahagung.go.id/jsons/radius04.json';
		$datajson = decode_json($urljason);
		$a['radiusList'] = decode_json($urljason);
	
		foreach ($datajson as $rowValues) {
			foreach ($rowValues as $key => $rowValue) {
				 $rowValues[$key] = $rowValues[$key];
				 $rowValues['satker_name']=$rowValues['satker_name'];
			}
				$row = implode(', ', $rowValues);
				
			
			$values[] = "(".$row.")";
			//$fields[] = implode(', ', array_shift($datajson));
		}
		array_push( $datajson, $rowValues);
		$this->db->insert_batch('tb_kelurahan_komdanas',$datajson);
		
		$this->session->set_flashdata("k", "<div class='alert alert-success'>Data Sukses ditarik dari json komdanas..</div>");
		redirect('kelurahan_komdanas/list_data_');
	}	
	
}
