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

class Rep_spp_siswa_bulan extends CI_Controller {
	
	var $gparam = array();
	
	public function __construct() {
        parent::__construct();
		$this->gparam['controller'] = $this->router->fetch_class();
		$this->load->model('rep_spp_model_bulan', '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_spp_view_bulan',$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");	
		$date = new DateTime($periode);
		$resultM = $date->format('M');
		$resultY = $date->format('Y');
		
		$smk = $this->input->post("smk");
		if((empty($smk))||($smk=='')){
			$get_bln = $this->rep_sis->get_id_siswa($periode, '');
		}else{
			$get_bln = $this->rep_sis->get_id_siswa($periode, $smk);
		}
		$header = $this->rep_sis->get_header();
		
		/*Header*/
		$html = '<table class="table table-bordered" id="activity_table"><thead>';
		$html .='<tr ><td colspan="4"><center><b>PESAT</b></center></td></tr>';
		$html .='<tr ><td colspan="4"><center><b>Report SPP Bulanan  (Bulan: '.$resultM.', Tahun: '.$resultY.')</b></center></td></tr>';
		$html .='<tr ><td colspan="4"></td></tr>';
		$html .='<tr><th style="text-align:center;">No</th>';
		$html .='<th style="text-align:center;">Periode Bulan</th>';
		$html .='<th style="text-align:center;">Tanggal</th>';
		$html .='<th style="text-align:center;">Total SPP</th>';
		
		$html .= '</tr></thead>';
		$html .= '<tbody>';
		/*Close Header*/
		
		$total_all = 0;
		$spp = 0;
		$trans = 0;
		$i=1;
		foreach ($get_bln as $v_bln) {
			/*
			$data_spp = $this->rep_sis->get_total_spp($v_bln['id_siswa'], $periode);
			$date = new DateTime($periode);
			$result = $date->format('d/m/Y');
			$temp_count=0;
				*/
			$data_spp = $this->rep_sis->get_total_spp($v_bln['tanggal_transaksi']);
			$dateTr = new DateTime($v_bln['tanggal_transaksi']);
			$resultTr = $dateTr->format('d/m/Y');
			$html .='<tr>';
			$html .='<td>'.$i.'</td>';	 $i++;
			$html .='<td>'.$resultM.'</td>';	
			$html .='<td>'.$resultTr.'</td>';		
			$html .='<td style="text-align:right;">Rp. '.number_format($data_spp->total,2,",",".").'</td></tr>';
			
			
		}
		
		
		$html .= '<tr><td style="text-align:center; font-weight: bold;" colspan="3" >JUMLAH</td>';
		/*Total SPP Harian*/
		$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;"><b>Rp. '.number_format(($spp_harian+$spp_harian_ppdb),2,",",".").'</b></td></tr>';
		/*****************/
		
		
		$smk = $this->input->post("smk");
		
		/**********************/
		
		/*****************/
		$html .= '</tbody>';
		$html .= '</table>';
		//$html = "Peiode -- ".$periode;
		$html .= '<script>
					var name = "report_spp_bulanan_'.$resultM.'-'.$resultY.'.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;
	}
	
	
}
