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

class Rep_trans_siswa extends CI_Controller {
	
	var $gparam = array();
	
	public function __construct() {
        parent::__construct();
		$this->gparam['controller'] = $this->router->fetch_class();
		$this->load->model('rep_siswa_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> ';
			
			// 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,
					'action'		=> $act
			);
			
			$this->load->view('rep_tr_siswa_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);
		 */
		
		$periode = $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 rowspan="2" style="text-align:center;">No.</th>';
		$html .='<th rowspan="2" style="text-align:center;">Nama</th>';
		$html .='<th rowspan="2" style="text-align:center;">Status</th>';
		$html .='<th rowspan="2" style="text-align:center;">Sekolah</th>';
		$html .='<th rowspan="2" style="text-align:center;">Kelas</th>';
		$html .='<th rowspan="2" style="text-align:center;">Total SPP</th>';
		$html .='<th rowspan="2" 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 rowspan="2" 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>';
		}
		$html .='<th rowspan="2" style="text-align:center;" >Jumlah</th>';
		$html .= '</tr></thead>';
		
		$html .= '<thead><tr>';
		/*foreach ($header_cicilan as $header_cicilan_r) {
			$html .='<th style="text-align:center;">'.$header_cicilan_r['daftarlist'].'</th>';
		}*/
		//$html .='<th style="text-align:center;">DSP</th>';
		foreach ($header as $value_header) {
			$html .='<th style="text-align:center;">'.$value_header['jenispembayaran'].'</th>';
		}
		$html .= '</tr></thead>';
		
		$html .= '<tbody>';
		/*Close Header*/
		
		
		$total_all = 0;
		$temp_count_1=1;
		foreach ($get_id_siswa as $v_siswa) {
			$html .='<tr><td>'.$temp_count_1.'</td>';	
			$temp_count_1++;
			if($v_siswa['sts_ppdb']==0){
				$spp = 0;
				$trans = 0;
				$cicilan = 0;
				/*Get Detail Siswa*/
				$html .='<td>'.$this->rep_sis->get_detail($v_siswa['id_siswa']).'</td>';	
				$html .='<td>SISWA</td><td>'.$this->rep_sis->get_ruang_kelas($v_siswa['id_siswa']).'</td>';
				$html .='<td>'.$this->rep_sis->get_kelas($v_siswa['id_siswa']).'</td>';					
				/******************/
				
				/*Get Detail SPP Siswa*/
				$spp = $this->rep_sis->get_total_spp($v_siswa['id_siswa'], $periode);	
				if(($spp==0)||($spp==NULL)){
					$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*/
				$tabsis = $this->rep_sis->get_total_tabsis($v_siswa['id_siswa'], $periode);	
				if(($tabsis==0)||($tabsis==NULL)){
					$html .='<td style="text-align:right;">Rp. - </td>';
				}else{		
					$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) {
					$cicilan = $this->rep_sis->get_total_cicilan($v_siswa['id_siswa'], $periode);	
					$html .='<td style="text-align:right;">Rp. '.number_format($cicilan,2,",",".").'</td>';
					$total_cicilan=$total_cicilan+$cicilan;
				/*
				$bayar_cicilan = $this->rep_sis->get_total_bayar_cicilan($v_siswa['id_siswa'], $header_cicilan_r['kodedesk'], $header_cicilan_r['kodesub']);	
				$total_bayar_cicilan=$bayar_cicilan-$cicilan;
				
				if($total_bayar_cicilan<=0){
					$html .='<td style="text-align:right;"> LUNAS </td>';
				}else{		
					$html .='<td style="text-align:right;">Rp. '.number_format($total_bayar_cicilan,2,",",".").'</td>';
				}
				$total_cicilan=$total_cicilan+$total_bayar_cicilan;
				*/
			//}
				
				/*Get Detail Transaksi*/
				foreach ($header as $vheader) {
					$gtrans = $this->rep_sis->get_total_trans($v_siswa['id_siswa'],$vheader['id'], $periode);
					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 + $tabsis;
				$total_all = $total_all + $jml;			
				$html .='<td style="text-align:right;"><b>Rp. '.number_format($jml,2,",",".").'</b></td></tr>';
				/********************/
			}else{
				$spp = 0;
				$trans = 0;
				$cicilan = 0;
				/*Get Detail Siswa*/
				$html .='<td>'.$this->rep_sis->get_detail_ppdb($v_siswa['id_siswa']).'</td>';	
				$html .='<td>PPDB</td><td>'.$this->rep_sis->get_ruang_kelas_ppdb($v_siswa['id_siswa']).'</td>';
				$html .='<td>'.$this->rep_sis->get_kelas_ppdb($v_siswa['id_siswa']).'</td>';					
				/******************/
				
				/*Get Detail SPP Siswa*/
				$spp = $this->rep_sis->get_total_spp_ppdb($v_siswa['id_siswa'], $periode);	
				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*/
				$tabsis = $this->rep_sis->get_total_tabsis_ppdb($v_siswa['id_siswa'], $periode);	
				if(($tabsis==0)||($tabsis==NULL)){
					$html .='<td style="text-align:right;">Rp. - </td>';
				}else{		
					$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) {
					$cicilan = $this->rep_sis->get_total_cicilan_ppdb($v_siswa['id_siswa'], $periode);	
					$html .='<td style="text-align:right;">Rp. '.number_format($cicilan,2,",",".").'</td>';
					$total_cicilan=$total_cicilan+$cicilan;
					/*
				$bayar_cicilan = $this->rep_sis->get_total_bayar_cicilan($v_siswa['id_siswa'], $header_cicilan_r['kodedesk'], $header_cicilan_r['kodesub']);	
				$total_bayar_cicilan=$bayar_cicilan-$cicilan;
				
				if($total_bayar_cicilan<=0){
					$html .='<td style="text-align:right;"> LUNAS </td>';
				}else{		
					$html .='<td style="text-align:right;">Rp. '.number_format($total_bayar_cicilan,2,",",".").'</td>';
				}
				$total_cicilan=$total_cicilan+$total_bayar_cicilan;
				*/
			//}
				
				
				/*Get Detail Transaksi*/
				foreach ($header as $vheader) {
					$gtrans = $this->rep_sis->get_total_trans_ppdb($v_siswa['id_siswa'],$vheader['id'], $periode);
					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 + $cicilan + $tabsis;
				$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="5" >JUMLAH</td>';
		/*Total SPP Harian*/
		if((empty($smk))||($smk=='')){
			$spp_harian = $this->rep_sis->total_spp_harian($periode, '');
			$spp_harian_ppdb = $this->rep_sis->total_spp_harian_ppdb($periode, '');
		}else{
			$spp_harian = $this->rep_sis->total_spp_harian($periode, $smk);
			$spp_harian_ppdb = $this->rep_sis->total_spp_harian_ppdb($periode, $smk);
		}
		$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, '');
			$tabsis_harian_ppdb = $this->rep_sis->total_tabsis_harian_ppdb($periode, '');
		}else{
			$tabsis_harian = $this->rep_sis->total_tabsis_harian($periode, $smk);
			$tabsis_harian_ppdb = $this->rep_sis->total_tabsis_harian_ppdb($periode, $smk);
		}
		
		$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,'');
				$cicilan_harian_ppdb = $this->rep_sis->total_cicilan_harian_ppdb($periode,'');
			}else{
				$cicilan_harian = $this->rep_sis->total_cicilan_harian($periode,$smk);
				$cicilan_harian_ppdb = $this->rep_sis->total_cicilan_harian_ppdb($periode,$smk);
			}
			
			$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['id'],$periode,'');
					$otal_daily_ppdb = $this->rep_sis->get_total_trans_harian_ppdb($vheader['id'],$periode,'');
					if(($vheader['id_smk']==$otal_daily_ppdb->id_smk)||($vheader['id_smk']==$otal_daily->id_smk)){
						$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['id'],$periode,$smk);
					$otal_daily_ppdb = $this->rep_sis->get_total_trans_harian_ppdb($vheader['id'],$periode,$smk);
					if(($vheader['id_smk']==$otal_daily_ppdb->id_smk)||($vheader['id_smk']==$otal_daily->id_smk)){
						$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;
	}
	
	
}
