<?php 
class Model_akun extends CI_model{

//xxxxxxxxxxxxxxxxxxxxxxxxx
    function list_(){
        return $this->db->query("SELECT * from tb_master_akun ORDER BY akun_id DESC");
    }
	
    function tambah_akun(){
      
		$datadb = array('nama_akun'=>$this->db->escape_str($this->input->post('a')),
						'tanggal_input'=>date('Y-m-d H:i:s'),
						'username_input'=>$this->session->username);
		$this->db->insert('tb_master_akun',$datadb);
			$title = "Tambah Data Master Akun <br />dengan ID [id=".$this->input->post('b')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("TAMBAH",$title,$descrip,'tb_master_akun');
    }
	function list_edit($id){
        return $this->db->query("SELECT * from tb_master_akun WHERE akun_id='$id'");
    }
	function list_detail_pembayaran_mahasiswa($id){
        return $this->db->query("SELECT A.*,B.nama_akun from tb_keuangan A
								LEFT JOIN tb_master_akun B ON A.akun_id=B.akun_id
							WHERE A.mahasiswa_id ='$id' ORDER BY A.id_semester DESC");
    }
	 function hapus_data($id){
        return $this->db->query("DELETE FROM tb_master_akun where akun_id='$id'");
		
    }
	function list_detail($id){
        return $this->db->query("SELECT A.*,B.keterangan AS agama FROM tb_mahasiswa A LEFT JOIN tb_agama B ON A.agama_id=B.agama_id
								where A.mahasiswa_id='$id'")->result_array();
    }
	
	function list_mahasiswa(){
        return $this->db->query("SELECT A.mahasiswa_id,A.nim,A.stambuk,A.nama_lengkap,A.alamat,A.semester,A.ruangan_id,A.semester_aktif,A.tanggal_lahir,A.status,A.photo,A.reguler,A.kategori_mahasiswa,
									B.keterangan AS angkatan,
									C.keterangan AS agama,
									D.nama_ruangan,
									E.nama_lengkap AS nama_dosen
								FROM tb_mahasiswa A
								LEFT JOIN tb_tahun_akademik B ON A.tahun_akademik_id=B.tahun_akademik_id
								LEFT JOIN tb_agama C ON A.agama_id=C.agama_id
								LEFT JOIN tb_ruangan D ON A.ruangan_id=D.ruangan_id
								LEFT JOIN tb_dosen E ON A.dosen_wali_id=E.dosen_id
								ORDER BY A.stambuk DESC,A.ruangan_id ASC");
    }
	function tambah_pembayaran(){
		$exxujian = explode('-',$this->input->post('tanggal_bayar'));
			$tanggal_bayar = $exxujian[2].'-'.$exxujian[1].'-'.$exxujian[0];
			
		$datadb = array('mahasiswa_id'=>$this->db->escape_str($this->input->post('mahasiswa_id')),
						'id_semester'=>$this->db->escape_str($this->input->post('semester')),
						'akun_id'=>$this->db->escape_str($this->input->post('pilih_akun')),
						'jumlah_bayar'=>$this->db->escape_str($this->input->post('jumlah')),
						'tanggal_bayar'=>$tanggal_bayar,
						'tanggal_input'=>date('Y-m-d H:i:s'),
						'username_input'=>$this->session->username);
		$this->db->insert('tb_keuangan',$datadb);
    }
	function hapus_data_bayar($id){
        return $this->db->query("DELETE FROM tb_keuangan where keuangan_id='$id'");
		
    }
	function list_update(){
		
		$datadb = array('nama_akun'=>$this->db->escape_str($this->input->post('a')),
								'tanggal_perubahan'=>date('Y-m-d H:i:s'),
								'username_perubahan'=>$this->session->admin_user);
		
        $this->db->where('akun_id',$this->input->post('id'));
        $this->db->update('tb_master_akun',$datadb);
			$title = "Update Data Master Akun <br />dengan ID [id=".$this->input->post('b')."]";
			$descrip = $this->fetch_description($title,$datadb);
			$this->add_audittrail("UPDATE",$title,$descrip,'tb_master_akun');
    }
	function fetch_description($title,$data){
		$descrip = '<br><table style="vertical-align:top" cellspacing="0" cellpadding="1" border="1">';
		$descrip .= '<tr><th>Nama Kolom</th><th>Nilai</th></tr>';
		foreach ($data as $key => $value) {
			$descrip .= '<tr>';
			$descrip .= '<td>'.$key.'</td>';
			$descrip .= '<td>'.$value.'</td>';
			$descrip .= '</tr>';
		}
		$descrip .= '</table>';
		return $descrip;
	}

	function add_audittrail($action,$title,$descrip,$tablename){
		try {
			$data = array(
				'datetime' => date("Y-m-d H:i:s"),
				'ipaddress' => $this->input->ip_address(),
				'username' => $this->session->userdata('username'),
				'tablename' => $tablename,
				'formname' => 'by komar',
				'action' => $action,
				'title' => $title,
				'description' => $descrip
			);
			$this->db->insert('sys_audittrail', $data);
		} catch (Exception $e) {
		}
	}

}