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

class Jurnal_all_perkara extends CI_Controller {

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

	function index(){
		
	}
	//Atria
	function cari(){
		$this->load->model('basic_query_m','basic');
		// tangkap variabel keyword dari URL
		$keyword = $this->uri->segment(3);
		// $keyword = $this->input->post('nomor_perkara');

		// cari di database
		$data = $this->basic->cari($keyword);

		// format keluaran di dalam array
		foreach($data->result() as $row)
		{
			$arr['query'] = $keyword;
			$arr['suggestions'][] = array(
				'value'	=>$row->nomor_perkara
			);
		}
		// minimal PHP 5.2
		echo json_encode($arr);
	}

	function set_jurnal_biaya(){
			if(isset($_POST['object'])){
				$object = json_decode($_POST['object']);
				$object =  (array) $object;
				
				$this->load->model('biaya_perkara/biaya_perkara_m','biaya');
				// $result_data_biaya = $this->biaya->get_biaya_perkara($perkara_id);
				$params = array();
				foreach ($object as $key => $value) {
					$tanggal = $object[$key]->tanggal_transaksi;
					$arr_tgl = explode('-', $tanggal);
					if(strlen($arr_tgl[0])==2){
						$tanggal = $arr_tgl[2].'-'.$arr_tgl[1].'-'.$arr_tgl[0];
					}
					$object[$key]->tanggal_transaksi = $tanggal;
					if(isset($object[$key]->perkara_id)){
						$perkara_id = $object[$key]->perkara_id;
					}
					if(floatval($object[$key]->pemasukan)>0){
						$object[$key]->jumlah = floatval($object[$key]->pemasukan);
						$object[$key]->jenis_transaksi = 1;
					}else{
						$object[$key]->jumlah = floatval($object[$key]->pengeluaran);
						$object[$key]->jenis_transaksi = -1;
						
					}
					$object[$key]->sisa = 0;
					unset($object[$key]->pemasukan);
					unset($object[$key]->pengeluaran);
					unset($object[$key]->editing);
					unset($object[$key]->pihak_nama);
					if(empty($object[$key]->selected_uraian))
						unset($object[$key]->selected_uraian);
					else{
						$object[$key]->kategori_id = $object[$key]->selected_uraian->originalObject->kategori_id;
						$object[$key]->uraian = $object[$key]->selected_uraian->originalObject->nama;
						unset($object[$key]->selected_uraian);
					}
					if(empty($object[$key]->selected_pihak))
						unset($object[$key]->selected_pihak);
					else{
						$object[$key]->pihak_id = $object[$key]->selected_pihak->originalObject->pihak_id;
						$object[$key]->pihak_ke = $object[$key]->selected_pihak->originalObject->pihak_ke;
						unset($object[$key]->selected_pihak);
					}
						
					$params[$key] = $object[$key];
				
					$this->biaya->manipulate_dbas($params);
				}
				// print_r($object);
			}
		
	}

	function all_jurnal_perkara(){ 
		if ($this->session->userdata('is_logged_in')==false){
				redirect('login');
		}
		$data['page_title'] = 'JURNAL KEUANGAN';
		$data['main_body'] = 'jurnal_perkara/all_jurnal_perkara';
		$data['page_url'] = 'all_jurnal_perkara';
		$this->load->vars($data);
		$this->load->view('header');
		$this->load->view('body/body');
	}

	public function get_jenis_biaya(){
		$this->load->model('basic_query_m','basic');
		$params['tahapan_id'] = 10;
		$data_jenis_biaya = $this->basic->get('jenis_biaya',array(), array('id', 'tahapan_id', 'jenis_transaksi', 'kategori_id', 'kode', 'nama'), array('tahapan_id' => 'asc', 'jenis_transaksi'=>'desc'));
		$res= ($data_jenis_biaya->result_array());
		$hasil = array();
		if(!empty($res)){
			foreach ($res as $key => $value) {
				$hasil[$value['tahapan_id']][]=$value;
			}
		}
		$this->output
                ->set_content_type('application/json')
                ->set_output(json_encode($hasil));
	}

	public function get_info_perkara_sipp(){
    	if ($_POST) {
    		$keyword = $this->input->post('search');
    		$this->load->model('basic_query_m','basic');
    		$params['nomor_perkara'] = $keyword;
    		$data_perkara = $this->basic->get('perkara',$params, array('perkara_id', 'nomor_perkara', 'tanggal_pendaftaran', 'pihak1_text', 'pihak2_text', 'prodeo'));
    		$res=array('st'=>0, 'perkara'=>array(),'data_biaya'=>array());
    		if($data_perkara->num_rows()>=1){
    			foreach ($data_perkara->result_array() as $key => $value) {
    				$perkara_id = $value['perkara_id'];
    				$value['tanggal_pendaftaran'] = $this->tanggalhelper->convertDayDate($value['tanggal_pendaftaran']);
    				if($value['prodeo']==0)$value['prodeo']='Tidak';
    					else $value['prodeo']='Ya';
    					$res['perkara']=$value;
    			}
    			$res['st']=1;
	    		$this->load->model('biaya_perkara/biaya_perkara_m','biaya');
	    		$result_data_biaya = $this->biaya->get_biaya_perkara($perkara_id);
	    		$data_biaya=array();
	    		$sisa = 0;
	    		$tahapan_sebelumnya = '';$i=0;
	    		// print_r($result_data_biaya->result_array());exit;
	    		foreach ($result_data_biaya->result_array() as $key => $value) {
	    			if($tahapan_sebelumnya!=$value['tahapan_id']){
	    				$sisa=0;
	    				$i=0;	
	    			} 
	    			$tahapan_sebelumnya=$value['tahapan_id'];
	    			$data_biaya[$value['tahapan_id']][$i]['id'] = $value['id'];
		            $data_biaya[$value['tahapan_id']][$i]['id_pembiayaan'] = $value['id_pembiayaan'];
		            $data_biaya[$value['tahapan_id']][$i]['perkara_id'] = $value['perkara_id'];
		            $data_biaya[$value['tahapan_id']][$i]['tahapan_id'] = $value['tahapan_id'];
		            $data_biaya[$value['tahapan_id']][$i]['kategori_id'] = $value['kategori_id'];
		            $data_biaya[$value['tahapan_id']][$i]['jenis_biaya_id'] = $value['jenis_biaya_id'];
		            $data_biaya[$value['tahapan_id']][$i]['pihak_id'] = $value['pihak_id'];
		            $data_biaya[$value['tahapan_id']][$i]['pihak_ke'] = $value['pihak_ke'];
		            $data_biaya[$value['tahapan_id']][$i]['urutan'] = $value['urutan'];
		            $data_biaya[$value['tahapan_id']][$i]['jenis_transaksi'] = $value['jenis_transaksi'];
		            $data_biaya[$value['tahapan_id']][$i]['tanggal_transaksi'] = $value['tanggal_transaksi'];
		            $data_biaya[$value['tahapan_id']][$i]['uraian'] = $value['uraian'];
		            if($value['jenis_transaksi']==1){
		            	$data_biaya[$value['tahapan_id']][$i]['pemasukan'] = $value['jumlah'];
		            	$data_biaya[$value['tahapan_id']][$i]['pengeluaran'] = 0;
		            	$sisa += $value['jumlah'];
		            }else{
		            	$data_biaya[$value['tahapan_id']][$i]['pemasukan'] = 0;
		            	$data_biaya[$value['tahapan_id']][$i]['pengeluaran'] = $value['jumlah'];
		            	$sisa -= $value['jumlah'];
		            }
		            $data_biaya[$value['tahapan_id']][$i]['jumlah'] = $value['jumlah'];
		            $data_biaya[$value['tahapan_id']][$i]['sisa'] = $sisa;
		            $data_biaya[$value['tahapan_id']][$i]['keterangan'] = $value['keterangan'];
		            if($value['pihak_ke']==NULL){
		            	$data_biaya[$value['tahapan_id']][$i]['pihak_nama'] = 'Penggugat';
		            }else{
		            	if($data_biaya[$value['tahapan_id']][$i]['pihak_id'] == NULL OR $data_biaya[$value['tahapan_id']][$key]['pihak_id'] == ''){
		            		switch ($data_biaya[$value['tahapan_id']][$i]['pihak_ke']) {
		            			case 1:
		            				$data_biaya[$value['tahapan_id']][$i]['pihak_nama'] = 'Penggugat';
		            			break;
		            			case 2:
		            				$data_biaya[$value['tahapan_id']][$i]['pihak_nama'] = 'Tergugat';
		            			break;
		            		}
		            	}else{
		            		switch ($data_biaya[$value['tahapan_id']][$i]['pihak_ke']) {
		            			case 1:
		            				$data_biaya[$value['tahapan_id']][$i]['pihak_nama'] = $value['nama']."(P)";
		            			break;
		            			case 2:
		            				$data_biaya[$value['tahapan_id']][$i]['pihak_nama'] = $value['nama']."(T)";
		            			break;
		            			case 3:
		            				$data_biaya[$value['tahapan_id']][$i]['pihak_nama'] = $value['nama']."(I)";
		            			break;
		            			case 4:
		            				$data_biaya[$value['tahapan_id']][$i]['pihak_nama'] = $value['nama']."(TT)";
		            			break;
		            		}
		            	}
		            }
		            $tahapan_sebelumnya=$value['tahapan_id'];
		            $i++;
	    		}
	    		$res['data_biaya']=$data_biaya;
	    		$query = "select null as pihak_id,1 as pihak_ke,'Penggugat' as nama
							union all
							select null,2,'Tergugat'
							union all
							select pihak_id, 1, concat(nama,'', '(P)' )  as nama
							from  perkara_pihak1
							where perkara_id=$perkara_id
							union all
							select pihak_id, 2, concat(nama,'', '(T)' )  as nama 
							from  perkara_pihak2
							where perkara_id=$perkara_id
							union all
							select pihak_id, 3, concat(nama,'', '(I)' )  as nama 
							from  perkara_pihak3
							where perkara_id=$perkara_id
							union all
							select pihak_id, 4, concat(nama,'', '(TT)' )  as nama 
							from  perkara_pihak4
							where perkara_id=$perkara_id;";
				$pihaks = array();
				$hasil = $this->basic->get_query($query);
				if($hasil->num_rows()>0){
					foreach ($hasil->result() as $row) {
						$pihaks[]=$row;
					}
				}
				$res['pihaks']=$pihaks;

	    		echo json_encode($res);
    		}
    	}else{
    		echo "Gagal";
    	}
    }
	//end
}

