<?php

class Main_mdl extends Base_Model
{

	function __construct()
	{

		parent::__construct();
		$this->table = 'sys_menu';
		$this->table2 = 'sys_submenu';
		$this->table3 = 'sys_subinmenu';
	}

	function getDataBC()
	{
		$q = "SELECT 
					(SELECT COUNT(*) FROM mat_inchdr_sub WHERE KdJnsDok='1' AND YEAR(matin_date) = YEAR(curdate())) AS 'BC23',
					(SELECT COUNT(*) FROM mat_inchdr_sub WHERE KdJnsDok='6' AND YEAR(matin_date) = YEAR(curdate())) AS 'BC27In',
					(SELECT COUNT(*) FROM mat_inchdr_sub WHERE KdJnsDok='5' AND YEAR(matin_date) = YEAR(curdate())) AS 'BC262',
					(SELECT COUNT(*) FROM mat_inchdr_sub WHERE KdJnsDok='2' AND YEAR(matin_date) = YEAR(curdate())) AS 'BC24',
					(SELECT COUNT(*) FROM mat_inchdr_sub WHERE KdJnsDok='8' AND YEAR(matin_date) = YEAR(curdate())) AS 'BC40',
					(SELECT COUNT(*) FROM mat_outhdr_sub WHERE KdJnsDok='7' AND YEAR(matout_date) = YEAR(curdate())) AS 'BC30',
					(SELECT COUNT(*) FROM mat_outhdr_sub WHERE KdJnsDok='6' AND YEAR(matout_date) = YEAR(curdate())) AS 'BC27Out',
					(SELECT COUNT(*) FROM mat_outhdr_sub WHERE KdJnsDok='4' AND YEAR(matout_date) = YEAR(curdate())) AS 'BC261',
					(SELECT COUNT(*) FROM mat_outhdr_sub WHERE KdJnsDok='3' AND YEAR(matout_date) = YEAR(curdate())) AS 'BC25',
					(SELECT COUNT(*) FROM mat_outhdr_sub WHERE KdJnsDok='9' AND YEAR(matout_date) = YEAR(curdate())) AS 'BC41'
			FROM DUAL";
		$rs = $this->db->query($q)->result();
		return $rs;
	}

	function getDataPo()
	{
		$q = "SELECT * FROM pendingpo_view";
		$rs = $this->db->query($q)->result();
		return $rs;
	}

	function getDataSo()
	{
		$q = "SELECT * FROM pendingso_view";
		$rs = $this->db->query($q)->result();
		return $rs;
	}

	function getDataBeacukai()
	{
		$q = "SELECT * FROM bc_order";
		$rs = $this->db->query($q)->result();
		return $rs;
	}

	function PendingSoGrafik()
	{
		$q = "SELECT * FROM pendingso_count";
		$rs = $this->db->query($q)->result();
		return $rs;
	}

	function PendingPoGrafik()
	{
		$q = "SELECT * FROM pendingpo_count";
		$rs = $this->db->query($q)->result();
		return $rs;
	}

	function IncomingGrafik()
	{
		$q = "SELECT * FROM incoming_count";
		$rs = $this->db->query($q)->result();
		return $rs;
	}

	function OutgoingGrafik()
	{
		$q = "SELECT * FROM outgoing_count";
		$rs = $this->db->query($q)->result();
		return $rs;
	}

	function buildMenu($nm_opr = "admin")
	{
		$q = "SELECT * FROM sys_menu 
			INNER JOIN ms_hak_akses ON fid_ms_module = menu_id
			WHERE nm_opr='$nm_opr'
			AND lihat=1
			ORDER BY no_urut";
		$rs = $this->db->query($q)->result();

		$opr_menu['selected']  = 0;
		foreach ($rs as $r) {
			$qSub = " SELECT * FROM sys_menugroup 
					INNER JOIN ms_hak_akses ON fid_ms_module = menugroup_id
					WHERE nm_opr='$nm_opr'
					AND lihat=1 AND status=1
					AND menu_id='" . $r->menu_id . "'";
			$rsSub = $this->db->query($qSub)->result_array();
			//echo $qSub;
			$opr_menu['tabs'][] = array(
				'title' => $r->menu,
				'groups' => (count($rsSub) > 0) ? $this->getMenuGroups($r->menu_id, $nm_opr) : 0
			);
		}

		return json_encode($opr_menu);
	}

	function getMenuGroups($id, $nm_opr)
	{
		$q = "SELECT * FROM sys_menugroup 
			INNER JOIN ms_hak_akses ON fid_ms_module = menugroup_id
			WHERE nm_opr='$nm_opr'
			AND lihat=1 AND status=1
			AND menu_id='$id'
			ORDER BY no_urut";
		$rs = $this->db->query($q)->result();

		$opr_menu = array();
		foreach ($rs as $r) {
			$qSub = " SELECT * FROM sys_submenu 
					INNER JOIN ms_hak_akses ON fid_ms_module = submenu_id
					WHERE nm_opr='$nm_opr'
					AND lihat=1 AND status=1
					AND menugroup_id='" . $r->menugroup_id . "'";
			//echo $qSub;		
			$rsSub = $this->db->query($qSub)->result();

			$opr_menu[] = array(
				'title' => $r->title,
				'tools' => (count($rsSub) > 0) ? $this->getChilds($r->menugroup_id, $nm_opr) : 0
			);
		}

		return $opr_menu;
	}

	function getChilds($id, $nm_opr)
	{
		$q = "SELECT * FROM sys_submenu 
			INNER JOIN ms_hak_akses ON fid_ms_module = submenu_id
			WHERE nm_opr='$nm_opr' 
			AND lihat=1 AND status=1
			AND menugroup_id='$id'
			ORDER BY no_urut";
		$rs = $this->db->query($q)->result();

		$opr_menu = array();
		foreach ($rs as $r) {
			$qSub = " SELECT * FROM sys_subinmenu 
					INNER JOIN ms_hak_akses ON fid_ms_module = subinmenu_id
					WHERE nm_opr='$nm_opr' 
					AND lihat=1 AND status=1
					AND submenu_id='" . $r->submenu_id . "'";
			$rsSub = $this->db->query($qSub)->result();

			$opr_menu[] = array(
				'type' => (count($rsSub) > 0) ? 'menubutton' : '',
				'name' => (count($rsSub) > 0) ? '' : $r->submenu_id . '~' . $r->submenu . '~' . $r->title . '~' . site_url($r->url) . '~' . $r->tambah . '~' . $r->ubah . '~' . $r->hapus,
				'text' => $r->submenu,
				'iconCls' => 'icon-' . $r->icon,
				'iconAlign' => 'top',
				'size' => 'large',
				'menuItems' => (count($rsSub) > 0) ? $this->getSubChilds($r->submenu_id, $nm_opr) : 0
			);
		}

		return $opr_menu;
	}

	function getSubChilds($id, $nm_opr)
	{
		$q = "SELECT * FROM sys_subinmenu 
			INNER JOIN ms_hak_akses ON fid_ms_module = subinmenu_id
			WHERE nm_opr='$nm_opr' 
			AND lihat=1 AND status=1
			AND submenu_id='$id'
			ORDER BY no_urut";
		$rs = $this->db->query($q)->result();

		$opr_menu = array();
		foreach ($rs as $r) {
			$opr_menu[] = array(
				'name' => $r->subinmenu_id . '~' . $r->subinmenu . '~' . $r->title . '~' . site_url($r->url) . '~' . $r->tambah . '~' . $r->ubah . '~' . $r->hapus,
				'text' => $r->subinmenu,
				'iconCls' => 'icon-' . $r->icon
			);
		}

		return $opr_menu;
	}
}
