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


class Rep_tunggakan_sis extends CI_Controller {
	
	var $gparam = array();
	public $test123="";
	public function __construct() {
        parent::__construct();
		$this->gparam['controller'] = $this->router->fetch_class();
		$this->load->model('rep_tunggakan_sis_model', 'rep_sis');
		$this->gparam['privilage'] = getPrivilage($this->gparam['controller']);
		$this->gparam['restrict'] = 'You Cannot Access This Menu';
		
    }
	
	public function index()	{
		//$smk_ses = "";
		
		
		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 = '';	
							 
			$pdf_download = '<div id="id-MappingPDF"></div>';	

			
			$get_smk = $this->rep_sis->getDataSMK();
			$nama_smk='	<select name="smk" class="chosen-select" style="width:195px;" onchange="getSis(this.value);getPDF(this.value, '.'0'.');" >
									<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> ';
			
			$mappingSis = '<div id="id-MappingSis">
					<label class="select">
						<select name="id_mapping_sis" class="chosen-select" style="width:300px;" id="id_mapping_sis" onchange="cekKelas();getRuang();">
							<option value="">--Pilih Nama Siswa--</option>																
						</select> <i></i> 
					</label></div>';
					
			
			
			// 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,
					'pdf' 		=> $pdf_download,
					'nama_smk' 		=> $nama_smk,
					'mappingSis'	=> $mappingSis,
					'action'		=> $act
			);
			
			$this->load->view('rep_tunggakan_sis_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);
		 */
		$bulan = array("July","August","September","October","November","December","January","February","March","April","May","June");
		$now = new \DateTime('now');
	   	$month = $now->format('m');
	   	$year = $now->format('Y');
		
		$curr_bulan = $month - 6;
		if($curr_bulan <= 0){
			$curr_bulan = $month + 6;
		}
		
		$periode = $this->input->post("periode");	
		$smk = $this->input->post("smk");
		$sis = $this->input->post("id_mapping_sis");
		
		
				
		$get_id_siswa = $this->rep_sis->get_id_siswa($smk, $sis);
		$thn_ajaran_sis = $this->rep_sis->get_thn_ajaran($sis);
		$thn_ajaran_sis_count = $this->rep_sis->get_jum_thn_ajaran($sis);
		
		
		$tem_c_thn=1;
		
		/*Header*/
		$html = '<table class="table table-bordered" width="100%" border="0" id="activity_table">';
		$html .= '<tr><td><b>Nama : '.$get_id_siswa->nama.'</b></td></tr>';
		$html .= '<tr><td><b>NIS : '.$get_id_siswa->nis.'</b></td></tr>';
		foreach($thn_ajaran_sis as $thn){
			$spp_rep = $this->rep_sis->get_spp_rep($sis, $thn['tahun_ajaran']);	
			$thn_ajaran=$this->rep_sis->get_tahun_ajaran();
			$html .= '<tr><td><h5>'.$thn['tahun_ajaran'].'</h5></td></tr>';
			$html .= '<tr><td><b>SPP :</b></td></tr>';
			
			//
			$count_spp_rep='';
			$temp_c=0;
			if($thn['tahun_ajaran']==$thn_ajaran){
				for($i=1;$i<=$curr_bulan;$i++){
					if($i==1){
						if((empty($spp_rep->July))||($spp_rep->July=='')){
							if($temp_c==0){
								$count_spp_rep=$count_spp_rep."July (".$spp_rep->biaya.")";
								$temp_c=1;
							}else{
								$count_spp_rep=$count_spp_rep.", July (".$spp_rep->biaya.")";
							}
						}
					}else if($i==2){
						if((empty($spp_rep->August))||($spp_rep->August=='')){
							if($temp_c==0){
								$count_spp_rep=$count_spp_rep."August (".$spp_rep->biaya.")";
								$temp_c=1;
							}else{
								$count_spp_rep=$count_spp_rep.", August (".$spp_rep->biaya.")";
							}
						}
					}else if($i==3){
						if((empty($spp_rep->September))||($spp_rep->September=='')){
							if($temp_c==0){
								$count_spp_rep=$count_spp_rep."September (".$spp_rep->biaya.")";
								$temp_c=1;
							}else{
								$count_spp_rep=$count_spp_rep.", September (".$spp_rep->biaya.")";
							}
						}
					}else if($i==4){
						if((empty($spp_rep->October))||($spp_rep->October=='')){
							if($temp_c==0){
								$count_spp_rep=$count_spp_rep."October (".$spp_rep->biaya.")";
								$temp_c=1;
							}else{
								$count_spp_rep=$count_spp_rep.", October (".$spp_rep->biaya.")";
							}
						}
					}else if($i==5){
						if((empty($spp_rep->November))||($spp_rep->November=='')){
							if($temp_c==0){
								$count_spp_rep=$count_spp_rep."November (".$spp_rep->biaya.")";
								$temp_c=1;
							}else{
								$count_spp_rep=$count_spp_rep.", November (".$spp_rep->biaya.")";
							}
						}
					}else if($i==6){
						if((empty($spp_rep->December))||($spp_rep->December=='')){
							if($temp_c==0){
								$count_spp_rep=$count_spp_rep."December (".$spp_rep->biaya.")";
								$temp_c=1;
							}else{
								$count_spp_rep=$count_spp_rep.", December (".$spp_rep->biaya.")";
							}
						}
					}else if($i==7){
						if((empty($spp_rep->January))||($spp_rep->January=='')){
							if($temp_c==0){
								$count_spp_rep=$count_spp_rep."January (".$spp_rep->biaya.")";
								$temp_c=1;
							}else{
								$count_spp_rep=$count_spp_rep.", January (".$spp_rep->biaya.")";
							}
						}
					}else if($i==8){
						if((empty($spp_rep->February))||($spp_rep->February=='')){
							if($temp_c==0){
								$count_spp_rep=$count_spp_rep."February (".$spp_rep->biaya.")";
								$temp_c=1;
							}else{
								$count_spp_rep=$count_spp_rep.", February (".$spp_rep->biaya.")";
							}
						}
					}else if($i==9){
						if((empty($spp_rep->March))||($spp_rep->March=='')){
							if($temp_c==0){
								$count_spp_rep=$count_spp_rep."March (".$spp_rep->biaya.")";
								$temp_c=1;
							}else{
								$count_spp_rep=$count_spp_rep.", March (".$spp_rep->biaya.")";
							}
						}
					}else if($i==10){
						if((empty($spp_rep->April))||($spp_rep->April=='')){
							if($temp_c==0){
								$count_spp_rep=$count_spp_rep."April (".$spp_rep->biaya.")";
								$temp_c=1;
							}else{
								$count_spp_rep=$count_spp_rep.", April (".$spp_rep->biaya.")";
							}
						}
					}else if($i==11){
						if((empty($spp_rep->May))||($spp_rep->May=='')){
							if($temp_c==0){
								$count_spp_rep=$count_spp_rep."May (".$spp_rep->biaya.")";
								$temp_c=1;
							}else{
								$count_spp_rep=$count_spp_rep.", May (".$spp_rep->biaya.")";
							}
						}
					}else if($i==12){
						if((empty($spp_rep->June))||($spp_rep->June=='')){
							if($temp_c==0){
								$count_spp_rep=$count_spp_rep."June (".$spp_rep->biaya.")";
								$temp_c=1;
							}else{
								$count_spp_rep=$count_spp_rep.", June (".$spp_rep->biaya.")";
							}
						}
					}
				}
			}else{
					for($i=1;$i<=12;$i++){
						
						if($i==1){
							if((empty($spp_rep->July))||($spp_rep->July=='')){
								if($temp_c==0){
									$count_spp_rep=$count_spp_rep."July (".$spp_rep->biaya.")";
									$temp_c=1;
								}else{
									$count_spp_rep=$count_spp_rep.", July (".$spp_rep->biaya.")";
								}
							}
						}else if($i==2){
							if((empty($spp_rep->August))||($spp_rep->August=='')){
								if($temp_c==0){
									$count_spp_rep=$count_spp_rep."August (".$spp_rep->biaya.")";
									$temp_c=1;
								}else{
									$count_spp_rep=$count_spp_rep.", August (".$spp_rep->biaya.")";
								}
							}
						}else if($i==3){
							if((empty($spp_rep->September))||($spp_rep->September=='')){
								if($temp_c==0){
									$count_spp_rep=$count_spp_rep."September (".$spp_rep->biaya.")";
									$temp_c=1;
								}else{
									$count_spp_rep=$count_spp_rep.", September (".$spp_rep->biaya.")";
								}
							}
						}else if($i==4){
							if((empty($spp_rep->October))||($spp_rep->October=='')){
								if($temp_c==0){
									$count_spp_rep=$count_spp_rep."October (".$spp_rep->biaya.")";
									$temp_c=1;
								}else{
									$count_spp_rep=$count_spp_rep.", October (".$spp_rep->biaya.")";
								}
							}
						}else if($i==5){
							if((empty($spp_rep->November))||($spp_rep->November=='')){
								if($temp_c==0){
									$count_spp_rep=$count_spp_rep."November (".$spp_rep->biaya.")";
									$temp_c=1;
								}else{
									$count_spp_rep=$count_spp_rep.", November (".$spp_rep->biaya.")";
								}
							}
						}else if($i==6){
							if((empty($spp_rep->December))||($spp_rep->December=='')){
								if($temp_c==0){
									$count_spp_rep=$count_spp_rep."December (".$spp_rep->biaya.")";
									$temp_c=1;
								}else{
									$count_spp_rep=$count_spp_rep.", December (".$spp_rep->biaya.")";
								}
							}
						}else if($i==7){
							if((empty($spp_rep->January))||($spp_rep->January=='')){
								if($temp_c==0){
									$count_spp_rep=$count_spp_rep."January (".$spp_rep->biaya.")";
									$temp_c=1;
								}else{
									$count_spp_rep=$count_spp_rep.", January (".$spp_rep->biaya.")";
								}
							}
						}else if($i==8){
							if((empty($spp_rep->February))||($spp_rep->February=='')){
								if($temp_c==0){
									$count_spp_rep=$count_spp_rep."February (".$spp_rep->biaya.")";
									$temp_c=1;
								}else{
									$count_spp_rep=$count_spp_rep.", February (".$spp_rep->biaya.")";
								}
							}
						}else if($i==9){
							if((empty($spp_rep->March))||($spp_rep->March=='')){
								if($temp_c==0){
									$count_spp_rep=$count_spp_rep."March (".$spp_rep->biaya.")";
									$temp_c=1;
								}else{
									$count_spp_rep=$count_spp_rep.", March (".$spp_rep->biaya.")";
								}
							}
						}else if($i==10){
							if((empty($spp_rep->April))||($spp_rep->April=='')){
								if($temp_c==0){
									$count_spp_rep=$count_spp_rep."April (".$spp_rep->biaya.")";
									$temp_c=1;
								}else{
									$count_spp_rep=$count_spp_rep.", April (".$spp_rep->biaya.")";
								}
							}
						}else if($i==11){
							if((empty($spp_rep->May))||($spp_rep->May=='')){
								if($temp_c==0){
									$count_spp_rep=$count_spp_rep."May (".$spp_rep->biaya.")";
									$temp_c=1;
								}else{
									$count_spp_rep=$count_spp_rep.", May (".$spp_rep->biaya.")";
								}
							}
						}else if($i==12){
							if((empty($spp_rep->June))||($spp_rep->June=='')){
								if($temp_c==0){
									$count_spp_rep=$count_spp_rep."June (".$spp_rep->biaya.")";
									$temp_c=1;
								}else{
									$count_spp_rep=$count_spp_rep.", June (".$spp_rep->biaya.")";
								}
							}
						}
						
					}	
				}
			
			//
			$html .= '<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;- '.$count_spp_rep.'</td></tr>';
			$GLOBALS['count_spp_rep'] = $count_spp_rep;
			
			$html .= '<tr><td><b>Biaya Siswa :</b></td></tr>';
			$gtotal_maps = $this->rep_sis->get_total_map_biaya_siswa($sis, $thn['tahun_ajaran']);
			foreach($gtotal_maps as $gtotal_map){
				$gtrans 	= $this->rep_sis->get_total_trans($sis, $gtotal_map['id'], $gtotal_map['tahun_ajaran']);
				if((empty($gtrans))||($gtrans==NULL)){
					
					if(($get_id_siswa->kelas)==($gtotal_map['kelas'])){
						if($gtotal_map['tahun_ajaran']==$thn_ajaran){
							if(($gtotal_map['periode'])<=($curr_bulan)){
								$html .= '<tr><td><b><font color="red">&nbsp;&nbsp;&nbsp;&nbsp;- '.$gtotal_map['jenis_pembayaran'].': '.$gtotal_map['total'].'</font></b></td></tr>';
							}else{
								$html .= '<tr><td><b><font color="#F87217">&nbsp;&nbsp;&nbsp;&nbsp;- '.$gtotal_map['jenis_pembayaran'].': Belum Ditagihkan </font></b></td></tr>';
							}
						}else{
							$html .= '<tr><td><b><font color="red">&nbsp;&nbsp;&nbsp;&nbsp;- '.$gtotal_map['jenis_pembayaran'].': '.$gtotal_map['total'].'</font></b></td></tr>';
						}
					}else if(($get_id_siswa->kelas)>($gtotal_map['kelas'])){
						$html .= '<tr><td><b><font color="red">&nbsp;&nbsp;&nbsp;&nbsp;- '.$gtotal_map['jenis_pembayaran'].': '.$gtotal_map['total'].'</font></b></td></tr>';
					}else{
						$html .= '<tr><td><b><font color="#F87217">&nbsp;&nbsp;&nbsp;&nbsp;- '.$gtotal_map['jenis_pembayaran'].': Belum Ditagihkan </font></b></td></tr>';
					}
				}else{
					
					if(empty($gtrans->total)||$gtrans->total==NULL){
						$min_trans = $gtotal_map['total'];
					}else{
						$min_trans = $gtotal_map['total']-$gtrans->total;
					}
					if(($get_id_siswa->kelas)==($gtotal_map['kelas'])){
						if($gtotal_map['tahun_ajaran']==$thn_ajaran){
							if(($gtotal_map['periode'])<=($curr_bulan)){
								if($min_trans<=0){
									$html .= '<tr><td><b>&nbsp;&nbsp;&nbsp;&nbsp;- '.$gtotal_map['jenis_pembayaran'].': LUNAS </b></td></tr>';
								}else{
									$html .= '<tr><td><b><font color="red">&nbsp;&nbsp;&nbsp;&nbsp;- '.$gtotal_map['jenis_pembayaran'].': '.$min_trans.' </font></b></td></tr>';
								}
							}else{
								$html .= '<tr><td><b><font color="#F87217">&nbsp;&nbsp;&nbsp;&nbsp;- '.$gtotal_map['jenis_pembayaran'].': Belum Ditagihkan </font></b></td></tr>';
							}
						}else{
							if($min_trans<=0){
								$html .= '<tr><td><b>&nbsp;&nbsp;&nbsp;&nbsp;- '.$gtotal_map['jenis_pembayaran'].': LUNAS </b></td></tr>';
							}else{
								$html .= '<tr><td><b><font color="red">&nbsp;&nbsp;&nbsp;&nbsp;- '.$gtotal_map['jenis_pembayaran'].': '.$min_trans.' </font></b></td></tr>';
							}
						}
					}else if(($get_id_siswa->kelas)>($gtotal_map['kelas'])){
						if($min_trans<=0){
							$html .= '<tr><td><b>&nbsp;&nbsp;&nbsp;&nbsp;- '.$gtotal_map['jenis_pembayaran'].': LUNAS </b></td></tr>';
						}else{
							$html .= '<tr><td><b><font color="red">&nbsp;&nbsp;&nbsp;&nbsp;- '.$gtotal_map['jenis_pembayaran'].': '.$min_trans.' </font></b></td></tr>';
						}
					}else{
						$html .= '<tr><td><b><font color="#F87217">&nbsp;&nbsp;&nbsp;&nbsp;- '.$gtotal_map['jenis_pembayaran'].': Belum Ditagihkan </font></b></td></tr>';
					}
					
					
				}
			}
			if($thn_ajaran_sis_count==$tem_c_thn){}
			
			$tem_c_thn=$tem_c_thn+1;
			$html .= '<tr bgcolor="#000000"><td></td></tr>';
		}
		
				$html .= '<tr><td><b>Biaya Cicilan Siswa :</b></td></tr>';
				$gtotal_maps = $this->rep_sis->get_total_map_cicilan_siswa($sis, $thn['tahun_ajaran']);
				foreach($gtotal_maps as $gtotal_map){
					$gtrans 	= $this->rep_sis->get_total_trans_cicilan($sis, $gtotal_map['id']);
					if(empty($gtrans->total)||$gtrans->total==NULL){
						$min_cicilan = $gtotal_map['total'];
					}else{
						$min_cicilan = $gtotal_map['total']-$gtrans->total;
					}
					
					if($min_cicilan<=0){
						$html .= '<tr><td><b>&nbsp;&nbsp;&nbsp;&nbsp;- '.$gtotal_map['jenis_pembayaran'].': LUNAS</b></td></tr>';
					}else{
						$html .= '<tr><td><b><font color="red">&nbsp;&nbsp;&nbsp;&nbsp;- '.$gtotal_map['jenis_pembayaran'].': '.$min_cicilan.'</font></b></td></tr>';	
					}
				}
			
		$html .= '<tbody>';
		/*Close Header*/
		
		
		$total_all = 0;
		$total_spp = 0;
		
		$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 getPDF() {
		$idsiswa = $this->input->post("idsiswa");
		$idsmk = $this->input->post("idsmk");
		if($idsiswa==NULL||empty($idsiswa)||$idsiswa==''||$idsiswa=='0'){
			$pdf_download ='';
		}else{
			//<div id="id-MappingPDF"><a class="btn btn-success" id="save_excell" href="#" >
								//<i class="fa fa-lg fa-fw fa-download"></i>Excell
							 // </a>
			$pdf_download = '<a style="margin:4px;" class="btn btn-success" onclick="cetakPDF('.$idsmk.','.$idsiswa.');"  href="javascript:void(0)"><i class="fa fa-lg fa-fw fa-download"></i>PDF
									  </a></div>';
		}
								  
		echo $pdf_download;
	}
	function getSis() {
		$id_smk = $this->input->post("smk");
		$options = $this->rep_sis->getSis($id_smk);
	
		$html = '<label class="select">
						<select name="id_mapping_sis" style="width:300px;" class="chosen-select" id="id_mapping_sis" onchange="getPDF('.$id_smk.', this.value);">
							<option value="">--Pilih Nama Siswa--</option>																
						'.$options.'
						</select> <i></i> 
					</label>';
		$html .='<script>
					$(function() {
						$(".chosen-select").chosen();
					})
				</script>';	
		echo $html;			
	}
	
	
	function convert_bln($month){
		$curr_bulan = $month - 6;
		if($curr_bulan <= 0){
			$curr_bulan = $month + 6;
		}
		return $curr_bulan;
	}
	
	function printPDF() {
	
		define('FPDF_FONTPATH',$this->config->item('fonts_path'));
		$idsiswa = $_GET['idsiswa'];
		$idsmk = $_GET['idsmk'];
		/*
		$faktur = $_GET['faktur'];
		$diserahkan = $this->trans_siswa->getDiserahkan($faktur);
		$diterima  	= $this->trans_siswa->getDiterima($faktur);
		$total = $this->trans_siswa->getTotal($faktur);
		$get = $this->trans_siswa->getDetailFaktur($faktur);
		
		//update faktur ke cetak
		//$upStatus = $this->trans_siswa->updateStatusFaktur($faktur);
		
		
		*/
		$data = array(
			'idsiswa'	 => $idsiswa,
			'idsmk'	 => $idsmk
		);
		$this->load->view("printTransSiswa", $data);
	}
}
