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

class Rep_penerimaan extends CI_Controller {
	
	var $gparam = array();
	
	public function __construct() {
        parent::__construct();
		$this->gparam['controller'] = $this->router->fetch_class();
		$this->load->model('rep_penerimaan_model', 'rep_sis');
		$this->gparam['privilage'] = getPrivilage($this->gparam['controller']);
		$this->gparam['restrict'] = 'You Cannot Access This Menu';
    }
	
	
	public function index()	{
		
		if ($this->gparam['privilage']->privilage_view == 'Y') {
		
			$gridopt = $this->input->get(array('psize', 'pnumber'));
			header('Content-Type: text/html');
			$act = base_url()."index.php/".$this->gparam['controller']."/load_data";
			
			$excell_download = '<a class="btn btn-success" id="save_excell" href="#" >
								<i class="fa fa-lg fa-fw fa-download"></i>Excell
							  </a>';		

			$get_smk = $this->rep_sis->getDataSMK();
			$nama_smk='	<select name="smk" class="chosen-select" style="width:195px;">
									<option value="">--Semua Sekolah--</option>';
					
			foreach ($get_smk as $v_smk) {
				$nama_smk .='<option value="'.$v_smk['id'].'">'.$v_smk['name'].'</option>';
			}
			$nama_smk .='</select> ';
			
			$get_periode = $this->rep_sis->getDataPeriode();
			$nama_periode='	<select name="periode" class="chosen-select" style="width:195px;">
									<option value="">--Pilih Tahun Ajaran--</option>';
					
			foreach ($get_periode as $v_periode) {
				$nama_periode .='<option value="'.$v_periode['tahun_ajaran'].'">'.$v_periode['tahun_ajaran'].'</option>';
			}
			$nama_periode .='</select> ';
			
			// pnumber, psize
			$data = array(
					'controller'  	=> $this->gparam['controller'],
					'psize'			=> (empty($gridopt['psize'])?10:$gridopt['psize']),
					'pnumber'  		=> (empty($gridopt['pnumber'])?1:$gridopt['pnumber']),
					'excell' 		=> $excell_download,
					'nama_smk' 		=> $nama_smk,
					'periode'		=> $nama_periode,
					'action'		=> $act
			);
			
			$this->load->view('rep_penerimaan_view',$data);
		} else {
			echo $this->gparam['restrict'];
		}
	}
	
	
	function load_data() {
	
		/* $periode = $this->input->post("periode");
		$until = $this->input->post("until");
	    $get_id_siswa = $this->rep_sis->get_id_siswa($peride,$until);
		 */
		
		$thn_ajaran = $this->input->post("periode");	
		$smk = $this->input->post("smk");
		if((empty($smk))||($smk=='')){
			//$get_id_siswa = $this->rep_sis->get_id_siswa($periode, '');
			$header = $this->rep_sis->get_header('');
			$header_count = $this->rep_sis->get_count_header('');
		}else{
			//$get_id_siswa = $this->rep_sis->get_id_siswa($periode, $smk);
			$header = $this->rep_sis->get_header($smk);
			$header_count = $this->rep_sis->get_count_header($smk);
		}
		
		/*Header*/
		$html = '<table class="table table-bordered" border="1" id="activity_table"><thead><tr>';
		$html .='<th style="text-align:center;">No.</th>';
		$html .='<th style="text-align:center;">Sekolah</th>';
		$html .='<th style="text-align:center;">Bulan</th>';
		$html .='<th style="text-align:center;">Tahun Ajaran</th>';
		$html .='<th style="text-align:center;">SPP</th>';
		$html .='<th style="text-align:center;">TABSIS</th>';
		$header_cicilan = $this->rep_sis->get_header_cicilan();
		$header_cicilan_count = $this->rep_sis->get_cicilan_count_header();
		$html .='<th style="text-align:center;">DSP</th>';
		/*
		foreach ($header_count as $value_header_c) {
			$nama_smk=$this->rep_sis->getSMK($value_header_c['id_smk']);
			
			$html .='<th colspan="'.$value_header_c['jenis_pem'].'" style="text-align:center;">'.$nama_smk->name.'</th>';
		}
		*/
		foreach ($header as $value_header) {
			$html .='<th style="text-align:center;">'.$value_header['daftarlist'].'</th>';
		}
		$html .='<th style="text-align:center;" >Jumlah</th>';
		$html .= '</tr></thead>';
		
		
		
		
		$html .= '<tbody>';
		/*Close Header*/
		
		
		$total_all = 0;
		$temp_count_1=1;
		//foreach ($get_id_siswa as $v_siswa) {
			//SELECT MONTH(tanggal_pembayaran) FROM `trans_siswa` WHERE no_faktur='FM2017033000002'
			$bulan = array("July", "August", "September", "October", "November", "December", "January", "February", "March", "April", "May", "June");
			$bulanInt = array(7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6);
		for($i=0 ; $i<12 ; $i++){
			
			$periode = $bulanInt[$i];
			$html .='<tr><td>'.$temp_count_1.'</td>';	
			if((empty($smk))||($smk=='')){
				$html .='<td>ALL</td>';	
			}else{
				$html .='<td>'.$this->rep_sis->getSMK($smk)->name.'</td>';	
			}
			
			$html .='<td>'.$bulan[$i].'</td>';
			if(empty($thn_ajaran)){
				$html .='<td>-</td>';
			}else{
				$html .='<td>'.$thn_ajaran.'</td>';
			}
			$temp_count_1++;
			
				$spp = 0;
				$trans = 0;
				$cicilan = 0;
				/*Get Detail Siswa*/
				
				//$html .='<td>'.$this->rep_sis->get_kelas_ppdb($v_siswa['id_siswa']).'</td>';					
				/******************/
				
				/*Get Detail SPP Siswa*/
				$spp=0;
				if((empty($smk))||($smk=='')){
					$spp = $this->rep_sis->get_total_spp($periode, "", $thn_ajaran);	
				}else{
					$spp = $this->rep_sis->get_total_spp($periode, $smk,$thn_ajaran);	
				}
				
				
				if($spp==0){
					$html .='<td style="text-align:right;">Rp. - </td>';
				}else{		
					$html .='<td style="text-align:right;">Rp. '.number_format($spp,2,",",".").'</td>';
				}
				/*****************/
				
				/*Get Detail TABSIS Siswa*/
				if((empty($smk))||($smk=='')){
					$tabsis_FM = $this->rep_sis->get_total_tabsis_FM($periode,"",$thn_ajaran);
					$tabsis_FK = $this->rep_sis->get_total_tabsis_FK($periode,"",$thn_ajaran);
				}else{
					$tabsis_FM = $this->rep_sis->get_total_tabsis_FM($periode,$smk,$thn_ajaran);
					$tabsis_FK = $this->rep_sis->get_total_tabsis_FK($periode,$smk,$thn_ajaran);
				}
				
				if((empty($tabsis_FM))){
					$tabFM=0;
				}else{
					$tabFM=$tabsis_FM->total;
				}
				
				if((empty($tabsis_FK))){
					$tabFK=0;
				}else{
					$tabFK=$tabsis_FK->total;
				}
				
				$tabsis=$tabFM-$tabFK;
				
				if(($tabsis==NULL)||($tabsis==0)){
					$ts=0;
					$html .='<td style="text-align:right;">Rp. - </td>';
				}else if($tabsis<0){
					$ts=$tabsis*(-1);
					$html .='<td style="text-align:right;">(Rp. '.number_format($tabsis,2,",",".").')</td>';
				}else{	
					$ts=$tabsis*(1);
					$html .='<td style="text-align:right;">Rp. '.number_format($tabsis,2,",",".").'</td>';
				}
				/*Get Detail Cicilan Siswa*/
				$total_cicilan=0;
				//foreach ($header_cicilan as $header_cicilan_r) {
				
				
				if((empty($smk))||($smk=='')){
					$cicilan_FM = $this->rep_sis->get_total_cicilan_FM($periode,"", $thn_ajaran);
					$cicilan_FK = $this->rep_sis->get_total_cicilan_FK($periode,"", $thn_ajaran);
				}else{
					$cicilan_FM = $this->rep_sis->get_total_cicilan_FM($periode,$smk, $thn_ajaran);
					$cicilan_FK = $this->rep_sis->get_total_cicilan_FK($periode,$smk, $thn_ajaran);
				}
				
				if((empty($cicilan_FM))){
					$cicilFM=0;
				}else{
					$cicilFM=$cicilan_FM->total;
				}
				
				if((empty($cicilan_FK))){
					$cicilFK=0;
				}else{
					$cicilFK=$cicilan_FK->total;
				}
				$cicilan=$cicilFM-$cicilFK;
				if(($cicilan==NULL)||($cicilan==0)){
					$tc=0;
					$html .='<td style="text-align:right;">Rp. - </td>';
				}else if($tabsis<0){
					$tc=$cicilan*(-1);
					$html .='<td style="text-align:right;">(Rp. '.number_format($cicilan,2,",",".").')</td>';
				}else{	
					$tc=$cicilan*(1);
					$html .='<td style="text-align:right;">Rp. '.number_format($cicilan,2,",",".").'</td>';
				}
				
				
				$total_cicilan=$total_cicilan+$tc;
				
			//}
				
				
				/*Get Detail Transaksi*/
				foreach ($header as $vheader) {
					if((empty($smk))||($smk=='')){
						$gtrans = $this->rep_sis->get_total_trans($periode, $vheader['kodedesk'], $vheader['kodesub'],"", $thn_ajaran);	
					}else{
						$gtrans = $this->rep_sis->get_total_trans($periode, $vheader['kodedesk'], $vheader['kodesub'],$smk, $thn_ajaran);		
					}
					
					//if($vheader['id_smk']==$gtrans->id_smk){
						$trans = $trans + $gtrans->total; 
						if($gtrans->total==0){
							$html .='<td style="text-align:right;">Rp. - </td>';
						}else{
							$html .='<td style="text-align:right;">Rp. '.number_format($gtrans->total,2,",",".").'</td>';
						}
						/*
					}else{
						$trans = $trans;
						$html .='<td style="text-align:right;">Rp. - </td>';
					}
					*/
				}			
				/*******************/
				
				/* Get Total Transaction */
				$jml = $spp + $trans + $total_cicilan + $ts;
				$total_all = $total_all + $jml;			
				$html .='<td style="text-align:right;"><b>Rp. '.number_format($jml,2,",",".").'</b></td></tr>';
				/********************/
				
			
		}
		
		
		$html .= '<tr><td style="text-align:center; font-weight: bold;" colspan="4" >JUMLAH</td>';
		//$html .= '<td style="text-align:center;">-</td>';
		/*Total SPP Harian*/
		if((empty($smk))||($smk=='')){
			$spp_harian = $this->rep_sis->total_spp_harian($periode, '', $thn_ajaran);
			$spp_harian_ppdb = $this->rep_sis->total_spp_harian_ppdb($periode, '', $thn_ajaran);
		}else{
			$spp_harian = $this->rep_sis->total_spp_harian($periode, $smk, $thn_ajaran);
			$spp_harian_ppdb = $this->rep_sis->total_spp_harian_ppdb($periode, $smk, $thn_ajaran);
		}
		$html .= '<td style="text-align:right;">Rp. '.number_format(($spp_harian + $spp_harian_ppdb),2,",",".").'</td>';
		/*****************/
		
		if((empty($smk))||($smk=='')){
			$tabsis_harian = $this->rep_sis->total_tabsis_harian($periode, '', $thn_ajaran);
			$tabsis_harian_ppdb = $this->rep_sis->total_tabsis_harian_ppdb($periode, '', $thn_ajaran);
		}else{
			$tabsis_harian = $this->rep_sis->total_tabsis_harian($periode, $smk, $thn_ajaran);
			$tabsis_harian_ppdb = $this->rep_sis->total_tabsis_harian_ppdb($periode, $smk, $thn_ajaran);
		}
		
		$html .= '<td style="text-align:right;">Rp. '.number_format($tabsis_harian + $tabsis_harian_ppdb,2,",",".").'</td>';
		
		/*Total Cicilan Harian*/
		//foreach ($header_cicilan as $header_cicilan_r) {
			if((empty($smk))||($smk=='')){
				$cicilan_harian = $this->rep_sis->total_cicilan_harian($periode,'', $thn_ajaran);
				$cicilan_harian_ppdb = $this->rep_sis->total_cicilan_harian_ppdb($periode,'', $thn_ajaran);
			}else{
				$cicilan_harian = $this->rep_sis->total_cicilan_harian($periode,$smk, $thn_ajaran);
				$cicilan_harian_ppdb = $this->rep_sis->total_cicilan_harian_ppdb($periode,$smk, $thn_ajaran);
			}
			
			$html .= '<td style="text-align:right;">Rp. '.number_format(($cicilan_harian + $cicilan_harian_ppdb),2,",",".").'</td>';
		//}
		
		/*****************/
		
		
		$smk = $this->input->post("smk");
		/*Total Transaksi Harian*/
		
		foreach ($header as $vheader) {
				if((empty($smk))||($smk=='')){
					$otal_daily = $this->rep_sis->get_total_trans_harian($vheader['kodedesk'], $vheader['kodesub'],$periode,'', $thn_ajaran);
					$otal_daily_ppdb = $this->rep_sis->get_total_trans_harian_ppdb($vheader['kodedesk'], $vheader['kodesub'],$periode,'', $thn_ajaran);
					if(($otal_daily_ppdb->total+$otal_daily->total)>0){
						$html .='<td style="text-align:right;"><b>Rp. '.number_format(($otal_daily_ppdb->total+$otal_daily->total),2,",",".").'</b></td>';
					}else{
						$html .='<td style="text-align:right;"><b>Rp. - </b></td>';
					}
						
				}else{
					$otal_daily = $this->rep_sis->get_total_trans_harian($vheader['kodedesk'], $vheader['kodesub'],$periode,$smk, $thn_ajaran);
					$otal_daily_ppdb = $this->rep_sis->get_total_trans_harian_ppdb($vheader['kodedesk'], $vheader['kodesub'],$periode,$smk, $thn_ajaran);
					if(($otal_daily_ppdb->total+$otal_daily->total)>0){
						$html .='<td style="text-align:right;"><b>Rp. '.number_format(($otal_daily_ppdb->total+$otal_daily->total),2,",",".").'</b></td>';
					}else{
						$html .='<td style="text-align:right;"><b>Rp. - </b></td>';
					}
				}
				
		}
		
		/**********************/
		
		/*Summary Closing*/
		$html .='<td style="text-align:right;"><b>Rp. '.number_format($total_all,2,",",".").'</b></td></tr>';
		/*****************/
		$html .= '</tbody>';
		$html .= '</table>';
		//$html = "Peiode -- ".$periode;
		$html .= '<script>
					var name = "DailyTrans.xls";
					$("#save_excell").on("click", function () {
						var uri = $("#activity_table").btechco_excelexport({
						containerid: "activity_table"
						, datatype: $datatype.Table
						, returnUri: true
						});
				$(this).attr("download", name).attr("href", uri).attr("target", "_blank");
			});
		</script>';
		echo $html;
	}
	
	function ConverToRoman($num){ 
    $n = intval($num); 
    $res = ''; 

    //array of roman numbers
    $romanNumber_Array = array( 
        'M'  => 1000, 
        'CM' => 900, 
        'D'  => 500, 
        'CD' => 400, 
        'C'  => 100, 
        'XC' => 90, 
        'L'  => 50, 
        'XL' => 40, 
        'X'  => 10, 
        'IX' => 9, 
        'V'  => 5, 
        'IV' => 4, 
        'I'  => 1); 

		foreach ($romanNumber_Array as $roman => $number){ 
			//divide to get  matches
			$matches = intval($n / $number); 
	
			//assign the roman char * $matches
			$res .= str_repeat($roman, $matches); 
	
			//substract from the number
			$n = $n % $number; 
		} 
	
		// return the result
		return $res; 
	} 

	
	
}
