<?php

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

class M_pad extends CI_Model {

    public function __construct() {
        parent::__construct();
    }

    function getPad($is_count = false, $limit = null, $start = null, $id = null){
        $param = array();

        if($is_count) 
            $query = "SELECT COUNT(p.id_pad) AS jumlah";
        else 
            $query ="SELECT p.*";

        $query .= " FROM data_pad p";

        if(!$is_count)
			$query .= " ";
        
        $query .= " WHERE p.is_active = '1'";
		
		if($id){
			$param[] = $id;
			$query .= " AND p.id_pad = ?";
		}

        $query .= " ORDER BY p.tahun, p.bulan DESC";

        if($is_count == false && !$id) {
            if (isset($limit)) {
                $query .= " LIMIT ?, ?";
                $param[] = intval($start);
                $param[] = intval($limit);
            }
        }
    
        $result = $this->db->query($query, $param);

        if($is_count){
			return $result->row_array()['jumlah'];
		} else {
			if($id != null)
				return $result->row_array();
			else
				return $result->result_array();
		}
    }
	
	function createEdit($data, $id_pad	= null){
		$this->db->trans_begin(); 
		
		if(!isset($id_pad) || $id_pad === ''){
		$this->db->query("INSERT INTO data_pad(tahun, bulan, nominal, keterangan, last_modified_user) VALUES
			(?,?,?,?,?)
		", $data);
		}else{
			$data[] = $id_pad;
			$this->db->query("UPDATE data_pad SET tahun = ?, bulan = ?, nominal = ?, keterangan = ?, last_modified_user = ? WHERE id_pad = ?", $data);
		}
		
		if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            return FALSE;
        }else{
            $this->db->trans_commit();
            return TRUE;
        }
	}
	
	function hapusPad($id_pad){
		$this->db->trans_begin();
		
		$this->db->query("UPDATE data_pad SET is_active = '0' WHERE id_pad = ?
		", $id_pad);
		
		if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            return FALSE;
        }else{
            $this->db->trans_commit();
            return TRUE;
        }
	}
}