<?php
  error_reporting(0);
  $ct_no = $_GET['ct_no'];
  // $ct_no = "0109640001";
  include '_connect.php';
  $sqlx = "SELECT * FROM EHONGDB.dbo.hps_ctmain
  WHERE ct_no = '$ct_no'";
  $selectx=$dbh->prepare($sqlx);
  $selectx->execute();
  $fetchx=$selectx->fetch();
    $ps_name = $fetchx['ps_name'];
    $ct_mobileno = $fetchx['ct_mobileno'];
    $product_name = $fetchx['product_name'];
    $due_amnt = $fetchx['due_amnt'];
?>

<?php
  $output = "";
  $outBK = "";

  $sql = "SELECT
  [ct_no] ,[ps_id] ,[ps_name] ,[TI_NAME] ,[PS_FNAME] ,[PS_LNAME] ,[PS_PHONE] ,[PS_MOBILE] ,[CD_DATA] ,[ct_date]
  ,[br_id] ,[sbr_id] ,[price] ,[payamnt] ,[due_amnt] ,[due_count] ,[product_id] ,[product_name] ,[serial_no] ,[status] ,[cnsvrefno]
  ,[cnsvrefcode],[cd_no],[cd_date],[cd_paydate],[ct_nscore],[slowly_amnt]
  FROM [View_To_Table].[dbo].[RCT_MAIN]  where ct_no = '$ct_no'";
  $stmt = $dbh->prepare($sql);
  $stmt->execute();
  // echo $sql;
  // $output .= '<form action="qrcode_pay_md2.php" method="POST">
  // <p>กรุณากรอกจำนวนเงินที่ต้องการชำระ<p>';
  while($row = $stmt->fetch()) {
    ////////////////////////////////////
    // $dueamnt=$row['due_amnt'];
    // $slowlyamnt=$row['slowly_amnt'];
    // $Camount=$dueamnt+$slowlyamnt;
    /////////////////////////////
    $billerid='|010556020330003';
    $psid=$row['ps_id'];
    $dueamnt=$row['due_amnt'];
    $slowlyamnt=$row['slowly_amnt'];
    // -- REF1 -- //
    $ctnoN1 = substr($ct_no ,0,1);
    $ctnoN2 = substr($ct_no ,1,1);
    $ctnoN3 = substr($ct_no ,2,1);
    $ctnoN4 = substr($ct_no ,3,1);
    $ctnoN5 = substr($ct_no ,4,1);
    $ctnoN6 = substr($ct_no ,5,1);
    $ctnoN7 = substr($ct_no ,6,1);
    $ctnoN8 = substr($ct_no ,7,1);
    $ctnoN9 = substr($ct_no ,8,1);
    $ctnoN10 = substr($ct_no ,9,1);
    $ctnoTotal=$ctnoN1+$ctnoN2+$ctnoN3+$ctnoN4+$ctnoN5+$ctnoN6+$ctnoN7+$ctnoN8+$ctnoN9+$ctnoN10+1;
    $digit1 = substr($ctnoTotal ,1,1);
    $log10 = log10($ct_no);
    $digit2 = substr($log10 ,2,1);
    $chkdigit3=strlen($ct_no);
    $sumdigit3 = $chkdigit3+$digit1+$digit2;
    $digit3 = substr($sumdigit3 ,1,1);
    $sumref1 = $digit1.$digit2.$digit3;
    $ref1 = $ct_no.$sumref1;
    // -- END REF1 -- //
    // -- REF2 -- //
    $date=Date('dmY');
    $sumdate1 = substr($date ,0,4);
    $sumdate2 = substr($date ,6,2);
    $sumdate3 = $sumdate1.$sumdate2;
    //$runNum=rand(1111,9999);
    $runNum=sprintf('%04d', rand(1111,9999));
    $rand=strlen($runNum);
    $zero="0";
    if($rand==1)
    {
        $randplus= $zero.$zero.$zero.$runNum;
    }
    else if($rand==2)
    {
        $randplus= $zero.$zero.$runNum;
    }
    else if($rand==3)
    {
        $randplus= $zero.$runNum;
    }
    else
    {
        $randplus= $runNum;
    }
    //$randplus."<br>";
    $totaldatenum=$sumdate3.$randplus;
    $dateN1 = substr($totaldatenum ,0,1);
    $dateN2 = substr($totaldatenum ,1,1);
    $dateN3 = substr($totaldatenum ,2,1);
    $dateN4 = substr($totaldatenum ,3,1);
    $dateN5 = substr($totaldatenum ,4,1);
    $dateN6 = substr($totaldatenum ,5,1);
    $dateN7 = substr($totaldatenum ,6,1);
    $dateN8 = substr($totaldatenum ,7,1);
    $dateN9 = substr($totaldatenum ,8,1);
    $dateN10 = substr($totaldatenum ,9,1);
    $dateTotal=$dateN1+$dateN2+$dateN3+$dateN4+$dateN5+$dateN6+$dateN7+$dateN8+$dateN9+$dateN10;
    $datedigitr1 = substr($dateTotal ,0,1);
    $datedigitr2 = substr($dateTotal ,1,1);
    $sumrefdigit = $datedigitr1+$datedigitr2;
    $Pre2_Digit1_1 = substr($sumrefdigit ,0,1);
    $Pre2_Digit1_2 = substr($sumrefdigit ,1,1);
    $chkPre2_Digit1_2=strlen($Pre2_Digit1_2);
    if($chkPre2_Digit1_2==0){$Pre2_Digit1_2="0";}
    $digit1 = $Pre2_Digit1_1+$Pre2_Digit1_2;
    $totaldatenum2="0.".$totaldatenum;
    $expDate = exp($totaldatenum2);
    $digit2 = substr($expDate ,3,1);
    $PreSquareRoot = $digit1.$digit2;
    $SquareRoot=sqrt($PreSquareRoot);
    $chk_SquareRoot=strlen($SquareRoot);
    if($chk_SquareRoot>1) {
      $digit3 = substr($SquareRoot ,2,1);
    } else {
      $digit3 ="0";
    }
    //$digit3 = substr($SquareRoot ,2,1);
    $preref=$digit1.$digit2.$digit3;
    $ref2=$totaldatenum.$preref;
    $amount=$dueamnt+$slowlyamnt."00";
    $Camount=$dueamnt;
    // echo $Camount;

    $sql_chk = "SELECT TOP 1 ct_no,status FROM [EHONGDB].[dbo].[hps_ctdue]
    WHERE ct_no ='$ct_no' and slowly_amnt is not null and slowly_amnt != '0' order by cd_no DESC";
    $stmt_chk = $dbh->prepare($sql_chk);
    $stmt_chk->execute();
    $row_chk = $stmt_chk->fetch();
    // echo $sql_chk;
    // echo $row_chk['status'];
    if($row_chk['status']=='D') {
      // -- END ตั้งเงื่อนไข -- //
      // -- คำนวณยอดที่ต้องชำระ -- //
      $sql_SD = "SELECT
      a.ct_no ,a.cd_no ,a.cd_date ,a.cd_paydate ,a.ct_nscore ,a.slowly_amnt ,b.due_amnt ,a.cd_payamnt ,b.due_amnt-a.cd_payamnt as due_amntTotal
      ,b.fw_status ,a.status ,b.ct_no ,b.ps_id ,b.ps_name ,b.ct_date ,b.br_id ,b.sbr_id ,b.price ,b.payamnt ,b.due_count ,b.product_id ,b.product_name
      ,b.serial_no ,b.status ,b.cnsvrefno ,b.cnsvrefcode ,c.TI_NAME ,c.PS_FNAME ,c.PS_LNAME ,c.PS_PHONE ,c.PS_MOBILE ,c.CD_DATA
      FROM [EHONGDB].[dbo].[hps_ctdue] a
      left outer join(SELECT
      ct_no ,ps_id ,ps_name ,ct_date ,br_id ,sbr_id ,price ,payamnt ,due_amnt ,due_count ,product_id ,product_name ,serial_no ,status
      ,cnsvrefno ,cnsvrefcode ,fw_status
      FROM [EHONGDB].[dbo].[hps_ctmain]) b on (a.ct_no=b.ct_no)
      left outer join( SELECT PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_PHONE ,PS_MOBILE ,CD_DATA ,img_path FROM [EHONGDB].[dbo].[PERSON]) c on (a.ps_id=c.PS_ID)
      WHERE a.ct_no = '$ct_no' and slowly_amnt is not null and slowly_amnt != '0' and a.status = 'D' order by cd_date asc";
      // echo $sql_SD;
      $stmt_SD = $dbh->prepare($sql_SD);
      $stmt_SD->execute();
      $row_SD = $stmt_SD->fetch();
      $due_amntTotal=$row_SD['due_amntTotal'];
      // $total_Camount=$Camount+$due_amntTotal;
      $total_Camount=$Camount;
      // -- END คำนวณยอดที่ต้องชำระ -- //
      //////////////////////////////
      if($row_SD['status'] == 'D') {
        if($due_amntTotal!=null){
          if($row_SD['fw_status'] != '10') {
              $due_amntTotal+=$row_SD['slowly_amnt'];
              if($row_SD['fw_status'] == '11') {
                // เบี้ยปรับ
                $total_Camount+=$row_SD['slowly_amnt']-$row_SD['cd_payamnt'];
                $outBK .= "เบี้ยปรับ";
                $output .= number_format($row_SD['slowly_amnt'],2);
              } else {
                // ค้างชำระ
                $total_Camount+=$due_amntTotal;
                $outBK .= "ค้างชำระ";
                $output .= number_format($due_amntTotal,2);
              }
          } else {
            $outBK .= "ยอดค้างชำระ";
            $output .= "0.00";
          }
        } else {
          $outBK .= "ยอดค้างชำระ";
          $output .= "0.00";
        }
      } else {
        $outBK .= "ยอดค้างชำระ";
        $output .= "0.00";
      }
    } else {

      $sql_SD = "SELECT a.ct_no ,a.cd_no ,a.cd_date ,a.cd_paydate ,a.ct_nscore ,a.slowly_amnt ,b.due_amnt ,a.cd_payamnt ,b.due_amnt-a.cd_payamnt as due_amntTotal
      ,b.fw_status ,a.status ,a.ct_ngrad ,b.ct_no ,b.ps_id ,b.ps_name ,b.ct_date ,b.br_id ,b.sbr_id ,b.price ,b.payamnt ,b.due_count ,b.product_id ,b.product_name
      ,b.serial_no ,b.cnsvrefno ,b.cnsvrefcode ,c.TI_NAME ,c.PS_FNAME ,c.PS_LNAME ,c.PS_PHONE ,c.PS_MOBILE ,c.CD_DATA
      FROM [EHONGDB].[dbo].[hps_ctdue] a
      left outer join(SELECT
        ct_no ,ps_id ,ps_name ,ct_date ,br_id ,sbr_id ,price ,payamnt ,due_amnt ,due_count ,product_id
        ,product_name ,serial_no ,status ,cnsvrefno ,cnsvrefcode ,fw_status
      FROM [EHONGDB].[dbo].[hps_ctmain]) b on (a.ct_no=b.ct_no)
      left outer join(SELECT
        PS_ID ,TI_NAME ,PS_FNAME ,PS_LNAME ,PS_PHONE ,PS_MOBILE ,CD_DATA ,img_path
      FROM [EHONGDB].[dbo].[PERSON]) c on (a.ps_id=c.PS_ID)
      WHERE a.ct_no = '$ct_no' and slowly_amnt is not null and slowly_amnt != '0' and a.status is null order by cd_date asc";
      $stmt_SD = $dbh->prepare($sql_SD);
      $stmt_SD->execute();
      $row_SD = $stmt_SD->fetch();
      // echo $sql_SD;
      $due_amntTotal = $row_SD['slowly_amnt']+$row_SD['due_amntTotal'];
      // echo $row_SD['slowly_amnt'];
      // echo $row_SD['due_amntTotal'];
      // echo $due_amntTotal;
      while ($row_SD2 = $stmt_SD->fetch()) {
        $due_amntTotal+=$row_SD2['slowly_amnt']+$row_SD2['due_amntTotal'];
      }
      // เบี้ยปรับค่าติดตามหรือเบี้ยปรับอื่นๆ //
      $sql_cflw = "SELECT COUNT(*) FROM [EHONGDB].[dbo].[hps_ctcost] WHERE ct_no = '$ct_no' AND cc_docno IS NULL AND cc_payamnt = '0'";
      $stmt_cflw = $dbh->prepare($sql_cflw);
      $stmt_cflw->execute();
      $row_cflw = $stmt_cflw->fetch();
      $sql_flw = "SELECT ct_no,cc_no,cc_docno,cc_doc,cc_date,cc_amnt,cc_discount,cc_payamnt,cc_type
      FROM [EHONGDB].[dbo].[hps_ctcost] WHERE ct_no = '$ct_no' AND cc_docno IS NULL AND cc_payamnt = '0'";
      // echo $sql_flw;
      $stmt_flw = $dbh->prepare($sql_flw);
      $stmt_flw->execute();
      $row_flw = $stmt_flw->fetch();
      $due_amntTotal=$due_amntTotal+$row_flw['cc_amnt'];
      while ($row_flw2 = $stmt_flw->fetch()) {
        $due_amntTotal+=$row_flw2['cc_amnt'];
      }
      // เบี้ยปรับค่าติดตามหรือเบี้ยปรับอื่นๆ //
      // $total_Camount=$Camount+$due_amntTotal;
      $total_Camount=$Camount;
      // -- END คำนวณยอดที่ต้องชำระ -- //
      //////////////////////////////
      if($due_amntTotal!=null) {
        if($row_SD['fw_status'] != '10') {
            if($row_SD['fw_status'] == '11') {
              // เบี้ยปรับ
              $total_Camount+=$row_SD['slowly_amnt']-$row_SD['cd_payamnt'];
              $outBK .= "เบี้ยปรับ";
              $output .= number_format($row_SD['slowly_amnt'],2);
            } else {
              // ค้างชำระ
              $total_Camount+=$due_amntTotal;
              $outBK .= "ค้างชำระ";
              $output .= number_format($due_amntTotal,2);
            }
        } else {
          $outBK .= "ยอดค้างชำระ";
          $output .="0.00";
        }
      } else {
        $outBK .= "ยอดค้างชำระ";
        $output .="0.00";
      }
    }
  }
?>
    <!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">
        <script src='https://kit.fontawesome.com/a076d05399.js' crossorigin='anonymous'></script>
        <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

        <style media="screen">
          table {
            border-collapse: collapse;
            border-radius: 15px;
            overflow: hidden;
          }
          body{
            font-size: 25px;
            background-color: #fff;
            background-image: url("img/xBG01.jpg");
            background-repeat: no-repeat;
            background-size: 210%;
          }
          .xxstable {
            height: 70px;
            overflow-y: scroll;
          }
        </style>
        <style>
          #customers {
            font-family: Arial, Helvetica, sans-serif;
            border-collapse: collapse;
            width: 100%;
            height: 50%;
            border-radius: 20px;
            overflow: hidden;
          }

          #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: red; */
            color: white;
          }
        </style>
        <style>
        .buttonx {
          /* background-color: yellow; /* Green */
          border: none;
          color: black;
          text-align: center;
          text-decoration: none;
          display: inline-block;
          font-size: 20px;
          margin: 4px 2px;
          cursor: pointer;
        }
        .button1 {padding: 10px 24px;}
        .button2 {padding: 12px 28px;}
        .button3 {padding: 14px 40px;}
        .button4 {padding: 32px 16px;}
        .button5 {padding: 10px;}
        </style>
      </head>
      <body>
        <br>
      <center>
      <table border="1" id="customers" style="width:97%; border-radius: 4px;">
        <tr style="background-color:red; color:#fff;">
          <th style="width:80%;" colspan="2"> ตรวจสอบยอดค้างชำระ </th>
          <!-- <th> <img src="img/logo.png" alt="" style="width:50%;"> </th> -->
        </tr>
        <tr>
          <td>
            <?php
              // ยอดรวม
              if ($output == ""){
                $price = $due_amnt+0.00;
              } else {
                $price = $due_amnt+$output;
              }

              $echoprice = $billerid."&ref1=".$ref1."&ref2=".$ref2."&price=".$price;
              // echo $echoprice;

              echo "เลขที่สัญญา : ".$ct_no."<br>";
              echo "ชื่อ-นามสกุล : ".$ps_name."<br>";
              echo "สินค้า : ".$product_name."<br>";
              echo "ชำระต่องวด : <b>".number_format($due_amnt,2)."</b> บาท<br>";
              echo $outBK." : <b><font color='red'>".$output."</font></b> บาท<br>";
              echo "<b><font color='red'> ยอดรวมชำระทั้งหมด : ".number_format($price,2)." บาท</font></b><br>";

              // echo $billerid." ".$ref1." ".$ref2." ".$price."00";
              // |010556020330003 2511630060538 0409219337248 73000
              
            ?>
          </td>
        </tr>
      </table>
      <table border="0" style="width:97%; ">
        <tr>
          <td style="width:90%; text-align:right;">
            
            <!-- line_barcode_x.php?ct_no=<?php echo $ct_no; ?> -->
            <a href="https://ehongmd.com/barcode_pay_md.php">
              <input type="submit" name="" class="buttonx button5" style="background-color: Gold;" value="ชำระเงินผ่าน Barcode">
            </a>
            <a href="line_qrcode.php?ct_no=<?php echo $ct_no; ?>&billerid=<?php echo $echoprice; ?>">
              <input type="submit" name="" class="buttonx button5" style="background-color: Gold;" value="ชำระเงินผ่าน QR Code">
            </a>

          </td>
        </tr>
        <div class="container">

        <tr>
          <td style="width:10%; text-align:right;">
              <input type="submit" name="" class="buttonx button5" style="background-color: LightSeaGreen;" data-toggle="collapse" data-target="#demo" value="ประวัติการชำระ">
          </td>
        </tr>
      </table>
      <hr>
        <!-- <button type="button" class="btn btn-info buttonx button5" style="background-color: LightSeaGreen;" >ประวัติการชำระ</button> -->
        <div id="demo" class="collapse">
          <table border="0" id="customers" class="xxstable" style="width:92%; background-color:;">
            <thead>
            <tr style="background-color:black; color:#fff;">
              <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>
          </thead>
          <tbody>
          <?php
            $sql = "SELECT * FROM EHONGDB.dbo.hps_tran WHERE ct_no = '$ct_no'
            ORDER BY tr_date";
            $select=$dbh->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)+543; $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; ?>;">
                <!-- <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"> -->
                  <?php echo $e." ".$pay_amnt; ?>
                <!-- </a> -->
              </td>
              <td style="text-align:center; background-color:<?php echo $bcolor; ?>; color:<?php echo $color; ?>;">
                <a href="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="">
                  <!-- <button style='font-size:;' class="buttonx button5" style="background-color: LightSeaGreen;"> </button> -->
                  <i class='fa fa-search-plus'></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=$dbh->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>
          </tbody>
          <?php
          }
          }
          }
          }
         ?>
         <!-- <tfoot>
         <tr>
           <th colspan="2" style="text-align:center; background-color:black; color:#fff;"> รวม </th>
           <th style="text-align:right; background-color:black; color:#fff;"><?php echo number_format($sum-$sumx,2);?></th>
           <th style="background-color:black; color:#fff;"></th>
         </tr>
       </tfoot> -->
       </table>
     <br><br>
     </center>
    </div>
  </div>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script charset="utf-8" src="https://static.line-scdn.net/liff/edge/2/sdk.js"></script>
    <script src="index_linemd1.js"></script>

    <?php include 'datatable.php' ?>

  </body>
</html>
