<?php
session_start();
require("../../../pdf/fpdf.php");
$connect=mysqli_connect($_SESSION['certificate_host'],$_SESSION['certificate_user'],$_SESSION['certificate_pass'],$_SESSION['certificate_db']) or die("µÔ´µèÍ°Ò¹¢éÍÁÙÅäÁèä´é");
mysqli_query($connect,"SET character_set_results=tis620");
mysqli_query($connect,"SET character_set_client=tis620");
mysqli_query($connect,"SET character_set_connection=tis620");

function ThaiBahtConversion($amount_number)
{
    $amount_number = number_format($amount_number, 2, ".","");
    //echo "<br/>amount = " . $amount_number . "<br/>";
    $pt = strpos($amount_number , ".");
    $number = $fraction = "";
    if ($pt === false) 
        $number = $amount_number;
    else
    {
        $number = substr($amount_number, 0, $pt);
        $fraction = substr($amount_number, $pt + 1);
    }
    $ret = "";
    $baht = ReadNumber ($number);
    if ($baht != "")
        $ret .= $baht . "ºÒ·";
    
    $satang = ReadNumber($fraction);
    if ($satang != "")
        $ret .=  $satang . "ÊµÒ§¤ì";
    else 
        $ret .= "¶éÇ¹";
    return $ret;
}

function ReadNumber($number)
{
    $position_call = array("áÊ¹", "ËÁ×è¹", "¾Ñ¹", "ÃéÍÂ", "ÊÔº", "");
    $number_call = array("", "Ë¹Öè§", "ÊÍ§", "ÊÒÁ", "ÊÕè", "ËéÒ", "Ë¡", "à¨ç´", "á»´", "à¡éÒ");
    $number = $number + 0;
    $ret = "";
    if ($number == 0) return $ret;
    if ($number > 1000000)
    {
        $ret .= ReadNumber(intval($number / 1000000)) . "ÅéÒ¹";
        $number = intval(fmod($number, 1000000));
    }
    
    $divider = 100000;
    $pos = 0;
    while($number > 0)
    {
        $d = intval($number / $divider);
        $ret .= (($divider == 10) && ($d == 2)) ? "ÂÕè" : 
            ((($divider == 10) && ($d == 1)) ? "" :
            ((($divider == 1) && ($d == 1) && ($ret != "")) ? "àÍç´" : $number_call[$d]));
        $ret .= ($d ? $position_call[$pos] : "");
        $number = $number % $divider;
        $divider = $divider / 10;
        $pos++;
    }
    return $ret;
}

function thai_date_3($date){
		if(!isset($date)){
		return;
		}
$thai_month_arr=array(
	"01"=>"Á.¤.",
	"02"=>"¡.¾.",
	"03"=>"ÁÕ.¤.",
	"04"=>"àÁ.Â.",
	"05"=>"¾.¤.",
	"06"=>"ÁÔ.Â.",	
	"07"=>"¡.¤.",
	"08"=>"Ê.¤.",
	"09"=>"¡.Â.",
	"10"=>"µ.¤.",
	"11"=>"¾.Â.",
	"12"=>"¸.¤."					
);
	$f_date_2=explode(" ", $date);
	$f_date=explode("-", $f_date_2[0]);
	$f_date[2]=intval($f_date[2]);
	$thai_date_return="";
	$thai_date_return.=	$f_date[2];
	$thai_date_return.= " ".$thai_month_arr[$f_date[1]]." ";
	$thai_date_return.=	$f_date[0]+543;
	if($date!=""){
	return $thai_date_return;
	}
	else{
	$thai_date_return="";
	return $thai_date_return;
	}
}

$sql = "select  * from budget_main where id='$_GET[id]' ";
$result = mysqli_query($connect,$sql);
$oj_re = mysqli_fetch_array($result);
$check_number=$oj_re['check_number']; 
$pay_date=$oj_re['pay_date']; 
$pay_date=thai_date_3($pay_date);
$pay_amount=$oj_re['pay_amount']; 
$pay_amount2=number_format($pay_amount,2);
$payee=$oj_re['payee']; 

///////////////////////////////////////

$pdf=new FPDF( 'P' , 'mm' , 'A4');
$pdf->AddPage();
$pdf->AddFont("AngsanaNew","","angsa.php");
$pdf->AddFont("AngsanaNew","B","angsab.php");
$pdf->AddFont("AngsanaNew","I","angsai.php");

$thai_baht=ThaiBahtConversion($pay_amount);
$pdf->Ln(1);
$pdf->SetFont("AngsanaNew","",18); 
$pdf->Text(160, 10, "{$pay_date}");
$pdf->Text(55, 27, "{$payee}");
$pdf->Text(70, 36, "{$thai_baht}");
$pdf->Text(110, 44, "***********{$pay_amount2}******");
$pdf->Output();
?>
