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

class Rep_penerimaan_harian extends CI_Controller {
	
	var $gparam = array();
	
	public function __construct() {
        parent::__construct();
		$this->gparam['controller'] = $this->router->fetch_class();
		$this->load->model('rep_penerimaan_harian_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:240px;">
									<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> ';
			/////baru
					
					$getMetodePembayaran = $this->rep_sis->getMetodePembayaran();
					$metode_bayar='<select name="metode_bayar" class="chosen-select" style="width:240px;">
									<option value="">--Semua Metode Pembayaran--</option>';
					
					foreach ($getMetodePembayaran as $v_metode) {
						//for($i=0;$i<$temp_i;$i++){
							//if($edit->kelas==$v_smk['name']){
							//	$nama_smk .='<option value="'.$v_smk['name'].'" selected>'.$v_smk['name'].'</option>';
							//}else{
								$metode_bayar .='<option value="'.$v_metode['id_jenis_bayar'].'">'.$v_metode['jenis'].'</option>';
							//}
						//}
						
					}
					$metode_bayar .='</select> ';
								
					//$data['metode_bayar']=$metode_bayar;
					
					////
			// 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,
					'metode_bayar' 	=> $metode_bayar,
					'action'		=> $act
			);
			
			$this->load->view('rep_penerimaan_harian_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");
		$metode_bayar = $this->input->post("metode_bayar");
		if((empty($smk))||($smk=='')){
		    if((empty($metode_bayar))||($metode_bayar=='')){
		        $get_id_siswa = $this->rep_sis->get_id_siswa($periode, '', '');
		    }else{
		        $get_id_siswa = $this->rep_sis->get_id_siswa($periode, '', $metode_bayar);
		    }
			
			$header = $this->rep_sis->get_header('');
			$header_count = $this->rep_sis->get_count_header('');
		}else{
		    if((empty($metode_bayar))||($metode_bayar=='')){
		        $get_id_siswa = $this->rep_sis->get_id_siswa($periode, $smk, '');
		    }else{
		        $get_id_siswa = $this->rep_sis->get_id_siswa($periode, $smk, $metode_bayar);
		        
		    }
			$header = $this->rep_sis->get_header($smk);
			$header_count = $this->rep_sis->get_count_header($smk);
		}
		
		
		
		
		/*Header*/
		$html = '<div style="overflow:auto;"><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;">Tanggal Bayar</th>';
		$html .='<th rowspan="2" style="text-align:center;">Nama</th>';
		$html .='<th rowspan="2" style="text-align:center;">Tahun Ajaran</th>';
		$html .='<th rowspan="2" style="text-align:center;">Sekolah</th>';
		$html .='<th rowspan="2" style="text-align:center;">Kelas/Asal Sekolah</th>';
		$html .='<th rowspan="2" style="text-align:center;">NIS</th>';
		$html .='<th rowspan="2" style="text-align:center;">Status</th>';
		$html .='<th rowspan="2" style="text-align:center;">No. Faktur</th>';
		$html .='<th rowspan="2" style="text-align:center;">Metode Pembayaran</th>';
		$html .='<th rowspan="2" style="text-align:center;">Teller</th>';
		//$html .='<th rowspan="2" style="text-align:center;">Kelas</th>';
		$html .='<th colspan="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>';
		}
		*/
		foreach ($header as $value_header) {
			$html .='<th style="text-align:center;" rowspan="2">'.$value_header['daftarlist'].'</th>';
		}
		$html .='<th rowspan="2" style="text-align:center;" >Jumlah</th>';
		$html .= '</tr></thead>';
		
		
		$html .= '<thead><tr>';
			$html .='<th style="text-align:center;">Dibayarkan</th>';
			$html .='<th style="text-align:center;">Rp.</th>';
			/*
		foreach ($header_cicilan as $header_cicilan_r) {
			$html .='<th style="text-align:center;">'.$header_cicilan_r['daftarlist'].'</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>';	
			$html .='<td>'.$v_siswa['tanggal_pembayaran'].'</td>';	
			$temp_count_1++;
			if($v_siswa['sts_ppdb']==0){
				$map_kls = $this->rep_sis->get_mapping_kelas($v_siswa['id_siswa']);
				$no_faktur = $this->rep_sis->get_no_faktur($v_siswa['id_siswa'], $periode);	
				$teller = strtoupper($this->rep_sis->get_teller($v_siswa['no_faktur']));	
				
				$spp = 0;
				$trans = 0;
				$cicilan = 0;
				/*Get Detail Siswa*/
				$html .='<td>'.$this->rep_sis->get_detail($v_siswa['id_siswa']).'</td>';	
				$html .='<td>'.$this->rep_sis->get_thn_ajaran_siswa($v_siswa['id_siswa']).'</td>';
				$html .='<td>'.$this->rep_sis->get_ruang_kelas($v_siswa['id_siswa']).'</td>';
				$html .='<td>'.$this->ConverToRoman($map_kls->kelas).' - '.$map_kls->ruang_kelas.'</td>';	
				$html .='<td  align="center">'.$this->rep_sis->get_nis($v_siswa['id_siswa']).'</td>';
				$html .='<td align="center">SISWA</td>';
				$html .='<td>'.$v_siswa['no_faktur'].'</td>';	
				$html .='<td align="center">'.$v_siswa['jenis'].'</td>';
				$html .='<td>'.$teller.'</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, $v_siswa['no_faktur']);	
				$html .='<td>'.$this->rep_sis->get_spp_bln($v_siswa['id_siswa'], $periode, $v_siswa['no_faktur']).'</td>';
				if(($spp==0)||($spp==NULL)){
					$html .='<td style="text-align:right;">0</td>';
				}else{		
					$html .='<td style="text-align:right;">'.number_format($spp,0,",","").'</td>';
				}
				
				
				
				/*****************/
				
				/*Get Detail TABSIS Siswa*/
				$tabsis = $this->rep_sis->get_total_tabsis($v_siswa['id_siswa'], $periode, $v_siswa['no_faktur']);	
				
				if(($tabsis->total==0)||($tabsis->total==NULL)){
					$ts=0;
					$html .='<td style="text-align:right;">0</td>';
				}else{		
					if($tabsis->nf=="FK"){
						$ts=$tabsis->total*(-1);
						$html .='<td style="text-align:right;">-'.number_format($tabsis->total,0,",","").'</td>';
					}else if($tabsis->nf=="FM"){
						$ts=$tabsis->total*(1);
						$html .='<td style="text-align:right;">'.number_format($tabsis->total,0,",","").'</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, $v_siswa['no_faktur']);	
					if(($cicilan->total==0)||($cicilan->total==NULL)){
						$tc=0;
						$html .='<td style="text-align:right;">0</td>';
					}else{	
						if($cicilan->nf=="FK"){
							$tc=$cicilan->total*(-1);
							$html .='<td style="text-align:right;">-'.number_format($cicilan->total,0,",","").'</td>';
						}else if($cicilan->nf=="FM"){
							$tc=$cicilan->total*(1);
							$html .='<td style="text-align:right;">'.number_format($cicilan->total,0,",","").'</td>';
						}
					}
				
					
					$total_cicilan=$total_cicilan+$tc;
				/*
				$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;">'.number_format($total_bayar_cicilan,0,",","").'</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'], $periode, $vheader['kodedesk'], $vheader['kodesub'], $v_siswa['no_faktur']);
					//if($vheader['id_smk']==$gtrans->id_smk){
						$trans = $trans + $gtrans->total; 
						if($gtrans->total==0){
							$html .='<td style="text-align:right;">0</td>';
						}else{
							$html .='<td style="text-align:right;">'.number_format($gtrans->total,0,",","").'</td>';
						}
						/*
					}else{
						$trans = $trans;
						$html .='<td style="text-align:right;">0</td>';
					}
					*/
					
				}			
				/*******************/
				
				/* Get Total Transaction */
				$jml = $spp + $trans + $total_cicilan + $ts;
				$total_all = $total_all + $jml;			
				$html .='<td style="text-align:right;"><b>'.number_format($jml,0,",","").'</b></td></tr>';
				/********************/
			}else{
				$spp = 0;
				$trans = 0;
				$cicilan = 0;
				/*Get Detail Siswa*/
				$no_faktur = $this->rep_sis->get_no_faktur_ppdb($v_siswa['id_siswa'], $periode);	
				$teller = strtoupper($this->rep_sis->get_teller($v_siswa['no_faktur']));	
				$html .='<td>'.$this->rep_sis->get_detail_ppdb($v_siswa['id_siswa']).'</td>';	
				$html .='<td>'.$this->rep_sis->get_thn_ajaran_siswa_ppdb($v_siswa['id_siswa']).'</td>';
				$html .='<td>'.$this->rep_sis->get_ruang_kelas_ppdb($v_siswa['id_siswa']).'</td>';
				$html .='<td>'.$this->rep_sis->get_asal_sekolah($v_siswa['id_siswa']).'</td>';
				$html .='<td align="center"> - </td>';
				$html .='<td align="center">PPDB</td>';
				$html .='<td>'.$v_siswa['no_faktur'].'</td>';
				$html .='<td align="center">'.$v_siswa['jenis'].'</td>';
				$html .='<td>'.$teller.'</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, $v_siswa['no_faktur']);	
				$html .='<td>'.$this->rep_sis->get_spp_ppdb_bln($v_siswa['id_siswa'], $periode, $v_siswa['no_faktur']).'</td>';
				if($spp==0){
					$html .='<td style="text-align:right;">0</td>';
				}else{		
					$html .='<td style="text-align:right;">'.number_format($spp,0,",","").'</td>';
				}
				/*****************/
				
				/*Get Detail TABSIS Siswa*/
				$tabsis = $this->rep_sis->get_total_tabsis_ppdb($v_siswa['id_siswa'], $periode, $v_siswa['no_faktur']);	
				if(($tabsis->total==0)||($tabsis->total==NULL)){
					$ts=0;
					$html .='<td style="text-align:right;">0</td>';
				}else{		
					if($tabsis->nf=="FK"){
						$ts=$tabsis->total*(-1);
						$html .='<td style="text-align:right;">-'.number_format($tabsis->total,0,",","").'</td>';
					}else if($tabsis->nf=="FM"){
						$ts=$tabsis->total*(1);
						$html .='<td style="text-align:right;">'.number_format($tabsis->total,0,",","").'</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, $v_siswa['no_faktur']);	
				if(($cicilan->total==0)||($cicilan->total==NULL)){
					$tc=0;
					$html .='<td style="text-align:right;">0</td>';
				}else{	
					if($cicilan->nf=="FK"){
						$tc=$cicilan->total*(-1);
						$html .='<td style="text-align:right;">-'.number_format($tc,0,",","").'</td>';
					}else if($cicilan->nf=="FM"){
						$tc=$cicilan->total*(1);
						$html .='<td style="text-align:right;">'.number_format($tc,0,",","").'</td>';
					}
				}
				$total_cicilan=$total_cicilan+$tc;
				
			//}
				
				
				/*Get Detail Transaksi*/
				foreach ($header as $vheader) {
					$gtrans = $this->rep_sis->get_total_trans_ppdb($v_siswa['id_siswa'], $periode, $vheader['kodedesk'], $vheader['kodesub'], $v_siswa['no_faktur']);
					//if($vheader['id_smk']==$gtrans->id_smk){
						$trans = $trans + $gtrans->total; 
						if($gtrans->total==0){
							$html .='<td style="text-align:right;">0</td>';
						}else{
							$html .='<td style="text-align:right;">'.number_format($gtrans->total,0,",","").'</td>';
						}
						/*
					}else{
						$trans = $trans;
						$html .='<td style="text-align:right;">0</td>';
					}
					*/
				}			
				/*******************/
				
				/* Get Total Transaction */
				$jml = $spp + $trans + $total_cicilan + $ts;
				$total_all = $total_all + $jml;			
				$html .='<td style="text-align:right;"><b>'.number_format($jml,0,",","").'</b></td></tr>';
				/********************/
				
			}
		}
		
		
		$html .= '<tr><td style="text-align:center; font-weight: bold;" colspan="11" >JUMLAH</td>';
		//$html .= '<td style="text-align:center;">-</td>';
		$html .= '<td style="text-align:center;">-</td>';
		/*Total SPP Harian*/
		if((empty($smk))||($smk=='')){
		    if((empty($metode_bayar))||($metode_bayar=='')){
		        $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, '', $metode_bayar);
			    $spp_harian_ppdb = $this->rep_sis->total_spp_harian_ppdb($periode, '', $metode_bayar);
		    }
			
		}else{
		    if((empty($metode_bayar))||($metode_bayar=='')){
		        $spp_harian = $this->rep_sis->total_spp_harian($periode, $smk,'');
    			$spp_harian_ppdb = $this->rep_sis->total_spp_harian_ppdb($periode, $smk,'');
		    }else{
		        $spp_harian = $this->rep_sis->total_spp_harian($periode, $smk, $metode_bayar);
    			$spp_harian_ppdb = $this->rep_sis->total_spp_harian_ppdb($periode, $smk, $metode_bayar);
		    }
			
		}
		$html .= '<td style="text-align:right;">'.number_format(($spp_harian + $spp_harian_ppdb),0,",","").'</td>';
		/*****************/
		
		if((empty($smk))||($smk=='')){
		    if((empty($metode_bayar))||($metode_bayar=='')){
		        $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, '',$metode_bayar);
    			$tabsis_harian_ppdb = $this->rep_sis->total_tabsis_harian_ppdb($periode, '',$metode_bayar);
		    }
			
		}else{
		    if((empty($metode_bayar))||($metode_bayar=='')){
		        $tabsis_harian = $this->rep_sis->total_tabsis_harian($periode, $smk,'');
			    $tabsis_harian_ppdb = $this->rep_sis->total_tabsis_harian_ppdb($periode, $smk,'');
		    }else{
		        $tabsis_harian = $this->rep_sis->total_tabsis_harian($periode, $smk,$metode_bayar);
			    $tabsis_harian_ppdb = $this->rep_sis->total_tabsis_harian_ppdb($periode, $smk,$metode_bayar);
		        
		    }
			
		}
		
		$html .= '<td style="text-align:right;">'.number_format($tabsis_harian + $tabsis_harian_ppdb,0,",","").'</td>';
		
		/*Total Cicilan Harian*/
		//foreach ($header_cicilan as $header_cicilan_r) {
			if((empty($smk))||($smk=='')){
			     if((empty($metode_bayar))||($metode_bayar=='')){
			         $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,'',$metode_bayar);
    				$cicilan_harian_ppdb = $this->rep_sis->total_cicilan_harian_ppdb($periode,'',$metode_bayar);
			         
			     }
				
			}else{
			    if((empty($metode_bayar))||($metode_bayar=='')){
			         $cicilan_harian = $this->rep_sis->total_cicilan_harian($periode,$smk,'');
    				$cicilan_harian_ppdb = $this->rep_sis->total_cicilan_harian_ppdb($periode,$smk,'');
			         
			     }else{
			         $cicilan_harian = $this->rep_sis->total_cicilan_harian($periode,$smk,$metode_bayar);
    				$cicilan_harian_ppdb = $this->rep_sis->total_cicilan_harian_ppdb($periode,$smk,$metode_bayar);
			         
			     }
				
			}
			
			$html .= '<td style="text-align:right;">'.number_format(($cicilan_harian + $cicilan_harian_ppdb),0,",","").'</td>';
		//}
		
		/*****************/
		
		
		$smk = $this->input->post("smk");
		/*Total Transaksi Harian*/
		
		foreach ($header as $vheader) {
				if((empty($smk))||($smk=='')){
				     if((empty($metode_bayar))||($metode_bayar=='')){
				         $otal_daily = $this->rep_sis->get_total_trans_harian($vheader['kodedesk'], $vheader['kodesub'],$periode,'','');
    					$otal_daily_ppdb = $this->rep_sis->get_total_trans_harian_ppdb($vheader['kodedesk'], $vheader['kodesub'],$periode,'','');
				     }else{
				         $otal_daily = $this->rep_sis->get_total_trans_harian($vheader['kodedesk'], $vheader['kodesub'],$periode,'',$metode_bayar);
    					$otal_daily_ppdb = $this->rep_sis->get_total_trans_harian_ppdb($vheader['kodedesk'], $vheader['kodesub'],$periode,'',$metode_bayar);
				         
				     }
					
					if(($otal_daily_ppdb->total+$otal_daily->total)>0){
						$html .='<td style="text-align:right;"><b>'.number_format(($otal_daily_ppdb->total+$otal_daily->total),0,",","").'</b></td>';
					}else{
						$html .='<td style="text-align:right;"><b>0</b></td>';
					}
						
				}else{
				    if((empty($metode_bayar))||($metode_bayar=='')){
				        $otal_daily = $this->rep_sis->get_total_trans_harian($vheader['kodedesk'], $vheader['kodesub'],$periode,$smk,'');
    					$otal_daily_ppdb = $this->rep_sis->get_total_trans_harian_ppdb($vheader['kodedesk'], $vheader['kodesub'],$periode,$smk,'');
				    }else{
				        $otal_daily = $this->rep_sis->get_total_trans_harian($vheader['kodedesk'], $vheader['kodesub'],$periode,$smk,$metode_bayar);
    					$otal_daily_ppdb = $this->rep_sis->get_total_trans_harian_ppdb($vheader['kodedesk'], $vheader['kodesub'],$periode,$smk,$metode_bayar);
				    }
					
					if(($otal_daily_ppdb->total+$otal_daily->total)>0){
						$html .='<td style="text-align:right;"><b>'.number_format(($otal_daily_ppdb->total+$otal_daily->total),0,",","").'</b></td>';
					}else{
						$html .='<td style="text-align:right;"><b>0</b></td>';
					}
				}
				
		}
		
		/**********************/
		
		/*Summary Closing*/
		$html .='<td style="text-align:right;"><b>'.number_format($total_all,0,",","").'</b></td></tr>';
		/*****************/
		$html .= '</tbody>';
		$html .= '</table></div>';
		//$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; 
	} 

	
	
}
