<?php
/** ensure this file is being included by a parent file */
defined( '_VALID_' ) or die( 'Direct Access to this location is not allowed.' );
if(!($_SESSION['login_status']<=5)){
exit();
}

require_once "modules/work/time_inc.php";	

//แปลงรูปแบบ date
if(isset($_GET['datepicker'])){
$f1_date=explode("-", $_GET['datepicker']);
$f2_date=$f1_date[2]."-".$f1_date[1]."-".$f1_date[0];  //ปี เดือน วัน
}
else{
$f2_date=date("Y-m-d");
}

$thai_date=thai_date($f2_date);
echo "<br />";
echo "<table width='75%' border='0' align='center'>";
echo "<tr align='center'><td colspan=2><font color='#006666' size='3'><strong>รายงานการจ่ายเงิน $thai_date</strong></font></td></tr>";
?>
	<link rel="stylesheet" type="text/css" media="all" href="./modules/work/css.css">
	<link rel="stylesheet" href="./jquery/themes/ui-lightness/jquery.ui.all.css">
	<script src="./jquery/jquery-1.5.1.js"></script>
	<script src="./jquery/ui/jquery.ui.core.js"></script>
	<script src="./jquery/ui/jquery.ui.widget.js"></script>
	<script src="./jquery/ui/jquery.ui.datepicker.js"></script>
	<script>
	$(function() {
		$( "#datepicker" ).datepicker({
			showButtonPanel: true,
			dateFormat: 'dd-mm-yy',
			changeMonth: true,
			changeYear: true,
			monthNamesShort: ['ม.ค.', 'ก.พ.', 'มี.ค.', 'เม.ย.', 'พ.ค.', 'มิ.ย.', 'ก.ค.', 'ส.ค.', 'ก.ย.', 'ต.ค.', 'พ.ย.', 'ธ.ค.'],
			dayNamesMin: ['อา','จ','อ','พ','พฤ','ศ','ส'],
			onSelect:function(dateText){  document.frmSearchDate.submit();}
		});
	});
	</script>
<tr align='center'>
	<td  align=left>
	</td>
	<td align=right  id=no_print>
<FORM name=frmSearchDate METHOD=GET>
<INPUT TYPE="hidden" name=option value="budget">
<INPUT TYPE="hidden" name=task value="check/check_8">
เลือกวันที่ <input type="text" id="datepicker" name=datepicker value=<?php echo (isset($_GET['datepicker']))? $_GET['datepicker']:date("d-m-Y");?>  readonly Size=10>
</FORM>
	</td>
</tr>

<?php
echo "</table>";

//ส่วนรายละเอียด
//ปีงบประมาณ
$sql = "select * from  budget_year where year_active='1' order by budget_year desc limit 1";
$dbquery = mysqli_query($connect,$sql);
$year_active_result = mysqli_fetch_array($dbquery);
if($year_active_result['budget_year']==""){
echo "<br />";
echo "<div align='center'>ยังไม่ได้กำหนดทำงานในปีงบประมาณใด ๆ  กรุณาไปที่เมนูตั้งค่าระบบ เพื่อกำหนดปีงบประมาณ</div>";
exit();
}

$sql = "select *,budget_main.status from budget_main left join person_main on budget_main.payer=person_main.person_id where budget_year='$year_active_result[budget_year]' and pay_date='$f2_date' order by '$f2_date', check_number ";
$dbquery = mysqli_query($connect,$sql);
$num_rows=mysqli_num_rows($dbquery);
if($num_rows<1){
echo "<div align='center'><font color='#CC0000' size='3'>ไม่มีรายการจ่ายวันนี้</font></div>";
echo exit();
}

echo  "<table width='75%' border='0' align='center'>";
echo "<Tr bgcolor='#FFCCCC' align='center'><Td width='50'>ที่</Td><Td>รายการ</Td><Td>จำนวนเงิน</Td><Td>ผู้รับเงิน</Td><Td>หลักฐานการจ่าย</td><Td>ผู้จ่ายเงิน</td></Tr>";
$N=1;
$day_amount_pay=0;
While ($result=mysqli_fetch_array($dbquery)){
		$item=$result['item'];
		$pay_amount=$result['pay_amount'];
		$pay_amount2=number_format($pay_amount,2);
		$payee=$result['payee'];
		$payer=$result['prename'].$result['name']." ".$result['surname'];
		$check_number=$result['check_number'];
		$status=$result['status'];
		$day_amount_pay=$day_amount_pay+$pay_amount;
			if(($N%2) == 0)
			$color="#FFFFC";
			else  	$color="#FFFFFF";
		if($status==3){
		$pay_type=" (เงินสด)";
		}	
		else if($status==4){
		$pay_type=" (เช็ค)";
		}
		else{
		$pay_type="";
		}				
		
echo "<tr bgcolor='$color'>";
echo "<td align='center'>$N</td><td align='left'>$item</td><td align='right'>$pay_amount2</td><td align='left'>$payee</td><td align='center'>$check_number$pay_type</td><td align='left'>$payer</td>";
echo "</tr>";
$N++;
}
$day_amount_pay=number_format($day_amount_pay,2);
echo "<tr bgcolor='#FFCCCC' align='center'>";
echo "<td align='center' colspan='2'>รวมจ่ายทั้งหมด</td><td align='right'>$day_amount_pay</td><td colspan='3'></td>";
echo "</tr>";
echo "</table>";

$sql = "select count(id) as item_num, sum(pay_amount) as amount_remain from budget_main where budget_year='$year_active_result[budget_year]' and pay_amount !='null' and (check_number='' or check_number is null)";
$dbquery = mysqli_query($connect,$sql);
$result=mysqli_fetch_array($dbquery);
$amount_remain=number_format($result['amount_remain'],2);

$sql2 = "select count(id) as item_num, sum(pay_amount) as amount_remain from budget_main where budget_year='$year_active_result[budget_year]' and pay_amount !='null' and (check_number='' or check_number is null) and DATEDIFF(rec_date,NOW())<=-15";
$dbquery2 = mysqli_query($connect,$sql2);
$result2=mysqli_fetch_array($dbquery2);
$amount_remain2=number_format($result2['amount_remain'],2);

echo  "<table width='75%' border='0' align='center'>";
echo "<tr><td align='left'><b><font color='FF0000'>ค้างจ่าย</font></b>ปีงบประมาณนี้ จำนวน $result[item_num] รายการ เป็นเงิน $amount_remain บาท</td></tr>";
echo "<tr><td align='left'><b><font color='FF0000'>ค้างจ่ายเกิน 15 วัน</font></b>ปีงบประมาณนี้ จำนวน $result2[item_num] รายการ เป็นเงิน $amount_remain2 บาท</td></tr>";
echo "</table>";
?>
