<?php

namespace App\Controllers;

class Dashboard extends BaseController {
	
	protected array $data;

	function __construct() {
		$this->data['stats'] = array('O'=>'Open', 'A'=>'Accepted', 'P'=>'Pending', 'C'=> 'Close', 'S' => 'Suspend');
	}
	
	public function index ($userid = null) {
		$db = \Config\Database::connect();
		
		if(!isset($userid) || $userid == 0) {
			$userid = $_SESSION['userid'];
		}
		$data['userid'] = $userid;
		
		$year = date('Y');
		$month = date('n');
		
		$sql = "SELECT * FROM users";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['users'] = $results;
		$data['stats'] = $this->data['stats'];
		
		// act status counter - open, accept, pending all time - suspend and close this year
		$sql = "SELECT 
				-- status counter
				SUM(CASE WHEN a.activitystatus='O' AND a.userid_owner='$userid' THEN 1 ELSE 0 END) AS act_open,
				SUM(CASE WHEN a.activitystatus='A' AND a.userid_owner='$userid' THEN 1 ELSE 0 END) AS act_accept,	
				SUM(CASE WHEN a.activitystatus='P' AND a.userid_owner='$userid' THEN 1 ELSE 0 END) AS act_pending,
				SUM(CASE WHEN a.activitystatus='S' AND a.userid_owner='$userid' THEN 1 ELSE 0 END) AS act_suspend,
				SUM(CASE WHEN a.activitystatus='C' AND a.userid_owner='$userid' AND YEAR(a.closedate)='$year' THEN 1 ELSE 0 END) AS act_close,
				-- year type counter user 
				SUM(CASE WHEN aty.`acttypecode`='IR' AND a.userid_owner='$userid' AND YEAR(a.`closedate`)='$year' THEN 1 ELSE 0 END) AS ir_y,
				SUM(CASE WHEN aty.`acttypecode`='MN' AND a.userid_owner='$userid' AND YEAR(a.`closedate`)='$year' THEN 1 ELSE 0 END) AS mn_y,
				SUM(CASE WHEN aty.`acttypecode`='CR' AND a.userid_owner='$userid' AND YEAR(a.`closedate`)='$year' THEN 1 ELSE 0 END) AS cr_y,
				SUM(CASE WHEN aty.`acttypecode`='PR' AND a.userid_owner='$userid' AND YEAR(a.`closedate`)='$year' THEN 1 ELSE 0 END) AS pr_y,
				SUM(CASE WHEN aty.`acttypecode`='SP' AND a.userid_owner='$userid' AND YEAR(a.`closedate`)='$year' THEN 1 ELSE 0 END) AS sp_y,
				SUM(CASE WHEN aty.`acttypecode`='TR' AND a.userid_owner='$userid' AND YEAR(a.`closedate`)='$year' THEN 1 ELSE 0 END) AS tr_y,
				SUM(CASE WHEN aty.`acttypecode`='RF' AND a.userid_owner='$userid' AND YEAR(a.`closedate`)='$year' THEN 1 ELSE 0 END) AS rf_y,
				-- year type counter all
				SUM(CASE WHEN aty.`acttypecode`='IR' AND YEAR(a.`closedate`)='$year' THEN 1 ELSE 0 END) AS ir_ya,
				SUM(CASE WHEN aty.`acttypecode`='MN' AND YEAR(a.`closedate`)='$year' THEN 1 ELSE 0 END) AS mn_ya,
				SUM(CASE WHEN aty.`acttypecode`='CR' AND YEAR(a.`closedate`)='$year' THEN 1 ELSE 0 END) AS cr_ya,
				SUM(CASE WHEN aty.`acttypecode`='PR' AND YEAR(a.`closedate`)='$year' THEN 1 ELSE 0 END) AS pr_ya,
				SUM(CASE WHEN aty.`acttypecode`='SP' AND YEAR(a.`closedate`)='$year' THEN 1 ELSE 0 END) AS sp_ya,
				SUM(CASE WHEN aty.`acttypecode`='TR' AND YEAR(a.`closedate`)='$year' THEN 1 ELSE 0 END) AS tr_ya,
				SUM(CASE WHEN aty.`acttypecode`='RF' AND YEAR(a.`closedate`)='$year' THEN 1 ELSE 0 END) AS rf_ya,
				-- month type counter user 
				SUM(CASE WHEN aty.`acttypecode`='IR' AND a.userid_owner='$userid' AND YEAR(a.`closedate`)='$year' AND MONTH(a.`closedate`)='$month' THEN 1 ELSE 0 END) AS ir_m,
				SUM(CASE WHEN aty.`acttypecode`='MN' AND a.userid_owner='$userid' AND YEAR(a.`closedate`)='$year' AND MONTH(a.`closedate`)='$month' THEN 1 ELSE 0 END) AS mn_m,
				SUM(CASE WHEN aty.`acttypecode`='CR' AND a.userid_owner='$userid' AND YEAR(a.`closedate`)='$year' AND MONTH(a.`closedate`)='$month' THEN 1 ELSE 0 END) AS cr_m,
				SUM(CASE WHEN aty.`acttypecode`='PR' AND a.userid_owner='$userid' AND YEAR(a.`closedate`)='$year' AND MONTH(a.`closedate`)='$month' THEN 1 ELSE 0 END) AS pr_m,
				SUM(CASE WHEN aty.`acttypecode`='SP' AND a.userid_owner='$userid' AND YEAR(a.`closedate`)='$year' AND MONTH(a.`closedate`)='$month' THEN 1 ELSE 0 END) AS sp_m,
				SUM(CASE WHEN aty.`acttypecode`='TR' AND a.userid_owner='$userid' AND YEAR(a.`closedate`)='$year' AND MONTH(a.`closedate`)='$month' THEN 1 ELSE 0 END) AS tr_m,
				SUM(CASE WHEN aty.`acttypecode`='RF' AND a.userid_owner='$userid' AND YEAR(a.`closedate`)='$year' AND MONTH(a.`closedate`)='$month' THEN 1 ELSE 0 END) AS rf_m,
				-- month type counter all
				SUM(CASE WHEN aty.`acttypecode`='IR' AND YEAR(a.`closedate`)='$year' AND MONTH(a.`closedate`)='$month' THEN 1 ELSE 0 END) AS ir_ma,
				SUM(CASE WHEN aty.`acttypecode`='MN' AND YEAR(a.`closedate`)='$year' AND MONTH(a.`closedate`)='$month' THEN 1 ELSE 0 END) AS mn_ma,
				SUM(CASE WHEN aty.`acttypecode`='CR' AND YEAR(a.`closedate`)='$year' AND MONTH(a.`closedate`)='$month' THEN 1 ELSE 0 END) AS cr_ma,
				SUM(CASE WHEN aty.`acttypecode`='PR' AND YEAR(a.`closedate`)='$year' AND MONTH(a.`closedate`)='$month' THEN 1 ELSE 0 END) AS pr_ma,
				SUM(CASE WHEN aty.`acttypecode`='SP' AND YEAR(a.`closedate`)='$year' AND MONTH(a.`closedate`)='$month' THEN 1 ELSE 0 END) AS sp_ma,
				SUM(CASE WHEN aty.`acttypecode`='TR' AND YEAR(a.`closedate`)='$year' AND MONTH(a.`closedate`)='$month' THEN 1 ELSE 0 END) AS tr_ma,
				SUM(CASE WHEN aty.`acttypecode`='RF' AND YEAR(a.`closedate`)='$year' AND MONTH(a.`closedate`)='$month' THEN 1 ELSE 0 END) AS rf_ma
			FROM activities a 
			LEFT JOIN acttype aty ON aty.`acttypeid`=a.`acttypeid`";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['act_counter'] = $results;
		
		$sql = "SELECT COUNT(userid) as nuser FROM users 
				WHERE enddate IS NULL
				AND userposid = ( SELECT userposid FROM users WHERE userid='$userid' ) ";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['nuser'] = $results;

		$sql = "select level from users where userid='$userid'";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$level = $results[0]['level'];
		
		// Tanggal Kurun Waktu 1 Bulan
		$lastday = cal_days_in_month(CAL_GREGORIAN,$month,$year);
		$opendate = $year.'-'.($month).'-01';
		$closedate = $year.'-'.$month.'-'.$lastday;
		
		// Cari User Position
		$sql = "SELECT userposid FROM users WHERE userid = '$userid'";
		$query = $db->query($sql);
		$results = $query->getResultArray();
		$data['user_position'] = $results[0]['userposid'];
		$user_position = $data['user_position'];
		
		// Tampilan Untuk Recent Activity semua User
		$sql = "SELECT s.sitename, pc.productname, v.vendorname, u.firstname as username, u.userposid, uc.firstname as creator_name, at.fulltext, a.* 
			FROM `activities` a 
			left join sites s on s.siteid=a.siteid 
			left join products p on a.productid=p.productid
			left join productcatalog pc on pc.catalogid=p.catalogid
			left join vendors v on v.vendorid=a.vendorid 
			left join productalias pa on pa.productaliasid=pc.productaliasid
			left join users u on u.userid=a.userid_owner 
			left JOIN (select userid, firstname from users) as uc on uc.userid=a.userid_creator
			left join acttype at on at.acttypeid=a.acttypeid
			where 
			(( a.closedate between '$opendate 00:00:00' and '$closedate 23:59:59')
			OR ( a.reportdate between '$opendate 00:00:00' and '$closedate 23:59:59')
			OR ( a.activitystatus='O' and a.opendate >= DATE_SUB(CURDATE(), INTERVAL 3 MONTH))
			OR (a.activitystatus = 'P' and a.opendate >= DATE_SUB(CURDATE(), INTERVAL 3 MONTH)))	
			AND (a.activitystatus <> 'S')
			order by field(a.activitystatus,'O','C','R'), a.closedate desc, a.reportdate desc";
		$query = $db->query($sql);
        $result = $query->getResultArray();
        $data['tampildata'] = $result;
		
		// Menu Table TSS/TSM dan TSO
		if(in_array($level,[1,2])) {
			$firstdate = date("Y-m-01");
			$lastdate = cal_days_in_month(CAL_GREGORIAN,date('m'),date('Y'));
			$lastdate = date('Y-m-'.$lastdate);
			
			$sql_pos = '';
			if($user_position == '1') { $sql_pos = 'AND u.userposid in (1,2,3,4,5)'; }
			elseif($user_position == '2') { $sql_pos = 'AND u.userposid in (2,4)'; }
			elseif($user_position == '3') { $sql_pos = 'AND u.userposid in (3,5)'; }
			
			$sql = "SELECT u.firstname, u.lastname, u.userdeptid, u.reportto, u.userid,
						SUM(CASE WHEN aty.acttypecode='CR' THEN 1 ELSE 0 END) AS CR,
						SUM(CASE WHEN aty.acttypecode='IR' THEN 1 ELSE 0 END) AS IR,
						SUM(CASE WHEN aty.acttypecode='MN' THEN 1 ELSE 0 END) AS MN,
						SUM(CASE WHEN aty.acttypecode='SP' THEN 1 ELSE 0 END) AS SP,
						SUM(CASE WHEN aty.acttypecode='PR' THEN 1 ELSE 0 END) AS PR,
						SUM(CASE WHEN aty.acttypecode='RF' THEN 1 ELSE 0 END) AS RF,
						SUM(CASE WHEN aty.acttypecode='TR' THEN 1 ELSE 0 END) AS TR,
						COUNT(a.actid) AS total
					FROM users u
					LEFT JOIN activities a ON u.userid = a.userid_owner AND a.closedate BETWEEN '$firstdate 00:00:00' AND '$lastdate 23:59:59'
					LEFT JOIN acttype aty ON aty.acttypeid = a.acttypeid
					WHERE u.userdeptid = 1 and ( u.enddate is null OR u.enddate < a.opendate )
					$sql_pos
					GROUP BY u.userid, u.firstname, u.lastname
					ORDER BY u.userid ASC, total DESC, u.firstname";
			$query = $db->query($sql);
			$results = $query->getResultArray();
			$data['count'] = $results;

			// MENU TSM & TSS
			// Menu Table TOP 3 PART REPLACED
			$sql = "SELECT pr.catalognumber, pr.productname, SUM( CAST(inv.qty as decimal(10,2))) as total_qty 
					FROM productcatalog pr
					LEFT JOIN unitgroup ug ON ug.catalogid=pr.catalogid
					LEFT JOIN invtrans inv ON inv.unitgroupid=ug.unitgroupid
					WHERE inv.purpose IN ('PR', 'PB', 'PU', 'PF') AND pr.productaliasid<>0
					GROUP BY pr.catalognumber
					ORDER BY total_qty DESC
					LIMIT 3";
			$query = $db->query($sql);
			$results = $query->getResultArray();
			$data['most_part_replaced'] = $results;

			// Menu Table SPARE PARTS REPLACEMENT THIS MONTH
			// Menu TSO BARAT
			$sql = "SELECT vn.vendorid, vn.vendorname, SUM( CAST(inv.qty as decimal(10,0))) as total_qty
					FROM productcatalog pr
					LEFT JOIN vendors vn ON vn.vendorid=pr.vendorid 
					LEFT JOIN unitgroup ug ON ug.catalogid=pr.catalogid
					LEFT JOIN invtrans inv ON inv.unitgroupid=ug.unitgroupid
					LEFT JOIN activities act ON act.actid=inv.actid
					LEFT JOIN sites st ON st.siteid=act.siteid
					LEFT JOIN accounts acc ON st.accountid = acc.accountid 
					LEFT JOIN zones zs ON acc.zoneid = zs.zoneid 
					LEFT JOIN areazone ae ON zs.zoneid = ae.zoneid 
					LEFT JOIN areas ars ON ae.areaid = ars.areaid 
					WHERE inv.purpose IN ('PR', 'PB', 'PU', 'PF') AND pr.productaliasid <> 0
					AND ars.areaid IN (8)
					AND (inv.itxdate between '$opendate 00:00:00' and '$closedate 23:59:59')
					GROUP BY vn.vendorid
					ORDER BY total_qty DESC;";
			$query = $db->query($sql);
			$results = $query->getResultArray();
			$data['replacement_barat'] = $results;
			$vendorid_filtersql=''; // Untuk Filter Vendorid
			foreach($data['replacement_barat'] as $row) { // Perulangan untuk menyimpan data vendor id ke string vendorid_filtersql
				if($row['vendorid']==null){continue;}
				$vendorid_filtersql .= $row['vendorid'];
				$vendorid_filtersql .= ',';
			}
			$vendorid_filtersql = substr_replace($vendorid_filtersql, "", -1); // Untuk menghilangkan tanda koma pada bagian akhir
			$vendorid_filtersql == "" ? "" : $vendorid_filtersql = sprintf("%s%s%s", "AND vn.vendorid IN (", $vendorid_filtersql, ")");
			$sql = "SELECT vn.vendorid, COUNT(pr.siteid) as banyak_vendor
					FROM vendors vn
					LEFT JOIN productcatalog prct ON vn.vendorid=prct.vendorid
					LEFT JOIN products pr ON prct.catalogid=pr.catalogid
					LEFT JOIN sites st ON pr.siteid=st.siteid
					LEFT JOIN accounts acc ON st.accountid = acc.accountid 
					LEFT JOIN zones zs ON acc.zoneid = zs.zoneid 
					LEFT JOIN areazone ae ON zs.zoneid = ae.zoneid 
					LEFT JOIN areas ars ON ae.areaid = ars.areaid
					WHERE ars.areaid IN (8) $vendorid_filtersql
					GROUP BY vn.vendorid";
			$query = $db->query($sql);
			$results = $query->getResultArray();
			$data['replacement_vendor_barat'] = $results;
			// Menu TSO TENGAH
			$sql = "SELECT vn.vendorid, vn.vendorname, SUM( CAST(inv.qty as decimal(10,0))) as total_qty
					FROM productcatalog pr
					LEFT JOIN vendors vn ON vn.vendorid=pr.vendorid 
					LEFT JOIN unitgroup ug ON ug.catalogid=pr.catalogid
					LEFT JOIN invtrans inv ON inv.unitgroupid=ug.unitgroupid
					LEFT JOIN activities act ON act.actid=inv.actid
					LEFT JOIN sites st ON st.siteid=act.siteid
					LEFT JOIN accounts acc ON st.accountid = acc.accountid 
					LEFT JOIN zones zs ON acc.zoneid = zs.zoneid 
					LEFT JOIN areazone ae ON zs.zoneid = ae.zoneid 
					LEFT JOIN areas ars ON ae.areaid = ars.areaid 
					WHERE inv.purpose IN ('PR', 'PB', 'PU', 'PF') AND pr.productaliasid <> 0
					AND ars.areaid IN (9)
					AND (inv.itxdate between '$opendate 00:00:00' and '$closedate 23:59:59')
					GROUP BY vn.vendorid
					ORDER BY total_qty DESC;";
			$query = $db->query($sql);
			$results = $query->getResultArray();
			$data['replacement_tengah'] = $results;
			$vendorid_filtersql=''; // Untuk Filter Vendorid
			foreach($data['replacement_tengah'] as $row) { // Perulangan untuk menyimpan data vendor id ke string vendorid_filtersql
				if($row['vendorid']==null){continue;}
				$vendorid_filtersql .= $row['vendorid'];
				$vendorid_filtersql .= ',';
			}
			$vendorid_filtersql = substr_replace($vendorid_filtersql, "", -1); // Untuk menghilangkan tanda koma pada bagian akhir
			$vendorid_filtersql == "" ? "" : $vendorid_filtersql = sprintf("%s%s%s", "AND vn.vendorid IN (", $vendorid_filtersql, ")");
			$sql = "SELECT vn.vendorid, COUNT(pr.siteid) as banyak_vendor
					FROM vendors vn
					LEFT JOIN productcatalog prct ON vn.vendorid=prct.vendorid
					LEFT JOIN products pr ON prct.catalogid=pr.catalogid
					LEFT JOIN sites st ON pr.siteid=st.siteid
					LEFT JOIN accounts acc ON st.accountid = acc.accountid 
					LEFT JOIN zones zs ON acc.zoneid = zs.zoneid 
					LEFT JOIN areazone ae ON zs.zoneid = ae.zoneid 
					LEFT JOIN areas ars ON ae.areaid = ars.areaid
					WHERE ars.areaid IN (9) $vendorid_filtersql
					GROUP BY vn.vendorid";
			$query = $db->query($sql);
			$results = $query->getResultArray();
			$data['replacement_vendor_tengah'] = $results;
			// Menu TSO TIMUR
			$sql = "SELECT vn.vendorid, vn.vendorname, SUM( CAST(inv.qty as decimal(10,0))) as total_qty
					FROM productcatalog pr
					LEFT JOIN vendors vn ON vn.vendorid=pr.vendorid 
					LEFT JOIN unitgroup ug ON ug.catalogid=pr.catalogid
					LEFT JOIN invtrans inv ON inv.unitgroupid=ug.unitgroupid
					LEFT JOIN activities act ON act.actid=inv.actid
					LEFT JOIN sites st ON st.siteid=act.siteid
					LEFT JOIN accounts acc ON st.accountid = acc.accountid 
					LEFT JOIN zones zs ON acc.zoneid = zs.zoneid 
					LEFT JOIN areazone ae ON zs.zoneid = ae.zoneid 
					LEFT JOIN areas ars ON ae.areaid = ars.areaid 
					WHERE inv.purpose IN ('PR', 'PB', 'PU', 'PF') AND pr.productaliasid <> 0
					AND ars.areaid IN (10)
					AND (inv.itxdate between '$opendate 00:00:00' and '$closedate 23:59:59')
					GROUP BY vn.vendorid
					ORDER BY total_qty DESC;";
			$query = $db->query($sql);
			$results = $query->getResultArray();
			$data['replacement_timur'] = $results;
			$vendorid_filtersql=''; // Untuk Filter Vendorid
			foreach($data['replacement_timur'] as $row) { // Perulangan untuk menyimpan data vendor id ke string vendorid_filtersql
				if($row['vendorid']==null){continue;}
				$vendorid_filtersql .= $row['vendorid'];
				$vendorid_filtersql .= ',';
			}
			$vendorid_filtersql = substr_replace($vendorid_filtersql, "", -1); // Untuk menghilangkan tanda koma pada bagian akhir
			$vendorid_filtersql == "" ? "" : $vendorid_filtersql = sprintf("%s%s%s", "AND vn.vendorid IN (", $vendorid_filtersql, ")");
			$sql = "SELECT vn.vendorid, COUNT(pr.siteid) as banyak_vendor
					FROM vendors vn
					LEFT JOIN productcatalog prct ON vn.vendorid=prct.vendorid
					LEFT JOIN products pr ON prct.catalogid=pr.catalogid
					LEFT JOIN sites st ON pr.siteid=st.siteid
					LEFT JOIN accounts acc ON st.accountid = acc.accountid 
					LEFT JOIN zones zs ON acc.zoneid = zs.zoneid 
					LEFT JOIN areazone ae ON zs.zoneid = ae.zoneid 
					LEFT JOIN areas ars ON ae.areaid = ars.areaid
					WHERE ars.areaid IN (10) $vendorid_filtersql
					GROUP BY vn.vendorid";
			$query = $db->query($sql);
			$results = $query->getResultArray();
			$data['replacement_vendor_timur'] = $results;

			// ACTIVITIES BY PRINCIPLE - THIS MONTH
			// TSO BARAT
			$sql = "SELECT vn.vendorid, vn.vendorname, COUNT(vn.vendorid) as banyak_act
					FROM activities act 
					LEFT JOIN sites st ON st.siteid=act.siteid
					LEFT JOIN products pr ON pr.productid=act.productid
					LEFT JOIN productcatalog prct ON prct.catalogid=pr.catalogid
					LEFT JOIN vendors vn ON vn.vendorid=prct.vendorid
					LEFT JOIN accounts acc ON st.accountid = acc.accountid 
					LEFT JOIN zones zs ON acc.zoneid = zs.zoneid 
					LEFT JOIN areazone ae ON zs.zoneid = ae.zoneid 
					LEFT JOIN areas ars ON ae.areaid = ars.areaid 
					WHERE act.productid IS NOT NULL 
					AND ars.areaid IN (8)
					AND (act.reportdate between '$opendate 00:00:00' and '$closedate 23:59:59')
					AND ( act.opendate between '$opendate 00:00:00' and '$closedate 23:59:59')
					GROUP BY vn.vendorid
					ORDER BY banyak_act DESC
					LIMIT 5";
			$query = $db->query($sql);
			$results = $query->getResultArray();
			$data['act_principle_barat'] = $results;
			$vendorid_filtersql=''; // Untuk Filter Vendorid
			foreach($data['act_principle_barat'] as $row) { // Perulangan untuk menyimpan data vendor id ke string vendorid_filtersql
				if($row['vendorid']==null){continue;}
				$vendorid_filtersql .= $row['vendorid'];
				$vendorid_filtersql .= ',';
			}
			$vendorid_filtersql = substr_replace($vendorid_filtersql, "", -1); // Untuk menghilangkan tanda koma pada bagian akhir
			$vendorid_filtersql == "" ? "" : $vendorid_filtersql = sprintf("%s%s%s", "AND vn.vendorid IN (", $vendorid_filtersql, ")");
			$sql = "SELECT vn.vendorid, COUNT(pr.siteid) as banyak_vendor
					FROM vendors vn
					LEFT JOIN productcatalog prct ON vn.vendorid=prct.vendorid
					LEFT JOIN products pr ON prct.catalogid=pr.catalogid
					LEFT JOIN sites st ON pr.siteid=st.siteid
					LEFT JOIN accounts acc ON st.accountid = acc.accountid 
					LEFT JOIN zones zs ON acc.zoneid = zs.zoneid 
					LEFT JOIN areazone ae ON zs.zoneid = ae.zoneid 
					LEFT JOIN areas ars ON ae.areaid = ars.areaid
					WHERE ars.areaid IN (8) $vendorid_filtersql
					GROUP BY vn.vendorid
					LIMIT 5";
			$query = $db->query($sql);
			$results = $query->getResultArray();
			$data['act_vendor_barat'] = $results;
			// TSO TENGAH
			$sql = "SELECT vn.vendorid, vn.vendorname, COUNT(vn.vendorid) as banyak_act
					FROM activities act 
					LEFT JOIN sites st ON st.siteid=act.siteid
					LEFT JOIN products pr ON pr.productid=act.productid
					LEFT JOIN productcatalog prct ON prct.catalogid=pr.catalogid
					LEFT JOIN vendors vn ON vn.vendorid=prct.vendorid
					LEFT JOIN accounts acc ON st.accountid = acc.accountid 
					LEFT JOIN zones zs ON acc.zoneid = zs.zoneid 
					LEFT JOIN areazone ae ON zs.zoneid = ae.zoneid 
					LEFT JOIN areas ars ON ae.areaid = ars.areaid 
					WHERE act.productid IS NOT NULL 
					AND ars.areaid IN (9)
					AND (act.reportdate between '$opendate 00:00:00' and '$closedate 23:59:59')
					AND ( act.opendate between '$opendate 00:00:00' and '$closedate 23:59:59')
					GROUP BY vn.vendorid
					ORDER BY banyak_act DESC
					LIMIT 5";
			$query = $db->query($sql);
			$results = $query->getResultArray();
			$data['act_principle_tengah'] = $results;
			$vendorid_filtersql=''; // Untuk Filter Vendorid
			foreach($data['act_principle_tengah'] as $row) { // Perulangan untuk menyimpan data vendor id ke string vendorid_filtersql
				if($row['vendorid']==null){continue;}
				$vendorid_filtersql .= $row['vendorid'];
				$vendorid_filtersql .= ',';
			}
			$vendorid_filtersql = substr_replace($vendorid_filtersql, "", -1); // Untuk menghilangkan tanda koma pada bagian akhir
			$vendorid_filtersql == "" ? "" : $vendorid_filtersql = sprintf("%s%s%s", "AND vn.vendorid IN (", $vendorid_filtersql, ")");
			$sql = "SELECT vn.vendorid, COUNT(pr.siteid) as banyak_vendor
					FROM vendors vn
					LEFT JOIN productcatalog prct ON vn.vendorid=prct.vendorid
					LEFT JOIN products pr ON prct.catalogid=pr.catalogid
					LEFT JOIN sites st ON pr.siteid=st.siteid
					LEFT JOIN accounts acc ON st.accountid = acc.accountid 
					LEFT JOIN zones zs ON acc.zoneid = zs.zoneid 
					LEFT JOIN areazone ae ON zs.zoneid = ae.zoneid 
					LEFT JOIN areas ars ON ae.areaid = ars.areaid
					WHERE ars.areaid IN (9) $vendorid_filtersql
					GROUP BY vn.vendorid
					LIMIT 5";
			$query = $db->query($sql);
			$results = $query->getResultArray();
			$data['act_vendor_tengah'] = $results;
			// TSO TIMUR
			$sql = "SELECT vn.vendorid, vn.vendorname, COUNT(vn.vendorid) as banyak_act
					FROM activities act 
					LEFT JOIN sites st ON st.siteid=act.siteid
					LEFT JOIN products pr ON pr.productid=act.productid
					LEFT JOIN productcatalog prct ON prct.catalogid=pr.catalogid
					LEFT JOIN vendors vn ON vn.vendorid=prct.vendorid
					LEFT JOIN accounts acc ON st.accountid = acc.accountid 
					LEFT JOIN zones zs ON acc.zoneid = zs.zoneid 
					LEFT JOIN areazone ae ON zs.zoneid = ae.zoneid 
					LEFT JOIN areas ars ON ae.areaid = ars.areaid 
					WHERE act.productid IS NOT NULL 
					AND ars.areaid IN (10)
					AND (act.reportdate between '$opendate 00:00:00' and '$closedate 23:59:59')
					AND ( act.opendate between '$opendate 00:00:00' and '$closedate 23:59:59')
					GROUP BY vn.vendorid
					ORDER BY banyak_act DESC
					LIMIT 5";
			$query = $db->query($sql);
			$results = $query->getResultArray();
			$data['act_principle_timur'] = $results;
			$vendorid_filtersql=''; // Untuk Filter Vendorid
			foreach($data['act_principle_timur'] as $row) { // Perulangan untuk menyimpan data vendor id ke string vendorid_filtersql
				if($row['vendorid']==null){continue;}
				$vendorid_filtersql .= $row['vendorid'];
				$vendorid_filtersql .= ',';
			}
			$vendorid_filtersql = substr_replace($vendorid_filtersql, "", -1); // Untuk menghilangkan tanda koma pada bagian akhir
			$vendorid_filtersql == "" ? "" : $vendorid_filtersql = sprintf("%s%s%s", "AND vn.vendorid IN (", $vendorid_filtersql, ")");
			$sql = "SELECT vn.vendorid, COUNT(pr.siteid) as banyak_vendor
					FROM vendors vn
					LEFT JOIN productcatalog prct ON vn.vendorid=prct.vendorid
					LEFT JOIN products pr ON prct.catalogid=pr.catalogid
					LEFT JOIN sites st ON pr.siteid=st.siteid
					LEFT JOIN accounts acc ON st.accountid = acc.accountid 
					LEFT JOIN zones zs ON acc.zoneid = zs.zoneid 
					LEFT JOIN areazone ae ON zs.zoneid = ae.zoneid 
					LEFT JOIN areas ars ON ae.areaid = ars.areaid
					WHERE ars.areaid IN (10) $vendorid_filtersql
					GROUP BY vn.vendorid
					LIMIT 5";
			$query = $db->query($sql);
			$results = $query->getResultArray();
			$data['act_vendor_timur'] = $results;

			// TOP 3 SITES WITH INCIDENTS - THIS MONTH
			// Menu Table TOP 3 SITES WITH INCIDENTS TSS IT
			$sql = "SELECT act.siteid ,st.sitename, acty.fulltext, COUNT(act.siteid) AS total, ars.description
					FROM activities act 
					LEFT JOIN users us on us.userid = act.userid_owner
					LEFT JOIN acttype acty ON act.acttypeid = acty.acttypeid 
					LEFT JOIN sites st ON act.siteid = st.siteid 
					LEFT JOIN accounts acc ON st.accountid = acc.accountid 
					LEFT JOIN zones zs ON acc.zoneid = zs.zoneid 
					LEFT JOIN areazone ae ON zs.zoneid = ae.zoneid 
					LEFT JOIN areas ars ON ae.areaid = ars.areaid
					WHERE act.acttypeid = 1 AND ars.areaid IN (8,9,10) AND us.userid IN (5,9,12,15,36)
					AND (act.reportdate between '$opendate 00:00:00' and '$closedate 23:59:59')
					AND ( act.opendate between '$opendate 00:00:00' and '$closedate 23:59:59')
					GROUP BY st.sitename
					ORDER BY total DESC
					LIMIT 3";
			$query = $db->query($sql);
			$results = $query->getResultArray();
			$data['incidents_site_tsoit'] = $results;
			// Menu Table TOP 3 SITES WITH INCIDENTS TSS IVD BARAT
			$sql = "SELECT act.siteid ,st.sitename, acty.fulltext, COUNT(act.siteid) AS total, ars.description
					FROM activities act 
					LEFT JOIN users us on us.userid = act.userid_owner
					LEFT JOIN acttype acty ON act.acttypeid = acty.acttypeid 
					LEFT JOIN sites st ON act.siteid = st.siteid 
					LEFT JOIN accounts acc ON st.accountid = acc.accountid 
					LEFT JOIN zones zs ON acc.zoneid = zs.zoneid 
					LEFT JOIN areazone ae ON zs.zoneid = ae.zoneid 
					LEFT JOIN areas ars ON ae.areaid = ars.areaid
					WHERE act.acttypeid = 1 AND ars.areaid=8 AND us.userid IN (3,6,13,16,19,44,45)
					AND (act.reportdate between '$opendate 00:00:00' and '$closedate 23:59:59')
					AND ( act.opendate between '$opendate 00:00:00' and '$closedate 23:59:59')
					GROUP BY st.sitename
					ORDER BY total DESC
					LIMIT 3";
			$query = $db->query($sql);
			$results = $query->getResultArray();
			$data['incidents_site_tsobarat'] = $results;
			// Menu Table TOP 3 SITES WITH INCIDENTS TSS IVD TENGAH
			$sql = "SELECT act.siteid ,st.sitename, acty.fulltext, COUNT(act.siteid) AS total, ars.description
					FROM activities act 
					LEFT JOIN users us on us.userid = act.userid_owner
					LEFT JOIN acttype acty ON act.acttypeid = acty.acttypeid 
					LEFT JOIN sites st ON act.siteid = st.siteid 
					LEFT JOIN accounts acc ON st.accountid = acc.accountid 
					LEFT JOIN zones zs ON acc.zoneid = zs.zoneid 
					LEFT JOIN areazone ae ON zs.zoneid = ae.zoneid 
					LEFT JOIN areas ars ON ae.areaid = ars.areaid
					AND (act.reportdate between '$opendate 00:00:00' and '$closedate 23:59:59')
					AND ( act.opendate between '$opendate 00:00:00' and '$closedate 23:59:59')
					WHERE act.acttypeid = 1 AND ars.areaid=9 AND us.userid IN (10,11,18,20)
					GROUP BY st.sitename
					ORDER BY total DESC
					LIMIT 3";
			$query = $db->query($sql);
			$results = $query->getResultArray();
			$data['incidents_site_tsotengah'] = $results;
			// Menu Table TOP 3 SITES WITH INCIDENTS TSS IVD TIMUR
			$sql = "SELECT act.siteid ,st.sitename, acty.fulltext, COUNT(act.siteid) AS total, ars.description
					FROM activities act 
					LEFT JOIN users us on us.userid = act.userid_owner
					LEFT JOIN acttype acty ON act.acttypeid = acty.acttypeid 
					LEFT JOIN sites st ON act.siteid = st.siteid
					LEFT JOIN accounts acc ON st.accountid = acc.accountid 
					LEFT JOIN zones zs ON acc.zoneid = zs.zoneid 
					LEFT JOIN areazone ae ON zs.zoneid = ae.zoneid 
					LEFT JOIN areas ars ON ae.areaid = ars.areaid				
					WHERE act.acttypeid = 1 AND ars.areaid=10 AND us.userid IN (2,14,17,21,46,47)
					AND (act.reportdate between '$opendate 00:00:00' and '$closedate 23:59:59')
					AND ( act.opendate between '$opendate 00:00:00' and '$closedate 23:59:59')
					GROUP BY st.sitename
					ORDER BY total DESC
					LIMIT 3";
			$query = $db->query($sql);
			$results = $query->getResultArray();
			$data['incidents_site_tsotimur'] = $results;

		}

		return view('dashboard',$data);
	}
	
}
