<?php 

/**
 * SharedController Controller
 * @category  Controller / Model
 */
class SharedController extends BaseController{
	
	/**
     * barang_id_kategori_option_list Model Action
     * @return array
     */
	function barang_id_kategori_option_list(){
		$db = $this->GetModel();
		$sqltext = "SELECT  DISTINCT id_kategori AS value,nama_kategori AS label FROM kategori";
		$queryparams = null;
		$arr = $db->rawQuery($sqltext, $queryparams);
		return $arr;
	}

	/**
     * barang_id_supplier_option_list Model Action
     * @return array
     */
	function barang_id_supplier_option_list(){
		$db = $this->GetModel();
		$sqltext = "SELECT  DISTINCT id_supplier AS value,nama_supplier AS label FROM supplier";
		$queryparams = null;
		$arr = $db->rawQuery($sqltext, $queryparams);
		return $arr;
	}

	/**
     * barang_keluar_id_barang_option_list Model Action
     * @return array
     */
	function barang_keluar_id_barang_option_list(){
		$db = $this->GetModel();
		$sqltext = "SELECT  DISTINCT id_barang AS value,nama_barang AS label FROM barang";
		$queryparams = null;
		$arr = $db->rawQuery($sqltext, $queryparams);
		return $arr;
	}

	/**
     * barang_masuk_id_barang_option_list Model Action
     * @return array
     */
	function barang_masuk_id_barang_option_list(){
		$db = $this->GetModel();
		$sqltext = "SELECT  DISTINCT id_barang AS value,nama_barang AS label FROM barang";
		$queryparams = null;
		$arr = $db->rawQuery($sqltext, $queryparams);
		return $arr;
	}

	/**
     * barang_masuk_id_supplier_option_list Model Action
     * @return array
     */
	function barang_masuk_id_supplier_option_list(){
		$db = $this->GetModel();
		$sqltext = "SELECT  DISTINCT id_supplier AS value,nama_supplier AS label FROM supplier";
		$queryparams = null;
		$arr = $db->rawQuery($sqltext, $queryparams);
		return $arr;
	}

	/**
     * users_username_value_exist Model Action
     * @return array
     */
	function users_username_value_exist($val){
		$db = $this->GetModel();
		$db->where("username", $val);
		$exist = $db->has("users");
		return $exist;
	}

	/**
     * users_email_value_exist Model Action
     * @return array
     */
	function users_email_value_exist($val){
		$db = $this->GetModel();
		$db->where("email", $val);
		$exist = $db->has("users");
		return $exist;
	}

	/**
     * getcount_barang Model Action
     * @return Value
     */
	function getcount_barang(){
		$db = $this->GetModel();
		$sqltext = "SELECT COUNT(*) AS num FROM barang";
		$queryparams = null;
		$val = $db->rawQueryValue($sqltext, $queryparams);
		
		if(is_array($val)){
			return $val[0];
		}
		return $val;
	}

	/**
     * getcount_barangmasuk Model Action
     * @return Value
     */
	function getcount_barangmasuk(){
		$db = $this->GetModel();
		$sqltext = "SELECT COUNT(*) AS num FROM barang_masuk";
		$queryparams = null;
		$val = $db->rawQueryValue($sqltext, $queryparams);
		
		if(is_array($val)){
			return $val[0];
		}
		return $val;
	}

	/**
     * getcount_barangkeluar Model Action
     * @return Value
     */
	function getcount_barangkeluar(){
		$db = $this->GetModel();
		$sqltext = "SELECT COUNT(*) AS num FROM barang_keluar";
		$queryparams = null;
		$val = $db->rawQueryValue($sqltext, $queryparams);
		
		if(is_array($val)){
			return $val[0];
		}
		return $val;
	}

	/**
	* barchart_stokbarang Model Action
	* @return array
	*/
	function barchart_stokbarang(){
		
		$db = $this->GetModel();
		$chart_data = array(
			"labels"=> array(),
			"datasets"=> array(),
		);
		
		//set query result for dataset 1
		$sqltext = "SELECT  b.id_barang, b.kode_barang, b.nama_barang, b.id_kategori, b.id_supplier, b.satuan, b.stok FROM barang AS b";
		$queryparams = null;
		$dataset1 = $db->rawQuery($sqltext, $queryparams);
		$dataset_data =  array_column($dataset1, 'stok');
		$dataset_labels =  array_column($dataset1, 'nama_barang');
		$chart_data["labels"] = array_unique(array_merge($chart_data["labels"], $dataset_labels));
		$chart_data["datasets"][] = $dataset_data;

		return $chart_data;
	}

}
