<?php
    // error_reporting(0);
    $ct_no = $_GET['ct_no'];
    // $ct_no = "257550043";
    include '_connect.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";
    $sql_array = array();
    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 = "";
      }

      $sql_array[] = array(
        "TR_NO" => $fetch['TR_NO'],
        "ct_no" => $fetch['ct_no'],
        "PAY_AMNT" => $pay_amnt,
        "tr_datex" => $tr_datex,
        "d" => $d,
        "e" => $e,
        "bcolor" => $bcolor,
        "color" => $color,
        "td_detail" => '',
        "pay_amnt_c" => '',
        "icon" => "<i class='fa fa-search-plus'></i>",
        "recbranch" => $fetch['recbranch'],
        "REF_NO" => $fetch['REF_NO'],
        "REF_SEQ" => $fetch['REF_SEQ']
        );
    
      // echo json_encode($sql_array);

      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'];

            $sql_array[] = array(
              // "TR_NO" => $fetch['TR_NO'],
              // "PAY_AMNT" => $pay_amnt,
              // "tr_datex" => $tr_datex,

              "TR_NO" => '',
              "ct_no" => '',
              "PAY_AMNT" => '',
              "tr_datex" => '',
              "d" => '',
              "e" => '',
              "bcolor" => '',
              "color" => '',
              "td_detail" => "<lable style='color:red'> - ".$fetchc['td_detail']."<lable>",
              "pay_amnt_c" => "<lable style='color:red'>".$pay_amnt_c."<lable>",
              "icon" => '',
              "recbranch" => '',
              "REF_NO" => '',
              "REF_SEQ" => ''
              );
          }
        }
    }
  }
  echo json_encode($sql_array);



?>