<?php
if($this->session->userdata('is_logged_in')===FALSE){
            redirect('login');
}


$url = base_url("resources/template/akta_cerai/tanda_terima_akta_cerai.rtf");


$Template = file_get_contents($url);

$Template = str_replace("#NOMOR_PERKARA#",$dataperkara->row(0)->nomor_perkara,$Template);
$Template = str_replace("#NOMOR_URUT_AKTA_CERAI#",$dataAktaCerai->row(0)->nomor_akta_cerai,$Template);
$Template = str_replace("#TAHUN_AKTA_CERAI#",$dataAktaCerai->row(0)->tahun_akta_cerai,$Template);
$Template = str_replace("#NO_SERI_AKTA_CERAI#",$dataAktaCerai->row(0)->no_seri_akta_cerai,$Template);
$Template = str_replace("#PERCERAIAN_KE#",$dataAktaCerai->row(0)->perceraian_ke,$Template);
//----------------------
if($dataAktaCerai->row(0)->qobla_bada == 0)
	{
		$Template = str_replace("#QOBLADIC#","qabla dukhul",$Template);
		
	}
else
	{
		$Template = str_replace("#QOBLADIC#","ba'da dukhul",$Template);
		
	}
switch ($dataAktaCerai->row(0)->keadaan_istri) {
			case 1:
				$Template = str_replace("#SUCIDIC#","suci",$Template);
			break;
			case 2:
				$Template = str_replace("#SUCIDIC#","haid",$Template);
			break;
			case 3:
				$Template = str_replace("#SUCIDIC#","sedang hamil",$Template);
			break;
			case 4:
				$Template = str_replace("#SUCIDIC#","tidak diketahui",$Template);
			break;
		}
if ($jenisPerkaraID==347) {
	$Template = str_replace("#IKRAR_YA#", "",$Template);
}elseif ($jenisPerkaraID==346){
	$Template = str_replace("#IKRAR_YA#", "Ikrar",$Template);
}

if ($jenisPerkaraID==347) {
	if($dataPutusan->row(0)->tanggal_putusan == 0) {
			$Template = str_replace("#TGL_PUTUSAN#","",$Template);
	} else {
			$Template = str_replace("#TGL_PUTUSA_IKRAR#", "",$Template);
			$Template = str_replace("#TGL_PUTUSAN#"," ".$this->templatehelper->convertKeTglIndo($dataPutusan->row(0)->tanggal_putusan),$Template);
	}
}elseif ($jenisPerkaraID==346){
	if($dataPutusan->row(0)->tanggal_putusan == 0) {
			$Template = str_replace("#TGL_PUTUSAN#","",$Template);
	} else {
			$Template = str_replace("#TGL_PUTUSAN#"," ".$this->templatehelper->convertKeTglIndo($dataPutusan->row(0)->tanggal_putusan),$Template);
			$Template = str_replace("#TGL_PUTUSA_IKRAR#"," ".$this->templatehelper->convertKeTglIndo($dataIkrar->row(0)->tgl_ikrar_talak),$Template);
	}
}

$Template = str_replace("#KUA_TEMPAT_NIKAH#",$datapernikahan->row(0)->kua_tempat_nikah,$Template);
$Template = str_replace("#NO_KUTIPAN_AKTA_NIKAH#",$datapernikahan->row(0)->no_kutipan_akta_nikah,$Template);
$Template = str_replace("#TGL_KUTIPAN_AKTA_NIKAH#",$this->templatehelper->convertKeTglIndo($datapernikahan->row(0)->tgl_kutipan_akta_nikah),$Template);

$Template = str_replace("#PERCERAIAN_KE_TERBILANG#",$this->templatehelper->Terbilang($dataAktaCerai->row(0)->perceraian_ke),$Template);
$Template = str_replace("#TGL_AKTA_CERAI#", $this->templatehelper->convertKeTglIndo($dataAktaCerai->row(0)->tgl_akta_cerai) ,$Template);
$Template = str_replace("#TGL_SEKARANG#", $this->templatehelper->convertKeTglIndo(date('Y-m-d')) ,$Template);
$Template = str_replace("#TGL_SEKARANG_HIJRIAH#",konvhijriah(date('Y-m-d')) ,$Template);
$Template = str_replace("#HARI_SEKARANG#", $this->templatehelper->getHari(date('Y-m-d')) ,$Template);

$Template = str_replace("#KODE_SATKER#",$datapengadilan->row(0)->KodePN,$Template);
$Template = str_replace("#NAMA_SATKER#",ucwords($datapengadilan->row(0)->NamaPN),$Template);
$Template = str_replace("#ALAMAT_SATKER#",$datapengadilan->row(0)->AlamatPN,$Template);
$Template = str_replace("#NAMA_PANITERA#",$datapengadilan->row(0)->PansekPNNama,$Template);
$Template = str_replace("#NIP_PANITERA#",$datapengadilan->row(0)->PansekNip,$Template);

//Pihaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaak
$counter = 1;
$pihak1 = '';
$pihak1_id='';
$arr_pihak1 = array();
$numbering = true;
if($dataPihak1->num_rows >1){
    for ($i=0; $i < $dataPihak1->num_rows; $i++) { 
        $text = 'penggugat'.($i+1);
        if($i==$dataPihak1->num_rows-1){
            $Template = str_replace('#loopingpenggugat#','\b#'.$text.'#\b0',$Template);
        }else{
            $Template = str_replace('#loopingpenggugat#','\b#'.$text.'#\b0\\par'.PHP_EOL.'#loopingpenggugat#',$Template);
        }
    }   
}else{
    $numbering = false;
    $Template = str_replace('#loopingpenggugat#','#penggugat#',$Template);
}

if (!empty($dataPihak1->num_rows)){   
    foreach ($dataPihak1->result() as $row){
        $pihak1_id=$row->pihak_id;
        $arr_pihak1[]=$row->id;
        if ($dataPihak1->num_rows==1) {
            $tab='';
            $penggugat = '';
        }else{
            $tab='\tab';
            $penggugat = '\b0\\par ';
        }
        
        $pihak1 = $penggugat;
        $pihak1 .= ''.' Nama '.$this->templatehelper->standradnaming($row->nama).', Umur' . ' tahun,' . ' agama '.
        $row->agama.
        ', pekerjaan '.$row->pekerjaan.', pendidikan  '.$row->pendidikan.', tempat kediaman di '.$row->alamat.', sebagai \b#jenis_pihak1# #penggugatke#\b0';

        if($numbering==true){
            $Template = str_replace('##jenis_pihak1#'.$counter.'#',$pihak1,$Template);
            $Template = str_replace('#penggugat#',$counter,$Template);
            $Template = str_replace('#penggugat/parapenggugat#','Para \b#jenis_pihak1#\b0',$Template);
        }else{
           $Template = str_replace('#penggugat#',$pihak1,$Template);
           $Template = str_replace('#penggugatke#','',$Template);
           $Template = str_replace('#penggugat/parapenggugat#','Untuk selanjutnya disebut sebagai \b#jenis_pihak1#\b0',$Template);
        }
        $counter++;    
    }
}


$counter = 1;
$pihak2 = '';
$pihak2_id='';
$arr_pihak2 = array();
$numbering = true;
if($dataPihak2->num_rows >1){
    for ($i=0; $i < $dataPihak2->num_rows; $i++) { 
        $text = 'tergugat'.($i+1);
        if($i==$dataPihak2->num_rows-1){
            $Template = str_replace('#loopingtergugat#','\b#'.$text.'#\b0',$Template);
        }else{
            $Template = str_replace('#loopingtergugat#','\b#'.$text.'#\b0\\par'.PHP_EOL.'#loopingtergugat#',$Template);
        }
    }   
}else{
    $numbering = false;
    $Template = str_replace('#loopingtergugat#','#tergugat#',$Template);
}

if (!empty($dataPihak2->num_rows)){   
    foreach ($dataPihak2->result() as $row){
        $pihak2_id=$row->pihak_id;
        $arr_pihak2[]=$row->id;
        if ($dataPihak2->num_rows==1) {
            $tab='';
            $tergugat = '';
        }else{
            $tab='\tab';
            $tergugat = '\b0\\par ';
        }
        
        $pihak2 = $tergugat;
        $pihak2 .= ''.' Nama '.$this->templatehelper->standradnaming($row->nama).', Umur' . ' tahun,' . ' agama '.
        $row->agama.
        ', pekerjaan '.$row->pekerjaan.', pendidikan  '.$row->pendidikan.', tempat kediaman di '.$row->alamat.', sebagai \b#jenis_pihak2# #tergugatke#\b0';

        if($numbering==true){
            $Template = str_replace('#tergugat'.$counter.'#',$pihak2,$Template);
            $Template = str_replace('#tergugatke#',$counter,$Template);
            $Template = str_replace('#tergugat/paratergugat#','Para \b#jenis_pihak2#\b0',$Template);
        }else{
           $Template = str_replace('#tergugat#',$pihak2,$Template);
           $Template = str_replace('#tergugatke#','',$Template);
           $Template = str_replace('#tergugat/paratergugat#','Untuk selanjutnya disebut sebagai \b#jenis_pihak2#\b0',$Template);
        }
        $counter++;    
    }
}

$idperkara=$this->encrypt->decode(base64_decode($enc));
$idalurperkara = $this->tanggalhelper->getIDAlurPerkara($idperkara);
$jenisPerkaraID=$this->tanggalhelper->getIDJenisPerkara($idperkara);

if ($idalurperkara==15) {
    if ($jenisPerkaraID==346) {
        $Template = str_replace("#jenis_pihak1#",''." Pemohon ".'',$Template);
        $Template = str_replace("#jenis_pihak2#",''." Termohon ".'',$Template);
		$Template = str_replace("#SEBAGAI_P#","Pemohon",$Template);
		$Template = str_replace("#SEBAGAI_T#","Termohon",$Template);
		$Template = str_replace("#GUGATAN_PERMOHONAN#",''." Permohonan ".'',$Template);   
		$Template = str_replace("#PUTUSAN_PENETAPAN#",''." Penetapan ".'',$Template);   
		$Template = str_replace("#TALAKDIC#",''." Cerai Talak ".'',$Template);
		//$Template = str_replace("#GUGATDIC#",''."".'',$Template);
    }else{
        $Template = str_replace("#jenis_pihak1#",''." Penggugat ".'',$Template);
        $Template = str_replace("#jenis_pihak2#",''." Tergugat ".'',$Template);
		$Template = str_replace("#SEBAGAI_P#","Penggugat",$Template);
		$Template = str_replace("#SEBAGAI_T#","Tergugat",$Template);
		$Template = str_replace("#GUGATAN_PERMOHONAN#",''." Gugatan ".'',$Template);
		$Template = str_replace("#PUTUSAN_PENETAPAN#",''." Putusan ".'',$Template);
		$Template = str_replace("#TALAKDIC#",'Cerai Gugat'."".'',$Template);
		//$Template = str_replace("#GUGATDIC#",''." ---------- ".'',$Template);
    }
}else{
    $Template = str_replace("#jenis_pihak1#",''." Pemohon ".'',$Template);
    $Template = str_replace("#jenis_pihak2#",''." Termohon ".'',$Template);
}

$Template = str_replace("#NAMA_P#",$dataPihak1->row(0)->nama,$Template);
$Template = str_replace("#PEKERJAAN_P#",$dataPihak1->row(0)->pekerjaan,$Template);// 
$Template = str_replace("#UMUR_P#",getUmur($dataPihak1->row(0)->tanggal_lahir),$Template);
$Template = str_replace("#ALAMAT_P#",$dataPihak1->row(0)->alamat,$Template);

$Template = str_replace("#NAMA_T#",$dataPihak2->row(0)->nama,$Template);
$Template = str_replace("#PEKERJAAN_T#",$dataPihak2->row(0)->pekerjaan,$Template);// 
$Template = str_replace("#UMUR_T#",getUmur($dataPihak2->row(0)->tanggal_lahir),$Template);
$Template = str_replace("#ALAMAT_T#",$dataPihak2->row(0)->alamat,$Template);
//Pihaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaak
foreach ($datapengadilan->result() as $row) {
	$namapn = ucwords(strtolower($this->templatehelper->standradnaming($row->NamaPN)));
	$namaketuapn = $this->templatehelper->standradnaming($row->KetuaPNNama);
	$namawakilpn = $this->templatehelper->standradnaming($row->WakilKetuaPNNama);
	$namakota =str_replace("Pengadilan Agama ",'',$namapn);
	$namakota =str_replace("Mahkamah Syar'Iyah ",'',$namakota);
}

$Template = str_replace("#namapa#",''.$namapn.'',$Template);
$Template = str_replace('#namakota#',''.$namakota.'',$Template);
header("Content-Type: application/vnd.ms-word");
header("Expires: 0");
header("Cache-Control:  must-revalidate, post-check=0, pre-check=0");
header("Content-disposition: attachment; filename=\"Tanda Terima Akta Cerai -".$dataperkara->row(0)->nomor_perkara.".rtf\"");
echo $Template;
?>

<?php

function makeInt($angka)
	{
		if ($angka < -0.0000001)
			{
				return ceil($angka-0.0000001);
			}
		else
			{
				return floor($angka+0.0000001);
			}
	}

function konvhijriah($tanggal)
	{
		$array_bulan = array("Muharram", "Safar", "Rabiul Awwal", "Rabiul Akhir","Jumadil Awwal","Jumadil Akhir", "Rajab", "Sya’ban","Ramadhan","Syawwal", "Zulqaidah", "Zulhijjah");
		$date = makeInt(substr($tanggal,8,2));
		$month = makeInt(substr($tanggal,5,2));
		$year = makeInt(substr($tanggal,0,4));
		if (($year>1582)||(($year == "1582") && ($month > 10))||(($year == "1582") && ($month=="10")&&($date >14)))
			{
				$jd = makeInt((1461*($year+4800+makeInt(($month-14)/12)))/4)+
				makeInt((367*($month-2-12*(makeInt(($month-14)/12))))/12)-
				makeInt( (3*(makeInt(($year+4900+makeInt(($month-14)/12))/100))) /4)+
				$date-32075;
			}
		else
			{
				$jd = 367*$year-makeInt((7*($year+5001+makeInt(($month-9)/7)))/4)+
				makeInt((275*$month)/9)+$date+1729777;
			}
		$wd = $jd%7;
		$l = $jd-1948440+10632;
		$n=makeInt(($l-1)/10631);
		$l=$l-10631*$n+354;
		$z=(makeInt((10985-$l)/5316))*(makeInt((50*$l)/17719))+(makeInt($l/5670))*(makeInt((43*$l)/15238));
		$l=$l-(makeInt((30-$z)/15))*(makeInt((17719*$z)/50))-(makeInt($z/16))*(makeInt((15238*$z)/43))+29;
		$m=makeInt((24*$l)/709);
		$d=$l-makeInt((709*$m)/24);
		$y=30*$n+$z-30;
		$g = $m-1;
		$final = "$d $array_bulan[$g] $y H";
		return $final;
	}

	
	
function getUmur($tgl)
	{
		$tgl_lahir=substr($tgl,8,2);
		$bln_lahir=substr($tgl,5,2);
		$thn_lahir=substr($tgl,0,4);

		$tanggal_today = date('d');
		$bulan_today = date('m');
		$tahun_today = date('Y');
			$harilahir=gregoriantojd($bln_lahir,$tgl_lahir,$thn_lahir);
			//menghitung jumlah hari sejak tahun 0 masehi
			$hariini=gregoriantojd($bulan_today,$tanggal_today,$tahun_today);
			//menghitung jumlah hari sejak tahun 0 masehi
			$umur=$hariini-$harilahir;
			//menghitung selisih hari antara tanggal sekarang dengan tanggal lahir
			$tahun=$umur/365;//menghitung usia tahun
			$sisa=$umur%365;//sisa pembagian dari tahun untuk menghitung bulan
			$bulan=$sisa/30;//menghitung usia bulan
			$hari=$sisa%30;//menghitung sisa hari
			$lahir= "$tgl_lahir-$bln_lahir-$thn_lahir";
			$today= "$tanggal_today-$bulan_today-$tahun_today";
			
			return floor($tahun);
	}
?>