<?php

//===GET===
//old sql
$sql['get_data_user_by_user_name'] = "
SELECT 
   UserId AS user_id,
   UserName AS user_name,
   RealName AS real_name,
   a.Description AS description,
   Active AS is_active,
   a.GroupId AS group_id,
   GroupName AS group_name,
   unitkerjaId,
   unitkerjaNama,
   userunitkerjaRoleId AS role_id
FROM 
   gtfw_user a
   JOIN gtfw_group b ON b.GroupId = a.GroupId
   LEFT JOIN user_unit_kerja ON UserId = userunitkerjaUserId
   LEFT JOIN unit_kerja_ref ON unitkerjaId = userunitkerjaUnitkerjaId
WHERE
   UserName = %s
";

$sql['get_count_data_unitkerja'] = 
   "SELECT 
	   COUNT(DISTINCT unitkerjaId) AS total 
	FROM
	   unit_kerja_ref 
	   LEFT JOIN 
		  (SELECT 
			 unitkerjaId AS tempUnitId,
			 unitkerjaKode AS tempUnitKode,
			 unitkerjaNama AS tempUnitNama,
			 unitkerjaParentId AS tempParentId 
		  FROM
			 unit_kerja_ref 
		  WHERE unitkerjaParentId = 0) tmpUnitKerja 
		  ON (unitkerjaParentId = tempUnitId) 
	   LEFT JOIN inventarisasi_detail 
		  ON invDetUnitKerja = unitkerjaId 
	WHERE (
		  unitkerjaKode LIKE '%s' 
		  OR unitkerjaKode LIKE '%s'
	   ) 
	   AND (
		  unitkerjaNama LIKE '%s' 
		  OR tempUnitNama LIKE '%s'
	   ) 
	   AND (
		  invDetUnitKerja = '%s' 
		  OR unitkerjaParentId = '%s' 
		  OR '1' = '%s'
	   ) 
 ";

$sql['get_data_unitkerja'] = "
	SELECT 
		   LPAD(
			  (
				 IF(
					tempUnitKode IS NULL,
					unitkerjaKode,
					tempUnitKode
				 )
			  ) * 100,
			  4,
			  '0'
		   ) AS kodesatker,
		   (
			  IF(
				 tempUnitNama IS NULL,
				 unitkerjaNama,
				 tempUnitNama
			  )
		   ) AS satker,
		   (
			  IF(
				 tempUnitId IS NULL,
				 unitkerjaId,
				 unitkerjaId
			  )
		   ) AS id,
		   unitkerjaKode 
		   AS kodeunit,
		   (
			  IF(
				 tempUnitNama IS NULL,
				 unitkerjaNama,
				 unitkerjaNama
			  )
		   ) AS unit,
		   unitkerjaParentId AS parentId 
		FROM
		   unit_kerja_ref 
		   LEFT JOIN 
			  (SELECT 
				 unitkerjaId AS tempUnitId,
				 unitkerjaKode AS tempUnitKode,
				 unitkerjaNama AS tempUnitNama,
				 unitkerjaParentId AS tempParentId 
			  FROM
				 unit_kerja_ref 
			  WHERE unitkerjaParentId = 0) tmpUnitKerja 
			  ON (unitkerjaParentId = tempUnitId) 
		   LEFT JOIN inventarisasi_detail 
			  ON invDetUnitKerja = unitkerjaId 
		WHERE (
			  unitkerjaKode 
			  
			  LIKE '%s' 
			  OR LPAD(
				 (
					IF(
					   tempUnitKode IS NULL,
					   unitkerjaKode,
					   tempUnitKode
					)
				 ) * 100,
				 4,
				 '0'
			  ) LIKE '%s'
		   ) 
		   AND (
			  unitkerjaNama LIKE '%s' 
			  OR tempUnitNama LIKE '%s'
		   ) 
		   AND (
			  invDetUnitKerja = '%s' 
			  OR unitkerjaParentId = '%s' 
			  OR '1' = '%s'
		   ) 
		GROUP BY unitkerjaId 
		ORDER BY kodesatker,
		   id 
	LIMIT %s, %s
";

?>
