<?php

        function login_validate() {
            $timeout = _LOGIN_TIMEOUT;
            $_SESSION["timeout"] = $timeout;
            $_SESSION["expires_by"] =  time() + $timeout;
        }

        function login_check() {
            $exp_time = $_SESSION["expires_by"];
            if (time() < $exp_time) {
                login_validate();
                return true;
            } else {
                unset($_SESSION["timeout"]);
                unset($_SESSION["expires_by"]);
                return false;
            }
        }

        function MsgBox($pesan) {
                echo "<script language='JavaScript'>window.alert('" . $pesan . "');</script>";
        }

        //==== Redirect... Try PHP header redirect, then Java redirect, then try http redirect.:
        function redirect($url) {
                //If headers are sent... do java redirect... if java disabled, do html redirect.
                echo '<script type="text/javascript">';
                echo 'parent.location.href="'.$url.'";';
                echo '</script>';
                echo '<noscript>';
                echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
                echo '</noscript>'; exit;
        }
        //==== End -- Redirect


        function terbilang_eng($number)
	{
		if (($number < 0) || ($number > 999999999999))
		{
			throw new Exception("Number is out of range");
		}

		$Bn = floor($number / 1000000000);  /* Billions (Bite) */
		$number -= $Bn * 1000000000;
		$Gn = floor($number / 1000000);  /* Millions (giga) */
		$number -= $Gn * 1000000;
		$kn = floor($number / 1000);     /* Thousands (kilo) */
		$number -= $kn * 1000;
		$Hn = floor($number / 100);      /* Hundreds (hecto) */
		$number -= $Hn * 100;
		$Dn = floor($number / 10);       /* Tens (deca) */
		$n = $number % 10;               /* Ones */

		$res = "";

		if ($Bn)
		{
			$res .= terbilang_eng($Gn) . " Billion";
		}

		if ($Gn)
		{
			$res .= terbilang_eng($Gn) . " Million";
		}

		if ($kn)
		{
			$res .= (empty($res) ? "" : " ") .
			terbilang_eng($kn) . " Thousand";
		}

		if ($Hn)
		{
			$res .= (empty($res) ? "" : " ") .
			terbilang_eng($Hn) . " Hundred";
		}

		$ones = array("", "One", "Two", "Three", "Four", "Five", "Six",
						"Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",
						"Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eightteen",
						"Nineteen");
		$tens = array("", "", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty",
						"Seventy", "Eigthy", "Ninety");

		if ($Dn || $n)
		{
			if (!empty($res))
			{
				$res .= " and ";
			}

			if ($Dn < 2)
			{
				$res .= $ones[$Dn * 10 + $n];
			}
			else
			{
				$res .= $tens[$Dn];
				if ($n)
				{
					$res .= "-" . $ones[$n];
				}
			}
		}
		if (empty($res))
		{
			$res = "zero";
		}
		return $res;
	}
	function bilang($x) {
		$x = abs($x);
		$angka = array("", "Satu", "Dua", "Tiga", "Empat", "Lima",
						"Enam", "Tujuh", "Delapan", "Sembilan", "Sepuluh", "Sebelas");
		$result = "";
                
		if ($x <12) {
			$result = " ". $angka[$x];
		}
		else if ($x <20) {
			$result = bilang($x - 10). " Belas";
		}
		else if ($x <100) {
			$result = bilang($x/10)." Puluh". bilang($x % 10);
		}
		else if ($x <200) {
			$result = " Seratus" . bilang($x - 100);
		}
		else if ($x <1000) {
			$result = bilang($x/100) . " Ratus" . bilang($x % 100);
		}
		else if ($x <2000) {
			$result = " Seribu" . bilang($x - 1000);
		}
		else if ($x <1000000) {
			$result = bilang($x/1000) . " Ribu" . bilang($x % 1000);
		}
		else if ($x <1000000000) {
			$result = bilang($x/1000000) . " Juta" . bilang($x % 1000000);
		}
		else if ($x <1000000000000) {
			$result = bilang($x/1000000000) . " Milyar" . bilang(fmod($x,1000000000));
		}
		else if ($x <1000000000000000) {
			$result = bilang($x/1000000000000) . " Trilyun" . bilang(fmod($x,1000000000000));
		}
               

		return $result;
	}
        
        function tkoma($x) {
		$x = stristr($x,'.');
                $angka = array("Nol", "Satu", "Dua", "Tiga", "Empat", "Lima",
                                "Enam", "Tujuh", "Delapan", "Sembilan");
                
		$result = "";
                $pjg = strlen($x);
                $pos = 1;
                $y = $x;

                for ($counter = strlen($x); $counter >= 0; $counter--) {
                    if (substr($x, $counter, 1) == 0) {
                        $y = substr($x, 0, $counter);
                    } else {
                        break;
                    }
                }
                $x = $y;

                $pjg = strlen($x);
                
		while ($pos < $pjg) {
                    $char = substr($x, $pos, 1);
                    $pos++;
                    $result .= " " . $angka[$char];
                }

		return $result;
	}
	function terbilang($x) {
		//$style=1;
		if($x<0) {
			$hasil = "minus ". trim(bilang($x));
		}
		else {
                        $poin = trim(tkoma($x));
			$hasil = trim(bilang($x));
		}
		switch ($style) {
			case 1:
                                if ($poin) {
                                    $hasil = strtoupper($hasil) . ' Koma ' . strtoupper($poin);
                                } else {
                                    $hasil = strtoupper($hasil);
                                }
			break;
			case 2:
				if ($poin) {
                                    $hasil = strtolower($hasil) . ' Koma ' . strtolower($poin);
                                } else {
                                    $hasil = strtolower($hasil);
                                }
			break;
			case 3:
				if ($poin) {
                                    $hasil = ucwords($hasil) . ' Koma ' . ucwords($poin);
                                } else {
                                    $hasil = ucwords($hasil);
                                }
			break;
			default:
				if ($poin) {
                                    $hasil = ucfirst($hasil) . ' Koma ' . ucfirst($poin);
                                } else {
                                    $hasil = ucfirst($hasil);
                                }
			break;
		}
		return $hasil;
	}
	function RetRstField($ssql) {
		$rs=mysql_query($ssql) or die( "RetRstField :".$ssql);
		$r= mysql_fetch_array($rs, MYSQL_NUM) ;
		if(isset($r[0]))
			return  js_esc($r[0]);
		else return '';
	}

	function get_rupiah($rp) {
		$str = number_format($rp,2, '.', ',');
		return $str;
	}
	function get_rupiah1($rp) {
		$str = number_format($rp,0, '', ',');
		return $str;
	}

        // Get Security untuk form
	function GetSecurity($id) {
    
                $ssql= "SELECT fbaccess FROM tluser_access WHERE fbaccess =1 and  ftusername = '" . $_SESSION['username'] . "' AND ftsecurity_code = '" . $id . "'";
		$ssql2="SELECT m.ftsecurity_code from (
					SELECT ftsecurity_code,ftmodule_code,ftparent_code,ftsecurity_name from tlsecurity
					union all
					SELECT concat('laporan_',ftreport_group,'_',ftreport_code) as ftsecurity_code,ftreport_group as ftmodule_code, concat('Laporan ',ftreport_group) as ftparent_code,ftreport_name from tlreport
					) m
		where  m.ftsecurity_code= '" . $id . "'";

                // Kalau tlsecurity nya belum ada / exist, diberikan akses lewat dahulu
                if(!RetRstField($ssql2)) {
			return 1;
                }
                else {
                        return RetRstField($ssql);
		}
	}
        
	function GetSecurityApprove($id) {
		$ssql= "SELECT fbaccess FROM tluser_access WHERE fbaccess =1 and  ftusername = '" . $_SESSION['usernameApprove'] . "' AND ftsecurity_code = '" . $id . "'";
		$ssql2="SELECT m.ftsecurity_code from (
					SELECT ftsecurity_code,ftmodule_code,ftparent_code,ftsecurity_name from tlsecurity
					union all
					SELECT concat('laporan_',ftreport_group,'_',ftreport_code) as ftsecurity_code,ftreport_group as ftmodule_code, concat('Laporan ',ftreport_group) as ftparent_code,ftreport_name from tlreport
					) m
		where  m.ftsecurity_code= '" . $id . "'";

		if(!RetRstField($ssql2)) {
			return 1;
		}
		else {
			return RetRstField($ssql);
		}
	}
	function GetsecurityByname($code="") {
		$ssql= "SELECT fbaccess FROM tluser_access WHERE fbaccess =1 and  ftusername = '" . $_SESSION['username'] . "' AND ftsecurity_code = '" . $code . "'";
		$ssql2="select ftsecurity_code from tlsecurity where  ftsecurity_code = '" . $code . "'";
		if(!RetRstField($ssql2)) {
			return '1';
		}
		else {
			if(RetRstField($ssql)=="1") {
				return "1";
			}
			else {
				return "";
			}
		}
	}
    
    function SaveStock($strType='+',$strItemCode='',$strBatchCode,$strWarehouseCode,$strLocationCode,$strExpDate,$intQtyOnHand,$intQtyAvail)
    {
        // STOCK CATEGORY : INV  only  
        $flagInv = RetRstField("SELECT ftitem_code FROM tlstock_hdr WHERE ftstock_category_code = 'inv' and ftitem_code = '{$strItemCode}' ");
        if ($flagInv != '') {    
        
            if($strBatchCode) {
                $strWhereBatch=" AND ftbatch_code = '{$strBatchCode}'";
                $strInsertBatchField=",ftbatch_code";
                $strInsertBatchValue=",{$strBatchCode}";
            }

            // Setting Expired Date
            if(RetRstField("SELECT ftvalue FROM tssys_setting WHERE ftsys_code = 'INV01' ")) {
                $strWhereExpDate=" AND fdexp_date = '{$strExpDate}' AND IFNULL(ftpallet_code,'') = '' ";     
                $strInsertExpDateValue=",'{$strExpDate}',''";                
            // Setting Pallet Code    
            } else if(RetRstField("SELECT ftvalue FROM tssys_setting WHERE ftsys_code = 'INV02' ")) {
                $strWhereExpDate=" AND IFNULL(fdexp_date,'') = '' AND ftpallet_code = '{$strExpDate}' ";           
                $strInsertExpDateValue=",'','{$strExpDate}'";
            // Else
            } else {
                $strWhereExpDate=" AND IFNULL(fdexp_date,'') = '' AND IFNULL(ftpallet_code,'') = '' ";                        
                $strInsertExpDateValue=",'',''";
            }    
            $strInsertExpDateField=",fdexp_date,ftpallet_code";

            $strWhere="WHERE  ftitem_code = '{$strItemCode}' {$strWhereExpDate} {$strWhereBatch}  AND ftwarehouse_code = '{$strWarehouseCode}' AND ftlocation_code = '{$strLocationCode}' ";
            //echo $strWhere;
            //die("");
            $rs=mysql_query("SELECT fnid FROM tlstock_det {$strWhere} ")    ;
            if(mysql_num_rows($rs)) {
                $ssql=" UPDATE tlstock_det
                        SET fnqty_available = IFNULL(fnqty_available,0) {$strType} {$intQtyAvail},
                            ftmodified_user ='{$_SESSION[username]}', fdmodified_date = Now()
                        WHERE ftbranch_code = '{$_SESSION[strbranch]}' AND ftitem_code = '{$strItemCode}'
                            AND ftwarehouse_code = '{$strWarehouseCode}' AND ftlocation_code = '{$strLocationCode}'
                            {$strWhereExpDate} {$strWhereBatch}";
                $strSQL['stock '.$strType][]=$ssql;
                echo $ssql;

            } else {
                $ssql=" INSERT INTO tlstock_det
                        (
                            ftbranch_code, ftitem_code {$strInsertBatchField},
                            ftwarehouse_code, ftlocation_code {$strInsertExpDateField}, 
                            fnqty_on_hand, fnqty_available, ftcreated_user, ftmodified_user,
                            fdcreated_date, fdmodified_date
                        )
                        VALUES
                            ('{$_SESSION[strbranch]}','{$strItemCode}' {$strInsertBatchValue},
                            '{$strWarehouseCode}', '{$strLocationCode}' {$strInsertExpDateValue},
                            0, 0 {$strType} {$intQtyAvail},'{$_SESSION[username]}', '{$_SESSION[username]}',
                            Now(),Now())";
                //mysql_query($ssql);
                //echo $ssql;
                /* $ssql=" UPDATE tlstock_det
                        SET fnqty_available = IFNULL(fnqty_available,0) {$strType} {$intQtyAvail},
                                ftmodified_user ='{$_SESSION[username]}', fdmodified_date = Now()
                        WHERE ftbranch_code = '{$_SESSION[strbranch]}' AND
                                ftitem_code = '{$strItemCode}' AND
                                ftwarehouse_code = '{$strWarehouseCode}' AND
                                ftlocation_code = '{$strLocationCode}' AND
                                fdexp_date = '{$strExpDate}' {$strWhereBatch}"; */
                $strSQL['stock '.$strType][]=$ssql; 
                echo $ssql;            
            }

            if($_REQUEST['DIRECT_EXECUTE_SQL']==1) {
                mysql_query($ssql) or die("SQL ERROR :{$ssql}");
            }
        } else {
            $strSQL['stock '.$strType][]="SELECT 1"; 
        }
        
        return $strSQL;
    }
    
    function Query_SqlArr($arr) {
		mysql_query("BEGIN");
		foreach($arr as $key1 => $value1) {
			foreach($value1 as $key2 => $value2) {
                                // pr("ERROR :".$value2);
				if(!mysql_query( $value2)) {
					mysql_query("ROLLBACK");
					pr("ERROR :".$value2);
					die("<hr>");
                }
                $i++;
            }
        }
        mysql_query("COMMIT");
    }
    
    function getAutoNumber($branch_code,$ftmodule_code,$table_name,$field_name,$thbln) {
        
        $strSql="SELECT * FROM tstransaction_number
				 WHERE ftmodule_code='{$ftmodule_code}'";
        $rs=mysql_query($strSql);
        $r=mysql_fetch_assoc($rs);
       
        if (empty($branch_code)) {
            $branch_code = "";
        }
        else if (strtolower($branch_code)=='pusat') {
            $branch_code = "";
        } else {
            $branch_code = "";
            // $branch_code = $branch_code."/";
        }
        
        // check if $thbln is alphabet (use for master table)
        if(preg_match("/[a-zA-Z]/i", $thbln)){
            $thbln = strtoupper($thbln);
            $left_len = strlen($thbln) + $r['fnlength']; 
            $strSql="SELECT LEFT(UPPER(ifnull(max({$field_name}),'')),{$left_len}) AS fttrans_no
                    FROM {$table_name}
                    WHERE {$field_name} REGEXP BINARY '{$r['ftcode']}{$thbln}[0-9]'";
                    // WHERE {$field_name} REGEXP BINARY '{$branch_code}{$r['ftcode']}{$thbln}[0-9]'";
                   
            // master table dont use branch code        
            $branch_code = "";        
        //  $thbln is date (use for transaction table)     
        } else {
            $thblnx=$thbln;
            $thblnx=CDate($thbln);

            $arr=array();
            $arr=explode("/",$thbln);
            $bln=$arr[1];
            $th=substr($arr[2],2,2);
            $thbln="$th$bln";
            
            $strSql="SELECT ifnull(max({$field_name}),'') AS fttrans_no
                    FROM {$table_name}
                    WHERE {$field_name} LIKE '{$branch_code}{$r['ftcode']}{$thbln}".str_repeat("_",$r['fnlength'])."'";
        
        }
                    
        $rs1 = mysql_query($strSql);
        $r1=mysql_fetch_assoc($rs1);
        if($r1['fttrans_no']=='') {
                $temp = str_repeat("0",$r['fnlength'] - 1);
                $html = $branch_code. $r['ftcode'] .$thbln. $temp . "1";
        } else {
                $tamp = substr($r1['fttrans_no'], -1*$r['fnlength']);
                $tamp = $tamp + 1;
                $temp = str_repeat("0", $r['fnlength']-strlen($tamp)) . $tamp;
                $html = "{$branch_code}{$r['ftcode']}{$thbln}{$temp}";
        }

        return $html;
    }
    
    function getAutoNumberTemp($ftmodule_code,$table_name,$field_name,$thbln,$value) {
        $arr=array();
        $arr=explode("/",$thbln);
        $bln=$arr[1];
        $th=substr($arr[2],2,2);
        $thbln="$th$bln";
        $strSql="SELECT * FROM tstransaction_number
				WHERE ftmodule_code='{$ftmodule_code}'";
        $rs=mysql_query($strSql);
        $r=mysql_fetch_assoc($rs);
        $strSql="SELECT '{$value}' AS fttrans_no ";
        $rs1 = mysql_query($strSql);
        $r1=mysql_fetch_assoc($rs1);
        if($r1['fttrans_no']=='') {
			$temp = str_repeat("0",$r['fnlength'] - 1);
			$html = $r['ftcode'] .$thbln. $temp . "1";
        }
		else {

			$tamp = substr($r1['fttrans_no'], -1*$r['fnlength']);
			$tamp = $tamp + 1;
			$temp = str_repeat("0", $r['fnlength']-strlen($tamp)) . $tamp;
			$html = "{$r['ftcode']}{$thbln}{$temp}";
        }
        return $html;
    }
    function pr($arr) {
        echo "<hr>";
        echo "<pre>";
        echo print_r($arr);
        echo "</pre>";
        echo "<hr>";
    }
    function getDateYmd($date) {
        list($tgl,$bln,$thn) = explode('/', $date);
        if ($tgl == '') {
            return ""; 
        } else {
           return  "{$thn}-{$bln}-{$tgl}";
        }       
    }
    
    function Updatefnindex($table,$where,$order) {
        global $GARR_SQL;
        $rs=mysql_query("SELECT fnid FROM {$table} WHERE {$where} ORDER by {$order}");
        $j=0;
        while($r=mysql_fetch_assoc($rs)) {
             $j++;
             $GARR_SQL["Updatefnindex {$table}"][] ="UPDATE {$table} fnindex='{$j}' WHERE fnid='{$r['fnid']}'";
		}
    }
    function CCur($expression)
    {
		return str_replace(array(',', '"'), '', $expression);
    }
    function CDbl($expression)
    {
		return str_replace(array(',', '"'), '', $expression);
    }
    function CDate($expression)
    {
            list($tgl, $bln, $thn) = explode('/', $expression);
            return ($thn . "-" . $bln . "-" . $tgl);
    }
    function CDateRev($expression)
    {
		list($thn,$bln,$tgl) = explode('-', $expression);
		return ( $tgl. "/" . $bln . "/" . $thn);
    }

    function js_esc($val)
    {
		$val = str_replace("\\", "\\\\", $val);
		$val = str_replace("\r", "\\r", $val);
		$val = str_replace("\n", "\\n", $val);
		$val = str_replace("'", "\\'", $val);
                return str_replace('"', '\\"', $val);
    }
        
	function pageDataLaporan2($form,$search="", $page=0, $order_field="", $order_type="",$total_pages)
	{
		$result="";
		$form=$_REQUEST["form"];
		$limit = 1; 								//how many items to show per page
		if($page)
			$start = ($page - 1) * $limit; 			//first item to display on this page
		else
			$start = 0;								//if no page var is given, set start to 0
		$total_records = $total_pages;

		/* Setup page vars for display. */
		if ($page == 0) $page = 1;					//if no page var is given, default to 1.
		$prev = $page - 1;							//previous page is page - 1
		$next = $page + 1;							//next page is page + 1
		$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.

		$lpm1 = $lastpage - 1;						//last page minus 1
		$path = "query2.php?code=xx";
		if($lastpage > 1)
		{
			$result.="
			<div class=\"pagination\">
			";
			//previous button
			if ($page > 1) {
				$result.="
                                    <a href=\"#\" onClick=\"showListLaporan(document.frmSearch, '{$path}', 'view', {$prev}, '', '{$order_field}', '{$order_type}',{$total_pages});\"><< previous</a>";
			} else {
				$result.="<span class=\"disabled\"><< previous</span>";
			}
			$result.="<select id='selpage' name='selpage' Onchange=\"showListLaporan(document.frmSearch, '{$path}', 'view', this.value, '', '{$order_field}', '{$order_type}',{$total_pages});\">";
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page) {
					$m=" selected ";
				}
				else {
					$m="";
				}
				$result.="<option value='$counter' $m >{$counter}</option>";
			}
			$result.="</select>";

			if($lastpage>$page) {
				$result.="<a href=\"#\" onClick=\"showListLaporan(document.frmSearch, '{$path}', 'view', {$next}, '', '{$order_field}', '{$order_type}',{$total_pages});\">next >> </a>";
			}
			else {
				$result.="<span class=\"disabled\">next >> </span>";
			}


		}
		return $result;
	}

	function pageDataLaporan($form,$search="", $page=0, $order_field="", $order_type="",$total_pages=0)
	{

		$result="";

		$form=$_REQUEST["form"];

                $adjacents = 3;
		//how many items to show per page
                $limit = LIMIT_PAGE_REPORT; 	
                
                $page=$page;
		if($page)
			$start = ($page - 1) * $limit; 			//first item to display on this page
		else
			$start = 0;								//if no page var is given, set start to 0



		$total_records = $total_pages;

		/* Setup page vars for display. */
		if ($page == 0) $page = 1;					//if no page var is given, default to 1.
		$prev = $page - 1;							//previous page is page - 1
		$next = $page + 1;							//next page is page + 1
		$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.

		$lpm1 = $lastpage - 1;						//last page minus 1
		$path = "query2.php?code=xx";



		if($lastpage > 1)
		{
			$result.="
			<div class=\"pagination\">";

			//previous button
			if ($page > 1) {
                            $result.="
                                    <a href=\"#\" onClick=\"showListLaporan(document.frmSearch, '{$path}', 'view', {$prev}, '', '{$order_field}', '{$order_type}',{$total_pages});\"><< previous</a>";
			 } else {
				$result.="<span class=\"disabled\"><< previous</span>";
			 }
			//pages
			if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
			{
				for ($counter = 1; $counter <= $lastpage; $counter++)
				{
                     //$counter1=($counter-1)*LIMIT_PAGE_REPORT+1;
                     //$counter2=$counter1+LIMIT_PAGE_REPORT-1;
                     $counter1=($counter-1)*$limit+1;
                     $counter2=$counter1+$limit-1;
                     if ($counter2>$total_pages) {
                         $counter2=$total_pages;
                     }

                     $counter3="[{$counter1}-{$counter2}]";
					if ($counter == $page) {
						$result.="<span class=\"current\">$counter3</span>";
					 } else {
                         $result.="
                                    <a href=\"#\" onClick=\"showListLaporan(document.frmSearch, '{$path}', 'view', {$counter}, '', '{$order_field}', '{$order_type}',{$total_pages});\">$counter3</a>";

					 }
				}
			}
			elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
			{
				//close to beginning; only hide later pages
				if($page < 1 + ($adjacents * 2))
				{
					for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
					{
                    // $counter1=($counter-1)*LIMIT_PAGE_REPORT+1;
                    // $counter2=$counter1+LIMIT_PAGE_REPORT-1;
                    $counter1=($counter-1)*$limit+1;
                    $counter2=$counter1+$limit-1;
                     if ($counter2>$total_pages) {
                         $counter2=$total_pages;
                     }
                         $counter3="[{$counter1}-{$counter2}]";
						if ($counter == $page) {
							$result.="<span class=\"current\">$counter3</span>";

						 } else {
							   $result.="
                                    <a href=\"#\" onClick=\"showListLaporan(document.frmSearch, '{$path}', 'view', {$counter}, '', '{$order_field}', '{$order_type}',{$total_pages});\">$counter3</a>";

						 }
					}
                    // $counter1=($lpm1-1)*LIMIT_PAGE_REPORT+1;
                    // $counter2=$counter1+LIMIT_PAGE_REPORT-1;
                    $counter1=($lpm1-1)*$limit+1;
                    $counter2=$counter1+$limit-1;
                     if ($counter2>$total_pages) {
                         $counter2=$total_pages;
                     }
                         $counter3="[{$counter1}-{$counter2}]";
					   $result.="...
                    <a href=\"#\" onClick=\"showListLaporan(document.frmSearch,'{$path}' , 'view','{$lpm1}', '', '{$order_field}', '{$order_type}',{$total_pages});\">$counter3</a>";
                    //$counter1=($lastpage-1)*LIMIT_PAGE_REPORT+1;
                    //$counter2=$counter1+LIMIT_PAGE_REPORT-1;
                    $counter1=($lastpage-1)*$limit+1;
                    $counter2=$counter1+$limit-1;

                     if ($counter2>$total_pages) {
                         $counter2=$total_pages;
                     }
                         $counter3="[{$counter1}-{$counter2}]";
                    $result.="
                    <a href=\"#\" onClick=\"showListLaporan(document.frmSearch,'{$path}', 'view',  '{$lastpage}', '', '{$order_field}', '{$order_type}',{$total_pages});\">$counter3</a>";



				}
				//in middle; hide some front and some back
				elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
				{


                    // $counter1=(1-1)*LIMIT_PAGE_REPORT+1;
                    // $counter2=$counter1+LIMIT_PAGE_REPORT-1;
                    $counter1=(1-1)*$limit+1;
                    $counter2=$counter1+$limit-1;

                     if ($counter2>$total_pages) {
                         $counter2=$total_pages;
                     }
                         $counter3="[{$counter1}-{$counter2}]";
						   $result.="
                                    <a href=\"#\" onClick=\"showListLaporan(document.frmSearch, '{$path}', 'view', 1, '', '{$order_field}', '{$order_type}',{$total_pages});\">$counter3</a>";
                    // $counter1=(2-1)*LIMIT_PAGE_REPORT+1;
                    // $counter2=$counter1+LIMIT_PAGE_REPORT-1;
                    $counter1=(2-1)*$limit+1;
                    $counter2=$counter1+$limit-1;
                     if ($counter2>$total_pages) {
                         $counter2=$total_pages;
                     }
                         $counter3="[{$counter1}-{$counter2}]";
                         $result.="
                                    <a href=\"#\" onClick=\"showListLaporan(document.frmSearch, '{$path}', 'view', 2, '', '{$order_field}', '{$order_type}',{$total_pages});\">$counter3</a>";
                                         $result.="...";
					for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
					{
                    // $counter1=($counter-1)*LIMIT_PAGE_REPORT+1;
                    // $counter2=$counter1+LIMIT_PAGE_REPORT-1;
                    $counter1=($counter-1)*$limit+1;
                    $counter2=$counter1+$limit-1;
                     if ($counter2>$total_pages) {
                         $counter2=$total_pages;
                     }
                         $counter3="[{$counter1}-{$counter2}]";
						if ($counter == $page) {
							$result.="<span class=\"current\">$counter3</span>";
						 } else {
						   $result.="
                                    <a href=\"#\" onClick=\"showListLaporan(document.frmSearch, '{$path}', 'view', {$counter}, '', '{$order_field}', '{$order_type}',{$total_pages});\">$counter3</a>";


						}
					}
                    //  $counter1=(1-1)*LIMIT_PAGE_REPORT+1;
                    //  $counter2=$counter1+LIMIT_PAGE_REPORT-1;
                     $counter1=(1-1)*$limit+1;
                     $counter2=$counter1+$limit-1;
                     if ($counter2>$total_pages) {
                         $counter2=$total_pages;
                     }
                         $counter3="[{$counter1}-{$counter2}]";
						   $result.="
                                    <a href=\"#\" onClick=\"showListLaporan(document.frmSearch, '{$path}', 'view', 1, '', '{$order_field}', '{$order_type}',{$total_pages});\">$counter3</a>";
                    // $counter1=(2-1)*LIMIT_PAGE_REPORT+1;
                    // $counter2=$counter1+LIMIT_PAGE_REPORT-1;
                    $counter1=(2-1)*$limit+1;
                    $counter2=$counter1+$limit-1;
                     if ($counter2>$total_pages) {
                         $counter2=$total_pages;
                     }
                         $counter3="[{$counter1}-{$counter2}]";
                         $result.="
                                    <a href=\"#\" onClick=\"showListLaporan(document.frmSearch, '{$path}', 'view', 2, '', '{$order_field}', '{$order_type}',{$total_pages});\">$counter3</a>";
                                         $result.="...";


				}
				//close to end; only hide early pages
				else
				{
                        // $counter1=(1-1)*LIMIT_PAGE_REPORT+1;
                        // $counter2=$counter1+LIMIT_PAGE_REPORT-1;
                        $counter1=(1-1)*$limit+1;
                        $counter2=$counter1+$limit-1;
                         if ($counter2>$total_pages) {
                             $counter2=$total_pages;
                         }
                         $counter3="[{$counter1}-{$counter2}]";
						 $result.="
                                    <a href=\"#\" onClick=\"showListLaporan(document.frmSearch, '{$path}', 'view', 1, '', '{$order_field}', '{$order_type}',{$total_pages});\">$counter3</a>";
                         // $counter1=(2-1)*LIMIT_PAGE_REPORT+1;
                         // $counter2=$counter1+LIMIT_PAGE_REPORT-1;
                         $counter1=(2-1)*$limit+1;
                         $counter2=$counter1+$limit-1;
                         if ($counter2>$total_pages) {
                             $counter2=$total_pages;
                         }
                         $counter3="[{$counter1}-{$counter2}]";
                         $result.="
                                    <a href=\"#\" onClick=\"showListLaporan(document.frmSearch, '{$path}', 'view', 2, '', '{$order_field}', '{$order_type}',{$total_pages});\">$counter3</a>";
                                         $result.="...";

					for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
					{
                        // $counter1=($counter-1)*LIMIT_PAGE_REPORT+1;
                        // $counter2=$counter1+LIMIT_PAGE_REPORT-1;
                         $counter1=($counter-1)*$limit+1;
                         $counter2=$counter1+$limit-1;
                         if ($counter2>$total_pages) {
                             $counter2=$total_pages;
                         }
                         $counter3="[{$counter1}-{$counter2}]";
						if ($counter == $page) {
							$result.="<span class=\"current\">$counter3</span>";
						 } else {
							   $result.="
                                    <a href=\"#\" onClick=\"showListLaporan(document.frmSearch, '{$path}', 'view', {$counter}, '', '{$order_field}', '{$order_type}',{$total_pages});\">$counter3</a>";

						 }
					}
				}
			}

			//next button
			if ($page < $counter - 1) {
				$result.="<a href=\"#\" onClick=\"showListLaporan(document.frmSearch, '$path', 'view', $next, '', '$order_field', '$order_type',{$total_pages});\">next >></a>";
			} else {
				$result.="<span class=\"disabled\">next >></span>
                </div>";
			}
		}
		return $result;
	}

    //=======================================================
    //      CEK BARANG DETAIL INVENTORY
    //=======================================================
    function  CekDetail_inventory($strItemCode='',$strBatchCode,$strWarehouseCode,$strLocationCode,$strExpDate) {

        if($strBatchCode) {
            $strWhereBatch="AND ftbatch_code = '{$strBatchCode}'";
            $strInsertBatchField=",ftbatch_code";
            $strInsertBatchValue=",{$strBatchCode}";
        }
        $strWhere="WHERE  ftitem_code = '{$strItemCode}' {$strWhereBatch}  AND ftwarehouse_code = '{$strWarehouseCode}' AND ftlocation_code = '{$strLocationCode}' AND fdexp_date = '{$strExpDate}'";
        $rs=mysql_query("SELECT fnid FROM tlstock_det {$strWhere} ")    ;

        if(mysql_num_rows($rs)) {

        }
        else {
            $ssql="INSERT INTO tlstock_det
                                  (ftbranch_code, ftitem_code {$strInsertBatchField},
                                  ftwarehouse_code, ftlocation_code, fdexp_date, fnqty_on_hand,
                                  fnqty_available, ftcreated_user, ftmodified_user)
                                  VALUES
                                  ('{$_SESSION[strbranch]}','{$strItemCode}' {$strInsertBatchValue},
                                    '{$strWarehouseCode}', '{$strLocationCode}', '{$strExpDate}',0,
                                    0,'{$_SESSION[username]}', '{$_SESSION[username]}')";

            mysql_query($ssql);
        }
        return $strSQL;
    }

    //=======================================================
    //      CEK BARANG DETAIL BAHAN BAKU
    //=======================================================
    function  CekDetail_bahan_baku($strItemCode='',$strBatchCode,$strWarehouseCode,$strLocationCode,$strExpDate) {

        if($strBatchCode) {
            $strWhereBatch="AND ftbatch_code = '{$strBatchCode}'";
            $strInsertBatchField=",ftbatch_code";
            $strInsertBatchValue=",{$strBatchCode}";
        }
        $strWhere="WHERE  ftitem_code = '{$strItemCode}' {$strWhereBatch}  AND ftwarehouse_code = '{$strWarehouseCode}' AND ftlocation_code = '{$strLocationCode}' AND fdexp_date = '{$strExpDate}'";
        $rs=mysql_query("SELECT fnid FROM tlstock_det_bahan_baku {$strWhere} ")    ;

        if(mysql_num_rows($rs)) {

        }
        else {
            $ssql="INSERT INTO tlstock_det_bahan_baku
                                  (ftbranch_code, ftitem_code {$strInsertBatchField},
                                  ftwarehouse_code, ftlocation_code, fdexp_date, fnqty_on_hand,
                                  fnqty_available, ftcreated_user, ftmodified_user)
                                  VALUES
                                  ('{$_SESSION[strbranch]}','{$strItemCode}' {$strInsertBatchValue},
                                    '{$strWarehouseCode}', '{$strLocationCode}', '{$strExpDate}',0,
                                    0,'{$_SESSION[username]}', '{$_SESSION[username]}')";

            mysql_query($ssql);
        }
        return $strSQL;
    }

    //=======================================================
    //      CEK BARANG DETAIL SETENGAH JADI
    //=======================================================
    function  CekDetail_setengah_jadi($strItemCode='',$strBatchCode,$strWarehouseCode,$strLocationCode,$strExpDate) {

        if($strBatchCode) {
            $strWhereBatch="AND ftbatch_code = '{$strBatchCode}'";
            $strInsertBatchField=",ftbatch_code";
            $strInsertBatchValue=",{$strBatchCode}";
        }
        $strWhere="WHERE  ftitem_code = '{$strItemCode}' {$strWhereBatch}  AND ftwarehouse_code = '{$strWarehouseCode}' AND ftlocation_code = '{$strLocationCode}' AND fdexp_date = '{$strExpDate}'";
        $rs=mysql_query("SELECT fnid FROM tlstock_det_setengah_jadi {$strWhere} ")    ;

        if(mysql_num_rows($rs)) {

        }
        else {
            $ssql="INSERT INTO tlstock_det_setengah_jadi
                                  (ftbranch_code, ftitem_code {$strInsertBatchField},
                                  ftwarehouse_code, ftlocation_code, fdexp_date, fnqty_on_hand,
                                  fnqty_available, ftcreated_user, ftmodified_user)
                                  VALUES
                                  ('{$_SESSION[strbranch]}','{$strItemCode}' {$strInsertBatchValue},
                                    '{$strWarehouseCode}', '{$strLocationCode}', '{$strExpDate}',0,
                                    0,'{$_SESSION[username]}', '{$_SESSION[username]}')";

            mysql_query($ssql);
        }
        return $strSQL;
    }


    //=======================================================
    //      CEK BARANG DETAIL SPAREPART
    //=======================================================
    function  CekDetail_sparepart($strItemCode='',$strBatchCode,$strWarehouseCode,$strLocationCode,$strExpDate) {

        if($strBatchCode) {
            $strWhereBatch="AND ftbatch_code = '{$strBatchCode}'";
            $strInsertBatchField=",ftbatch_code";
            $strInsertBatchValue=",{$strBatchCode}";
        }
        $strWhere="WHERE  ftitem_code = '{$strItemCode}' {$strWhereBatch}  AND ftwarehouse_code = '{$strWarehouseCode}' AND ftlocation_code = '{$strLocationCode}' AND fdexp_date = '{$strExpDate}'";
        $rs=mysql_query("SELECT fnid FROM tlstock_det_sparepart {$strWhere} ")    ;

        if(mysql_num_rows($rs)) {

        }
        else {
            $ssql="INSERT INTO tlstock_det_sparepart
                                  (ftbranch_code, ftitem_code {$strInsertBatchField},
                                  ftwarehouse_code, ftlocation_code, fdexp_date, fnqty_on_hand,
                                  fnqty_available, ftcreated_user, ftmodified_user)
                                  VALUES
                                  ('{$_SESSION[strbranch]}','{$strItemCode}' {$strInsertBatchValue},
                                    '{$strWarehouseCode}', '{$strLocationCode}', '$strExpDate',0,
                                    0,'{$_SESSION[username]}', '{$_SESSION[username]}')";

            mysql_query($ssql);
        }
        return $strSQL;
    }

        //=======================================================
        //      BAHAN BAKU
        //=======================================================
        function  SaveStock_bahan_baku($strType='+',$strItemCode='',$strBatchCode,$strWarehouseCode,$strLocationCode,$strExpDate,$intQtyOnHand,$intQtyAvail) {

        if($strBatchCode) {
            $strWhereBatch="AND ftbatch_code = '{$strBatchCode}'";
            $strInsertBatchField=",ftbatch_code";
            $strInsertBatchValue=",{$strBatchCode}";
        }
        $strWhere="WHERE  ftitem_code = '{$strItemCode}' {$strWhereBatch}  AND ftwarehouse_code = '{$strWarehouseCode}' AND ftlocation_code = '{$strLocationCode}' AND fdexp_date = '{$strExpDate}'";
        $rs=mysql_query("SELECT fnid FROM tlstock_det_bahan_baku {$strWhere} ")    ;

        if(mysql_num_rows($rs)) {
            $ssql="UPDATE tlstock_det_bahan_baku
                    SET fnqty_available = IFNULL(fnqty_available,0) {$strType} {$intQtyAvail},
                        ftmodified_user ='{$_SESSION[username]}', fdmodified_date = Now()
                    WHERE ftbranch_code = '{$_SESSION[strbranch]}' AND
                          ftitem_code = '{$strItemCode}' AND
                          ftwarehouse_code = '{$strWarehouseCode}' AND
                          ftlocation_code = '{$strLocationCode}' AND
                          fdexp_date = '{$strExpDate}' {$strWhereBatch}";
                  
            if($_REQUEST['DIRECT_EXECUTE_SQL']==1) {
                mysql_query($ssql) or die("SQL ERROR :{$ssql}");
            }

            $strSQL['stock '.$strType][]=$ssql;
        }
        else {
            $ssql="INSERT INTO tlstock_det_bahan_baku
                                    (ftbranch_code, ftitem_code {$strInsertBatchField},
                                    ftwarehouse_code, ftlocation_code, fdexp_date, fnqty_on_hand,
                                    fnqty_available, ftcreated_user, ftmodified_user,
                                    fdcreated_date, fdmodified_date)
                                  VALUES
                                  ('{$_SESSION[strbranch]}','{$strItemCode}' {$strInsertBatchValue},
                                    '{$strWarehouseCode}', '{$strLocationCode}', '{$strExpDate}',0,
                                    0,'{$_SESSION[username]}', '{$_SESSION[username]}',
                                    now(), now())";

             mysql_query($ssql);

              $ssql="UPDATE tlstock_det_bahan_baku
                                        SET
                                            fnqty_available = IFNULL(fnqty_available,0) {$strType} {$intQtyAvail},
                                            ftmodified_user ='{$_SESSION[username]}', fdmodified_date = Now() 
                                        WHERE ftbranch_code = '{$_SESSION[strbranch]}' AND
                                              ftitem_code = '{$strItemCode}' AND
                                              ftwarehouse_code = '{$strWarehouseCode}' AND
                                              ftlocation_code = '{$strLocationCode}' AND
                                              fdexp_date = '{$strExpDate}' {$strWhereBatch}";

                if($_REQUEST['DIRECT_EXECUTE_SQL']==1) {
                    mysql_query($ssql) or die("SQL ERROR :{$ssql}");
                }

                     $strSQL['stock '.$strType][]=$ssql;

        }
//       pr($ssql);die("mmm");

        return $strSQL;
    }

        //=======================================================
        //      SETENGAH JADI
        //=======================================================
        function  SaveStock_setengah_jadi($strType='+',$strItemCode='',$strBatchCode,$strWarehouseCode,$strLocationCode,$strExpDate,$intQtyOnHand,$intQtyAvail) {

        if($strBatchCode) {
            $strWhereBatch="AND ftbatch_code = '{$strBatchCode}'";
            $strInsertBatchField=",ftbatch_code";
            $strInsertBatchValue=",{$strBatchCode}";
        }
        $strWhere="WHERE  ftitem_code = '{$strItemCode}' {$strWhereBatch}  AND ftwarehouse_code = '{$strWarehouseCode}' AND ftlocation_code = '{$strLocationCode}' AND fdexp_date = '{$strExpDate}'";
        $rs=mysql_query("SELECT fnid FROM tlstock_det_setengah_jadi {$strWhere} ")    ;

        if(mysql_num_rows($rs)) {
            $ssql="UPDATE tlstock_det_setengah_jadi
                                        SET fnqty_available = IFNULL(fnqty_available,0) {$strType} {$intQtyAvail},
                                            ftmodified_user ='{$_SESSION[username]}', fdmodified_date = Now()
                                        WHERE ftbranch_code = '{$_SESSION[strbranch]}' AND
                                              ftitem_code = '{$strItemCode}' AND
                                              ftwarehouse_code = '{$strWarehouseCode}' AND
                                              ftlocation_code = '{$strLocationCode}' AND
                                              fdexp_date = '{$strExpDate}' {$strWhereBatch}";
             $strSQL['stock '.$strType][]=$ssql;
              if($_REQUEST['DIRECT_EXECUTE_SQL']==1) {
                    mysql_query($ssql) or die("SQL ERROR :{$ssql}");
               }
        }
        else {
            $ssql="INSERT INTO tlstock_det_setengah_jadi
                                  (ftbranch_code, ftitem_code {$strInsertBatchField},
                                  ftwarehouse_code, ftlocation_code, fdexp_date,
                                  fnqty_available, ftcreated_user, ftmodified_user,
                                  fdcreated_date, fdmodified_date)
                                  VALUES
                                  ('{$_SESSION[strbranch]}','{$strItemCode}' {$strInsertBatchValue},
                                    '{$strWarehouseCode}', '{$strLocationCode}', '{$strExpDate}'
                                    0,'{$_SESSION[username]}', '{$_SESSION[username]}',
                                    now(), now())";

             mysql_query($ssql);

              $ssql="UPDATE tlstock_det_setengah_jadi
                                        SET fnqty_available = IFNULL(fnqty_available,0) {$strType} {$intQtyAvail},
                                            ftmodified_user ='{$_SESSION[username]}', fdmodified_date = Now()
                                        WHERE ftbranch_code = '{$_SESSION[strbranch]}' AND
                                              ftitem_code = '{$strItemCode}' AND
                                              ftwarehouse_code = '{$strWarehouseCode}' AND
                                              ftlocation_code = '{$strLocationCode}' AND
                                              fdexp_date = '{$strExpDate}' {$strWhereBatch}";
                $strSQL['stock '.$strType][]=$ssql;
                if($_REQUEST['DIRECT_EXECUTE_SQL']==1) {
                    mysql_query($ssql) or die("SQL ERROR :{$ssql}");
                }
        }
       //pr($ssql);die("mmm");

        return $strSQL;
    }

        //=======================================================
        //      SPAREPART
        //=======================================================
        function  SaveStock_sparepart($strType='+',$strItemCode='',$strBatchCode,$strWarehouseCode,$strLocationCode,$strExpDate,$intQtyOnHand,$intQtyAvail) {

        if($strBatchCode) {
            $strWhereBatch="AND ftbatch_code = '{$strBatchCode}'";
            $strInsertBatchField=",ftbatch_code";
            $strInsertBatchValue=",{$strBatchCode}";
        }
        $strWhere="WHERE  ftitem_code = '{$strItemCode}' {$strWhereBatch}  AND ftwarehouse_code = '{$strWarehouseCode}' AND ftlocation_code = '{$strLocationCode}' AND fdexp_date = '{$strExpDate}'";
        $rs=mysql_query("SELECT fnid FROM tlstock_det_sparepart {$strWhere} ")    ;

        if(mysql_num_rows($rs)) {
            $ssql="UPDATE tlstock_det_sparepart
                                        SET
                                            fnqty_available = IFNULL(fnqty_available,0) {$strType} {$intQtyAvail},
                                            ftmodified_user ='{$_SESSION[username]}', fdmodified_date = Now()
                                        WHERE ftbranch_code = '{$_SESSION[strbranch]}' AND
                                              ftitem_code = '{$strItemCode}' AND
                                              ftwarehouse_code = '{$strWarehouseCode}' AND
                                              ftlocation_code = '{$strLocationCode}' AND
                                              fdexp_date = '{$strExpDate}'  {$strWhereBatch}";
             $strSQL['stock '.$strType][]=$ssql;
              if($_REQUEST['DIRECT_EXECUTE_SQL']==1) {
                    mysql_query($ssql) or die("SQL ERROR :{$ssql}");
               }
        }
        else {
            $ssql="INSERT INTO tlstock_det_sparepart
                                  (ftbranch_code, ftitem_code {$strInsertBatchField},
                                  ftwarehouse_code, ftlocation_code, fdexp_date, fnqty_on_hand,
                                  fnqty_available, ftcreated_user, ftmodified_user,
                                  fdcreated_date, fdmodified_date)
                                  VALUES
                                  ('{$_SESSION[strbranch]}','{$strItemCode}' {$strInsertBatchValue},
                                    '{$strWarehouseCode}', '{$strLocationCode}', '{$strExpDate}',0,
                                    0,'{$_SESSION[username]}', '{$_SESSION[username]}',
                                    now(), now())";

             mysql_query($ssql);

              $ssql="UPDATE tlstock_det_sparepart
                                        SET fnqty_available = IFNULL(fnqty_available,0) {$strType} {$intQtyAvail},
                                            ftmodified_user ='{$_SESSION[username]}', fdmodified_date = Now()
                                        WHERE ftbranch_code = '{$_SESSION[strbranch]}' AND
                                              ftitem_code = '{$strItemCode}' AND
                                              ftwarehouse_code = '{$strWarehouseCode}' AND
                                              ftlocation_code = '{$strLocationCode}' AND
                                              fdexp_date = '{$strExpDate}'  {$strWhereBatch}";
                $strSQL['stock '.$strType][]=$ssql;
                if($_REQUEST['DIRECT_EXECUTE_SQL']==1) {
                    mysql_query($ssql) or die("SQL ERROR :{$ssql}");
                }
        }
      # pr($ssql);die("mmm");

        return $strSQL;
    }

//==============================================================================
 function datediff($interval, $datefrom, $dateto, $using_timestamps = false) {
     /*
         $interval can be:
         yyyy - Number of full years
         q - Number of full quarters
         m - Number of full months
         y - Difference between day numbers
         (eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".)
         d - Number of full days
         w - Number of full weekdays
         ww - Number of full weeks
         h - Number of full hours
         n - Number of full minutes
         s - Number of full seconds (default)
     */

     if (!$using_timestamps) {
         $datefrom = strtotime($datefrom, 0);
         $dateto = strtotime($dateto, 0);
     }
     $difference = $dateto - $datefrom; // Difference in seconds

     switch($interval) {

     case 'yyyy': // Number of full years
         $years_difference = floor($difference / 31536000);
         if (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom), date("j", $datefrom), date("Y", $datefrom)+$years_difference) > $dateto) {
            $years_difference--;
         }
         if (mktime(date("H", $dateto), date("i", $dateto), date("s", $dateto), date("n", $dateto), date("j", $dateto), date("Y", $dateto)-($years_difference+1)) > $datefrom) {
            $years_difference++;
         }
         $datediff = $years_difference;
     break;
     case "q": // Number of full quarters
         $quarters_difference = floor($difference / 8035200);
         while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($quarters_difference*3), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
         $months_difference++;
         }
         $quarters_difference--;
         $datediff = $quarters_difference;
     break;

     case "m": // Number of full months
         $months_difference = floor($difference / 2678400);
         while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($months_difference), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
         $months_difference++;
         }
         $months_difference--;
         $datediff = $months_difference;
     break;
     case 'y': // Difference between day numbers
        $datediff = date("z", $dateto) - date("z", $datefrom);
     break;
     case "d": // Number of full days
        $datediff = floor($difference / 86400);
     break;
     case "w": // Number of full weekdays
         $days_difference = floor($difference / 86400);
         $weeks_difference = floor($days_difference / 7); // Complete weeks
         $first_day = date("w", $datefrom);
         $days_remainder = floor($days_difference % 7);
         $odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder?
         if ($odd_days > 7) { // Sunday
            $days_remainder--;
         }
         if ($odd_days > 6) { // Saturday
            $days_remainder--;
         }
         $datediff = ($weeks_difference * 5) + $days_remainder;
     break;

     case "ww": // Number of full weeks
        $datediff = floor($difference / 604800);
     break;

     case "h": // Number of full hours
        $datediff = floor($difference / 3600);
     break;

     case "n": // Number of full minutes
        $datediff = floor($difference / 60);
     break;

     default: // Number of full seconds (default)
         $datediff = $difference;
     break;
     }

     return $datediff;

 }

function GetID() {
    switch($_REQUEST['mode']) {
        case "simpan_bank":
        case "simpan_penerimaan":
        case "simpan":
        case "update":
        case "final":
            switch($_REQUEST["form"]) {
                // form dimasukkan ke case apabila setelah simpan mau kembali ke form detailnya
                // form jangan dimasukkan ke case apabila setelah simpan mau buka transaksi baru
                /* akunting */
                case "penyusutan_asset":
                     $ssql=" SELECT fnid FROM txdepriciation_hdr WHERE fttrans_code='{$_REQUEST['fttrans_code']}'";
                     break;
                  /* absensi */
                case "absensi":
                     $ssql=" SELECT fnid FROM txattendance_hdr WHERE ftattendance_no='{$_REQUEST['ftattendance_no']}'";
                     break;
                case "perhitungan_lembur":
                     $ssql=" SELECT fnid FROM txovertime_hdr WHERE ftovertime_no='{$_REQUEST['ftovertime_no']}'";
                     break;
                  /*hrd*/
                case "cuti":
                     $ssql="SELECT fnid FROM txleave WHERE ftleave_no='{$_REQUEST['ftleave_no']}'";
                     break;
                case "pembayaran_pinjaman":
                     $ssql="SELECT fnid FROM txloan_payment_hdr WHERE ftloan_payment_no='{$_REQUEST['ftloan_payment_no']}'";
                     break;
                case "pembayaran_uang_lembur":
                     $ssql="SELECT fnid FROM txovertime_money_hdr WHERE ftovertime_money_no='{$_REQUEST['ftovertime_money_no']}'";
                     break;
                case "pinjaman":
                     $ssql="SELECT fnid FROM txloan WHERE ftloan_no='{$_REQUEST['ftloan_no']}'";
                     break;
                case "slip_gaji":
                     $ssql="SELECT fnid FROM txpayroll_slip_hdr WHERE ftpayroll_slip_no='{$_REQUEST['ftpayroll_slip_no']}'";
                     break;
                case "thr":
                     $ssql="SELECT fnid FROM txthr_hdr WHERE ftthr_no='{$_REQUEST['ftthr_no']}'";
                     break;
                case "uang_makan":
                     $ssql="SELECT fnid FROM txfood_money_hdr WHERE ftfood_money_no='{$_REQUEST['ftfood_money_no']}'";
                     break;
                case "pegawai_update2":
                     $ssql="SELECT fnid FROM thshift_change_hdr WHERE fttrans_no='{$_REQUEST['fttrans_no']}'";
                     break;

                /*inventory */
                case "barang_keluar":
                     $ssql="SELECT fnid FROM txstock_out_hdr WHERE ftstock_out_no='{$_REQUEST['ftstock_out_no']}'";
                     break;
                case "barang_masuk":
                     $ssql="SELECT fnid FROM txstock_in_hdr WHERE ftstock_in_no='{$_REQUEST['ftstock_in_no']}'";
                     break;
                case "mutasi_barang":
                     $ssql="SELECT fnid FROM txstock_mutation_hdr WHERE ftstock_mutation_no='{$_REQUEST['ftstock_mutation_no']}'";
                     break;
                case "penyesuaian_barang":
                     $ssql="SELECT fnid FROM txstock_adjustment_hdr WHERE ftstock_adjustment_no='{$_REQUEST['ftstock_adjustment_no']}'";
                     break;
                case "assembly":
                     $ssql="SELECT fnid FROM txassembly_hdr WHERE ftassembly_no='{$_REQUEST['ftassembly_no']}'";
                     break;
                case "disassembly":
                     $ssql="SELECT fnid FROM txdisassembly_hdr WHERE ftdisassembly_no='{$_REQUEST['ftdisassembly_no']}'";
                     break;
                
                /*inventory bahan baku */
                case "inventorybb_stock_keluar":
                     $ssql="SELECT fnid FROM txstock_out_hdr_bahan_baku WHERE ftstock_out_no='{$_REQUEST['ftstock_out_no']}'";
                     break;
                case "inventorybb_stock_masuk":
                     $ssql="SELECT fnid FROM txstock_in_hdr WHERE ftstock_in_no='{$_REQUEST['ftstock_in_no']}' and ftstock_type_code='invbb'";
                     break;
                case "inventorybb_stock_mutasi":
                     $ssql="SELECT fnid FROM txstock_mutation_hdr_bahan_baku WHERE ftstock_mutation_no='{$_REQUEST['ftstock_mutation_no']}'";
                     break;
                case "inventorybb_stock_penyesuaian":
                     $ssql="SELECT fnid FROM txstock_adjustment_hdr_bahan_baku WHERE ftstock_adjustment_no='{$_REQUEST['ftstock_adjustment_no']}'";
                     break;

                //INVENTORY 1/2 JADI
                case "inventorysj_barang_keluar":
                     $ssql="SELECT fnid FROM txstock_out_hdr_setengah_jadi WHERE ftstock_out_no='{$_REQUEST['ftstock_out_no']}'";
                     break;
                case "inventorysj_barang_masuk":
                     $ssql="SELECT fnid FROM txstock_in_hdr WHERE ftstock_in_no='{$_REQUEST['ftstock_in_no']}' and ftstock_type_code='invbb'";
                     break;
                case "inventorysj_mutasi_barang":
                     $ssql="SELECT fnid FROM txstock_mutation_hdr_setengah_jadi WHERE ftstock_mutation_no='{$_REQUEST['ftstock_mutation_no']}'";
                     break;
                case "inventorysj_stock_penyesuaian":
                     $where=" WHERE ftstock_adjustment_no='{$_REQUEST['ftstock_adjustment_no']}'";

                     $ssql="SELECT fnid FROM txstock_adjustment_hdr_setengah_jadi {$where}";
                     break;
               
                //INVENTORY sparepart
                case "inventorysp_barang_keluar":
                     $ssql="SELECT fnid FROM txstock_out_hdr_sparepart WHERE ftstock_out_no='{$_REQUEST['ftstock_out_no']}'";
                     break;
                case "inventorysp_barang_masuk":
                     $ssql="SELECT fnid FROM txstock_in_hdr WHERE ftstock_in_no='{$_REQUEST['ftstock_in_no']}' and ftstock_type_code='invbb'";
                     break;
                case "inventorysp_mutasi_barang":
                     $ssql="SELECT fnid FROM txstock_mutation_hdr_sparepart WHERE ftstock_mutation_no='{$_REQUEST['ftstock_mutation_no']}'";
                     break;
                case "inventorysp_stock_penyesuaian":
                     $where=" WHERE ftstock_adjustment_no='{$_REQUEST['ftstock_adjustment_no']}'";

                     $ssql="SELECT fnid FROM txstock_adjustment_hdr_sparepart {$where}";
                     break;
                
                /*keuangan */
                case "bank_keluar":
                     $ssql="SELECT fnid FROM txbankpym_hdr WHERE ftbankpym_no='{$_REQUEST['ftbankpym_no']}'";
                     break;
                case "bank_masuk":
                     $ssql="SELECT fnid FROM txbankrcp_hdr WHERE ftbankrcp_no='{$_REQUEST['ftbankrcp_no']}'";
                     break;
                case "credit_memo":
                     $ssql=" SELECT fnid FROM txcredit_memo_hdr WHERE ftcm_no='{$_REQUEST['ftcm_no']}'";
                     break;
                case "debit_memo":
                     $ssql="SELECT fnid FROM txdebit_memo_hdr WHERE ftdm_no='{$_REQUEST['ftdm_no']}'";
                     break;
                case "kas_keluar":
                     $ssql="SELECT fnid FROM txcashpym_hdr WHERE ftcashpym_no='{$_REQUEST['ftcashpym_no']}'";
                     break;
                case "kas_masuk":
                     $ssql="SELECT fnid FROM txcashrcp_hdr WHERE ftcashrcp_no='{$_REQUEST['ftcashrcp_no']}'";
                     break;
                case "penerimaan_piutang":
                     $ssql="SELECT fnid FROM txar_hdr WHERE ftar_no='{$_REQUEST['ftar_no']}'";
                     break;
                case "proyeksi_cashflow":
                     $ssql="SELECT fnid FROM txcashflow_projection_hdr WHERE ftcp_no='{$_REQUEST['ftcp_no']}'";
                     break;
                case "proyeksi_pembayaran":
                     $ssql="SELECT fnid FROM txpayment_projection_hdr WHERE ftpp_no='{$_REQUEST['ftpp_no']}'";
                     break;
                case "rekap_bank_voucher":
                     $ssql="SELECT fnid FROM txrecap_bank_voucher_hdr WHERE fntype=0 AND ftcvs_no='{$_REQUEST['ftcvs_no']}'";
                     break;
                case "rekap_bank_voucher_import":
                     $ssql="SELECT fnid FROM txrecap_bank_voucher_hdr WHERE fntype=1 AND ftcvs_no='{$_REQUEST['ftcvs_no']}'";
                     break;
                case "uang_muka_penjualan":
                     $ssql="SELECT fnid FROM txadvanced_receive WHERE fttrans_no='{$_REQUEST['fttrans_no']}'";
                     break;
                case "uang_muka_pembelian":
                     $ssql="SELECT fnid FROM txadvanced_payment WHERE fttrans_no='{$_REQUEST['fttrans_no']}'";
                     break;

                /*pembelian*/
                case "pembelian_purchase_order":
                case "pembelian_purchase_order_local":
                case "pembelian_purchase_order_import":
                case "pembelian_purchase_order_local_jasa":
                case "pembelian_purchase_order_local_jasa2":
                     $ssql="SELECT fnid FROM txpurchase_order_hdr WHERE ftpo_no='{$_REQUEST['ftpo_no']}'";
                     break;
                case "pembelian_purchase_invoice":
                case "pembelian_purchase_invoice_local":
                case "pembelian_purchase_invoice_import":
                     $ssql="SELECT fnid FROM txpurchase_invoice_hdr WHERE ftpurchase_invoice_no='{$_REQUEST['ftpurchase_invoice_no']}'";
                     break;
                case "pembelian_stock_masuk":
                     $ssql="SELECT fnid FROM txstock_in_hdr WHERE ftstock_in_no='{$_REQUEST['ftstock_in_no']}'";
                     break;
                case "pembelian_stock_masuk_jasa":
                     $ssql="SELECT fnid FROM txstock_in_hdr WHERE ftstock_in_no='{$_REQUEST['ftstock_in_no']}'";
                     break;
                case "pembelian_stock_masuk_jasa2":
                     $ssql="SELECT fnid FROM txstock_in_hdr WHERE ftstock_in_no='{$_REQUEST['ftstock_in_no']}'";
                     break;
                case "pembelian_tracking_po":
                     $ssql="SELECT fnid FROM txpurchase_order_hdr WHERE ftpo_no='{$_REQUEST['ftpo_no']}'";
                     break;
                case "return_pembelian_import":
                     $ssql="SELECT fnid FROM txpurchase_return_hdr WHERE ftpurchase_return_no='{$_REQUEST['ftpurchase_return_no']}'";
                     break;
                case "return_pembelian_local":
                     $ssql="SELECT fnid FROM txpurchase_return_hdr WHERE ftpurchase_return_no='{$_REQUEST['ftpurchase_return_no']}'";
                     break;
                case "tanda_terima":
                     $ssql="SELECT fnid FROM txtanda_terima_hdr WHERE fttanda_terima_no='{$_REQUEST['fttanda_terima_no']}'";
                     break;
                case "history_pending_delivery_schedule":
                     $ssql="SELECT fnid FROM txpurchase_order_history_pending_delivery_schedule_hdr WHERE fthistory_pending_delivery_schedule_no='{$_REQUEST['fthistory_pending_delivery_schedule_no']}'";
                     break;

                /*penjualan */
                case "delivery_order":
                     $ssql="SELECT fnid FROM txdelivery_order_hdr WHERE ftdo_no='{$_REQUEST['ftdo_no']}'";
                     break;
                case "sales_invoice":
                     $ssql="SELECT fnid FROM txsales_invoice_hdr WHERE ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}'";
                     break;
                case "sales_order":
                     $ssql="SELECT fnid FROM txsales_order_hdr WHERE ftso_no='{$_REQUEST['ftso_no']}'";
                     break;
                case "sales_return":
                     $ssql="SELECT fnid FROM txsales_return_hdr WHERE ftsales_return_no='{$_REQUEST['ftsales_return_no']}'";
                     break;
                case "rekap_delivery_order":
                     $ssql="SELECT fnid FROM txrekap_delivery_order_hdr WHERE ftrekap_do_no='{$_REQUEST['ftrekap_do_no']}'";
                     break;

                /*preproduksi */
                case "monitoring_por":
                     $ssql="SELECT fnid FROM txproduction_monitoring_por_hdr WHERE ftmpor_no='{$_REQUEST['ftmpor_no']}'";
                     break;
                case "outstanding_mingguan":
                     $ssql="SELECT fnid FROM txproduction_outstanding_weekly_hdr WHERE ftpow_no='{$_REQUEST['ftpow_no']}'";
                     break;
                case "production_order_request":
                     $ssql="SELECT fnid FROM txproduction_order_request_hdr WHERE ftpor_no='{$_REQUEST['ftpor_no']}'";
                     break;
                case "production_order_request_custom":
                     $ssql="SELECT fnid FROM txproduction_order_request_hdr WHERE ftpor_no='{$_REQUEST['ftpor_no']}'";
                     break;
                case "production_order_sheet":
                     $ssql="SELECT fnid FROM txproduction_order_sheet_hdr WHERE ftpos_no='{$_REQUEST['ftpos_no']}'";
                     break;
                case "production_order_sheet_custom":
                     $ssql="SELECT fnid FROM txproduction_order_sheet_hdr WHERE ftpos_no='{$_REQUEST['ftpos_no']}'";
                     break;

                /*saldo awal */
                case "saldoawal_hutang":
                     $ssql="SELECT fnid FROM txpayment_projection_hdr WHERE ftpp_no='{$_REQUEST['ftpp_no']}'";
                     break;
                case "saldoawal_piutang":
                     $ssql="SELECT fnid FROM txsales_invoice_hdr WHERE ftsales_invoice_no='{$_REQUEST['ftsales_invoice_no']}'";
                     break;
                case "saldoawal_stock":
                     $ssql="SELECT fnid FROM txinnitial_stock_hdr WHERE ftstock_in_no='{$_REQUEST['ftstock_in_no']}'";
                     break;
                case "saldoawal_stock_bahan_baku":
                     $ssql="SELECT fnid FROM txinnitial_stock_hdr_bahan_baku WHERE ftstock_in_no='{$_REQUEST['ftstock_in_no']}'";
                     break;
                case "saldoawal_stock_setengah_jadi":
                     $ssql="SELECT fnid FROM txinnitial_stock_hdr_bahan_baku WHERE ftstock_in_no='{$_REQUEST['ftstock_in_no']}'";
                     break;
                case "saldoawal_stock_sparepart":
                     $ssql="SELECT fnid FROM txinnitial_stock_hdr_bahan_baku WHERE ftstock_in_no='{$_REQUEST['ftstock_in_no']}'";
                     break;
                  case "slip_gaji_x":
                     $where=" WHERE ftpayroll_slip_no='{$_REQUEST['ftpayroll_slip_no']}' AND
                               ftemployee_code='{$_REQUEST['ftemployee_code']}' #";
                     $ssql="SELECT fnid FROM txpayroll_slip_emp {$where}";
                     break;
                  case "slip_gaji2_x":
                     $where=" WHERE ftpayroll_slip_no='{$_REQUEST['ftpayroll_slip_no']}' AND
                               ftemployee_code='{$_REQUEST['ftemployee_code']}' #";
                     $ssql="SELECT fnid FROM txpayroll_slip_emp {$where}";
                     break;
                 case "slip_gaji3_x":
                     $where=" WHERE ftpayroll_slip_no='{$_REQUEST['ftpayroll_slip_no']}' AND
                               ftemployee_code='{$_REQUEST['ftemployee_code']}' #";
                     $ssql="SELECT fnid FROM txpayroll_slip_emp {$where}";
                     break;


                 #PPIC
                   case "spp":
                     $where=" WHERE ftspp_no='{$_REQUEST['ftspp_no']}'";

                     $ssql="SELECT fnid FROM txspp_hdr {$where}";
                     break;
                  case "estimasi":
                     $where=" WHERE ftestimation_no='{$_REQUEST['ftestimation_no']}'";

                     $ssql="SELECT fnid FROM txestimation_hdr {$where}";
                     break;
                  case "bom":
                     $where=" WHERE ftitem_code='{$_REQUEST['ftitem_code']}'";

                     $ssql="SELECT fnid FROM tlbom_header {$where}";
                     break;

                 #PRODUKSI
                   case "spk_proses":
                        $where=" WHERE ftspk_no='{$_REQUEST['ftspk_no']}'";
                        $ssql="SELECT fnid FROM txspk_hdr {$where}";
                     break;
                   case "hasil_produksi":
                        //$where=" WHERE ftstock_in_no='{$_REQUEST['ftstock_in_no']}'";
                        //$ssql="SELECT fnid FROM txtstock_in_production_hdr {$where}";
                        $where=" WHERE ftproduction_no='{$_REQUEST['ftproduction_no']}'";
                        $ssql="SELECT fnid FROM txproduction_hdr {$where}";
                     break;
                    case "reject_produksi":
                        $where=" WHERE ftreject_production_department_no='{$_REQUEST['ftreject_production_department_no']}'";
                        $ssql="SELECT fnid FROM txreject_production_hdr {$where}";
                     break;
                    case "giling":
                        $where=" WHERE ftgiling_no='{$_REQUEST['ftgiling_no']}'";
                        $ssql="SELECT fnid FROM txgiling_hdr {$where}";
                     break;
                        case "hasil_giling":
                        $where=" WHERE ftgiling_result_no='{$_REQUEST['ftgiling_result_no']}'";
                        $ssql="SELECT fnid FROM txgiling_result_hdr {$where}";
                     break;
                     case "produksi":
                        $where=" WHERE ftproduction_no='{$_REQUEST['ftproduction_no']}'";
                        $ssql="SELECT fnid FROM txproduction_hdr {$where}";
                     break;
                     case "spb":
                        $where=" WHERE ftspb_no='{$_REQUEST['ftspb_no']}'";
                        $ssql="SELECT fnid FROM txspb_hdr {$where}";
                     break;

                    #IC
                      case "sj_outsource":
                        $where=" WHERE ftdo_no='{$_REQUEST['ftdo_no']}'";
                        $ssql="SELECT fnid FROM txdelivery_order_hdr_outsource {$where}";
                     break;
                      case "stock_in_outsource":
                        $where=" WHERE ftstock_in_return_no='{$_REQUEST['ftstock_in_return_no']}'";
                        $ssql="SELECT fnid FROM txstock_in_return_hdr_outsource {$where}";
                     break;
                      case "sj_service_outsource":
                        $where=" WHERE ftservice_no='{$_REQUEST['ftservice_no']}'";
                        $ssql="SELECT fnid FROM txdelivery_order_hdr_outsource_service {$where}";
                     break;
             }
             break;
          
     }

		if($ssql!="") {
			$id=RetRstField($ssql);
			if($id) {
				return $id;
			}
			else {
				return 0;
			}
		}
		else {
			return 0;
		}
	}

   function ListEmployee($ftpayroll_type_codex,$include_jhtx,$include_tunjanganx="",$salary_plus2x="") {
        //daftar employee
        $ssql_1="/*
            AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
            AND a.fdovertime_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdovertime_date<='".CDate($_REQUEST['fdend_date'])."'
            AND  a.fdtrans_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdtrans_date<='".CDate($_REQUEST['fdend_date'])."'
            AND b1.fnpayroll_slip=1)
        */

        SELECT ax.fnindex AS fnindex, x1.ftemployee_code AS ftemployee_code,
			SUM(x1.fcincome_1)+ifnull(x2.fcincome_2,0) AS fcincome_amount,
			ifnull(x3.fcdeduction_1,0)+ifnull(x5.fcdeduction_2,0)+ifnull(X6.fcdeduction_3,0) AS fcdeduction_amount,
			(ifnull(sum(x1.fcincome_1),0)+ifnull(x2.fcincome_2,0))-(ifnull(x3.fcdeduction_1,0)+ifnull(x5.fcdeduction_2,0)+ifnull(X6.fcdeduction_3,0)) AS fcamount ,
			x1.fncount_attendance_days AS fnattendance_days,IFNULL(x4.fnleave_days,0) AS fnleave_days,IFNULL(x4_1.fnsick_days,0) AS fnsick_days ,
			IFNULL(x4_2.fnalfaizin_days,0) AS fnalfaizin_days
		FROM
			(
				/*===================================
					PENDAPATAN SELAIN LEMBUR
				====================================*/
				SELECT IFNULL(a2.fncount_attendance_days,0) AS  fncount_attendance_days, a1.ftemployee_code,a1.ftemployee_name,
				/*===================================
					( selain lembur)
				=====================================*/
					SUM(CASE WHEN b1.fnsalary_period=0 THEN IFNULL(b1.fcamount,0)*IFNULL(a3.fncount_attendance_hours,0)
							 WHEN b1.fnsalary_period=1 THEN IFNULL(b1.fcamount,0)*IFNULL(a2.fncount_attendance_days,0)
							 WHEN b1.fnsalary_period=2 THEN IFNULL(b1.fcamount,0)*IFNULL(a4.fncount_attendance_weeks,0)
							 WHEN b1.fnsalary_period=3 THEN IFNULL(b1.fcamount,0)*1
							 WHEN b1.fnsalary_period=4 THEN IFNULL(b1.fcamount,0)*1
						ELSE
							0
						END
					) AS fcincome_1

				FROM tlemployee a1

				LEFT JOIN ( /* day */
					SELECT b.ftemployee_code,
						COUNT(*) AS fncount_attendance_days
					FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
					WHERE a.fnstatus=1 AND b.fnemployee_status=0
						AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
						AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
					GROUP BY  b.ftemployee_code
				) a2 ON a1.ftemployee_code=a2.ftemployee_code
				LEFT JOIN ( /* hours */
					SELECT b.ftemployee_code,
						SUM(HOUR(b.ftworking_hours)*3600+MINUTE(b.ftworking_hours)*60+SECOND(b.ftworking_hours)/3600) AS fncount_attendance_hours
					FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
					WHERE a.fnstatus=1 AND b.fnemployee_status=0
						AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
						AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
					GROUP BY  b.ftemployee_code
				) a3 ON a1.ftemployee_code=a3.ftemployee_code
				LEFT JOIN ( /* week */
					SELECT b.ftemployee_code,
						/*COUNT(*)/7*/ 1 AS fncount_attendance_weeks
					FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
					WHERE a.fnstatus=1  AND ( b.fnemployee_status=0 OR (b.fnemployee_status=1 AND b.ftabsen_code='C') OR (b.fnemployee_status=1 AND b.ftabsen_code='S') )
						AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
						AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
					GROUP BY  b.ftemployee_code
				) a4 ON  a1.ftemployee_code=a4.ftemployee_code
				LEFT JOIN tlemployee_salary_inc b1 ON a1.ftemployee_code=b1.ftemployee_code AND b1.ftsalary_category_code<>'A05' AND b1.fnpayroll_slip=1 {$include_tunjanganx}
					{$ftpayroll_type_codex}
				GROUP BY a1.ftemployee_code

				UNION ALL

				SELECT IFNULL(a2.fncount_attendance_days,0) AS  fncount_attendance_days, a1.ftemployee_code,a1.ftemployee_name,	/*
				/*===================================
					( selain lembur)
				=====================================*/
					SUM(CASE WHEN b1.fnsalary_period=0 THEN IFNULL(b1.fcamount,0)*IFNULL(a3.fncount_attendance_hours,0)
							 WHEN b1.fnsalary_period=1 THEN IFNULL(b1.fcamount,0)*IFNULL(a2.fncount_attendance_days,0)
							 WHEN b1.fnsalary_period=2 THEN IFNULL(b1.fcamount,0)*IFNULL(a4.fncount_attendance_weeks,0)
							 WHEN b1.fnsalary_period=3 THEN IFNULL(b1.fcamount,0)*1
							 WHEN b1.fnsalary_period=4 THEN IFNULL(b1.fcamount,0)*1
						ELSE 0 END
					) AS fcincome_1
				FROM tlemployee a1

				LEFT JOIN ( /* day */
					SELECT b.ftemployee_code,
						COUNT(*) AS fncount_attendance_days
					FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
					WHERE a.fnstatus<>1 AND b.fnemployee_status=0
						AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
						AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
					GROUP BY  b.ftemployee_code
				) a2 ON a1.ftemployee_code=a2.ftemployee_code
				LEFT JOIN ( /* hours */
					SELECT b.ftemployee_code,
						SUM(HOUR(b.ftworking_hours)*3600+MINUTE(b.ftworking_hours)*60+SECOND(b.ftworking_hours)/3600) AS fncount_attendance_hours
					FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
					WHERE a.fnstatus<>1 AND b.fnemployee_status=0
						AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
						AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
					GROUP BY  b.ftemployee_code
				) a3 ON a1.ftemployee_code=a3.ftemployee_code
				LEFT JOIN ( /* week */
					SELECT b.ftemployee_code,
						/*COUNT(*)/7*/ 1 AS fncount_attendance_weeks
					FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
					WHERE a.fnstatus<>1  AND ( b.fnemployee_status=0 OR (b.fnemployee_status=1 AND b.ftabsen_code='C') OR (b.fnemployee_status=1 AND b.ftabsen_code='S') )
						AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
						AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
					GROUP BY  b.ftemployee_code
				) a4 ON  a1.ftemployee_code=a4.ftemployee_code

				INNER JOIN tlemployee_salary_inc_plus2 b1  ON (a1.ftemployee_code=b1.ftemployee_code AND b1.ftsalary_category_code<>'A05' AND b1.fnpayroll_slip=1)  {$include_tunjanganx}
					{$salary_plus2x}
				GROUP BY a1.ftemployee_code
			) x1
		LEFT JOIN
			(
                /* =================================
                    PENDAPATAN LEMBUR
                ====================================*/
				SELECT  a1.ftemployee_code,
				/*
                    -------------------------------------------------*/

                            /* (lembur and periode=jam) */
                            B2.fcamount_overtime_periode
                         AS fcincome_2
                FROM tlemployee a1
                LEFT JOIN (
					SELECT  IFNULL(c.ftsalary_code,c_1.ftsalary_code) AS ftsalary_code,b.ftemployee_code,
						SUM(CASE WHEN c_1.fnsalary_period=1 and ff13_hari_libur(a.fdovertime_date,a.ftdivision_code,a.ftdepartment_code)<>0 THEN
									IFNULL(c_1.fcamount,0)
							ELSE
								ROUND( 	CASE WHEN ff13_hari_libur(a.fdovertime_date,a.ftdivision_code,a.ftdepartment_code)=0 THEN
											ff09_get_overtime_biasa(b.ftovertime_hours,IFNULL(c.fcamount,0))
										ELSE
											ff09_get_overtime_libur(b.ftovertime_hours,IFNULL(c.fcamount,0))
										END,0
								)
							END
						) AS fcamount_overtime_periode
					FROM txovertime_hdr a
					INNER JOIN txovertime_det b ON a.ftovertime_no=b.ftovertime_no
					LEFT JOIN tlemployee_salary_inc c ON b.ftemployee_code=c.ftemployee_code AND c.ftsalary_category_code='A05' AND c.fnpayroll_slip=1 AND c.fnsalary_period=0
					LEFT JOIN tlemployee_salary_inc c_1 ON b.ftemployee_code=c_1.ftemployee_code AND c_1.ftsalary_category_code='A05' AND c.fnpayroll_slip=1	 AND c_1.fnsalary_period=1
					WHERE a.fnstatus=1 AND b.fnemployee_status=0
						AND a.fdovertime_date>='".CDate($_REQUEST['fdstart_date_overtime'])."'
						AND a.fdovertime_date<='".CDate($_REQUEST['fdend_date_overtime'])."'
					GROUP BY IFNULL(c.ftsalary_code,c_1.ftsalary_code), b.ftemployee_code
					HAVING fcamount_overtime_periode>0
				) b2 ON a1.ftemployee_code=b2.ftemployee_code
					{$ftpayroll_type_codex}
                GROUP BY a1.ftemployee_code
			) x2 ON x1.ftemployee_code=x2.ftemployee_code

		LEFT JOIN
			(
				/* =================================
				POTONGAN SELAIN CUTI & ABSEN
				====================================*/
				SELECT  a1.ftemployee_code,
				/* =================================
				POTONGAN
				====================================*/
                    SUM(CASE WHEN c1.fnsalary_period=0 THEN IFNULL(c1.fcamount,0)*IFNULL(a3.fncount_attendance_hours,0)
							 WHEN c1.fnsalary_period=1 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_attendance_days,0)
							 WHEN c1.fnsalary_period=2 THEN IFNULL(c1.fcamount,0)*IFNULL(a4.fncount_attendance_weeks,0)
							 WHEN c1.fnsalary_period=3 THEN IFNULL(c1.fcamount,0)*1
							 WHEN c1.fnsalary_period=4 THEN IFNULL(c1.fcamount,0)*1
                        ELSE
							0
						END
                    ) AS fcdeduction_1
                FROM tlemployee a1
                LEFT JOIN ( /* day */
                    SELECT b.ftemployee_code,
                        COUNT(*) AS fncount_attendance_days
                    FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
                    WHERE a.fnstatus=1 AND b.fnemployee_status=0
                    AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'

                    GROUP BY  b.ftemployee_code
                ) a2 ON a1.ftemployee_code=a2.ftemployee_code
                LEFT JOIN ( /* hours */
                    SELECT b.ftemployee_code,
                        SUM(HOUR(b.ftworking_hours)*3600+MINUTE(b.ftworking_hours)*60+SECOND(b.ftworking_hours)/3600) AS fncount_attendance_hours
                    FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
                    WHERE a.fnstatus=1 AND b.fnemployee_status=0
                    AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'

                    GROUP BY  b.ftemployee_code
                ) a3 ON a1.ftemployee_code=a3.ftemployee_code
                LEFT JOIN ( /* week */
                    SELECT b.ftemployee_code,
                        COUNT(*)/7 AS fncount_attendance_weeks
                    FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
                    WHERE a.fnstatus=1 AND b.fnemployee_status=0
                    AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'

                    GROUP BY  b.ftemployee_code
                ) a4 ON  a1.ftemployee_code=a4.ftemployee_code

                LEFT JOIN tlemployee_salary_dec c1 ON a1.ftemployee_code=c1.ftemployee_code  AND c1.ftsalary_category_code<>'B06' AND
                                c1.ftsalary_category_code<>'B07'  AND c1.fnpayroll_slip=1 {$include_jhtx}
                 {$ftpayroll_type_codex}
                GROUP BY a1.ftemployee_code

            ) x3 ON x1.ftemployee_code=x3.ftemployee_code
		LEFT JOIN
			(
				SELECT b.ftemployee_code,
						COUNT(*) AS fnleave_days
				FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
				WHERE a.fnstatus=1 AND b.fnemployee_status=1  AND b.ftabsen_code='C'
					AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
					AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'

				GROUP BY  b.ftemployee_code
			) x4 ON x1.ftemployee_code=x4.ftemployee_code
		LEFT JOIN
			(
				SELECT b.ftemployee_code,
                        COUNT(*) AS fnsick_days
				FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
				WHERE a.fnstatus=1 AND b.fnemployee_status=1  AND b.ftabsen_code='S'
                    AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
					AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'

				GROUP BY  b.ftemployee_code
            ) x4_1 ON x1.ftemployee_code=x4_1.ftemployee_code
		LEFT JOIN
			(
				SELECT b.ftemployee_code,
                        COUNT(*) AS fnalfaizin_days
				FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
				WHERE a.fnstatus=1 AND b.fnemployee_status=1  AND (b.ftabsen_code='A' OR  b.ftabsen_code='I')
					AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
					AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'

				GROUP BY  b.ftemployee_code
            ) x4_2 ON x1.ftemployee_code=x4_2.ftemployee_code
		LEFT JOIN
			(
				/* =================================
				POTONGAN  ABSEN
				====================================*/
				SELECT  a1.ftemployee_code,
					/* =================================
						POTONGAN
					====================================*/
                    SUM(CASE WHEN c1.fnsalary_period=0 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=1 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=2 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=3 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=4 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_absen_days,0)
                        ELSE
							0
						END
                    ) AS fcdeduction_2
                FROM tlemployee a1
                LEFT JOIN ( /* day */
                    SELECT b.ftemployee_code,
                        COUNT(*) AS fncount_absen_days
                    FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
                    WHERE a.fnstatus=1 AND b.fnemployee_status=1 AND b.ftabsen_code<>'C'
						AND  b.ftabsen_code<>'S'  AND DATE_FORMAT(a.fdattendance_date,'%W')<>'Sunday'
						AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
						AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'

                    GROUP BY  b.ftemployee_code
                ) a2 ON a1.ftemployee_code=a2.ftemployee_code

                LEFT JOIN tlemployee_salary_dec c1 ON a1.ftemployee_code=c1.ftemployee_code  AND c1.ftsalary_category_code='B07'  AND c1.fnpayroll_slip=1
                 {$ftpayroll_type_codex}
                GROUP BY a1.ftemployee_code
			)  x5 ON x1.ftemployee_code=x5.ftemployee_code
		LEFT JOIN
			(
				/* =================================
				POTONGAN  CUTI
				====================================*/
				SELECT  a1.ftemployee_code,
					/* =================================
					POTONGAN
					====================================*/
					SUM(CASE WHEN c1.fnsalary_period=0 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fnleave_days,0)
							 WHEN c1.fnsalary_period=1 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fnleave_days,0)
							 WHEN c1.fnsalary_period=2 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fnleave_days,0)
							 WHEN c1.fnsalary_period=3 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fnleave_days,0)
							 WHEN c1.fnsalary_period=4 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fnleave_days,0)
						ELSE
							0
						END
					) AS fcdeduction_3
				FROM tlemployee a1
                LEFT JOIN
					( /* day */
						SELECT b.ftemployee_code,
							COUNT(*) AS fnleave_days
						FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
						WHERE a.fnstatus=1 AND b.fnemployee_status=1  AND b.ftabsen_code='C'
							AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
							AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
							{$attendance_overtime}
						GROUP BY  b.ftemployee_code
					) a2 ON a1.ftemployee_code=a2.ftemployee_code

                LEFT JOIN tlemployee_salary_dec c1 ON a1.ftemployee_code=c1.ftemployee_code
					AND c1.ftsalary_category_code='B06'
					AND c1.fnpayroll_slip=1
					{$ftpayroll_type_codex}
                GROUP BY a1.ftemployee_code
			)  x6 ON x1.ftemployee_code=x6.ftemployee_code
		LEFT JOIN txpayroll_slip_emp ax ON 1=0
		GROUP BY ax.fnindex, x1.ftemployee_name";
                #die($ssql_1);
		return $ssql_1;
    }
    function ListEmployeeCoatingInjection($ftpayroll_type_codex,$include_jhtx,$include_tunjanganx="",$salary_plus2x="") {
        //daftar employee
        $ssql_1_CoatingInjection="/*
            AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
            AND a.fdovertime_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdovertime_date<='".CDate($_REQUEST['fdend_date'])."'
            AND  a.fdtrans_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdtrans_date<='".CDate($_REQUEST['fdend_date'])."'
            AND b1.fnpayroll_slip=1)
        */

        SELECT ax.fnindex AS fnindex, x1.ftemployee_code AS ftemployee_code,
			SUM(x1.fcincome_1)+ifnull(x2.fcincome_2,0) AS fcincome_amount,
			ifnull(x3.fcdeduction_1,0)+ifnull(x5.fcdeduction_2,0)+ifnull(X6.fcdeduction_3,0) AS fcdeduction_amount,
			(ifnull(sum(x1.fcincome_1),0)+ifnull(x2.fcincome_2,0))-(ifnull(x3.fcdeduction_1,0)+ifnull(x5.fcdeduction_2,0)+ifnull(X6.fcdeduction_3,0)) AS fcamount ,
			x1.fncount_attendance_days AS fnattendance_days,IFNULL(x4.fnleave_days,0) AS fnleave_days,IFNULL(x4_1.fnsick_days,0) AS fnsick_days ,
			IFNULL(x4_2.fnalfaizin_days,0) AS fnalfaizin_days
		FROM
			(
				/*===================================
					PENDAPATAN SELAIN LEMBUR
				====================================*/
				SELECT IFNULL(a2.fncount_attendance_days,0) AS  fncount_attendance_days, a1.ftemployee_code,a1.ftemployee_name,
				/*===================================
					( selain lembur)
				=====================================*/
					SUM(CASE WHEN b1.fnsalary_period=0 THEN IFNULL(b1.fcamount,0)*IFNULL(a3.fncount_attendance_hours,0)
							 WHEN b1.fnsalary_period=1 THEN IFNULL(b1.fcamount,0)*IFNULL(a2.fncount_attendance_days,0)
							 WHEN b1.fnsalary_period=2 THEN IFNULL(b1.fcamount,0)*IFNULL(a4.fncount_attendance_weeks,0)
							 WHEN b1.fnsalary_period=3 THEN IFNULL(b1.fcamount,0)*1
							 WHEN b1.fnsalary_period=4 THEN IFNULL(b1.fcamount,0)*1
						ELSE
							0
						END
					) AS fcincome_1

				FROM tlemployee a1

				LEFT JOIN ( /* day */
					SELECT b.ftemployee_code,
						COUNT(*) AS fncount_attendance_days
					FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
					WHERE a.fnstatus=1 AND b.fnemployee_status=0
						AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
						AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
					GROUP BY  b.ftemployee_code
				) a2 ON a1.ftemployee_code=a2.ftemployee_code
				LEFT JOIN ( /* hours */
					SELECT b.ftemployee_code,
						SUM(HOUR(b.ftworking_hours)*3600+MINUTE(b.ftworking_hours)*60+SECOND(b.ftworking_hours)/3600) AS fncount_attendance_hours
					FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
					WHERE a.fnstatus=1 AND b.fnemployee_status=0
						AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
						AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
					GROUP BY  b.ftemployee_code
				) a3 ON a1.ftemployee_code=a3.ftemployee_code
				LEFT JOIN ( /* week */
					SELECT b.ftemployee_code,
						/*COUNT(*)/7*/ 1 AS fncount_attendance_weeks
					FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
					WHERE a.fnstatus=1  AND ( b.fnemployee_status=0 OR (b.fnemployee_status=1 AND b.ftabsen_code='C') OR (b.fnemployee_status=1 AND b.ftabsen_code='S') )
						AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
						AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
					GROUP BY  b.ftemployee_code
				) a4 ON  a1.ftemployee_code=a4.ftemployee_code
				LEFT JOIN tlemployee_salary_inc b1 ON a1.ftemployee_code=b1.ftemployee_code AND b1.ftsalary_category_code<>'A05' AND b1.fnpayroll_slip=1 {$include_tunjanganx}
					{$ftpayroll_type_codex}
				GROUP BY a1.ftemployee_code

				UNION ALL

				SELECT IFNULL(a2.fncount_attendance_days,0) AS  fncount_attendance_days, a1.ftemployee_code,a1.ftemployee_name,	/*
				/*===================================
					( selain lembur)
				=====================================*/
					SUM(CASE WHEN b1.fnsalary_period=0 THEN IFNULL(b1.fcamount,0)*IFNULL(a3.fncount_attendance_hours,0)
							 WHEN b1.fnsalary_period=1 THEN IFNULL(b1.fcamount,0)*IFNULL(a2.fncount_attendance_days,0)
							 WHEN b1.fnsalary_period=2 THEN IFNULL(b1.fcamount,0)*IFNULL(a4.fncount_attendance_weeks,0)
							 WHEN b1.fnsalary_period=3 THEN IFNULL(b1.fcamount,0)*1
							 WHEN b1.fnsalary_period=4 THEN IFNULL(b1.fcamount,0)*1
						ELSE 0 END
					) AS fcincome_1
				FROM tlemployee a1

				LEFT JOIN ( /* day */
					SELECT b.ftemployee_code,
						COUNT(*) AS fncount_attendance_days
					FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
					WHERE a.fnstatus<>1 AND b.fnemployee_status=0
						AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
						AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
					GROUP BY  b.ftemployee_code
				) a2 ON a1.ftemployee_code=a2.ftemployee_code
				LEFT JOIN ( /* hours */
					SELECT b.ftemployee_code,
						SUM(HOUR(b.ftworking_hours)*3600+MINUTE(b.ftworking_hours)*60+SECOND(b.ftworking_hours)/3600) AS fncount_attendance_hours
					FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
					WHERE a.fnstatus<>1 AND b.fnemployee_status=0
						AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
						AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
					GROUP BY  b.ftemployee_code
				) a3 ON a1.ftemployee_code=a3.ftemployee_code
				LEFT JOIN ( /* week */
					SELECT b.ftemployee_code,
						/*COUNT(*)/7*/ 1 AS fncount_attendance_weeks
					FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
					WHERE a.fnstatus<>1  AND ( b.fnemployee_status=0 OR (b.fnemployee_status=1 AND b.ftabsen_code='C') OR (b.fnemployee_status=1 AND b.ftabsen_code='S') )
						AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
						AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
					GROUP BY  b.ftemployee_code
				) a4 ON  a1.ftemployee_code=a4.ftemployee_code

				INNER JOIN tlemployee_salary_inc_plus2 b1  ON (a1.ftemployee_code=b1.ftemployee_code AND b1.ftsalary_category_code<>'A05' AND b1.fnpayroll_slip=1)  {$include_tunjanganx}
					{$salary_plus2x}
				GROUP BY a1.ftemployee_code
			) x1
		LEFT JOIN
			(
                /* =================================
                    PENDAPATAN LEMBUR
                ====================================*/
				SELECT  a1.ftemployee_code,
				/*
                    -------------------------------------------------*/

                            /* (lembur and periode=jam) */
                            B2.fcamount_overtime_periode
                         AS fcincome_2
                FROM tlemployee a1
                LEFT JOIN (
					SELECT  IFNULL(c.ftsalary_code,c_1.ftsalary_code) AS ftsalary_code,b.ftemployee_code,
						SUM(CASE WHEN c_1.fnsalary_period=1 and ff13_hari_libur(a.fdovertime_date,a.ftdivision_code,a.ftdepartment_code)<>0 THEN
									IFNULL(c_1.fcamount,0)
							ELSE
								ROUND( 	CASE WHEN ff13_hari_libur(a.fdovertime_date,a.ftdivision_code,a.ftdepartment_code)=0 THEN
											ff09_get_overtime_biasa(b.ftovertime_hours,IFNULL(c.fcamount,0))
										ELSE
											ff09_get_overtime_libur(b.ftovertime_hours,IFNULL(c.fcamount,0))
										END,0
								)
							END
						) AS fcamount_overtime_periode
					FROM txovertime_hdr a
					INNER JOIN txovertime_det b ON a.ftovertime_no=b.ftovertime_no
					LEFT JOIN tlemployee_salary_inc c ON b.ftemployee_code=c.ftemployee_code AND c.ftsalary_category_code='A05' AND c.fnpayroll_slip=1 AND c.fnsalary_period=0
					LEFT JOIN tlemployee_salary_inc c_1 ON b.ftemployee_code=c_1.ftemployee_code AND c_1.ftsalary_category_code='A05' AND c.fnpayroll_slip=1	 AND c_1.fnsalary_period=1
					WHERE a.fnstatus=1 AND b.fnemployee_status=0
						AND a.fdovertime_date>='".CDate($_REQUEST['fdstart_date_overtime'])."'
						AND a.fdovertime_date<='".CDate($_REQUEST['fdend_date_overtime'])."'
					GROUP BY IFNULL(c.ftsalary_code,c_1.ftsalary_code), b.ftemployee_code
					HAVING fcamount_overtime_periode>0
				) b2 ON a1.ftemployee_code=b2.ftemployee_code
					{$ftpayroll_type_codex}
                GROUP BY a1.ftemployee_code
			) x2 ON x1.ftemployee_code=x2.ftemployee_code

		LEFT JOIN
			(
				/* =================================
				POTONGAN SELAIN CUTI & ABSEN
				====================================*/
				SELECT  a1.ftemployee_code,
				/* =================================
				POTONGAN
				====================================*/
                    SUM(CASE WHEN c1.fnsalary_period=0 THEN IFNULL(c1.fcamount,0)*IFNULL(a3.fncount_attendance_hours,0)
							 WHEN c1.fnsalary_period=1 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_attendance_days,0)
							 WHEN c1.fnsalary_period=2 THEN IFNULL(c1.fcamount,0)*IFNULL(a4.fncount_attendance_weeks,0)
							 WHEN c1.fnsalary_period=3 THEN IFNULL(c1.fcamount,0)*1
							 WHEN c1.fnsalary_period=4 THEN IFNULL(c1.fcamount,0)*1
                        ELSE
							0
						END
                    ) AS fcdeduction_1
                FROM tlemployee a1
                LEFT JOIN ( /* day */
                    SELECT b.ftemployee_code,
                        COUNT(*) AS fncount_attendance_days
                    FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
                    WHERE a.fnstatus=1 AND b.fnemployee_status=0
                    AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'

                    GROUP BY  b.ftemployee_code
                ) a2 ON a1.ftemployee_code=a2.ftemployee_code
                LEFT JOIN ( /* hours */
                    SELECT b.ftemployee_code,
                        SUM(HOUR(b.ftworking_hours)*3600+MINUTE(b.ftworking_hours)*60+SECOND(b.ftworking_hours)/3600) AS fncount_attendance_hours
                    FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
                    WHERE a.fnstatus=1 AND b.fnemployee_status=0
                    AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'

                    GROUP BY  b.ftemployee_code
                ) a3 ON a1.ftemployee_code=a3.ftemployee_code
                LEFT JOIN ( /* week */
                    SELECT b.ftemployee_code,
                        COUNT(*)/7 AS fncount_attendance_weeks
                    FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
                    WHERE a.fnstatus=1 AND b.fnemployee_status=0
                    AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'

                    GROUP BY  b.ftemployee_code
                ) a4 ON  a1.ftemployee_code=a4.ftemployee_code

                LEFT JOIN tlemployee_salary_dec c1 ON a1.ftemployee_code=c1.ftemployee_code  AND c1.ftsalary_category_code<>'B06' AND
                                c1.ftsalary_category_code<>'B07'  AND c1.fnpayroll_slip=1 {$include_jhtx}
                 {$ftpayroll_type_codex}
                GROUP BY a1.ftemployee_code

            ) x3 ON x1.ftemployee_code=x3.ftemployee_code
		LEFT JOIN
			(
				SELECT b.ftemployee_code,
						COUNT(*) AS fnleave_days
				FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
				WHERE a.fnstatus=1 AND b.fnemployee_status=1  AND b.ftabsen_code='C'
					AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
					AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'

				GROUP BY  b.ftemployee_code
			) x4 ON x1.ftemployee_code=x4.ftemployee_code
		LEFT JOIN
			(
				SELECT b.ftemployee_code,
                        COUNT(*) AS fnsick_days
				FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
				WHERE a.fnstatus=1 AND b.fnemployee_status=1  AND b.ftabsen_code='S'
                    AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
					AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'

				GROUP BY  b.ftemployee_code
            ) x4_1 ON x1.ftemployee_code=x4_1.ftemployee_code
		LEFT JOIN
			(
				SELECT b.ftemployee_code,
                        COUNT(*) AS fnalfaizin_days
				FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
				WHERE a.fnstatus=1 AND b.fnemployee_status=1  AND (b.ftabsen_code='A' OR  b.ftabsen_code='I')
					AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
					AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'

				GROUP BY  b.ftemployee_code
            ) x4_2 ON x1.ftemployee_code=x4_2.ftemployee_code
		LEFT JOIN
			(
				/* =================================
				POTONGAN  ABSEN
				====================================*/
				SELECT  a1.ftemployee_code,
					/* =================================
						POTONGAN
					====================================*/
                    SUM(CASE WHEN c1.fnsalary_period=0 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=1 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=2 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=3 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=4 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_absen_days,0)
                        ELSE
							0
						END
                    ) AS fcdeduction_2
                FROM tlemployee a1
                LEFT JOIN ( /* day */
                    SELECT b.ftemployee_code,
                        COUNT(*) AS fncount_absen_days
                    FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
                    WHERE a.fnstatus=1 AND b.fnemployee_status=1 AND b.ftabsen_code<>'C' AND  b.ftabsen_code<>'S'  AND DATE_FORMAT(a.fdattendance_date,'%W')<>'Saturday' AND  DATE_FORMAT(a.fdattendance_date,'%W')<>'Sunday'
                    AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'

                    GROUP BY  b.ftemployee_code
                ) a2 ON a1.ftemployee_code=a2.ftemployee_code


                LEFT JOIN tlemployee_salary_dec c1 ON a1.ftemployee_code=c1.ftemployee_code  AND c1.ftsalary_category_code='B07'  AND c1.fnpayroll_slip=1
                 {$ftpayroll_type_codex}
                GROUP BY a1.ftemployee_code
			)  x5 ON x1.ftemployee_code=x5.ftemployee_code
		LEFT JOIN
			(
				/* =================================
				POTONGAN  CUTI
				====================================*/
				SELECT  a1.ftemployee_code,
					/* =================================
					POTONGAN
					====================================*/
					SUM(CASE WHEN c1.fnsalary_period=0 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fnleave_days,0)
							 WHEN c1.fnsalary_period=1 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fnleave_days,0)
							 WHEN c1.fnsalary_period=2 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fnleave_days,0)
							 WHEN c1.fnsalary_period=3 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fnleave_days,0)
							 WHEN c1.fnsalary_period=4 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fnleave_days,0)
						ELSE
							0
						END
					) AS fcdeduction_3
				FROM tlemployee a1
                LEFT JOIN
					( /* day */
						SELECT b.ftemployee_code,
							COUNT(*) AS fnleave_days
						FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
						WHERE a.fnstatus=1 AND b.fnemployee_status=1  AND b.ftabsen_code='C'
							AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
							AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
							{$attendance_overtime}
						GROUP BY  b.ftemployee_code
					) a2 ON a1.ftemployee_code=a2.ftemployee_code

                LEFT JOIN tlemployee_salary_dec c1 ON a1.ftemployee_code=c1.ftemployee_code
					AND c1.ftsalary_category_code='B06'
					AND c1.fnpayroll_slip=1
					{$ftpayroll_type_codex}
                GROUP BY a1.ftemployee_code
			)  x6 ON x1.ftemployee_code=x6.ftemployee_code
		LEFT JOIN txpayroll_slip_emp ax ON 1=0
		GROUP BY ax.fnindex, x1.ftemployee_name";
                #die($ssql_1);
		return $ssql_1_CoatingInjection;
    }
    function ListInc($ftpayroll_type_codex,$include_jhtx,$include_tunjanganx="",$salary_plus2x="") {
        //pendapatan
        $ssql_2="/*
					AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
					AND a.fdovertime_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdovertime_date<='".CDate($_REQUEST['fdend_date'])."'
					AND a.fdtrans_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdtrans_date<='".CDate($_REQUEST['fdend_date'])."'
					AND b1.fnpayroll_slip=1)
				*/

				SELECT x1.*
                    FROM(
                            /* =================================
								PENDAPATAN SELAIN LEMBUR
                            ====================================*/
                            SELECT  # ax.fnindex AS fnindex,
									NULL AS fnindex,
                                    b1.ftsalary_code AS ftsalary_code,
                                            SUM(CASE WHEN b1.fnsalary_period=0 THEN IFNULL(  b1.fcamount,0)*IFNULL(a3.fncount_attendance_hours,0)
                                                     WHEN  b1.fnsalary_period=1 THEN IFNULL(  b1.fcamount,0)*IFNULL(a2.fncount_attendance_days,0)
                                                     WHEN  b1.fnsalary_period=2 THEN IFNULL(  b1.fcamount,0)*IFNULL(a4.fncount_attendance_weeks,0)
                                                     WHEN  b1.fnsalary_period=3 THEN IFNULL(  b1.fcamount,0)*1
                                                     WHEN b1.fnsalary_period=4 THEN IFNULL(  b1.fcamount,0)*1
                                                    ELSE 0 END
                                                    ) AS fcamount,a1.ftemployee_code


                                    FROM tlemployee a1

                                    LEFT JOIN ( /* day */
                                            SELECT b.ftemployee_code,
                                                    COUNT(*) AS fncount_attendance_days
                                            FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
                                            WHERE a.fnstatus=1 AND b.fnemployee_status=0
                                            AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'

                                            GROUP BY  b.ftemployee_code
                                    ) a2 ON a1.ftemployee_code=a2.ftemployee_code
                                    LEFT JOIN ( /* hours */
                                            SELECT b.ftemployee_code,
                                                    SUM(HOUR(b.ftworking_hours)*3600+MINUTE(b.ftworking_hours)*60+SECOND(b.ftworking_hours)/3600) AS fncount_attendance_hours
                                            FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
                                            WHERE a.fnstatus=1 AND b.fnemployee_status=0
                                            AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'

                                            GROUP BY  b.ftemployee_code
                                    ) a3 ON a1.ftemployee_code=a3.ftemployee_code
                                    LEFT JOIN ( /* week */
                                            SELECT b.ftemployee_code,
                                                    /*COUNT(*)/7*/ 1  AS fncount_attendance_weeks
                                            FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
                                            WHERE a.fnstatus=1  AND ( b.fnemployee_status=0 OR (b.fnemployee_status=1 AND b.ftabsen_code='C') OR (b.fnemployee_status=1 AND b.ftabsen_code='S') )
                                            AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'

                                            GROUP BY  b.ftemployee_code
                                    ) a4 ON  a1.ftemployee_code=a4.ftemployee_code

                                    INNER JOIN tlemployee_salary_inc b1 ON (a1.ftemployee_code=b1.ftemployee_code AND b1.ftsalary_category_code<>'A05' AND b1.fnpayroll_slip=1)  {$include_tunjanganx}
                                    # LEFT JOIN txpayroll_slip_emp ax ON 1=0
                                      {$ftpayroll_type_codex}
                                    GROUP BY a1.ftemployee_code,b1.ftsalary_code
                            UNION ALL
                                     SELECT  # ax.fnindex AS fnindex,
											NULL AS fnindex,
                                    b1.ftsalary_code AS ftsalary_code,
                                            SUM(CASE WHEN b1.fnsalary_period=0 THEN IFNULL(b1.fcamount,0)*IFNULL(a3.fncount_attendance_hours,0)
                                                     WHEN b1.fnsalary_period=1 THEN IFNULL(b1.fcamount,0)*IFNULL(a2.fncount_attendance_days,0)
                                                     WHEN b1.fnsalary_period=2 THEN IFNULL(b1.fcamount,0)*IFNULL(a4.fncount_attendance_weeks,0)
                                                     WHEN b1.fnsalary_period=3 THEN IFNULL(b1.fcamount,0)*1
                                                     WHEN b1.fnsalary_period=4 THEN IFNULL(b1.fcamount,0)*1
                                                    ELSE 0 END
                                                    ) AS fcamount,a1.ftemployee_code

                                    FROM tlemployee a1

                                    LEFT JOIN ( /* day */
                                            SELECT b.ftemployee_code,
                                                    COUNT(*) AS fncount_attendance_days
                                            FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
                                            WHERE a.fnstatus<>1 AND b.fnemployee_status=0
                                            AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'

                                            GROUP BY  b.ftemployee_code
                                    ) a2 ON a1.ftemployee_code=a2.ftemployee_code
                                    LEFT JOIN ( /* hours */
                                            SELECT b.ftemployee_code,
                                                    SUM(HOUR(b.ftworking_hours)*3600+MINUTE(b.ftworking_hours)*60+SECOND(b.ftworking_hours)/3600) AS fncount_attendance_hours
                                            FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
                                            WHERE a.fnstatus<>1 AND b.fnemployee_status=0
                                            AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'

                                            GROUP BY  b.ftemployee_code
                                    ) a3 ON a1.ftemployee_code=a3.ftemployee_code
                                    LEFT JOIN ( /* week */
                                            SELECT b.ftemployee_code,
                                                    /*COUNT(*)/7*/ 1  AS fncount_attendance_weeks
                                            FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
                                            WHERE a.fnstatus<>1 AND b.fnemployee_status=0
                                            AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'

                                            GROUP BY  b.ftemployee_code
                                    ) a4 ON  a1.ftemployee_code=a4.ftemployee_code

                                    INNER JOIN tlemployee_salary_inc_plus2 b1 ON (a1.ftemployee_code=b1.ftemployee_code AND b1.ftsalary_category_code<>'A05' AND b1.fnpayroll_slip=1)  {$include_tunjanganx}
                                    # LEFT JOIN txpayroll_slip_emp ax ON 1=0
                                    #{$salary_plus2x}
                                    GROUP BY a1.ftemployee_code,b1.ftsalary_code
                            UNION ALL

                            /* =================================
                                    PENDAPATAN  LEMBUR
                            ====================================*/
                            SELECT  # ax.fnindex AS fnindex,
									NULL AS fnindex,
                                    b2.ftsalary_code AS ftsalary_code,
                                            b2.fcamount_overtime_periode AS fcamount,a1.ftemployee_code
                                    FROM tlemployee a1
									 INNER JOIN (
												SELECT  IFNULL(c.ftsalary_code,c_1.ftsalary_code) AS ftsalary_code,b.ftemployee_code,
											SUM(
												CASE WHEN c.fnsalary_period is not null AND c_1.fnsalary_period  is not null THEN
													CASE WHEN c_1.fnsalary_period=1 and ff13_hari_libur(a.fdovertime_date,a.ftdivision_code,a.ftdepartment_code)<>0 THEN
														IFNULL(c_1.fcamount,0)
													ELSE
														round( CASE WHEN ff13_hari_libur(a.fdovertime_date,a.ftdivision_code,a.ftdepartment_code)=0 THEN
																ff09_get_overtime_biasa(b.ftovertime_hours,IFNULL(c.fcamount,0))
															ELSE ff09_get_overtime_libur(b.ftovertime_hours,IFNULL(c.fcamount,0))
															END,0  )
													END
												ELSE
													CASE WHEN c.fnsalary_period is not null AND c_1.fnsalary_period  is  null THEN
														CASE WHEN c_1.fnsalary_period=1 and ff13_hari_libur(a.fdovertime_date,a.ftdivision_code,a.ftdepartment_code)<>0 THEN
														IFNULL(c_1.fcamount,0)
														ELSE
															round( CASE WHEN ff13_hari_libur(a.fdovertime_date,a.ftdivision_code,a.ftdepartment_code)=0 THEN
																ff09_get_overtime_biasa(b.ftovertime_hours,IFNULL(c.fcamount,0))
																ELSE ff09_get_overtime_libur(b.ftovertime_hours,IFNULL(c.fcamount,0))
																END,0  )
														end
												ELSE
												CASE WHEN c.fnsalary_period is  null AND c_1.fnsalary_period  is not null THEN
													IFNULL(c_1.fcamount,0)

												END END END ) AS fcamount_overtime_periode
										FROM txovertime_hdr a
										INNER JOIN txovertime_det b ON a.ftovertime_no=b.ftovertime_no
										LEFT JOIN tlemployee_salary_inc c ON b.ftemployee_code=c.ftemployee_code AND c.ftsalary_category_code='A05' AND c.fnpayroll_slip=1 AND c.fnsalary_period=0
										LEFT JOIN tlemployee_salary_inc c_1 ON b.ftemployee_code=c_1.ftemployee_code AND c_1.ftsalary_category_code='A05' AND c_1.fnpayroll_slip=1	AND c_1.fnsalary_period=1
												WHERE a.fnstatus=1 AND b.fnemployee_status=0
														AND a.fdovertime_date>='".CDate($_REQUEST['fdstart_date_overtime'])."' AND a.fdovertime_date<='".CDate($_REQUEST['fdend_date_overtime'])."'

                                               GROUP BY IFNULL(c.ftsalary_code,c_1.ftsalary_code), b.ftemployee_code
											   having fcamount_overtime_periode>0
                                                    ) b2 ON a1.ftemployee_code=b2.ftemployee_code
                                    # LEFT JOIN txpayroll_slip_emp ax ON 1=0
                                      {$ftpayroll_type_codex}
                                    GROUP BY a1.ftemployee_code,b2.ftsalary_code
                    ) X1

                    ORDER BY x1.ftemployee_code,x1.ftsalary_code
                               ";
		return $ssql_2;
    }

	function ListIncPlus($ftpayroll_type_codex,$include_jhtx,$include_tunjanganx="",$salary_plus2x="") {
        //pendapatan
        $ssql_2="/*
                            AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
                            AND a.fdovertime_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdovertime_date<='".CDate($_REQUEST['fdend_date'])."'
                            AND  a.fdtrans_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdtrans_date<='".CDate($_REQUEST['fdend_date'])."'
                            AND b1.fnpayroll_slip=1)
                    */

                    SELECT x1.*
                    FROM(
                            /* =================================
                                    PENDAPATAN SELAIN LEMBUR
                            ====================================*/
                            SELECT  # ax.fnindex AS fnindex,
									NULL AS fnindex,
                                    b1.ftsalary_code AS ftsalary_code,
                                            SUM(CASE WHEN b1.fnsalary_period=0 THEN IFNULL(  b1.fcamount,0)*IFNULL(a3.fncount_attendance_hours,0)
                                                     WHEN  b1.fnsalary_period=1 THEN IFNULL(  b1.fcamount,0)*IFNULL(a2.fncount_attendance_days,0)
                                                     WHEN  b1.fnsalary_period=2 THEN IFNULL(  b1.fcamount,0)*IFNULL(a4.fncount_attendance_weeks,0)
                                                     WHEN  b1.fnsalary_period=3 THEN IFNULL(  b1.fcamount,0)*1
                                                     WHEN b1.fnsalary_period=4 THEN IFNULL(  b1.fcamount,0)*1
                                                    ELSE 0 END
                                                    ) AS fcamount,a1.ftemployee_code


                                    FROM tlemployee a1

                                    LEFT JOIN ( /* day */
                                            SELECT b.ftemployee_code,
                                                    COUNT(*) AS fncount_attendance_days
                                            FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
                                            WHERE a.fnstatus=1 AND b.fnemployee_status=0
                                            AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'

                                            GROUP BY  b.ftemployee_code
                                    ) a2 ON a1.ftemployee_code=a2.ftemployee_code
                                    LEFT JOIN ( /* hours */
                                            SELECT b.ftemployee_code,
                                                    SUM(HOUR(b.ftworking_hours)*3600+MINUTE(b.ftworking_hours)*60+SECOND(b.ftworking_hours)/3600) AS fncount_attendance_hours
                                            FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
                                            WHERE a.fnstatus=1 AND b.fnemployee_status=0
                                            AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'

                                            GROUP BY  b.ftemployee_code
                                    ) a3 ON a1.ftemployee_code=a3.ftemployee_code
                                    LEFT JOIN ( /* week */
                                            SELECT b.ftemployee_code,
                                                    /*COUNT(*)/7*/ 1  AS fncount_attendance_weeks
                                            FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
                                            WHERE a.fnstatus=1 AND b.fnemployee_status=0
                                            AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'

                                            GROUP BY  b.ftemployee_code
                                    ) a4 ON  a1.ftemployee_code=a4.ftemployee_code

                                    INNER JOIN tlemployee_salary_inc_plus b1 ON (a1.ftemployee_code=b1.ftemployee_code AND b1.ftsalary_category_code<>'A05' AND b1.fnpayroll_slip=1)
                                    # LEFT JOIN txpayroll_slip_emp ax ON 1=0
                                      {$ftpayroll_type_codex}
                                    GROUP BY a1.ftemployee_code,b1.ftsalary_code

                            UNION ALL

                            /* =================================
                                    PENDAPATAN  LEMBUR
                            ====================================*/
                            SELECT  # ax.fnindex AS fnindex,
									NULL AS fnindex,
                                    b2.ftsalary_code AS ftsalary_code,
                                            B2.fcamount_overtime_periode AS fcamount,a1.ftemployee_code

                                    FROM tlemployee a1

                                    INNER JOIN (
												SELECT  IFNULL(c.ftsalary_code,c_1.ftsalary_code) AS ftsalary_code,b.ftemployee_code,
													SUM(
														CASE WHEN c_1.fnsalary_period=1 and ff13_hari_libur(a.fdovertime_date,a.ftdivision_code,a.ftdepartment_code)<>0 THEN
															IFNULL(c_1.fcamount,0)
														ELSE
															round( CASE WHEN ff13_hari_libur(a.fdovertime_date,a.ftdivision_code,a.ftdepartment_code)=0 THEN
																ff09_get_overtime_biasa(b.ftovertime_hours,IFNULL(c.fcamount,0))
																ELSE ff09_get_overtime_libur(b.ftovertime_hours,IFNULL(c.fcamount,0))
																END,0  )
														end) as 	fcamount_overtime_periode
												FROM txovertime_hdr a
												INNER JOIN txovertime_det b ON a.ftovertime_no=b.ftovertime_no
												LEFT JOIN tlemployee_salary_inc c ON b.ftemployee_code=c.ftemployee_code AND c.ftsalary_category_code='A05' AND c.fnpayroll_slip=1 AND c.fnsalary_period=0
												LEFT JOIN tlemployee_salary_inc c_1 ON b.ftemployee_code=c_1.ftemployee_code AND c_1.ftsalary_category_code='A05' AND c.fnpayroll_slip=1	 AND c_1.fnsalary_period=1
												WHERE a.fnstatus=1 AND b.fnemployee_status=0
														AND a.fdovertime_date>='".CDate($_REQUEST['fdstart_date_overtime'])."' AND a.fdovertime_date<='".CDate($_REQUEST['fdend_date_overtime'])."'

                                               GROUP BY IFNULL(c.ftsalary_code,c_1.ftsalary_code), b.ftemployee_code
											   having fcamount_overtime_periode>0
                                                    ) b2 ON a1.ftemployee_code=b2.ftemployee_code
                                    # LEFT JOIN txpayroll_slip_emp ax ON 1=0
                                      {$ftpayroll_type_codex}
                                    GROUP BY a1.ftemployee_code,b2.ftsalary_code
                    ) X1

                    ORDER BY x1.ftemployee_name,x1.ftsalary_code
                               ";
                return $ssql_2;
    }

    function ListDec($ftpayroll_type_codex,$include_jhtx,$include_tunjanganx="",$salary_plus2x="") {
        $ssql_3="/*
                AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
                AND a.fdovertime_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdovertime_date<='".CDate($_REQUEST['fdend_date'])."'
                AND a.fdtrans_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdtrans_date<='".CDate($_REQUEST['fdend_date'])."'
                AND b1.fnpayroll_slip=1)*/
        SELECT x1.*
			FROM
			(
				/* =================================
				POTONGAN SELAIN CUTI & ABSEN
				====================================*/
				SELECT  # ax.fnindex AS fnindex
							null AS fnindex
						, c1.ftsalary_code,
					/* =================================
					POTONGAN
					===================================*/
					SUM(CASE WHEN c1.fnsalary_period=0 THEN IFNULL(c1.fcamount,0)*IFNULL(a3.fncount_attendance_hours,0)
							 WHEN c1.fnsalary_period=1 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_attendance_days,0)
							 WHEN c1.fnsalary_period=2 THEN IFNULL(c1.fcamount,0)*IFNULL(a4.fncount_attendance_weeks,0)
							 WHEN c1.fnsalary_period=3 THEN IFNULL(c1.fcamount,0)*1
							 WHEN c1.fnsalary_period=4 THEN IFNULL(c1.fcamount,0)*1
						ELSE 0 END
					) AS fcamount ,a1.ftemployee_code
				FROM tlemployee a1
				LEFT JOIN
					( /* day */
						SELECT b.ftemployee_code,
							COUNT(*) AS fncount_attendance_days
						FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
							AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
						WHERE a.fnstatus=1 AND b.fnemployee_status=0
							{$attendance_overtime}
						GROUP BY  b.ftemployee_code
					) a2 ON a1.ftemployee_code=a2.ftemployee_code
				LEFT JOIN
					( /* hours */
						SELECT b.ftemployee_code,
							SUM(HOUR(b.ftworking_hours)*3600+MINUTE(b.ftworking_hours)*60+SECOND(b.ftworking_hours)/3600) AS fncount_attendance_hours
						FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
						WHERE a.fnstatus=1 AND b.fnemployee_status=0
							AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
							{$attendance_overtime}
						GROUP BY  b.ftemployee_code
					) a3 ON a1.ftemployee_code=a3.ftemployee_code
				LEFT JOIN
					( /* week */
						SELECT b.ftemployee_code,
							/*COUNT(*)/7*/ 1  AS fncount_attendance_weeks
						FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
						WHERE a.fnstatus=1 AND b.fnemployee_status=0
							AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
							{$attendance_overtime}
						GROUP BY  b.ftemployee_code
					) a4 ON a1.ftemployee_code=a4.ftemployee_code
				INNER JOIN tlemployee_salary_dec c1 ON a1.ftemployee_code=c1.ftemployee_code
					AND c1.ftsalary_category_code<>'B06'
					AND c1.ftsalary_category_code<>'B07'
					AND c1.fnpayroll_slip=1 {$include_jhtx}
				# LEFT JOIN txpayroll_slip_emp ax ON 1=0
					{$ftpayroll_type_codex}
				GROUP BY # ax.fnindex,
				c1.ftsalary_code,a1.ftemployee_code

				UNION ALL

				/* =================================
				POTONGAN CUTI
				====================================*/
				SELECT  # ax.fnindex AS fnindex
					null AS fnindex , c1.ftsalary_code,
					/* =================================
					potongan
					====================================*/
					SUM(CASE WHEN c1.fnsalary_period=0 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fnleave_days,0)
							 WHEN c1.fnsalary_period=1 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fnleave_days,0)
							 WHEN c1.fnsalary_period=2 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fnleave_days,0)
							 WHEN c1.fnsalary_period=3 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fnleave_days,0)
							 WHEN c1.fnsalary_period=4 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fnleave_days,0)
						ELSE 0 END
					) AS fcamount ,a1.ftemployee_code
				FROM tlemployee a1
				LEFT JOIN
					( /* day */
						SELECT b.ftemployee_code,
							COUNT(*) AS fnleave_days
						FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
						WHERE a.fnstatus=1 AND b.fnemployee_status=1  AND b.ftabsen_code='C'
							AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
							{$attendance_overtime}
						GROUP BY  b.ftemployee_code
					) a2 ON a1.ftemployee_code=a2.ftemployee_code
				INNER JOIN tlemployee_salary_dec c1 ON a1.ftemployee_code=c1.ftemployee_code  AND c1.ftsalary_category_code='B06' AND c1.fnpayroll_slip=1

				# LEFT JOIN txpayroll_slip_emp ax ON 1=0
					{$ftpayroll_type_codex}
				GROUP BY # ax.fnindex,
					c1.ftsalary_code,a1.ftemployee_code
				UNION ALL
				/* =================================
				POTONGAN ABSEN
				====================================*/
				SELECT  # ax.fnindex AS fnindex
				NULL AS fnindex , c1.ftsalary_code,
					/*====================================
					POTONGAN
					====================================*/
					SUM(CASE WHEN c1.fnsalary_period=0 AND IFNULL(a1.fnsalary_type,0)=0  THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=0 AND IFNULL(a1.fnsalary_type,0)<>0 THEN IFNULL(c1.fcamount,0)*IFNULL(a3.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=1 AND IFNULL(a1.fnsalary_type,0)=0  THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=1 AND IFNULL(a1.fnsalary_type,0)<>0 THEN IFNULL(c1.fcamount,0)*IFNULL(a3.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=2 AND IFNULL(a1.fnsalary_type,0)=0  THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=2 AND IFNULL(a1.fnsalary_type,0)<>0 THEN IFNULL(c1.fcamount,0)*IFNULL(a3.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=3 AND IFNULL(a1.fnsalary_type,0)=0  THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=3 AND IFNULL(a1.fnsalary_type,0)<>0 THEN IFNULL(c1.fcamount,0)*IFNULL(a3.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=4 AND IFNULL(a1.fnsalary_type,0)=0  THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=4 AND IFNULL(a1.fnsalary_type,0)<>0 THEN IFNULL(c1.fcamount,0)*IFNULL(a3.fncount_absen_days,0)
						ELSE 0 END
					) AS fcamount ,a1.ftemployee_code
				FROM tlemployee a1
				LEFT JOIN
					( /* day */
						SELECT b.ftemployee_code,
							COUNT(*) AS fncount_absen_days
						FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
						WHERE a.fnstatus=1 AND b.fnemployee_status=1 AND b.ftabsen_code<>'C' AND  b.ftabsen_code<>'S'
							AND  DATE_FORMAT(a.fdattendance_date,'%W')<>'Sunday'
							AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
							AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
						{$attendance_overtime}
						GROUP BY  b.ftemployee_code
					) a2 ON a1.ftemployee_code=a2.ftemployee_code
				LEFT JOIN
					( /* day */
						SELECT b.ftemployee_code,
							COUNT(*) AS fncount_absen_days
						FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
						WHERE a.fnstatus=1 AND b.fnemployee_status=1  AND  b.ftabsen_code='A'
							AND DATE_FORMAT(a.fdattendance_date,'%W')<>'Sunday'
							AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
							AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
						{$attendance_overtime}
						GROUP BY  b.ftemployee_code
					) a3 ON a1.ftemployee_code=a3.ftemployee_code
				INNER JOIN tlemployee_salary_dec c1 ON a1.ftemployee_code=c1.ftemployee_code  AND c1.ftsalary_category_code='B07' AND c1.fnpayroll_slip=1

				# LEFT JOIN txpayroll_slip_emp ax ON 1=0
					{$ftpayroll_type_codex}
				GROUP BY  # ax.fnindex,
					c1.ftsalary_code,a1.ftemployee_code
			) x1
        ORDER BY x1.ftemployee_code, x1.ftsalary_code";
        return $ssql_3;
    }
    function ListDecCoatingInjection($ftpayroll_type_codex,$include_jhtx,$include_tunjanganx="",$salary_plus2x="") {
        $ssql_3_CoatingInjection="/*
                AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
                AND a.fdovertime_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdovertime_date<='".CDate($_REQUEST['fdend_date'])."'
                AND a.fdtrans_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdtrans_date<='".CDate($_REQUEST['fdend_date'])."'
                AND b1.fnpayroll_slip=1)*/
        SELECT x1.*
			FROM
			(
				/* =================================
				POTONGAN SELAIN CUTI & ABSEN
				====================================*/
				SELECT  # ax.fnindex AS fnindex
							null AS fnindex
						, c1.ftsalary_code,
					/* =================================
					POTONGAN
					===================================*/
					SUM(CASE WHEN c1.fnsalary_period=0 THEN IFNULL(c1.fcamount,0)*IFNULL(a3.fncount_attendance_hours,0)
							 WHEN c1.fnsalary_period=1 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_attendance_days,0)
                                                         WHEN c1.ftsalary_code='B10' AND c1.fnsalary_period=1 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_attendance_days,0)
							 WHEN c1.fnsalary_period=2 THEN IFNULL(c1.fcamount,0)*IFNULL(a4.fncount_attendance_weeks,0)
							 WHEN c1.fnsalary_period=3 THEN IFNULL(c1.fcamount,0)*1
							 WHEN c1.fnsalary_period=4 THEN IFNULL(c1.fcamount,0)*1
						ELSE 0 END
					) AS fcamount ,a1.ftemployee_code
				FROM tlemployee a1
				LEFT JOIN
					( /* day */
						SELECT b.ftemployee_code,
							COUNT(*) AS fncount_attendance_days
						FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
							AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
						WHERE a.fnstatus=1 AND b.fnemployee_status=0
							{$attendance_overtime}
						GROUP BY  b.ftemployee_code
					) a2 ON a1.ftemployee_code=a2.ftemployee_code
				LEFT JOIN
					( /* hours */
						SELECT b.ftemployee_code,
							SUM(HOUR(b.ftworking_hours)*3600+MINUTE(b.ftworking_hours)*60+SECOND(b.ftworking_hours)/3600) AS fncount_attendance_hours
						FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
						WHERE a.fnstatus=1 AND b.fnemployee_status=0
							AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
							{$attendance_overtime}
						GROUP BY  b.ftemployee_code
					) a3 ON a1.ftemployee_code=a3.ftemployee_code
				LEFT JOIN
					( /* week */
						SELECT b.ftemployee_code,
							/*COUNT(*)/7*/ 1  AS fncount_attendance_weeks
						FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
						WHERE a.fnstatus=1 AND b.fnemployee_status=0
							AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
							{$attendance_overtime}
						GROUP BY  b.ftemployee_code
					) a4 ON a1.ftemployee_code=a4.ftemployee_code
				INNER JOIN tlemployee_salary_dec c1 ON a1.ftemployee_code=c1.ftemployee_code
					AND c1.ftsalary_category_code<>'B06'
					AND c1.ftsalary_category_code<>'B07'
					AND c1.fnpayroll_slip=1 {$include_jhtx}
				# LEFT JOIN txpayroll_slip_emp ax ON 1=0
					{$ftpayroll_type_codex}
				GROUP BY # ax.fnindex,
					c1.ftsalary_code,a1.ftemployee_code

				UNION ALL

				/* =================================
				POTONGAN CUTI
				====================================*/
				SELECT  # ax.fnindex AS fnindex
					null AS fnindex , c1.ftsalary_code,
					/* =================================
					potongan
					====================================*/
					SUM(CASE WHEN c1.fnsalary_period=0 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fnleave_days,0)
							 WHEN c1.fnsalary_period=1 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fnleave_days,0)
							 WHEN c1.fnsalary_period=2 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fnleave_days,0)
							 WHEN c1.fnsalary_period=3 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fnleave_days,0)
							 WHEN c1.fnsalary_period=4 THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fnleave_days,0)
						ELSE 0 END
					) AS fcamount ,a1.ftemployee_code
				FROM tlemployee a1
				LEFT JOIN
					( /* day */
						SELECT b.ftemployee_code,
							COUNT(*) AS fnleave_days
						FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
						WHERE a.fnstatus=1 AND b.fnemployee_status=1  AND b.ftabsen_code='C'
							AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
							{$attendance_overtime}
						GROUP BY  b.ftemployee_code
					) a2 ON a1.ftemployee_code=a2.ftemployee_code
				INNER JOIN tlemployee_salary_dec c1 ON a1.ftemployee_code=c1.ftemployee_code  AND c1.ftsalary_category_code='B06' AND c1.fnpayroll_slip=1

				# LEFT JOIN txpayroll_slip_emp ax ON 1=0
					{$ftpayroll_type_codex}
				GROUP BY # ax.fnindex,
					c1.ftsalary_code,a1.ftemployee_code
				UNION ALL
				/* =================================
				POTONGAN ABSEN
				====================================*/
				SELECT  # ax.fnindex AS fnindex
				NULL AS fnindex , c1.ftsalary_code,
					/*====================================
					POTONGAN
					====================================*/
					SUM(CASE WHEN c1.fnsalary_period=0 AND IFNULL(a1.fnsalary_type,0)=0  THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=0 AND IFNULL(a1.fnsalary_type,0)<>0 THEN IFNULL(c1.fcamount,0)*IFNULL(a3.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=1 AND IFNULL(a1.fnsalary_type,0)=0  THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=1 AND IFNULL(a1.fnsalary_type,0)<>0 THEN IFNULL(c1.fcamount,0)*IFNULL(a3.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=2 AND IFNULL(a1.fnsalary_type,0)=0  THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=2 AND IFNULL(a1.fnsalary_type,0)<>0 THEN IFNULL(c1.fcamount,0)*IFNULL(a3.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=3 AND IFNULL(a1.fnsalary_type,0)=0  THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=3 AND IFNULL(a1.fnsalary_type,0)<>0 THEN IFNULL(c1.fcamount,0)*IFNULL(a3.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=4 AND IFNULL(a1.fnsalary_type,0)=0  THEN IFNULL(c1.fcamount,0)*IFNULL(a2.fncount_absen_days,0)
							 WHEN c1.fnsalary_period=4 AND IFNULL(a1.fnsalary_type,0)<>0 THEN IFNULL(c1.fcamount,0)*IFNULL(a3.fncount_absen_days,0)
						ELSE 0 END
					) AS fcamount ,a1.ftemployee_code
				FROM tlemployee a1
				LEFT JOIN
					( /* day */
						SELECT b.ftemployee_code,
							COUNT(*) AS fncount_absen_days
						FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
						WHERE a.fnstatus=1 AND b.fnemployee_status=1 AND b.ftabsen_code<>'C'
							AND b.ftabsen_code<>'S'
							AND DATE_FORMAT(a.fdattendance_date,'%W')<>'Saturday'
							AND DATE_FORMAT(a.fdattendance_date,'%W')<>'Sunday'
							AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
							AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
						{$attendance_overtime}
						GROUP BY  b.ftemployee_code
					) a2 ON a1.ftemployee_code=a2.ftemployee_code
				LEFT JOIN
					( /* day */
						SELECT b.ftemployee_code,
							COUNT(*) AS fncount_absen_days
						FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
						WHERE a.fnstatus=1 AND b.fnemployee_status=1  AND  b.ftabsen_code='A'
							AND DATE_FORMAT(a.fdattendance_date,'%W')<>'Saturday'
							AND DATE_FORMAT(a.fdattendance_date,'%W')<>'Sunday'
							AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
							AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
						{$attendance_overtime}
						GROUP BY  b.ftemployee_code
					) a3 ON a1.ftemployee_code=a3.ftemployee_code
				INNER JOIN tlemployee_salary_dec c1 ON a1.ftemployee_code=c1.ftemployee_code  AND c1.ftsalary_category_code='B07' AND c1.fnpayroll_slip=1

				# LEFT JOIN txpayroll_slip_emp ax ON 1=0
					{$ftpayroll_type_codex}
				GROUP BY  # ax.fnindex,
					c1.ftsalary_code,a1.ftemployee_code
			) x1
        ORDER BY x1.ftemployee_code, x1.ftsalary_code";
        return $ssql_3_CoatingInjection;
    }
	// function ListEmployeeTunjanganTambahan($ftpayroll_type_codex,$include_jhtx,$include_tunjanganx="",$salary_plus2x="",$attendance_overtime="") {
    // $ssql="SELECT a.fnindex AS fnindex, a.ftemployee_code AS ftemployee_code,
                    // SUM(a.fcamount) AS fcincome_amount,
                    // 0 AS fcdeduction_amount, SUM(a.fcamount) AS fcamount ,
                    // IFNULL(a2.fncount_absen_days,0) AS fnleave_days,
                    // IFNULL(a3.fncount_absen_days,0) AS fnalfaizin_days ,
                    // case when IFNULL(a3.fncount_absen_days,0)=1 then
                            // SUM(a.fcamount)*25/100
                         // when IFNULL(a3.fncount_absen_days,0)=2 then
                            // SUM(a.fcamount)*50/100
                         // when IFNULL(a3.fncount_absen_days,0)>=3 then
                            // SUM(a.fcamount)
                         // ELSE 0
                    // END AS fcdeduction_amount_1
            // FROM tlemployee_salary_inc_plus a
            // INNER JOIN tlemployee b ON a.ftemployee_code=b.ftemployee_code
            // LEFT JOIN ( /* CUTI COATING INJECTION*/
                    // SELECT b.ftemployee_code,
                    // COUNT(*) AS fncount_absen_days
                    // FROM txattendance_hdr a
                    // INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
                    // INNER JOIN tlemployee_salary_inc_plus c ON c.ftemployee_code=b.ftemployee_code
                    // WHERE a.fnstatus=1 AND b.fnemployee_status=1 AND b.ftabsen_code='C' AND DATE_FORMAT(a.fdattendance_date,'%W')<>'Saturday' AND  DATE_FORMAT(a.fdattendance_date,'%W')<>'Sunday'
                    // AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
                    // AND (a.ftdivision_code='INJ' AND a.ftdepartment_code='D02') OR (a.ftdivision_code='CTG' AND a.ftdepartment_code='D02')
					// GROUP BY  b.ftemployee_code
            // ) a2 ON a.ftemployee_code=a2.ftemployee_code
            // LEFT JOIN ( /* SELAIN CUTI COATING INJECTION */
                    // SELECT b.ftemployee_code,
                    // COUNT(*) AS fncount_absen_days
                    // FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
                    // WHERE a.fnstatus=1 AND b.fnemployee_status=1  AND  b.ftabsen_code<>'C' AND DATE_FORMAT(a.fdattendance_date,'%W')<>'Saturday' AND  DATE_FORMAT(a.fdattendance_date,'%W')<>'Sunday'
                    // AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
                    // AND (a.ftdivision_code='INJ' AND a.ftdepartment_code='D02') OR (a.ftdivision_code='CTG' AND a.ftdepartment_code='D02')
					// GROUP BY  b.ftemployee_code
            // ) a3 ON a.ftemployee_code=a3.ftemployee_code
            // LEFT JOIN ( /* CUTI COATING NON COATING INJECTION */
                    // SELECT b.ftemployee_code,
                    // COUNT(*) AS fncount_absen_days
                    // FROM txattendance_hdr a
                    // INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
                    // INNER JOIN tlemployee_salary_inc_plus c ON c.ftemployee_code=b.ftemployee_code
                    // WHERE a.fnstatus=1 AND b.fnemployee_status=1 AND b.ftabsen_code='C' DATE_FORMAT(a.fdattendance_date,'%W')<>'Sunday'
                    // AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
                    // AND (a.ftdivision_code<>'INJ' AND a.ftdepartment_code<>'D02') OR (a.ftdivision_code<>'CTG' AND a.ftdepartment_code<>'D02')
					// GROUP BY  b.ftemployee_code
            // ) a4 ON a.ftemployee_code=a4.ftemployee_code
            // LEFT JOIN ( /* SELAIN CUTI NON COATING INJECTION */
                    // SELECT b.ftemployee_code,
                    // COUNT(*) AS fncount_absen_days
                    // FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
                    // WHERE a.fnstatus=1 AND b.fnemployee_status=1  AND  b.ftabsen_code<>'C' AND  DATE_FORMAT(a.fdattendance_date,'%W')<>'Sunday'
                    // AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."' AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
                    // AND (a.ftdivision_code<>'INJ' AND a.ftdepartment_code<>'D02') OR (a.ftdivision_code<>'CTG' AND a.ftdepartment_code<>'D02')
					// GROUP BY  b.ftemployee_code
            // ) a5 ON a.ftemployee_code=a5.ftemployee_code
            // WHERE b.fnstatus<>4 AND a.fnsalary_period='3'
            // GROUP BY a.fnindex, a.ftemployee_code
            // HAVING SUM(a.fcamount)>0
            // order by b.ftemployee_name ";

		// return $ssql;
	// }
	function ListEmployeeTunjanganTambahan($ftpayroll_type_codex,$include_jhtx,$include_tunjanganx="",$salary_plus2x="",$attendance_overtime="") {
		$ssql="
			SELECT a.fnindex AS fnindex, a.ftemployee_code AS ftemployee_code,
				SUM(a.fcamount) AS fcincome_amount,
				0 AS fcdeduction_amount, SUM(a.fcamount) AS fcamount ,
				IFNULL(a2.fncount_absen_days,0) AS fnleave_days,
				IFNULL(a3.fncount_absen_days,0) AS fnalfaizin_days ,
				CASE WHEN IFNULL(a3.fncount_absen_days,0)=1 THEN
					SUM(a.fcamount)*25/100
				WHEN IFNULL(a3.fncount_absen_days,0)=2 THEN
					SUM(a.fcamount)*50/100
				WHEN IFNULL(a3.fncount_absen_days,0)>=3 THEN
					SUM(a.fcamount)
				ELSE
					0
				END AS fcdeduction_amount_1
            FROM tlemployee_salary_inc_plus a
            INNER JOIN tlemployee b ON a.ftemployee_code=b.ftemployee_code
            LEFT JOIN ( /* CUTI */
				SELECT b.ftemployee_code,
                    COUNT(*) AS fncount_absen_days
				FROM txattendance_hdr a
				INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
				INNER JOIN tlemployee_salary_inc_plus c ON c.ftemployee_code=b.ftemployee_code
				WHERE a.fnstatus=1 AND b.fnemployee_status=1 AND b.ftabsen_code='C'
					/*AND DATE_FORMAT(a.fdattendance_date,'%W')<>'Saturday'*/
					AND  DATE_FORMAT(a.fdattendance_date,'%W')<>'Sunday'
                    AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
					AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
				GROUP BY  b.ftemployee_code
            ) a2 ON a.ftemployee_code=a2.ftemployee_code
            LEFT JOIN ( /* SELAIN CUTI */
				SELECT b.ftemployee_code,
                    COUNT(*) AS fncount_absen_days
				FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
				WHERE a.fnstatus=1 AND b.fnemployee_status=1 AND b.ftabsen_code<>'C'
					/*AND DATE_FORMAT(a.fdattendance_date,'%W')<>'Saturday'*/
					AND DATE_FORMAT(a.fdattendance_date,'%W')<>'Sunday'
					AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
					AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
				GROUP BY  b.ftemployee_code
            ) a3 ON a.ftemployee_code=a3.ftemployee_code
            WHERE b.fnstatus<>4 AND a.fnsalary_period='3'
            GROUP BY a.fnindex, a.ftemployee_code
            HAVING SUM(a.fcamount)>0
            ORDER BY b.ftemployee_name ";

		return $ssql;
	}
	function ListEmployeeTunjanganTambahanCoatingInjection($ftpayroll_type_codex,$include_jhtx,$include_tunjanganx="",$salary_plus2x="",$attendance_overtime="") {
		$ssql="
			SELECT a.fnindex AS fnindex, a.ftemployee_code AS ftemployee_code,
				SUM(a.fcamount) AS fcincome_amount,
				0 AS fcdeduction_amount, SUM(a.fcamount) AS fcamount ,
				IFNULL(a2.fncount_absen_days,0) AS fnleave_days,
				IFNULL(a3.fncount_absen_days,0) AS fnalfaizin_days ,
				CASE WHEN IFNULL(a3.fncount_absen_days,0)=1 THEN
					SUM(a.fcamount)*25/100
				WHEN IFNULL(a3.fncount_absen_days,0)=2 THEN
					SUM(a.fcamount)*50/100
				WHEN IFNULL(a3.fncount_absen_days,0)>=3 THEN
					SUM(a.fcamount)
				ELSE
					0
				END AS fcdeduction_amount_1
            FROM tlemployee_salary_inc_plus a
            INNER JOIN tlemployee b ON a.ftemployee_code=b.ftemployee_code
            LEFT JOIN ( /* CUTI */
				SELECT b.ftemployee_code,
                    COUNT(*) AS fncount_absen_days
				FROM txattendance_hdr a
				INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
				INNER JOIN tlemployee_salary_inc_plus c ON c.ftemployee_code=b.ftemployee_code
				WHERE a.fnstatus=1 AND b.fnemployee_status=1 AND b.ftabsen_code='C'
					AND DATE_FORMAT(a.fdattendance_date,'%W')<>'Saturday'
					AND DATE_FORMAT(a.fdattendance_date,'%W')<>'Sunday'
                    AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
					AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
				GROUP BY  b.ftemployee_code
            ) a2 ON a.ftemployee_code=a2.ftemployee_code
            LEFT JOIN ( /* SELAIN CUTI */
				SELECT b.ftemployee_code,
                    COUNT(*) AS fncount_absen_days
				FROM txattendance_hdr a INNER JOIN txattendance_det b ON  a.ftattendance_no=b.ftattendance_no
				WHERE a.fnstatus=1 AND b.fnemployee_status=1 AND b.ftabsen_code<>'C'
					AND DATE_FORMAT(a.fdattendance_date,'%W')<>'Saturday'
					AND DATE_FORMAT(a.fdattendance_date,'%W')<>'Sunday'
					AND a.fdattendance_date>='".CDate($_REQUEST['fdstart_date'])."'
					AND a.fdattendance_date<='".CDate($_REQUEST['fdend_date'])."'
				GROUP BY  b.ftemployee_code
            ) a3 ON a.ftemployee_code=a3.ftemployee_code
            WHERE b.fnstatus<>4 AND a.fnsalary_period='3'
            GROUP BY a.fnindex, a.ftemployee_code
            HAVING SUM(a.fcamount)>0
            ORDER BY b.ftemployee_name ";

		return $ssql;
	}
	function ListIncTunjanganTambahan($ftpayroll_type_codex,$include_jhtx,$include_tunjanganx="",$salary_plus2x="",$attendance_overtime="") {
		$ssql="
			SELECT a.fnindex AS fnindex, a.ftsalary_code AS ftsalary_code,
                a.fcamount AS fcamount, a.ftemployee_code
            FROM tlemployee_salary_inc_plus a
            INNER JOIN tlemployee b ON a.ftemployee_code=b.ftemployee_code
				{$ftpayroll_type_codex}
            order by b.ftemployee_name

             ";
		return $ssql;
	}

	function SaveLogStock($fttrans_code, $ftdescription) {
		//
		//    $ssql=" INSERT INTO thlogstock (
		//                ftitem_code, ftwarehouse_code, ftlocation_code,
		//                fttrans_code, fnqty_on_hand, fnqty , ftdescription,
		//                fdcreated_date, ftcreated_user
		//            )
		//
		//            SELECT x1.ftitem_code as ftitem_code,x1.ftwarehouse_code as ftwarehouse_code,x1.ftlocation_code as ftlocation_code,
		//            '{$fttrans_code}' as fttrans_code, x1.fnqty_on_hand as fnqty_on_hand,x2.fnqty as fnqty, '{$ftdescription}' as ftdescription,
		//            now() as ftcreated_user, '{$_SESSION['username']}' as ftcreated_user
		//            FROM
		//             tlstock_det_bahan_baku x1,
		//            (
		//            select m.ftitem_code,m.ftwarehouse_code,m.ftlocation_code,sum(m.fnmasuk)-sum(m.fnkeluar) as fnqty
		//            from (
		//
		//                    select b.ftitem_code,b.ftwarehouse_code,b.ftlocation_code,sum(fnqty) as fnmasuk,0 as fnkeluar
		//                            from txinnitial_stock_hdr_bahan_baku a,txinnitial_stock_det_bahan_baku b
		//                            where a.ftstock_in_no=b.ftstock_in_no
		//                                    and a.fnstatus=1
		//                            group by b.ftitem_code,b.ftwarehouse_code,b.ftlocation_code
		//                            union all
		//                            /*stock masuk */
		//                            select b.ftitem_code,b.ftwarehouse_code,b.ftlocation_code,sum(ifnull(fnqty_1,fnqty)) as fnmasuk,0 as fnkeluar
		//                            from txstock_in_hdr_bahan_baku a,txstock_in_det_bahan_baku b
		//                            where a.ftstock_in_no=b.ftstock_in_no
		//                                    and a.fnstatus=1  and
		//                            (
		//                                    (a.fnstatus<>9 and a.ftcategory_code<>0)
		//                                    or
		//                                    (a.fnstatus<>9 and fnpo_type3<>1 and a.ftcategory_code=0)
		//                            )	 and
		//                                    a.fdtrans_date<=now()
		//                            group by b.ftitem_code,b.ftwarehouse_code,b.ftlocation_code
		//                            /*stock keluar */
		//                            union all
		//                            select b.ftitem_code,b.ftwarehouse_code,b.ftlocation_code,0 as fnmasuk,sum(fnqty)  as fnkeluar
		//                            from txstock_out_hdr_bahan_baku a,txstock_out_det_bahan_baku b
		//                            where a.ftstock_out_no=b.ftstock_out_no
		//                                    and a.fnstatus=1  and
		//                                    a.fdtrans_date<=now()
		//                            group by b.ftitem_code,b.ftwarehouse_code,b.ftlocation_code
		//                            /*mutasi */
		//                            /* keluar */
		//                            union all
		//                            select b.ftitem_code,b.ftwarehouse_from as ftwarehouse_code,b.ftlocation_from as ftlocation_code,0 as fnmasuk,sum(fnqty) as fnkeluar
		//                            from txstock_mutation_hdr_bahan_baku a,txstock_mutation_det_bahan_baku b
		//                            where a.ftstock_mutation_no=b.ftstock_mutation_no
		//                                    and a.fnstatus=1 and
		//                                    a.fdtrans_date<=now()
		//                            group by  b.ftitem_code,b.ftwarehouse_from,b.ftlocation_from
		//                            /*masuk */
		//                            union all
		//                            select b.ftitem_code,b.ftwarehouse_to as ftwarehouse_code,b.ftlocation_to as ftlocation_code,sum(fnqty)  as fnmasuk,0 as fnkeluar
		//                            from txstock_mutation_hdr_bahan_baku a,txstock_mutation_det_bahan_baku b
		//                            where a.ftstock_mutation_no=b.ftstock_mutation_no
		//                                    and a.fnstatus=1 and
		//                                    a.fdtrans_date<=now()
		//                            group by b.ftitem_code,b.ftwarehouse_to,b.ftlocation_to
		//                            /*penyesuaian*/
		//                            union all
		//                            select b.ftitem_code,b.ftwarehouse_code,b.ftlocation_code,
		//                                    case when sum(fnqty_opname)>sum(fnqty_available) then sum(fnqty_opname)-sum(fnqty_available) else 0 end as fnmasuk,
		//                                    case when sum(fnqty_opname)<sum(fnqty_available) then sum(fnqty_available)-sum(fnqty_opname) else 0 end as fnkeluar
		//                            from txstock_adjustment_hdr_bahan_baku a,txstock_adjustment_det_bahan_baku b
		//                            where a.ftstock_adjustment_no=b.ftstock_adjustment_no and
		//                                    a.fdtrans_date<=now()  and a.fnstatus=2
		//                            group by  b.ftitem_code,b.ftwarehouse_code,b.ftlocation_code
		//            ) m
		//            group by m.ftitem_code,m.ftwarehouse_code,m.ftlocation_code
		//            ) x2
		//            #SET  x1.fnqty_on_hand=x2.fnqty ,x1.fnqty_available=x2.fnqty
		//            where x1.ftitem_code=x2.ftitem_code and
		//                    ROUND(x1.fnqty_on_hand,2) not like ROUND(x2.fnqty,2) and
		//                    x1.ftwarehouse_code=x2.ftwarehouse_code and
		//                    x1.ftlocation_code=x2.ftlocation_code and
		//                    not exists (select x3.fnid from thlogstock x3 where x1.ftitem_code=x3.ftitem_code)
		//
		//";
		//#die($ssql);
		//mysql_query($ssql) or ($ssql);
	}


	function pageDataGridMPOR() {

		$ssql_1="SELECT b.fnid, b.ftstock_group_code,b.ftstock_subgroup_code,b.ftdecal_code,b.ftcolor_code ,
                                        b.ftstock_group_name,b.ftstock_subgroup_name,b.ftdecal_name,b.ftcolor_name ,
                                        b.fnqty_available AS fnqty_available,b.fnqty_painting  as fnqty_painting,b.fnqty_neto as fnqty_neto ,
                                        b.fnqty_total_por AS fnqty_total_por,b.fnqty_rekap AS fnqty_rekap,
                                        b.fnqty_container as fnqty_container ,b.fnqty_jakarta as fnqty_jakarta,
                                        b.fnqty_total_order as fnqty_total_order,
                                        b.fnqty_result_temp AS fnqty_result_temp,
                                        b.fnminim_stock,
                                        b.fnqty_result AS fnqty_result
                        FROM  txproduction_monitoring_por_det b
                        WHERE   b.ftmpor_no='{$_REQUEST['ftmpor_no']}'
                        ORDER BY b.ftstock_group_name,b.ftstock_subgroup_name,b.ftdecal_name,b.ftcolor_name";


		//echo $tbl_name;

		$adjacents = 3;

		$ssql_2 = "SELECT COUNT( fnid) as num
                            FROM txproduction_monitoring_por_det
                            WHERE ftmpor_no='{$_REQUEST['ftmpor_no']}'";
                $rs=mysql_query($ssql_2) or die($ssql_2);
		$total_pages = mysql_fetch_array($rs);
		$total_pages = $total_pages[num];

		$limit = 50;
                $page=$_REQUEST['page'];
		if($page)
			$start = ($page - 1) * $limit;
		else
			$start = 0;

		$result = mysql_query($ssql_1);
		$total_records = mysql_num_rows($result);


		if ($page == 0) $page = 1;
		$prev = $page - 1;
		$next = $page + 1;
		$lastpage = ceil($total_pages/$limit);
		$lpm1 = $lastpage - 1;
		#$path = $this->url . "?form=" . $form;

		$html_page="";
		if($lastpage > 1)
		{
			$html_page.="<div class=\"pagination\">";
			//previous button
			if ($page > 1) {
                                 $js=" OnClick=\"ajax_do_grid0('preproduksi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id={$_REQUEST['id']}&ajax=1&ftmpor_no={$_REQUEST['ftmpor_no']}&page={$prev}&code=show_grid'); \"";

				$html_page.="<a href=\"#\"  {$js} ><< previous</a>";
			  } else {
				$html_page.="<span class=\"disabled\"><< previous</span>";
			  }
			//pages
			if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
			{
				for ($counter = 1; $counter <= $lastpage; $counter++)
				{
					if ($counter == $page) {
						$html_page.="<span class=\"current\">{$counter}</span>";
					  } else {
                                                 $js=" OnClick=\"ajax_do_grid0('preproduksi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id={$_REQUEST['id']}&ajax=1&ftmpor_no={$_REQUEST['ftmpor_no']}&page={$counter}&code=show_grid'); \"";
						 $html_page.="<a href=\"#\" {$js} >{$counter}</a>";
					 }
				}
			}
			elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
			{
				//close to beginning; only hide later pages
				if($page < 1 + ($adjacents * 2))
				{
					for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
					{
						if ($counter == $page) {
							$html_page.="<span class=\"current\">{$counter}</span>";
						  } else {
                                                        $js=" OnClick=\"ajax_do_grid0('preproduksi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id={$_REQUEST['id']}&ajax=1&ftmpor_no={$_REQUEST['ftmpor_no']}&page={$counter}&code=show_grid'); \"";
							$html_page.="<a href=\"#\"  {$js} >{$counter}</a>";
						 }
					}
                                        $js_1=" OnClick=\"ajax_do_grid0('preproduksi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id={$_REQUEST['id']}&ajax=1&ftmpor_no={$_REQUEST['ftmpor_no']}&page={$lpm1}&code=show_grid'); \"";
                                        $js_2=" OnClick=\"ajax_do_grid0('preproduksi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id={$_REQUEST['id']}&ajax=1&ftmpor_no={$_REQUEST['ftmpor_no']}&page={$lastpage}&code=show_grid'); \"";
					$html_page.="...
                                            <a href=\"#\" {$js_1}>{$lpm1}</a>
                                            <a href=\"#\" {$js_2}>{$lastpage}</a>";

				}
				//in middle; hide some front and some back
				elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
				{
                                        $js_1=" OnClick=\"ajax_do_grid0('preproduksi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id={$_REQUEST['id']}&ajax=1&ftmpor_no={$_REQUEST['ftmpor_no']}&page=1&code=show_grid'); \"";
                                        $js_2=" OnClick=\"ajax_do_grid0('preproduksi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id={$_REQUEST['id']}&ajax=1&ftmpor_no={$_REQUEST['ftmpor_no']}&page=2&code=show_grid'); \"";
					$html_page.="<a href=\"#\" {$js_1} >1</a>
                                                        <a href=\"#\" {$js_2} >2</a>
                                                        ...";

					for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
					{
						if ($counter == $page) {
							$html_page.="<span class=\"current\">{$counter}</span>";
						  } else {
                                                        $js=" OnClick=\"ajax_do_grid0('preproduksi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id={$_REQUEST['id']}&ajax=1&ftmpor_no={$_REQUEST['ftmpor_no']}&page={$counter}&code=show_grid'); \"";
							$html_page.="<a href=\"#\"  {$js} >{$counter}</a>";
						  }
					}
                                        $js_1=" OnClick=\"ajax_do_grid0('preproduksi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id={$_REQUEST['id']}&ajax=1&ftmpor_no={$_REQUEST['ftmpor_no']}&page={$lpm1}&code=show_grid'); \"";
                                        $js_2=" OnClick=\"ajax_do_grid0('preproduksi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id={$_REQUEST['id']}&ajax=1&ftmpor_no={$_REQUEST['ftmpor_no']}&page={$lastpage}&code=show_grid'); \"";

					$html_page.="
					...
					<a href=\"#\"  {$js_1} >{$lpm1}</a>
					<a href=\"#\"   {$js_2} >{$lastpage}</a>";

				}
				//close to end; only hide early pages
				else
				{
                                        $js_1=" OnClick=\"ajax_do_grid0('preproduksi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id={$_REQUEST['id']}&ajax=1&ftmpor_no={$_REQUEST['ftmpor_no']}&page=1&code=show_grid'); \"";
                                        $js_2=" OnClick=\"ajax_do_grid0('preproduksi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id={$_REQUEST['id']}&ajax=1&ftmpor_no={$_REQUEST['ftmpor_no']}&page=2&code=show_grid'); \"";

                                        $html_page.="
                                                <a href=\"#\" {$js_1} >1</a>
                                                <a href=\"#\" {$js_2}>2</a>
                                                ...
                                                ";

					for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
					{
						if ($counter == $page) {
							$html_page.=" <span class=\"current\">{$counter}</span>";
						  } else {
                                                        $js_1=" OnClick=\"ajax_do_grid0('preproduksi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id={$_REQUEST['id']}&ajax=1&ftmpor_no={$_REQUEST['ftmpor_no']}&page={$counter}&code=show_grid'); \"";
							$html_page.=" <a href=\"#\" {$js_1} >{$counter}</a>";
						  }
					}
				}
			}

			//next button
			if ($page < $counter - 1) {
                                 $js_1=" OnClick=\"ajax_do_grid0('preproduksi.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id={$_REQUEST['id']}&ajax=1&ftmpor_no={$_REQUEST['ftmpor_no']}&page={$next}&code=show_grid'); \"";
				$html_page.="<a href=\"#\" {$js_1} >next >></a>";
			  } else {
				$html_page.="<span class=\"disabled\">next >></span>
                                                </div>";
			  }
		}

		$start_record = ($page-1)*$limit+1;
		if ($page == $lastpage || $last_page =="")
			$end_record = $total_records;
		else
			$end_record = $page*$limit;

		$html_page.="Records {$start_record} of {$end_record} from {$total_records}";
		return $html_page;
	}

	function pageDataGridEtimasi()
	{

		$ssql_1="SELECT  fnid
                         FROM txestimation_det
                         WHERE ftestimation_no='{$_REQUEST['ftestimation_no']}'";


		$ssql_2 = "SELECT max(fnindex) as num FROM txestimation_det
                           WHERE ftestimation_no='{$_REQUEST['ftestimation_no']}'";
                $rs=mysql_query($ssql_2) or die($ssql_2);
		$total_pages = mysql_fetch_array($rs);
		$total_pages = $total_pages[num]+1;


		$limit = 1;
                $page=$_REQUEST['page'];
		if($page)
			$start = ($page - 1) * $limit;
		else
			$start = 0;

		$result = mysql_query($ssql_1);
		$total_records = mysql_num_rows($result);


		if ($page == 0) $page = 1;
		$prev = $page - 1;
		$next = $page + 1;
		$lastpage = $total_pages;
		$lpm1 = $lastpage - 1;
		#$path = $this->url . "?form=" . $form;
                $adjacents = 4;
		$html_page="";
		if($lastpage > 1)
		{
			$html_page.="<div class=\"pagination\">";
			//previous button
			if ($page > 1) {
                                 $js=" OnClick=\"ajax_do_grid0('ppic.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id={$_REQUEST['id']}&ajax=1&ftestimation_no={$_REQUEST['ftestimation_no']}&page={$prev}&code=show_grid'); \"";

				$html_page.="<a href=\"#\"  {$js} ><< previous</a>";
			  } else {
				$html_page.="<span class=\"disabled\"><< previous</span>";
			  }
			//pages
                        if ($total_pages <= $adjacents )
                        ////enough pages to bother breaking it up
			{
                            for ($counter = 1; $counter <= $lastpage; $counter++)
                            {
                            $idn = $counter - 1;
                            $ssql_no="SELECT ftitem_code FROM txestimation_det
                                    WHERE ftestimation_no='{$_REQUEST['ftestimation_no']}' AND fnindex = {$idn}
                                    GROUP BY ftitem_code";
                            $no = RetRstField($ssql_no);
                                 if ($counter == $page)
                                 {
                                        $html_page.="<span class=\"current\">$no</span>";
                                 }
                                 else
                                 {
                                        $js=" OnClick=\"ajax_do_grid0('ppic.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id={$_REQUEST['id']}&ajax=1&ftestimation_no={$_REQUEST['ftestimation_no']}&page={$counter}&code=show_grid'); \"";
                                        $html_page.="<a href=\"#\" {$js} >$no</a>";
                                 }
                            }
			}
                        else
                        ////not enough pages to bother breaking it up
			{
                            if ($page <= $total_pages-$adjacents )
                            {
                                for ($counter = $page; $counter < $adjacents+$page; $counter++)
                                {
                                $idn = $counter - 1;
                                $ssql_no="SELECT ftitem_code FROM txestimation_det
                                        WHERE ftestimation_no='{$_REQUEST['ftestimation_no']}' AND fnindex = {$idn}
                                        GROUP BY ftitem_code";
                                $no = RetRstField($ssql_no);
                                     if ($counter == $page)
                                     {
                                            $html_page.="<span class=\"current\">$no</span>";
                                     }
                                     else
                                     {
                                            $js=" OnClick=\"ajax_do_grid0('ppic.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id={$_REQUEST['id']}&ajax=1&ftestimation_no={$_REQUEST['ftestimation_no']}&page={$counter}&code=show_grid'); \"";
                                            $html_page.="<a href=\"#\" {$js} >$no</a>";
                                     }
                                }
                            }
                            else
                            {
                                for ($counter = ($lastpage-$adjacents)+1; $counter <= $lastpage; $counter++)
                                {
                                $idn = $counter - 1;
                                $ssql_no="SELECT ftitem_code FROM txestimation_det
                                        WHERE ftestimation_no='{$_REQUEST['ftestimation_no']}' AND fnindex = {$idn}
                                        GROUP BY ftitem_code";
                                $no = RetRstField($ssql_no);
                                     if ($counter == $page)
                                     {
                                            $html_page.="<span class=\"current\">$no</span>";
                                     }
                                     else
                                     {
                                            $js=" OnClick=\"ajax_do_grid0('ppic.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id={$_REQUEST['id']}&ajax=1&ftestimation_no={$_REQUEST['ftestimation_no']}&page={$counter}&code=show_grid'); \"";
                                            $html_page.="<a href=\"#\" {$js} >$no</a>";
                                     }
                                }
                            }
                        }
			//next button
			if ($page <= $total_pages-1)
                        {
                                $js_1=" OnClick=\"ajax_do_grid0('ppic.php?form=".$_REQUEST['form']."&mode=".$_REQUEST['mode']."&id={$_REQUEST['id']}&ajax=1&ftestimation_no={$_REQUEST['ftestimation_no']}&page={$next}&code=show_grid'); \"";
				$html_page.="<a href=\"#\" {$js_1} >next >></a>";
			}
                        else
                        {
				$html_page.="<span class=\"disabled\">next >></span>
                                </div>";
			}
		}
		$start_record = ($page-1)*$limit+1;
		if ($page == $lastpage || $last_page =="")
			$end_record = $total_records;
		else
			$end_record = $page*$limit;
		$html_page.="Barang {$page} dari {$lastpage}";
			return $html_page;
	}

    function to_time($duration)
    {
        $hours = (int)($duration/60/60);
        $minutes = (int)($duration/60)-$hours*60;
        $seconds = (float)$duration-$hours*60*60-$minutes*60;

        return $hours.":".$minutes.":".$seconds;
    }

// Return No Revisi berikutnya
function getAutoRevisiNo($table, $field, $number)
{
	$number = str_replace(' ', '', $number);
        $result = mysql_query("SELECT * FROM $table WHERE $field LIKE '$number%'");
	$rownum = mysql_num_rows($result);

	return $number . "-" . $rownum;
}
?>



