

<?php
//include("sys.database.php");
include("hps.function.php");
//calcDownTable($ct_no);


$sql="select *,datediff(day,tr_date,getdate()) as dvdate from (select * from hps_tran where ct_no='".$_GET["ct_no"]."' and recbranch='".$_GET["recbranch"]."' and ref_no=".$_GET["ref_no"]." and ref_seq=".$_GET["ref_seq"].") a left outer join (select ct_no as xct_no,ps_name,ct_mobileno,brand_name,model_name,serial_no,pserial_no,reg_no,df_addrhome,df_tuname,df_amname,df_prname,df_zip,gct_type from hps_ctmain  where ct_no='".$_GET["ct_no"]."') b on(a.ct_no=b.xct_no) left outer join (select emp_id,FNAME+' '+LNAME as recusername from employee) c on(a.recuser=c.emp_id)  where ct_no='".$_GET["ct_no"]."' and recbranch='".$_GET["recbranch"]."' and ref_no=".$_GET["ref_no"]." and ref_seq=".$_GET["ref_seq"]."";
//echo $sql;
$hps_tran=$database->getRow($sql);

if($hps_tran[slip_status]=='0' || $hps_tran[slip_status]==""){

calcNotpayCount($ct_no);
calcLate($ct_no);
//$hps_ctmain = $database->getRow("select * from hps_ctmain  where ct_no = '$ct_no' ");
//$allpay_count = $database->getOne("select count(ct_no) from hps_ctdue where ct_no='$ct_no' and cd_amnt<=cd_payamnt and cd_type =1");
$hps_ctmain = $database->getRow("select * from (select * from hps_ctmain where ct_no='$ct_no') a left outer join (select ct_no as xct_no,count(ct_no) as allpaycnt from hps_ctdue where ct_no='$ct_no' and cd_amnt<=cd_payamnt and cd_type=1 group by ct_no) b on(a.ct_no=b.xct_no)");
$allpay_count=$hps_ctmain["allpaycnt"];
$sumlate=0;
if($hps_ctmain[sum_late]>0) $sumlate=$hps_ctmain[sum_late];
 //$database->Execute("update hps_tran set npy_lateamnt=$sumlate where ct_no='$ct_no' and recbranch='$recbranch' and ref_no=$ref_no and ref_seq=$ref_seq");
 $database->Execute("update hps_tran set npy_lateamnt=$sumlate,price=$hps_ctmain[price],due_count=$hps_ctmain[due_count],payed_count=$allpay_count,payed_amnt=$hps_ctmain[payamnt],notpay_count=$hps_ctmain[notpay_count],notpay_amnt=$hps_ctmain[notpay_amnt],slip_status='1' where ct_no='$ct_no' and recbranch='$recbranch' and ref_no=$ref_no and ref_seq=$ref_seq");

 $sql="select *,datediff(day,tr_date,getdate()) as dvdate from (select * from hps_tran where ct_no='".$_GET["ct_no"]."' and recbranch='".$_GET["recbranch"]."' and ref_no=".$_GET["ref_no"]." and ref_seq=".$_GET["ref_seq"].") a left outer join (select ct_no as xct_no,ps_name,ct_mobileno,brand_name,model_name,serial_no,pserial_no,reg_no,df_addrhome,df_tuname,df_amname,df_prname,df_zip,gct_type from hps_ctmain where ct_no='".$_GET["ct_no"]."') b on(a.ct_no=b.xct_no) left outer join (select emp_id,FNAME+' '+LNAME as recusername from employee) c on(a.recuser=c.emp_id) where ct_no='".$_GET["ct_no"]."' and recbranch='".$_GET["recbranch"]."' and ref_no=".$_GET["ref_no"]." and ref_seq=".$_GET["ref_seq"]."";

  $hps_tran=$database->getRow($sql);
//  $allpay_count=$hps_tran[payed_count];
//  $paid_amnt=$hps_tran[payed_amnt];
//  $avl_amnt = $hps_ctmain['price'] - $hps_tran['payed_amnt'];
};
//echo $hps_ctmain["gct_type"];
if($hps_tran["gct_type"]=="002") $typeproduct="รถจักรยานยนต์";
else $typeproduct="สินค้า";
    ?>

<?if($hps_tran[tr_status]<0){?>
<div style="position:absolute;width:240px;left:0;top:<?=$top?>;height:300;z-index=1">
<table width="100%" align="center" style="border-style:none;border-width:1" height="300" cellpadding=0 cellspacing=1>
<tr>
<td width="70%" align="center">
<table>
<tr  valign="center" align="center"><td>
<big><b><font size="4" color="#FF0000">** ยกเลิก Void **</font></b></big><br> <font size="4" color="#FF0000"><?=$hps_tran["void_name"]?> <br><?=NormalDateTime($hps_tran["void_date"])?> <br><?=$hps_tran["void_hint"]?></font>
</td>
</tr>
</table>
</td>
</tr></table>
</div>
<?};?>
<?if($hps_tran["dvdate"]>0){?>
<div style="position:absolute;width:270px;left:0;top:150;height:300;z-index=1;">
<table width="100%" align="left" style="border-style:none;border-width:1" height="300" cellpadding=0 cellspacing=1>

<tr  valign="center" align="center">
<td nowrap>
<big><b><font size="4" color="#FF0000">** พิมพ์ซ้ำ **</font></b></big>
</td>
</tr>
</table>
</div>
<?};?>
<table  align="left" style="width:240px">
    <tr align="center">
        <td><img alt="testing" style="width: 200px;height: 40px;" src="images/ehongmdlogo.png" />
        <br><font size="+1">ใบเสร็จรับเงิน</font></td>
    </tr>
    <tr>
        <td  align="left"><hr>บริษัท มดแดง ลิสซิ่ง จำกัด<br>เลขที่ 14  ซ.จรัญสนิทวงศ์61 <br>ถ.เจริญสนิทวงศ์<br>แขวงบางบำหรุ เขตบางพลัด<br>กรุงเทพมหานคร 10700<br>โทร.0864556789<br>TAX ID: 0105560203300<br>
ใบเสร็จเลขที่  <?php echo $hps_tran["recbranch"]?>-<?php echo $hps_tran["ref_no"]?>/<?php echo $hps_tran["ref_seq"]?></td>
    </tr>

    <tr valign="top">
        <td><hr>วันที่ <?php echo NormalDate($hps_tran["tr_date"])?><br>
รับเงินจาก  <?php echo $hps_tran["ps_name"]?><br>
เบอร์โทร  <?php echo $hps_tran["ct_mobileno"]?><br>
<!--<?php if($hps_tran["df_addrhome"]!=""){?>
 <?php echo $hps_tran["df_addrhome"]?><br>
 ต.<?php echo $hps_tran["df_tuname"]?> อ.<?php echo $hps_tran["df_amname"]?><br>
 จ.<?php echo $hps_tran["df_prname"]?> <?php echo $hps_tran["df_zip"]?><br>
 <?php };?>
 -->
 </td>

    </tr>
    <tr valign="top">
        <td nowrap>
		<hr>
		<?php echo $typeproduct;?>   ยีห้อ <?php echo $hps_tran["brand_name"]?><br>
		รุ่น <?php echo $hps_tran["model_name"]?><br>
เลขเครื่อง <?php echo $hps_tran["serial_no"]?><br>
เลขตัวถัง <?php echo $hps_tran["pserial_no"]?><br>
หมายเลขทะเบียน <?php echo $hps_tran["reg_no"]?>  </td>

    </tr>
    <tr valign="top">
        <td nowrap><hr>
		เลขที่สัญญา  <?php echo $hps_tran["ct_no"]?><br>
		<table width="100%">

		<?php
$sql="select * from hps_trandet a left outer join (select * from hps_tdtypename) c on(a.td_type=c.xtd_type) where ct_no='".$_GET["ct_no"]."' and recbranch='".$_GET["recbranch"]."' and ref_no=".$_GET["ref_no"]." and ref_seq=".$_GET["ref_seq"]." and td_type in ('1','2')";
//echo $sql;
$rs=$database->getAll($sql);
foreach($rs as $hps_trandet){
		?>
		<tr><td>
<?php echo $hps_trandet["xtd_name"]?> ที่ <?php echo $hps_trandet["td_no"]?></td><td width="20%" nowrap align="right"><?php echo number_format($hps_trandet["pay_amnt"],2)?> บาท</td>
</tr>
<?php };?>
<tr><td>
ยอดคงเหลือ</td><td nowrap align="right"> (<?php echo $hps_tran["due_count"]-$hps_tran["payed_count"]?> งวด)</td></tr>
<tr><td>
ยอดคงเหลือ</td><td nowrap align="right">( <?php echo number_format($hps_tran["price"]-$hps_tran["payed_amnt"])?> บาท)</td></tr>
<tr><td>
ค้างชำระ</td><td nowrap align="right">( <?php echo $hps_tran["notpay_count"]?> งวด)</td></tr>
<?php
$sql="select * from hps_trandet a left outer join (select * from hps_tdtypename) c on(a.td_type=c.xtd_type)  where ct_no='".$_GET["ct_no"]."' and recbranch='".$_GET["recbranch"]."' and ref_no=".$_GET["ref_no"]." and ref_seq=".$_GET["ref_seq"]." and td_type not in ('1','2')";
//echo $sql;
$rs=$database->getAll($sql);
foreach($rs as $hps_trandet){
		?>
		<tr><td>
		<?php echo $hps_trandet["xtd_name"]?></td><td nowrap align="right"><?php echo number_format($hps_trandet["pay_amnt"],2)?> บาท</td></tr>
<?php };?>
<tr><td>
ยอดชำระ</td><td nowrap align="right"><b><?php echo number_format($hps_tran["pay_amnt"],2)?> บาท</b>
</td></tr>
</table>

</td>


    </tr>
    <tr>
        <td >
		<hr>ผู้รับเงิน <?php echo $hps_tran["recusername"]?><br></td>
    </tr>
	<tr>
        <td >
		<hr>ผู้สั่งพิมพ์ <?php echo $userdata["fullname"]?><!--<br>พิมพ์ <?php echo NormalDateTime();?>--></td>
    </tr>
	<tr>
	<td><center>
<img src="https://reg.ezmember.org/slip/phpqrcode/qrpng.php?ctno=<?=$ct_no?>" style="width:100px;height:100px">
</center></td>
	</tr>
</table>

<script type="text/javascript">

    window.focus();
    window.print();
  //  window.close();


    </script>
