<?php 
class Ppdb_komponen_biaya_model extends CI_Model {
	
	function get_value_edit($id) {
		$this->db->select('*');
		$this->db->from('ppdb_siswa');
		$this->db->where('id_daftar',$id);
		$data = $this->db->get()->row();
		return $data;
	}
	function getJurEdit($id_smk){
		$this->db->select('*');
		$this->db->from('ref_jur');
		$this->db->where('id_smk',$id_smk);
		$data = $this->db->get()->result_array();
		return $data;
	}
	
	function getKomponenBiaya($id){
		$this->db->select('*');
		$this->db->from('ref_mapping_biaya_siswa');
		$this->db->where('status',0);
		$this->db->where('id_ppdb',$id);
		$this->db->order_by("id", "asc");
		$data = $this->db->get()->result_array();
		return $data;
	}
	function getKomponenBiayaNumRow($id){
		$this->db->select('*');
		$this->db->from('ref_mapping_biaya_siswa');
		$this->db->where('status',0);
		$this->db->where('id_ppdb',$id);
		$data = $this->db->get()->num_rows();
		return $data;
	}
	
	function getKomponenSPP($id){
		$this->db->select('*');
		$this->db->from('rep_spp');
		$this->db->where('status',0);
		$this->db->where('id_ppdb',$id);
		$this->db->order_by("id", "asc");
		$data = $this->db->get()->result_array();
		return $data;
	}
	function getKomponenSPPNumRow($id){
		$this->db->select('*');
		$this->db->from('rep_spp');
		$this->db->where('status',0);
		$this->db->where('id_ppdb',$id);
		$data = $this->db->get()->num_rows();
		return $data;
	}
	
	function getKomponenTabsis($id){
		$this->db->select('*');
		$this->db->from('rep_tabsis');
		$this->db->where('status',0);
		$this->db->where('id_ppdb',$id);
		$this->db->order_by("id", "asc");
		$data = $this->db->get()->result_array();
		return $data;
	}
	function getKomponenTabsisNumRow($id){
		$this->db->select('*');
		$this->db->from('rep_tabsis');
		$this->db->where('status',0);
		$this->db->where('id_ppdb',$id);
		$data = $this->db->get()->num_rows();
		return $data;
	}
	
	
	function getJurusan($id_smk) {
		$return = '';
		$q = $this->db->query("select a.name, a.id as id_smk, b.id as id_jur, b.nama_jurusan from ref_smk a, ref_jur b where a.id = b.id_smk and a.id='".$id_smk."'");
		
		$data = $q->result_array();
		foreach ($data as $val) {
			$return .='<option value="'.$val['id_jur'].'">'.$val['nama_jurusan'].'</option>';
		} 
		return $return;
	}
	function cekKelas($id_smk){
		$return='';
		$this->db->select('*');
		$this->db->from("ref_kelas");
		$this->db->where('id_smk',$id_smk);
		$num = $this->db->get()->result_array();
		
		foreach ($num as $v_kls) {
			$return.='<option value="'.$v_kls['kelas'].'">Kelas '.$v_kls['kelas'].'</option>';
		}
								
		return $return;
	}
	function getType($id_smk){
		$return='';
		//echo "SELECT DISTINCT(no_type)  FROM ref_komponen_cicilan WHERE id_smk='".$id_smk."'";die();
		$q = $this->db->query("SELECT DISTINCT(no_type), jenis_pembayaran  FROM ref_komponen_cicilan WHERE id_smk='".$id_smk."' order by no_type asc");
		
		$num = $q->result_array();
		
			
		return $num;
	}
	function cekType($id_smk){
		$return='';
		//echo "SELECT DISTINCT(no_type)  FROM ref_komponen_cicilan WHERE id_smk='".$id_smk."'";die();
		$q = $this->db->query("SELECT DISTINCT(no_type), jenis_pembayaran  FROM ref_komponen_cicilan WHERE id_smk='".$id_smk."' order by no_type asc");
		
		$num = $q->result_array();
		
		
		
		foreach ($num as $v_kls) {
			if($v_kls['no_type']==2){
				$return.='<option value="'.$v_kls['no_type'].'" selected="selected">'.$v_kls['no_type'].' - '.$v_kls['jenis_pembayaran'].'</option>';
			}else{
				$return.='<option value="'.$v_kls['no_type'].'">'.$v_kls['no_type'].' - '.$v_kls['jenis_pembayaran'].'</option>';
			}
		}
								
		return $return;
	}
	function reject_data() {
		
		$reject = false;
		$id = $this->input->post('id_daftar');
		if (!empty($id)) {
			
			$data['approved_date']			= date("Y-m-d H:i:s");
			$data['approved_by']			= $this->session->userdata('username');
			$data['approval_status']		= 3;
			
			$this->db->where("id_daftar",$id);
			$reject = $this->db->update("ppdb_siswa",$data);		
			/*
			if ($reject) {
				$this->db->query("insert into ref_siswa(id_siswa,nama,jenis_kelamin,nisn,tempat_lahir,tanggal_lahir,nama_ayah,nama_ibu,
				alamat,telepon,tahun_ajaran,created_by,created_date,modified_by,modified_date) select 
					id_daftar,nama,jenis_kelamin,nisn,tempat_lahir,tanggal_lahir,nama_ayah,nama_ibu,
				alamat,telepon,tahun_ajaran,created_by,created_date,modified_by,modified_date from ppdb_siswa where id_daftar = ".$id."
				");
			}*/
		}
		
		return $reject;
		
	}
	
	function approve_siswa() {
			
		$approve = false;
		$id = $this->input->post('id_daftar');
		if (!empty($id)) {
			
			$data['approved_date']			= date("Y-m-d H:i:s");
			$data['approved_by']			= $this->session->userdata('username');
			$data['approval_status']		= 1;
			
			$this->db->where("id_daftar",$id);
			$approve = $this->db->update("ppdb_siswa",$data);		
			
			if ($approve) {
				$this->db->query("insert into ref_siswa(nama,jenis_kelamin,nisn,tempat_lahir,tanggal_lahir,asal_sekolah,nama_ayah,pekerjaan_ayah,nama_ibu,pekerjaan_ibu,
				alamat, id_smk,id_jur,telepon,tahun_ajaran,kelas,created_by,created_date,modified_by,modified_date, status,id_ppdb, no_type) select 
					nama,jenis_kelamin,nisn,tempat_lahir,tanggal_lahir,asal_sekolah,nama_ayah,pekerjaan_ayah,nama_ibu,pekerjaan_ibu,
				alamat,id_smk,id_jur,telepon,tahun_ajaran,kelas,created_by,created_date,modified_by,modified_date,2, ".$id.", no_type from ppdb_siswa where id_daftar = ".$id."
				");
			}
		}
		
		return $approve;
	}
	
	function delete_data() {
		
		$del = false;
		$id = $this->input->post('id_daftar');
		if (!empty($id)) {
			$this->db->where("id_daftar",$id);
			$del = $this->db->delete("ppdb_siswa");			
		}
		
		return $del;
	}
	function getDataSMK(){
		$this->db->select('*');
		$this->db->from("ref_smk");
		$num = $this->db->get()->result_array();
		return $num;
	}
	function getjumcicilan(){
		/*
		echo "SELECT sum(besarnya) FROM ref_komponen_cicilan WHERE id_smk=1 and no_type=1"; die();
		$q = $this->db->query("SELECT sum(besarnya) FROM ref_komponen_cicilan WHERE id_smk=1 and no_type=1");
		$data = $q->result_array();
		foreach ($data as $val) {
			$return =$val['besarnya'];
		} 
		*/
		return 100;	
	}
	function insert_data() {
		$idsmk=$this->input->post('smk');
		$q_cicil = $this->db->query("SELECT sum(besarnya) besar FROM ref_komponen_cicilan WHERE id_smk='".$idsmk."' and no_type='".$this->input->post('id_mapping_type')."'");
		$data_cicil = $q_cicil->result_array();
		foreach ($data_cicil as $val) {
			$jmlcicil =$val['besar'];
		} 
		
		//$t_lahir = $this->input->post('tanggal_lahir');
		//$t_lahir = date_create_from_format('d-m-Y', $t_lahir);		
		//$tanggal_lahir = date_format($t_lahir, 'Y-m-d');
		if($idsmk==2){
			$data['id_jur'] 		= 4;
		}else if($idsmk==3){
			$data['id_jur'] 		= 5;
		}else{
			$data['id_jur'] 		= 0;
		}
		//$data['id_jur'] 			= $this->input->post('id_mapping_jur');
		$data['no_type'] 			= $this->input->post('id_mapping_type');
		
		
		$data['asal_sekolah'] 			= $this->input->post('asal');
		//$data['pekerjaan_ayah'] 		= $this->input->post('ker_ayah');
		//$data['pekerjaan_ibu'] 			= $this->input->post('ker_ibu');
		
		
		$data['total_cicilan']		= $jmlcicil;
		$data['id_smk'] 			= $idsmk;
		$data['nama'] 				= $this->input->post('nama');
		$data['kelas'] 				= $this->input->post('kelas');
		//$data['tempat_lahir']	= $this->input->post('tempat_lahir');
		$data['nisn']			= $this->input->post('nisn');
		//$data['agama']			= $this->input->post('agama');
		//$data['tanggal_lahir']	= $tanggal_lahir;
		//$data['nama_ayah']		= $this->input->post('nama_ayah');
		//$data['nama_ibu']		= $this->input->post('nama_ibu');
		$data['telepon']		= $this->input->post('telepon');
		//$data['alamat']			= $this->input->post('alamat');
		//$data['jenis_kelamin']	= $this->input->post('jenis_kelamin');
		$data['tahun_ajaran']	= $this->get_tahun_ajaran();
		$data['approval_status']= $this->input->post('status');
		$data['created_date']	= date("Y-m-d H:i:s");
		$data['created_by']		= $this->session->userdata('username');
		
		//print_r($data); die();
		$q = $this->db->insert('ppdb_siswa',$data);
		
		if ($q) {
			$id = $this->db->insert_id();
			//upload image
			//$this->upload_image($this->db->insert_id());
			//$this->insertRepSpp($id, $this->input->post('id_type_spp'));
			$this->insertRepSppPSB($id);
			$this->insert_map_biaya_siswa($id,0);
			$this->insertRepTabsisPSB($id);
			$this->insert_map_cicilan($id, 0);
		}
		return $q;	
	}	
	
	function insertRepSppPSB($id) {
		
		//get tahun ajaran
		$thn = $this->get_tahun_ajaran();
		//get data siswa
		$this->db->select("id_daftar,id_smk,kelas");
		$this->db->from("ppdb_siswa");
		$this->db->where("id_daftar",$id);
		$q = $this->db->get()->row();
		
		//print_r($q); die();
		//$nis = $q->nis;
		$id_siswa = $q->id_daftar;
		$kelas = $q->kelas;
		$smk = $q->id_smk;
		
		if ($kelas == '10' && $smk=='1') {
			$kdsub = 1;
		} else if ($kelas == '11' && $smk=='1') { 
			$kdsub = 2;
		} else if ($kelas == '12' && $smk=='1') { 
			$kdsub = 3;
		} else if ($kelas == '10' && $smk=='2') { 
			$kdsub = 4;
		} else if ($kelas == '11' && $smk=='2') { 
			$kdsub = 5;
		} else if ($kelas == '12' && $smk=='2') { 
			$kdsub = 6;
		} else if ($kelas == '7' && $smk='3') { 
			$kdsub = 7;
		} else if ($kelas == '8' && $smk=='3') { 
			$kdsub = 8;
		} else if ($kelas == '9' && $smk=='3') { 
			$kdsub = 9;
		} else{
			$kdsub = 0;
		}
		/*
		$this->db->select("type_dis, besarnya");
		$this->db->from("ref_spp_type");
		$this->db->where("id", $type_spp);
		$temp_dis = $this->db->get()->row();
		$type_dis = $temp_dis->type_dis;
		$besar_dis = $temp_dis->besarnya;
		*/
		//get besarnya 
		$this->db->select("besarnya");
		$this->db->from("ref_spp");
		$this->db->where("kodesub",$kdsub);
		$temp_bes = $this->db->get()->row();
		
		//$this->db->where("tahun_ajaran",$tahun);
		if($kdsub == 0){
			$besar = 0;
		}else{
			$besar = $temp_bes->besarnya;
		}
		
		/*	
		if($type_dis == 1){
			if($kdsub == 0){
				$besar = 0;
			}else{
				$besar = $temp_bes->besarnya;
			}
		}else if($type_dis == 2){
			$besar = $besar_dis;
		}else if($type_dis == 3){
			$besar = 0;
		}else{
			if($kdsub == 0){
				$besar = 0;
			}else{
				$besar = $temp_bes->besarnya;
			}
		}
		*/
		//cek 
		$this->db->select("id");
		$this->db->from("rep_spp");
		$this->db->where(
			array(
				"tahun_ajaran" => $thn,
				//"nis"		   => $nis,
				"id_ppdb"	   => $id_siswa,
				"status"	   => 0
			)
		);
		$num = $this->db->get()->num_rows();
		
		if ($num == 0) {
			$data = array(
				"tahun_ajaran" => $thn,
				"id_ppdb"  => $id,
				"biaya"  	=> $besar,
				"status"  	=> 0
			);
			$q = $this->db->insert("rep_spp",$data);
		}else{
			$data_b['biaya']	= $besar;
			$this->db->where("id_ppdb",$id);
			$this->db->where("tahun_ajaran",$thn);
			$q = $this->db->update("rep_spp",$data_b);
		}
		 
		//return true;
		
	}
	
	
	function insertRepTabsisPSB($id) {
		
		//get tahun ajaran
		$thn = $this->get_tahun_ajaran();
		//get data siswa
		$this->db->select("id_daftar,id_smk,kelas");
		$this->db->from("ppdb_siswa");
		$this->db->where("id_daftar",$id);
		$q = $this->db->get()->row();
		
		//print_r($q); die();
		//$nis = $q->nis;
		$id_siswa = $q->id_daftar;
		$kelas = $q->kelas;
		$smk = $q->id_smk;
		
		if ($smk=='1') {
			$kdsub = 10;
		} else if ($smk=='2') { 
			$kdsub = 11;
		} else if ($smk=='3') { 
			$kdsub = 12;
		} else{
			$kdsub = 0;
		}
		//get besarnya 
		$this->db->select("besarnya");
		$this->db->from("ref_tabsis");
		$this->db->where("kodesub",$kdsub);
		$temp_bes = $this->db->get()->row();
		
		//$this->db->where("tahun_ajaran",$tahun);
		if($kdsub == 0){
			$besar = 0;
		}else{
			$besar = $temp_bes->besarnya;
		}
		
		//cek 
		$this->db->select("id");
		$this->db->from("rep_tabsis");
		$this->db->where(
			array(
				"tahun_ajaran" => $thn,
				//"nis"		   => $nis,
				"id_ppdb"	   => $id_siswa,
				"status"	   => 0
			)
		);
		$num = $this->db->get()->num_rows();
		
		if ($num == 0) {
			$data = array(
				"tahun_ajaran" => $thn,
				"id_ppdb"  => $id,
				"biaya"  	=> $besar,
				"status"  	=> 0
			);
			$q = $this->db->insert("rep_tabsis",$data);
		}else{
			$data_b['biaya']	= $besar;
			$this->db->where("id_ppdb",$id);
			$this->db->where("tahun_ajaran",$thn);
			$q = $this->db->update("rep_tabsis",$data_b);
		}
		 
		//return true;
		
	}
	
	function insert_map_biaya_siswa($id, $kelas_bef) {
		//get tahun ajaran
		$thn = $this->get_tahun_ajaran();
		//get data siswa
		$this->db->select("id_daftar, id_smk, kelas");
		$this->db->from("ppdb_siswa");
		$this->db->where("id_daftar",$id);
		$q = $this->db->get()->row();
		//$nis = $q->nis;
		$id_siswa = $q->id_daftar;
		//$id_ppdb  = $q->id_ppdb;
		$kelas = $q->kelas;
		$smk = $q->id_smk;
		
		//cek 
		$this->db->select("id");
		$this->db->from("ref_mapping_biaya_siswa");
		$this->db->where(
			array(
				"tahun_ajaran" 	=> $thn,
				"sts_dsp" 		=> 0,
				"id_ppdb"	   	=> $id_siswa	
			)
		);
		$num = $this->db->get()->num_rows();
		
		if($num<=0){
			if(($kelas>10&&$smk==1)||($kelas>10&&$smk==2)||($kelas>7&&$smk==3)){
				$this->db->select("id");
				$this->db->from("ref_list_biaya");
				$this->db->where(
					array(
						//45 id pendaftran
						"id_coa"		=> 45,
						"id_smk"	   => $smk
					)
				);
				$id_rlb = $this->db->get()->row()->id;
				
				$this->db->select("id");
				$this->db->from("ref_mapping_biaya_siswa");
				$this->db->where(
					array(
						"id_coa"		=> $id_rlb,
						"sts_dsp" 		=> 0,
						"id_ppdb"	   	=> $id	
					)
				);
				$num_rmbs = $this->db->get()->num_rows();
				if($num_rmbs<=0){
					$this->db->select("id, jenispembayaran, id_smk, besarnya, periode, id_coa, dicicil");
					$this->db->from("ref_list_biaya");
					$this->db->where("id_smk",$smk);
					$this->db->where("id_coa",45);
					$datas = $this->db->get()->result_array();
					foreach ($datas as $data) {
						$this->db->select("*");
						$this->db->from("ref_mapping_biaya_siswa");
						$this->db->where("tahun_ajaran",$thn);
						$this->db->where("status",0);
						$this->db->where("sts_dsp",0);
						$this->db->where("id_ppdb",$id_siswa);
						$this->db->where("id_coa",$data['id']);
						$q = $this->db->get()->row();
						if(empty($q)){
							$data = array(
								"tahun_ajaran" => $thn,
								"id_ppdb"  => $id_siswa,
								"dicicil"  => $data['dicicil'],
								"jenis_pembayaran"  => $data['jenispembayaran'],
								"kelas"  => $kelas,
								"id_smk"  => $data['id_smk'],
								"id_coa"  => $data['id'],
								"periode"  => $data['periode'],
								"besarnya"  => $data['besarnya'],
								"status"  => 0
							);
							$q = $this->db->insert("ref_mapping_biaya_siswa",$data);
						}
					}
				}
			}
			
			$this->db->select("id");
			$this->db->from("ref_list_biaya");
			$this->db->where(
				array(
					//45 id pendaftran
					"id_coa"		=> 45,
					"id_smk"	   => $smk
				)
			);
			$id_rlb_2 = $this->db->get()->row()->id;
				
			$this->db->select("id");
			$this->db->from("ref_mapping_biaya_siswa");
			$this->db->where(
				array(
					"id_coa"		=> $id_rlb_2,
					"id_ppdb"	   	=> $id	,
					"sts_dsp"	   	=> 0
				)
			);
			$num_rmbs_2 = $this->db->get()->num_rows();
			if($num_rmbs_2<=0){
				$this->db->select("id, jenispembayaran, id_smk, besarnya, periode, id_coa, dicicil");
				$this->db->from("ref_list_biaya");
				$this->db->where("id_smk",$smk);
				$this->db->where("kelas",$kelas);
				$datas = $this->db->get()->result_array();
			}else{
				$this->db->select("id, jenispembayaran, id_smk, besarnya, periode, id_coa, dicicil");
				$this->db->from("ref_list_biaya");
				$this->db->where("id_smk",$smk);
				$this->db->where("id_coa !=",45);
				$this->db->where("kelas",$kelas);
				$datas = $this->db->get()->result_array();
			}
			
			foreach ($datas as $data) {
				$this->db->select("*");
				$this->db->from("ref_mapping_biaya_siswa");
				$this->db->where("tahun_ajaran",$thn);
				$this->db->where("status",0);
				$this->db->where("sts_dsp",0);
				$this->db->where("id_ppdb",$id_siswa);
				$this->db->where("id_coa",$data['id']);
				$q = $this->db->get()->row();
				if(empty($q)){
					$data = array(
						"tahun_ajaran" => $thn,
						"id_ppdb"  => $id_siswa,
						"dicicil"  => $data['dicicil'],
						"jenis_pembayaran"  => $data['jenispembayaran'],
						"kelas"  => $kelas,
						"id_smk"  => $data['id_smk'],
						"id_coa"  => $data['id'],
						"periode"  => $data['periode'],
						"besarnya"  => $data['besarnya'],
						"status"  => 0
					);
					$q = $this->db->insert("ref_mapping_biaya_siswa",$data);
				}
			}
		}else{
			if($kelas!=$kelas_bef){
				$this->db->select("id");
				$this->db->from("ref_list_biaya");
				$this->db->where(
					array(
						//45 id pendaftran
						"id_coa"		=> 45,
						"id_smk"	   => $smk
					)
				);
				$id_rlb2 = $this->db->get()->row()->id;
				
				$data_bb['kelas']	= $kelas;
				$this->db->where("id_ppdb",$id_siswa);
				$this->db->where("id_coa",$id_rlb2);
				$this->db->where("sts_dsp",0);
				$q = $this->db->update("ref_mapping_biaya_siswa",$data_bb);
				
				$this->db->where('tahun_ajaran', $thn);
				$this->db->where('id_ppdb', $id_siswa);
				$this->db->where("sts_dsp",0);
				$this->db->where('id_coa !=', $id_rlb2);
				$this->db->delete('ref_mapping_biaya_siswa'); 
				
				
				
				$this->db->select("id, jenispembayaran, id_smk, besarnya, periode, id_coa, dicicil");
				$this->db->from("ref_list_biaya");
				$this->db->where("id_smk",$smk);
				$this->db->where("kelas",$kelas);
				$this->db->where("id_coa !=",45);
				$datas = $this->db->get()->result_array();
				
				
				foreach ($datas as $data) {
					$this->db->select("*");
					$this->db->from("ref_mapping_biaya_siswa");
					$this->db->where("tahun_ajaran",$thn);
					$this->db->where("status",0);
					$this->db->where("sts_dsp",0);
					$this->db->where("id_ppdb",$id_siswa);
					$this->db->where("id_coa",$data['id']);
					$q = $this->db->get()->row();
					
					if(empty($q)){
						//if(($id_ppdb!=NULL)||($id_ppdb!=0)){
							$data = array(
								"tahun_ajaran" => $thn,
								"id_ppdb"  => $id_siswa,
								//"id_ppdb"  => $id_ppdb,
								"jenis_pembayaran"  => $data['jenispembayaran'],
								"dicicil"  => $data['dicicil'],
								"kelas"  => $kelas,
								"id_smk"  => $data['id_smk'],
								"id_coa"  => $data['id'],
								"periode"  => $data['periode'],
								"besarnya"  => $data['besarnya'],
								"status"  => 0
							);
							$q = $this->db->insert("ref_mapping_biaya_siswa",$data);
							
						/*
						}else{
							$data = array(
								"tahun_ajaran" => $thn,
								"id_ppdb"  => $id_siswa,
								"jenis_pembayaran"  => $data['jenispembayaran'],
								"dicicil"  => $data['dicicil'],
								"kelas"  => $kelas,
								"id_smk"  => $data['id_smk'],
								"id_coa"  => $data['id'],
								"periode"  => $data['periode'],
								"besarnya"  => $data['besarnya'],
								"status"  => 0
							);
							$q = $this->db->insert("ref_mapping_biaya_siswa",$data);
						}
						*/
					}
					
				}
			}
		}
	}
	
	function get_last_map_cicilan_id(){
		$this->db->select("id");
		$this->db->from("ref_mapping_cicilan_siswa");
		$this->db->order_by("id", "desc");
		$this->db->limit(1);
		$return = $this->db->get()->row()->id;
		
		return $return+1;
	}
	
	function insert_map_cicilan($id, $no_type_bef) {
		//get tahun ajaran
		$thn = $this->get_tahun_ajaran();
		//tambah
		//$last_cicil_id = $this->get_last_map_cicilan_id();
		//get data siswa
		$this->db->select("id_daftar, no_type, id_smk, kelas");
		$this->db->from("ppdb_siswa");
		$this->db->where("id_daftar",$id);
		$q = $this->db->get()->row();
		//$nis = $q->nis;
		$id_siswa = $q->id_daftar;
		$kelas = $q->kelas;
		$smk = $q->id_smk;
		$no_type = $q->no_type;
		if($no_type_bef==0){
			$this->db->select("id, jenis_pembayaran, id_smk, besarnya");
			$this->db->from("ref_komponen_cicilan");
			$this->db->where("id_smk",$smk);
			//$this->db->where("tahun_ajaran",$thn);
			$this->db->where("no_type",$no_type);
			$datas = $this->db->get()->result_array();
			
			foreach ($datas as $data) {
				$id_ref_list_biaya = $data['id'];
				$this->db->select("*");
				//$this->db->from("ref_mapping_cicilan_siswa");
				$this->db->from("ref_mapping_biaya_siswa");
				//$this->db->where("tahun_ajaran",$thn);
				$this->db->where("sts_dsp",1);
				$this->db->where("id_ppdb",$id_siswa);
				$this->db->where("id_coa",$id_ref_list_biaya);
				$q = $this->db->get()->row();
				if(empty($q)){
					$data = array(
					//tambah
						//"id" => $last_cicil_id,
						//"id_id" => $last_cicil_id.'cc',
						"tahun_ajaran" => $thn,
						"id_ppdb"  => $id_siswa,
						"kelas"  => $kelas,
						"id_smk"  => $data['id_smk'],
						"id_coa"  => $id_ref_list_biaya,
						"jenis_pembayaran" =>  $data['jenis_pembayaran'],
						"besarnya"  => $data['besarnya'],
						"status"  => 0,
						"sts_dsp"  => 1
					);
					//$q = $this->db->insert("ref_mapping_cicilan_siswa",$data);
					$q = $this->db->insert("ref_mapping_biaya_siswa",$data);
				}
			}
		}else if($no_type!=$no_type_bef){
			$this->db->where('id_ppdb', $id_siswa);
			$this->db->where('sts_dsp', 1);
			//$this->db->delete('ref_mapping_cicilan_siswa'); 
			$this->db->delete('ref_mapping_biaya_siswa'); 
			
			$this->db->select("id, jenis_pembayaran, id_smk, besarnya");
			$this->db->from("ref_komponen_cicilan");
			$this->db->where("id_smk",$smk);
			//$this->db->where("tahun_ajaran",$thn);
			$this->db->where("no_type",$no_type);
			$datas = $this->db->get()->result_array();
			
			foreach ($datas as $data) {
				$id_ref_list_biaya = $data['id'];
				$this->db->select("*");
				$this->db->from("ref_mapping_biaya_siswa");
				//$this->db->from("ref_mapping_cicilan_siswa");
				//$this->db->where("tahun_ajaran",$thn);
				//$this->db->where("status",0);
				$this->db->where("sts_dsp",1);
				$this->db->where("id_ppdb",$id_siswa);
				$this->db->where("id_coa",$id_ref_list_biaya);
				$q = $this->db->get()->row();
				if(empty($q)){
					$data = array(
						//tambah
						//"id" => $last_cicil_id,
						//"id_id" => $last_cicil_id.'cc',
						"tahun_ajaran" => $thn,
						"id_ppdb"  => $id_siswa,
						"kelas"  => $kelas,
						"id_smk"  => $data['id_smk'],
						"id_coa"  => $id_ref_list_biaya,
						"jenis_pembayaran" =>  $data['jenis_pembayaran'],
						"besarnya"  => $data['besarnya'],
						"status"  => 0,
						"sts_dsp"  => 1
					);
					$q = $this->db->insert("ref_mapping_biaya_siswa",$data);
					//$q = $this->db->insert("ref_mapping_cicilan_siswa",$data);
					
					$temp_id = $this->db->insert_id();
					$data_b['id_list_biaya']		= $temp_id;
					$data_b['id_ref_list_biaya']	= $id_ref_list_biaya;
					$this->db->where("id_siswa",$id_siswa);
					$this->db->where("cicilan",1);
					$this->db->where("sts_ppdb",1);
					$q = $this->db->update("trans_siswa",$data_b);
				}
			}
		
			
		}
		
	}
	
	
	function update_data() {
		
		$nr = $this->input->post('nr');
		$nr_spp = $this->input->post('nr_spp');
		$nr_tabsis = $this->input->post('nr_tabsis');
		
		
		$id = $this->input->post('id_daftar');
		for($i=1;$i<=$nr;$i++){
			$data['besarnya']	= $this->input->post('biaya_komp_'.$i.'');
			
			$this->db->where("id", $this->input->post('id_komp_'.$i.''));
			$this->db->update("ref_mapping_biaya_siswa",$data);
		}
		
		for($i=1;$i<=$nr_spp;$i++){
		    
			$data_spp['biaya']	= $this->input->post('biaya_spp_'.$i.'');
			
			$this->db->where("id", $this->input->post('id_spp_'.$i.''));
			$this->db->update("rep_spp",$data_spp);
		}
		
		for($i=1;$i<=$nr_tabsis;$i++){
			$data_tabsis['biaya']	= $this->input->post('biaya_tabsis_'.$i.'');
			
			$this->db->where("id", $this->input->post('id_tabsis_'.$i.''));
			$this->db->update("rep_tabsis",$data_tabsis);
		}
		return true;
		
	}
	
	function upload_image($id_daftar) {
		
		if (isset($_FILES['myfile'])) {
			$this->load->library('uploaderfiler');
			$data = $this->uploaderfiler->upload($_FILES['myfile'], $this->uploaderfiler->getConfigAdmin(DIR_SISWA));
			// get success upload
			if ($data['isComplete']) {
				$files = $data['data'];
				// loop upload image data, temp image
				$imagesMeta = array();
				$i = 1;
				foreach ($files['metas'] as $key => $value) {
					//array_push($imagesMeta, array('id_siswa' => $id_daftar, 'seq_number' => $i, 'image_name' => $value['name'], 'size' => $value['size']));
					$insert_image = array (
						'id_siswa' => $id_daftar, 
						'seq_number' => $i, 
						'image_name' => $value['name'], 
						'size' => $value['size']
					);
						
					$this->db->insert('image_siswa',$insert_image);
					$i++;
				}
				
				//print_r($imagesMeta); die();
				//$this->db->insert_batch('image_siswa', $imagesMeta);
			}
			if ($data['hasErrors']) {
				$errors = $data['errors'];
				print_r($errors);
			}
		}
		
	}
	
	function get_fancy() {
		
		$param = $this->input->post(array('id_daftar'));
        $sql = 'select seq_number, image_name, size from image_siswa where id_siswa =?';
        $result_array = $this->db->query($sql, array($param['id_daftar']));
        $response['images'] = $result_array->result();
        return $response;
		
		/*
		$id_siswa = $this->input->post('id_daftar');
		$this->db->select("*");
		$this->db->from("image_siswa");
		$this->db->where("id_siswa",$id_siswa);
		$data = $this->db->get()->result_array();
		return $data;*/
		
	}
	
	function get_tahun_ajaran() {
	
	
		$this->db->select("tahun_ajaran");
		$this->db->from("ref_tahun_ajaran");
		$this->db->where("status","U");
		$this->db->where("sts_ppdb",1);
		$return = $this->db->get()->row()->tahun_ajaran;
		
		return $return;
	}
	
	
	function get_list_data() {
		$thn = $this->get_tahun_ajaran();
		$param = $this->input->post(array('page', 'rows', 'sort', 'order', 'filterRules'));
        $offset = intval(($param['page'] - 1) * $param['rows']);
		
		// clause filter, array not null
        $cond = '';
        if (count($param['filterRules']) > 0) {
            $filter = json_decode($param['filterRules']);
            $loop = 0;
            foreach ($filter as $json) {
                // convert to array
                $rule = get_object_vars($json);
                // declare variable
				if($rule['field']=='smk'){
					$field = 'b.name';
				}else{
					//declare variable
                	$field = $rule['field'];
				}
				
				
                $opt = $rule['op'];
                $value = $rule['value'];
                if ($loop == 0) {
                    // user where
                    if (!empty($value)) {
                        if ($opt == 'contains') {
                            $cond .= "where ($field like '%$value%')";
                        } else if ($opt == 'greater') {
                            $cond .= "where $field > '$value'";
                        } else if ($opt == 'less') {
                            $cond .= "where $field < '$value'";
                        } else if ($opt == 'notequal') {
                            $cond .= "where $field != '$value'";
                        } else if ($opt == 'equal') {
                            $cond .= "where $field = '$value'";
                        }
                        $loop++; // flag where
                    }
                } else {
                    // user and
                    if (!empty($value)) {
                        if ($opt == 'contains') {
                            $cond .= " and ($field like '%$value%')";
                        } else if ($opt == 'greater') {
                            $cond .= " and $field > '$value'";
                        } else if ($opt == 'less') {
                            $cond .= " and $field < '$value'";
                        } else if ($opt == 'notequal') {
                            $cond .= " and $field != '$value'";
                        } else if ($opt == 'equal') {
                            $cond .= " and $field = '$value'";
                        }
                    }
                }
            }
        }
		
		$response = array();
		 $table = 'ppdb_siswa'; 
		 
        if (empty($param['sort'])) {
            if (empty($cond)) {
                $sql = "select a.*, b.name as smk from $table a, ref_smk b where a.id_smk=b.id and a.approval_status = 0 and a.tahun_ajaran='".$thn."' order by b.id desc, a.id_daftar desc limit ?, ?";
                $sqlcount = "select a.* from $table a, ref_smk b where a.id_smk=b.id and a.approval_status = 0 and a.tahun_ajaran='".$thn."'";
            } else {
                $sql = "select a.*, b.name as smk from $table a, ref_smk b $cond and a.id_smk=b.id and a.approval_status = 0 and a.tahun_ajaran='".$thn."' order by b.id desc, a.id_daftar desc limit ?, ?";
                $sqlcount = "select a.* from $table a, ref_smk b $cond and a.id_smk=b.id and a.approval_status = 0 and a.tahun_ajaran='".$thn."'";
            }
            $result_array = $this->db->query($sql, array($offset, intval($param['rows'])));
            $response['total'] = $this->db->query($sqlcount)->num_rows();
            $response['rows'] = $result_array->result();
        } else {
            $sort = $param['sort'];
            $order = $param['order'];
            if (empty($cond)) {
                $sql = "select a.*, b.name as smk from $table a, ref_smk b where a.id_smk=b.id and a.approval_status = 0 and a.tahun_ajaran='".$thn."' order by $sort $order limit ?, ?";
                $sqlcount = "select a.* from $table a, ref_smk b where  a.id_smk=b.id and a.approval_status = 0 and a.tahun_ajaran='".$thn."'";
            } else {
                $sql = "select a.*, b.name as smk from $table a, ref_smk b $cond  and a.id_smk=b.id and a.approval_status = 0 and a.tahun_ajaran='".$thn."' order by $sort $order limit ?, ?";
                $sqlcount = "select a.* from $table a, ref_smk b  $cond  and a.id_smk=b.id and a.approval_status = 0 and a.tahun_ajaran='".$thn."'";
            }
            $result_array = $this->db->query($sql, array($offset, intval($param['rows'])));
            $response['total'] = $this->db->query($sqlcount)->num_rows();
            $response['rows'] = $result_array->result();
        }
        return $response;
	}
	
	function getKelas($id_smk){
		$this->db->select('*');
		$this->db->from("ref_kelas");
		$this->db->where("id_smk",$id_smk);
		$num = $this->db->get()->result_array();
		return $num;
	}
	
}