<?php
ob_start();
class Trans_spp_model extends CI_Model {
	
	function getDiserahkan($faktur) {
		$q=$this->db->query("
							select nama 
							from ref_siswa 
							where id_siswa = (select distinct id_siswa from trans_spp where no_faktur = '".$faktur."') 
						");
		$nama = $q->row()->nama;
		return $nama;
	}
	
	function getDiterima($faktur) {
		$q = $this->db->query("
			select distinct diterima from trans_uang_masuk
			    where no='".$faktur."'
		");
		$diterima = $q->row()->diterima;
		return $diterima;
	}
	
	function getTotal($faktur) {

		$q=$this->db->query("select sum(a.besarnya) as total from (select besarnya from trans_spp where no_faktur='".$faktur."'
			UNION ALL
			select besarnya from trans_tabsis where no_faktur='".$faktur."') a
		");
		$total = $q->row()->total;
		if(empty($total)||$total==NULL){
			$total=0;
		}else{
			$total=$total;
		}
		return $total;
	
	
	}
	
	function getDetailFaktur($faktur) {
		$q=$this->db->query("select bulan,besarnya,tahun_ajaran, 'SPP Bulan' as det from trans_spp where no_faktur='".$faktur."'
			UNION ALL
			select bulan,besarnya, tahun_ajaran, 'Tabsis Bulan' as det from trans_tabsis where no_faktur='".$faktur."'
		");
		
		return $q->result_array();;
	}
	
	function getThn($id){
		$return = '';
		$this->db->select("tahun_ajaran");
		$this->db->from("rep_spp");
		$this->db->where("id_siswa",$id);
		$this->db->order_by("tahun_ajaran", "asc");
		$thn = $this->db->get()->result_array();
		foreach ($thn as $val) {
			$return .='<option value="'.$val['tahun_ajaran'].'">'.$val['tahun_ajaran'].'</option>';
		} 
		return $return;
	}
	function getBulan($id,$thn) {
		
		$tahun_ajaran = $this->get_tahun_ajaran();
		$vbln = '';
		//cek num rows
		$this->db->select("id_siswa");
		$this->db->from("rep_spp");
		$this->db->where("id_siswa",$id);
		$this->db->where("tahun_ajaran",$thn);
		
		$count = $this->db->get()->num_rows();
		
		//cek num rows
		$this->db->select("id_siswa, tahun_ajaran");
		$this->db->from("rep_spp");
		$this->db->where("id_siswa",$id);
		$this->db->where("tahun_ajaran",$thn);
		$this->db->order_by("tahun_ajaran", "asc");
		
		//$count = $this->db->get()->num_rows();
		$data = $this->db->get()->result_array();
		$arr_bulan = array (7,8,9,10,11,12,1,2,3,4,5,6);
		if ($count > 0) {
		foreach($data as $val_siswa) {
			$thn_ajaran = $val_siswa['tahun_ajaran'];
			for($m = 0;$m < 12; $m++){
			
				$month = date('F', mktime(0,0,0,$arr_bulan[$m], 1, date('Y'))); 
				$this->db->select($month);
				$this->db->from("rep_spp");
				$this->db->where(
					array(
						'id_siswa' 		=> $id,
						'tahun_ajaran'	=> $thn_ajaran
					)
				);
				
				$value = $this->db->get()->row()->$month;
				if ($value == "") {
					//$vbln .= '<option value="'.$month.'-'.$tahun_ajaran.'">'.$month.' ( '.$thn_ajaran.' )</option>';
					$vbln .= '<option value="'.$month.'">'.$month.' ( '.$thn_ajaran.' )</option>';
				}				
			} 
		}	
		}
			
		return $vbln;
	}
	
	/*
	function getBulan($id) {
		
		$tahun_ajaran = $this->get_tahun_ajaran();
		$vbln = '';
		
		//cek num rows
		$this->db->select("id_siswa");
		$this->db->from("rep_spp");
		$this->db->where(
			array(
					'id_siswa' 		=> $id,
					'tahun_ajaran'	=> $tahun_ajaran
				)
		);
		
		$count = $this->db->get()->num_rows();
		
		if ($count > 0) {
			for($m = 1;$m <= 12; $m++){
			
				$month = date('F', mktime(0,0,0,$m, 1, date('Y'))); 
				$this->db->select($month);
				$this->db->from("rep_spp");
				$this->db->where(
					array(
						'id_siswa' 		=> $id,
						'tahun_ajaran'	=> $tahun_ajaran
					)
				);
				
				$value = $this->db->get()->row()->$month;
				if ($value == "") {
					$vbln .= '<option value="'.$month.'">'.$month.' ( '.$tahun_ajaran.' )</option>';
				}				
			} 
		}	
			
		return $vbln;
	}
	*/
	function getKode($id_siswa) {
		
		//get kelas
		$this->db->select("kelas, id_smk, tahun_ajaran");
		$this->db->from("ref_siswa");
		$this->db->where("id_siswa",$id_siswa);
		$data = $this->db->get();
		if ($data->num_rows()) {
			$kelas = $data->row()->kelas;
			$smk = $data->row()->id_smk;
			$tahun = $data->row()->tahun_ajaran;
			
			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;
			}
			
			//get besarnya 
			$this->db->select("kodesub,kodedesk");
			$this->db->from("ref_spp");
			$this->db->where("kodesub",$kdsub);
			//$this->db->where("tahun_ajaran",$tahun);
			
			$data = $this->db->get()->row();
			//echo "TEST: ".$data->kodesub;die();
		} else {
			$data = 0;
		}
		
		return $data;
	}
	
	function getKodeTabsis($id_siswa) {
		
		//get kelas
		$this->db->select("kelas, id_smk, tahun_ajaran");
		$this->db->from("ref_siswa");
		$this->db->where("id_siswa",$id_siswa);
		$data = $this->db->get();
		if ($data->num_rows()) {
			$kelas = $data->row()->kelas;
			$smk = $data->row()->id_smk;
			$tahun = $data->row()->tahun_ajaran;
			
			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("kodesub,kodedesk");
			$this->db->from("ref_tabsis");
			$this->db->where("kodesub",$kdsub);
			//$this->db->where("tahun_ajaran",$tahun);
			
			$data = $this->db->get()->row();
			//echo "TEST: ".$data->kodesub;die();
		} else {
			$data = 0;
		}
		
		return $data;
	}
	
	
	function getBesarnya($id_siswa, $thn) {
		
		//get kelas
		$this->db->select("biaya, tahun_ajaran");
		$this->db->from("rep_spp");
		$this->db->where("id_siswa",$id_siswa);
		$this->db->where("tahun_ajaran",$thn);
		$data = $this->db->get();
		
		$this->db->select("biaya, tahun_ajaran");
		$this->db->from("rep_spp");
		$this->db->where("id_siswa",$id_siswa);
		$this->db->where("tahun_ajaran",$thn);
		$this->db->order_by("tahun_ajaran", "asc");
		$dataSis = $this->db->get()->result_array();
		
		$besar='';
		
		if ($data->num_rows()) {
			foreach($dataSis as $datas) {
				//$besar=$besar.'('.$datas['tahun_ajaran'].': Rp '.number_format($datas['biaya'],2,",",".").') ';
				$besar=$datas['biaya'];
			}
		} else {
			$besar = 0;
		}
		 
		
		return $besar;
	}
	
	
	function get_last_seq() {
		
		//compare date
		$now = date("Y-m-d");		
		$this->db->select('last_date');
		$this->db->from('ref_sequence_trans');
		$this->db->where('nama_transaksi','uang_masuk');
		$last_date = $this->db->get()->row()->last_date;
		if ($now != $last_date) {
			$update['last_seq'] = 1;
			$update['last_date'] = $now;
			
			$this->db->where('nama_transaksi','uang_masuk');
			$this->db->update('ref_sequence_trans',$update);
		}
		
		//get sequnce
		$this->db->select('last_seq');
		$this->db->from('ref_sequence_trans');
		$this->db->where('nama_transaksi','uang_masuk');
		$data = $this->db->get()->row()->last_seq;
		
		
		
		return $data;
	}
	
	function get_kd_detail() {
		$this->db->select("kodesub,daftarlist");
		$this->db->from("ref_list_uang_masuk");
		$data = $this->db->get()->result_array();
		return $data;
	}
	
	function get_value_edit($no_trans) {
		$this->db->distinct();
		$this->db->select('no,tanggaltransaksi,diserahkan,diterima');
		$this->db->from('trans_uang_masuk');
		$this->db->where('no',$no_trans);
		$data = $this->db->get()->row();
		//echo $this->db->last_query(); die();
		return $data;
	}
	
	function get_trans_detail($no_trans) {
		$this->db->select('kodedesk,kodesub,besarnya');
		$this->db->from('trans_uang_masuk');
		$this->db->where('no',$no_trans);
		$data = $this->db->get()->result_array();
		//echo $this->db->last_query(); die();
		return $data;
	}
	
	function get_kodedesk() {
		$this->db->select("kodedesk,deskripsi");
		$this->db->from("ref_uang_masuk");
		$data = $this->db->get()->result_array();
		return $data;
	}
	
	function get_list_data() {
	
		$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
                
				// declare variable
				if($rule['field']=='nis'){
					$field = 'a.nis';
				}else if($rule['field']=='created_by'){
					$field = 'a.created_by';
				}else if($rule['field']=='jenis'){
					$field = 'c.jenis';
				}else{
                	$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 = 'trans_spp'; 
		 
        if (empty($param['sort'])) {
            if (empty($cond)) {
                $sql = "select a.*, b.nama, c.jenis from $table a, ref_siswa b, ref_jenis_bayar c where a.id_siswa=b.id_siswa and a.sts_ppdb=0 and a.id_jenis_bayar = c.id_jenis_bayar order by id limit ?, ?";
                $sqlcount = "select id from $table a, ref_siswa b where a.id_siswa=b.id_siswa and a.sts_ppdb=0";
            } else {
                $sql = "select a.*, b.nama, c.jenis from $table a, ref_siswa b, ref_jenis_bayar c $cond and a.id_siswa=b.id_siswa and a.sts_ppdb=0 and a.id_jenis_bayar = c.id_jenis_bayar order by id limit ?, ?";
                $sqlcount = "select id from $table a, ref_siswa b, ref_jenis_bayar c  $cond and a.id_siswa=b.id_siswa and a.sts_ppdb=0 and a.id_jenis_bayar = c.id_jenis_bayar";
            }
            $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.nama, c.jenis from $table a, ref_siswa b, ref_jenis_bayar c where a.id_siswa=b.id_siswa and a.sts_ppdb=0 and a.id_jenis_bayar = c.id_jenis_bayar order by $sort $order limit ?, ?";
                $sqlcount = "select id from $table a, ref_siswa b, ref_jenis_bayar c where a.id_siswa=b.id_siswa and a.sts_ppdb=0 and a.id_jenis_bayar = c.id_jenis_bayar";
            } else {
                $sql = "select a.*, b.nama, c.jenis from $table a, ref_siswa b, ref_jenis_bayar c $cond and a.id_siswa=b.id_siswa and a.sts_ppdb=0 and a.id_jenis_bayar = c.id_jenis_bayar order by $sort $order limit ?, ?";
                $sqlcount = "select id from $table a, ref_siswa b, ref_jenis_bayar c $cond and a.id_siswa=b.id_siswa and a.sts_ppdb=0 and a.id_jenis_bayar = c.id_jenis_bayar";
            }
            $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 update_data() {
		
		$trans_id = $this->input->post('no');	
		$this->db->where('no',$trans_id);
		$del = $this->db->delete('trans_uang_masuk');
		
		if ($del) {
			$t_trans = $this->input->post('tanggaltransaksi');
			$t_trans = date_create_from_format('d-m-Y', $t_trans);		
			$tanggaltransaksi = date_format($t_trans, 'Y-m-d');
			
			
			$last_id = $this->input->post('last_id');
			
			$kodedesk = array();
			$kodesub = array();
			$besarnya = array();
			
			
			$kodedesk = $this->input->post('kodedesk');
			$kodesub = $this->input->post('kodesub');
			$besarnya = $this->input->post('besarnya');
			$count = count($kodedesk);
			
			
			for ($i=0; $i<$count; $i++) {
				
				$data['no']		= $this->input->post('no');
				$data['tanggaltransaksi']	= $tanggaltransaksi;
				$data['tanggalinput']	= date('Y-m-d');
				$data['diserahkan']	= $this->input->post('diserahkan');
				$data['diterima']		= $this->input->post('diterima');
				$data['tahunajaran']	= $this->get_tahun_ajaran();
				$data['created_date']	= date("Y-m-d H:i:s");
				$data['created_by']	= $this->session->userdata('username');
			
				
				$data['kodedesk'] = $kodedesk[$i]; 
				$data['kodesub'] = $kodesub[$i]; 
				$data['besarnya'] = $besarnya[$i];
				
				$q = $this->db->insert("trans_uang_masuk",$data);
			}
		}
		
		return true;	
		
	}*/
	
	function getIdWaliKelas($id_siswa) {
		$this->db->select("id_wali_kelas");
		$this->db->from("ref_siswa");
		$this->db->where("id_siswa",$id_siswa);
		$idWali = $this->db->get()->row()->id_wali_kelas;
		return $idWali; 
	}
	
	function next_faktur(){
		$last_id = $this->get_last_seq();
					
		$last_id = $last_id + 1;
		$length = strlen($last_id);
					
		if ($length == 1) {
			$num = '0000';
		} else if ($length == 2){
			$num = '000';
		} else if ($length == 3) {
			$num = '00';
		}else if ($length == 4) {
			$num = '0';		
		} else {
			$num = '';
		}	
		//print_r(date('Ymd').$num.$last_id);die();
		return 'FM'.date('Ymd').$num.$last_id;
	}
	
	function insert_data($id_fak) {
		/*
		$t_trans 		= $this->input->post('tanggal_transaksi');
		$t_trans 		= date_create_from_format('d-m-Y', $t_trans);		
		$tgl_trans 		= date_format($t_trans, 'Y-m-d');
		*/
		$last_id 		= $this->get_last_seq();
		$last_id 		= $last_id + 1;
		$thn 			= $this->input->post('thn');
		//$last_id 		= $this->input->post('last_id');
		$id_siswa		= $this->input->post('id_siswa');
		$bulan 			= array();		
		$bulan 			= $this->input->post('bulan');
		$kodedesk 		= $this->input->post('kodedesk');
		$kodesub 		= $this->input->post('kodesub');
		
		$kodedesk_tabsis 		= $this->input->post('kodedesk_tabsis');
		$kodesub_tabsis 		= $this->input->post('kodesub_tabsis');
	
		$besarnya 		= $this->input->post('besarnya');
		$besarnya_spp 		= $this->input->post('besarnyaspp');
		$besarnya_tabsis 		= $this->input->post('besarnyatabsis');
		$metode_bayar = $this->input->post('metode_bayar');
		$count 			= count($bulan);
		//print_r($bulan); die();
		//get nis
		$this->db->select("nis");
		$this->db->from("ref_siswa");
		$this->db->where("id_siswa",$id_siswa);		
		$nis = $this->db->get()->row()->nis;
		//$tahun_ajaran 	= $this->get_tahun_ajaran();
		$tahun_ajaran 	= $thn;
		$now 			= date('Y-m-d');
		$idWaliKelas = $this->getIdWaliKelas($id_siswa);
		$besarSpp = 0;
		$besarTabsis = 0;
		for ($i=0; $i<$count; $i++) {
			//SPP
			if($besarnya_spp>0){
				$bl 						= $bulan[$i];
				$data['no_faktur']			= $id_fak;
				$data['tanggal_transaksi']	= date("Y-m-d");
				$data['tanggal_input']		= date('Y-m-d');
				$data['tahun_ajaran']		= $tahun_ajaran;
				$data['created_date']		= date("Y-m-d H:i:s");
				$data['created_by']			= $this->session->userdata('username');		
				$data['bulan'] 				= $bl;			
				$data['besarnya'] 			= $besarnya_spp;
				$data['kodedesk'] 			= $kodedesk;
				$data['kodesub'] 			= $kodesub;
				$data['nis'] 				= $nis;
				$data['id_siswa'] 			= $id_siswa;
				$data['id_wali_kelas']		= $idWaliKelas;
				$data['sts_ppdb'] 			= 0;
				$data['id_jenis_bayar']		= $metode_bayar;
				$q = $this->db->insert("trans_spp",$data);
				if ($q) {
					$this->db->where(
								array(
									'tahun_ajaran'	=> $tahun_ajaran,
									'id_siswa'		=> $id_siswa,
									'status'		=> 1
								)
							);
					//$this->db->set($bl,$now,false);
					$update = array(
						$bl	=> date("Y-m-d")
					);
					$q = $this->db->update("rep_spp",$update);	
				}
				
				$besarSpp = $besarSpp + $besarnya_spp; 
			}
			
			//TABSIS
			if($besarnya_tabsis>0){
				//SPP
				$bl 						= $bulan[$i];
				$data['no_faktur']			= $id_fak;
				$data['tanggal_transaksi']	= date("Y-m-d");
				$data['tanggal_input']		= date('Y-m-d');
				$data['tahun_ajaran']		= $tahun_ajaran;
				$data['created_date']		= date("Y-m-d H:i:s");
				$data['created_by']			= $this->session->userdata('username');		
				$data['bulan'] 				= $bl;			
				$data['besarnya'] 			= $besarnya_tabsis;
				$data['kodedesk'] 			= $kodedesk_tabsis;
				$data['kodesub'] 			= $kodesub_tabsis;
				$data['nis'] 				= $nis;
				$data['id_siswa'] 			= $id_siswa;
				$data['id_wali_kelas']		= $idWaliKelas;
				$data['sts_ppdb'] 			= 0;
				$data['id_jenis_bayar']		= $metode_bayar;
				$q = $this->db->insert("trans_tabsis",$data);
				if ($q) {
					$this->db->where(
								array(
									'tahun_ajaran'	=> $tahun_ajaran,
									'id_siswa'		=> $id_siswa,
									'status'		=> 1
								)
							);
					//$this->db->set($bl,$now,false);
					$update = array(
						$bl	=> date("Y-m-d")
					);
					$q = $this->db->update("rep_tabsis",$update);	
				}
				
				$besarTabsis = $besarTabsis + $besarnya_tabsis; 
			}
		}
		if($besarSpp>0){
			//insert trans uang masuk
			$diserahkan 					= $this->getDiserahkan($id_fak);
			$data_masuk['diserahkan'] 		= $diserahkan;
			$data_masuk['diterima'] 		= $this->session->userdata('username');
			$data_masuk['kodedesk'] 		= $kodedesk;
			$data_masuk['kodesub'] 			= $kodesub;
			$data_masuk['tanggaltransaksi'] = date("Y-m-d");
			$data_masuk['besarnya'] 		= $besarSpp;
			$data_masuk['tanggalinput'] 	= date("Y-m-d");
			$data_masuk['tahunajaran'] 		= $tahun_ajaran;
			$data_masuk['no'] 				= $id_fak;
			$data_masuk['created_date']		= date("Y-m-d H:i:s");
			$data_masuk['created_by']		= $this->session->userdata('username');		
			$this->db->insert("trans_uang_masuk",$data_masuk);
			
			//update last_sequence
			$update_sq['last_seq'] = $last_id;
			$this->db->where("nama_transaksi","uang_masuk");
			$this->db->update("ref_sequence_trans",$update_sq);	
			
			//$q = $this->db->insert("trans_uang_masuk",$data);
		}
		
		if($besarTabsis>0){
			//insert trans uang masuk
			$diserahkan 					= $this->getDiserahkan($id_fak);
			$data_masuk['diserahkan'] 		= $diserahkan;
			$data_masuk['diterima'] 		= $this->session->userdata('username');
			$data_masuk['kodedesk'] 		= $kodedesk_tabsis;
			$data_masuk['kodesub'] 			= $kodesub_tabsis;
			$data_masuk['tanggaltransaksi'] = date("Y-m-d");
			$data_masuk['besarnya'] 		= $besarTabsis;
			$data_masuk['tanggalinput'] 	= date("Y-m-d");
			$data_masuk['tahunajaran'] 		= $tahun_ajaran;
			$data_masuk['no'] 				= $id_fak;
			$data_masuk['created_date']		= date("Y-m-d H:i:s");
			$data_masuk['created_by']		= $this->session->userdata('username');		
			$this->db->insert("trans_uang_masuk",$data_masuk);
			
			//update last_sequence
			$update_sq['last_seq'] = $last_id;
			$this->db->where("nama_transaksi","uang_masuk");
			$this->db->update("ref_sequence_trans",$update_sq);	
			
			//$q = $this->db->insert("trans_uang_masuk",$data);
		}
		return true;	
		
		
	}
	
	function getBesarnyaTabsis($id_siswa, $thn) {
		
		//get kelas
		$this->db->select("biaya, tahun_ajaran");
		$this->db->from("rep_tabsis");
		$this->db->where("id_siswa",$id_siswa);
		$this->db->where("tahun_ajaran",$thn);
		$data = $this->db->get();
		
		$this->db->select("biaya, tahun_ajaran");
		$this->db->from("rep_tabsis");
		$this->db->where("id_siswa",$id_siswa);
		$this->db->where("tahun_ajaran",$thn);
		$this->db->order_by("tahun_ajaran", "asc");
		$dataSis = $this->db->get()->result_array();
		
		$besar='';
		
		if ($data->num_rows()) {
			foreach($dataSis as $datas) {
				//$besar=$besar.'('.$datas['tahun_ajaran'].': Rp '.number_format($datas['biaya'],2,",",".").') ';
				$besar=$datas['biaya'];
			}
		} else {
			$besar = 0;
		}
		 
		
		return $besar;
	}
	
	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",0);
		$return = $this->db->get()->row()->tahun_ajaran;
		
		return $return;
	}
	
	function getSiswa($tahunAjaran) {
		$this->db->select("id_siswa,nama");
		$this->db->from("ref_siswa");
		$this->db->where("tahun_ajaran",$tahunAjaran);
		$data = $this->db->get()->result_array();
		/*echo $this->db->last_query()."<br>";
		print_r($data); die();*/
		return $data;
	}
	
	function updateStatusFaktur($faktur) {
		
		$data['print_status'] = 'CETAK';
		$this->db->where("no_faktur",$faktur);
		$q = $this->db->update("trans_spp",$data);
		return $q;
		
	}
	function getDataSMK(){
		$this->db->select('*');
		$this->db->from("ref_smk");
		$num = $this->db->get()->result_array();
		return $num;
	}
	function getNama($id_smk) {
		$return = '';
		$q = $this->db->query("SELECT * FROM ref_siswa WHERE id_smk='".$id_smk."' and status='1'");
		$data = $q->result_array();
		foreach ($data as $val) {
			$return .='<option value="'.$val['id_siswa'].'">'.$val['nama'].' ( '.$val['nis'].' )'.'</option>';
		} 
		return $return;
	}
	
	
	//faktur
	
	function getSiswaFaktur($faktur){
		$this->db->select("*");
		$this->db->from("trans_spp");
		$this->db->where("no_faktur",$faktur);
		$this->db->group_by("no_faktur");
		$q = $this->db->get()->row();
		return $q;
	}
	
	function getDetSiswaFaktur($id_siswa, $sts_ppdb){
		if($sts_ppdb==0){
			$this->db->select("nis as id, kelas,id_smk");
			$this->db->from("ref_siswa");
			$this->db->where("id_siswa",$id_siswa);
		}else{
			$this->db->select("nisn as id, kelas,id_smk");
			$this->db->from("ppdb_siswa");
			$this->db->where("id_daftar",$id_siswa);
		}
		$q = $this->db->get()->row();
		return $q;
	}
	function getSMKFaktur($id){
		$this->db->select("name");
		$this->db->from("ref_smk");
		$this->db->where("id",$id);
		$q = $this->db->get()->row()->name;
		return $q;
	}
	
	function delete_data() {
		
		$del = false;
		
		$faktur = $this->input->post('faktur');
		
		
		
		if (!empty($faktur)) {
			$this->db->select('id_siswa, tahun_ajaran, bulan');
			$this->db->from("trans_spp");
			$this->db->where("no_faktur",$faktur);
			$this->db->where("sts_ppdb",0);
			$num = $this->db->get()->result_array();
			foreach ($num as $v_num) {
				$this->db->set($v_num['bulan'], NULL);
				$this->db->where("tahun_ajaran", $v_num['tahun_ajaran']);
				$this->db->where("id_siswa", $v_num['id_siswa']);
				$this->db->where("status", 1);
				$this->db->update('rep_spp');
			}
			
			$this->db->select('id_siswa, tahun_ajaran, bulan');
			$this->db->from("trans_tabsis");
			$this->db->where("no_faktur",$faktur);
			$this->db->where("sts_ppdb",0);
			$num = $this->db->get()->result_array();
			foreach ($num as $v_num) {
				$this->db->set($v_num['bulan'], NULL);
				$this->db->where("tahun_ajaran", $v_num['tahun_ajaran']);
				$this->db->where("id_siswa", $v_num['id_siswa']);
				$this->db->where("status", 1);
				$this->db->update('rep_tabsis');
			}
			
			$this->db->where("no_faktur",$faktur);
			$this->db->delete("trans_spp");
			
			$this->db->where("no_faktur",$faktur);
			$this->db->delete("trans_tabsis");
			
			$this->db->where("no",$faktur);
			$del = $this->db->delete("trans_uang_masuk");
		}
		
		return $del;
	}
	
	function getMetodePembayaran() {
		
		$tahunAjaran = $this->get_tahun_ajaran();
		
		$this->db->select("id_jenis_bayar,jenis");
		$this->db->from("ref_jenis_bayar");
		$data = $this->db->get()->result_array();		
		return $data;
	}
	function getNamaMetodePembayaran($id){
		$this->db->select("jenis");
		$this->db->from("ref_jenis_bayar");
		$this->db->where("id_jenis_bayar",$id);
		$q = $this->db->get()->row()->jenis;
		return $q;
	}
	
	//faktur
}