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

class Db_saldo extends CI_Model{
	function get_lits_alur_perkara($saldo_id=''){
		if($saldo_id!='' OR $saldo_id>0){
			$where =" WHERE saldo_id=".$saldo_id;
		}else{
			$where ="";
		}
		try {
			return $this->db->query("SELECT * from saldo_awal $where Order by tahun_saldo desc");
		} catch (Exception $e) {
			
		}
	}

	function get_year(){
		try{
			return $this->db->query("SELECT DISTINCT YEAR(tanggal_transaksi) AS tahun FROM perkara_biaya Order By tahun desc;");
		}catch (Exception $e){
			log_message('error',$e);
		}
	}

	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){
		try {
			$data = array(
				'datetime' => date("Y-m-d H:i:s"),
				'ipaddress' => $this->input->ip_address(),
				'username' => $this->session->userdata('username'),
				'tablename' => 'sys_users',
				'formname' => 'Data User',
				'action' => $action,
				'title' => $title,
				'description' => $descrip
			);
			$this->db->insert('sys_audittrail', $data);
		} catch (Exception $e) {
			
		}
	}

	function update_data($data,$action){
		if($action==0){
			$title = "Tambah Saldo Awal";	
			$aksi="Insert";
		}elseif($action>0){
			$title = "EDIT Saldo Awal";	
			$aksi="update";
		}
		try {
				$this->db->replace('saldo_awal', $data);
				$descrip = $this->fetch_description($title,$data);
				$this->add_audittrail("UPDATE",$title,$descrip);
			return TRUE;
		} catch (Exception $e) {
			return $e;
		}
	}

}