<?php
  include '_connect.php';

  $sqlx = "SELECT * FROM EHONGDB.dbo.hps_ctmain
  WHERE ct_mobileno IN ('0822535103')";
  $selectx=$conn->prepare($sqlx);
  $selectx->execute();
  while ($fetchx=$selectx->fetch()) {
    // echo $fetchx['ps_id']."<br>";
    $ct_no = $fetchx['ct_no'];
    $ps_name = $fetchx['ps_name'];
    $ct_mobileno = $fetchx['ct_mobileno'];
  ?>

    <!DOCTYPE html>
    <html lang="en" dir="ltr">
      <head>
        <meta charset="utf-8">
        <title>-</title>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
        <style>
          #customers {
            font-family: Arial, Helvetica, sans-serif;
            border-collapse: collapse;
            width: 100%;
          }

          #customers td, #customers th {
            border: 1px solid #ddd;
            padding: 8px;
          }

          #customers tr:nth-child(even){background-color: #f2f2f2;}

          #customers tr:hover {background-color: #ddd;}

          #customers th {
            padding-top: 12px;
            padding-bottom: 12px;
            text-align: left;
            background-color: #008B8B;
            color: white;
          }
        </style>
      </head>
      <body>

      รายการใบเสร็จ (ตัวอย่าง) <br>
      <?php
        echo $ct_no."<br>".$ps_name." ".$ct_mobileno;
      ?>
      <table border="0" id="customers">
        <tr>
          <th style="text-align:center;">ครั้งที่</th>
          <th style="text-align:center;">วันที่</th>
          <th style="text-align:center;">จำนวนเงิน / บาท</th>
          <th style="text-align:center;">ใบเสร็จ</th>
        </tr>
      <?php
        $sql = "SELECT * FROM EHONGDB.dbo.hps_tran WHERE ct_no = '$ct_no'
        ORDER BY tr_date";
        $select=$conn->prepare($sql);
        $select->execute();
        // echo $sql;
        $no = 0;
        $sum = 0;
        $sumx = "0";
        while ($fetch=$select->fetch()) {
          $no ++;
          $TR_NO = $fetch['TR_NO'];
          $tr_date =  $fetch['TR_DATE'];
          $tr_date1 = substr($tr_date,0,4); $tr_date2 = substr($tr_date,5,2); $tr_date3 = substr($tr_date,8,2);
          $tr_datex = $tr_date3."/".$tr_date2."/".$tr_date1;
          $pay_amnt = number_format($fetch['PAY_AMNT'],2);
          $d='';
       		$e='';
          if($fetch['tr_status']<0){
            $d = "red";
            $e = "*ยกเลิก**";
            $bcolor = "red";
            $color = "yellow";
          }else{
             $sum += $fetch['PAY_AMNT'];
             $bcolor = "";
             $color = "";
          }
        ?>
        <tr>
          <td style="text-align:; background-color:<?php echo $bcolor; ?>; color:<?php echo $color; ?>;"> <?php echo number_format($fetch['TR_NO']); ?>.<br> </td>
          <td style="text-align:; background-color:<?php echo $bcolor; ?>; color:<?php echo $color; ?>;"> <?php echo $tr_datex; ?> </td>
          <td style="text-align:right; background-color:<?php echo $bcolor; ?>; color:<?php echo $color; ?>;"> <?php echo $e." ".$pay_amnt; ?> </td>
          <td style="text-align:center; background-color:<?php echo $bcolor; ?>; color:<?php echo $color; ?>;">
            <a a href="http://192.168.10.53/xehong-linux/printslip.php?sid=hps&fid=printslip&ct_no=<?php echo $ct_no; ?>&recbranch=<?php echo $fetch['recbranch']; ?>&ref_no=<?php echo $fetch['REF_NO']; ?>&ref_seq=<?php echo $fetch['REF_SEQ']; ?>&flg=1" target="_blank">
              <i style="font-size:24px" class="fa">&#xf0d6;</i>
            </a>
          </td>
        </tr>
        <?php
          if($fetch['tr_status']>=0){
            $sqlc = "SELECT * FROM EHONGDB.dbo.hps_trandet WHERE ct_no = '$ct_no' and tr_no = '$TR_NO'
            ORDER BY tr_date";
            $selectc=$conn->prepare($sqlc);
            $selectc->execute();
            // echo $sqlc;
            $sum_c = "0";
            // $sumx = "0";
            while ($fetchc=$selectc->fetch()) {
              if ($fetchc['td_type'] == "3") {
                $sum_c -= $fetchc['pay_amnt'];
                $pay_amnt_c = number_format($fetchc['pay_amnt'],2);
                $sumx += $fetchc['pay_amnt'];
                // echo $sumx;
        ?>
        <tr>
          <td></td>
          <td style="text-align:; background-color:; color:red;"> - <?php echo $fetchc['td_detail']; ?></td>
          <td style="text-align:right; background-color:; color:red;">(<?php echo $pay_amnt_c; ?>)</td>
          <td></td>
        </tr>
      <?php
      }
      }
      }
      }
     ?>
     <tr>
       <th colspan="2" style="text-align:center; background-color:; color:;"> รวม </th>
       <th style="text-align:right; background-color:; color:;"><?php echo number_format($sum-$sumx,2);?></th>
       <th></th>
     </tr>
   </table>
   <br><br>
   <?php
    }
   ?>

  </body>
</html>
