<?php
  include '_connect.php';
  $ctnos = "0065630001";

  if(isset($ctnos)) {
    $ctnos = $ctnos;
    $output = '';
    $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 = '$ctnos'";
    $stmt = $dbh->prepare($sql);
    $stmt->execute();
    // echo $sql;
    // $output .= '<form action="qrcode_pay_md2.php" method="POST">
    // <p>กรุณากรอกจำนวนเงินที่ต้องการชำระ<p>';
    while($row = $stmt->fetch())
    {
      $ctno = $row['ct_no'];
      ////////////////////////////////////
      // $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($ctnos ,0,1);
      $ctnoN2 = substr($ctnos ,1,1);
      $ctnoN3 = substr($ctnos ,2,1);
      $ctnoN4 = substr($ctnos ,3,1);
      $ctnoN5 = substr($ctnos ,4,1);
      $ctnoN6 = substr($ctnos ,5,1);
      $ctnoN7 = substr($ctnos ,6,1);
      $ctnoN8 = substr($ctnos ,7,1);
      $ctnoN9 = substr($ctnos ,8,1);
      $ctnoN10 = substr($ctno ,9,1);
      $ctnoTotal=$ctnoN1+$ctnoN2+$ctnoN3+$ctnoN4+$ctnoN5+$ctnoN6+$ctnoN7+$ctnoN8+$ctnoN9+$ctnoN10+1;
      $digit1 = substr($ctnoTotal ,1,1);
      $log10 = log10($ctnos);
      $digit2 = substr($log10 ,2,1);
      $chkdigit3=strlen($ctnos);
      $sumdigit3 = $chkdigit3+$digit1+$digit2;
      $digit3 = substr($sumdigit3 ,1,1);
      $sumref1 = $digit1.$digit2.$digit3;
      $ref1 = $ctnos.$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;
      // -- END REF2 -- //














      // -- ตั้งเงื่อนไข -- //
      $sql_chk = "SELECT TOP 1 ct_no,status FROM [EHONGDB].[dbo].[hps_ctdue]
      WHERE ct_no ='$ctno' 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 = '$ctnos' 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 คำนวณยอดที่ต้องชำระ -- //
      //////////////////////////////
      // $output .='
      //   <input type="text" name="billerid" id="billerid" value="'.$billerid.'" hidden>
      //   <input type="text" name="ref1" id="ref1" value="'.$ref1.'" hidden>
      //   <input type="text" name="ref2" id="ref2" value="'.$ref2.'" hidden>
      //   <input type="text" name="productname" id="productname" value="'.$row['product_name'].'" hidden>
      //   <input type="text" name="cusname" id="cusname" value="'.$row['TI_NAME'].$row['PS_FNAME']."  ".$row['PS_LNAME'].'" hidden>
      //   <input type="hidden" name="ifempty" id="ifempty" value="'.$total_Camount.'">
      //   <p>ผู้ซื้อ '.$row['TI_NAME'].$row['PS_FNAME']."  ".$row['PS_LNAME'].'</p>
      //   <p>สินค้า '.$row['product_name'].'</p>
      //   <p>*** จำนวนเงินต่องวด <font color="black"><u>'.$Camount.'</u></font> บาท</p>';
      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'];
                $output .='<p> เบี้ยปรับ <font color="black"><u>'.$row_SD['slowly_amnt'].'</u></font> บาท</p>';
              }else{
                $total_Camount+=$due_amntTotal;
                $output .='<p> ค้างชำระ <font color="black"><u>'.$due_amntTotal.'</u></font> บาท</p>';
              }
          }else{
            $output .='<p> ไม่มียอดค้างชำระ</p>';
          }
        }else{
          $output .='<p> ไม่มียอดค้างชำระ</p>';
        }
      }else{
            $output .='<p> ไม่มียอดค้างชำระ</p>';
      }
        // $output .='<p>*** ยอดทั้งหมด <font class="modal-pop-up">'.$total_Camount.'</font> บาท</p>
        // ลูกค้าต้องการชำระ  <input type="text" name="price" id="price" onkeyup="chk()" size="8" onBlur="checkValue(this,this.defaultValue)" onFocus="clearValue(this,this.defaultValue)" value="กรุณากรอกจำนวนเงินที่ต้องการชำระ"><br>
        // <input type="submit" id="btsearchp" class="btn btn-info btn-lg" value="สร้างคิวอาร์โค้ด">';
      } 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 = '$ctnos' 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'];
        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 = '$ctnos' 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 = '$ctnos' 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 คำนวณยอดที่ต้องชำระ -- //
        //////////////////////////////
        // $output .='
        //   <input type="text" name="billerid" id="billerid" value="'.$billerid.'" hidden>
        //   <input type="text" name="ref1" id="ref1" value="'.$ref1.'" hidden>
        //   <input type="text" name="ref2" id="ref2" value="'.$ref2.'" hidden>
        //   <input type="text" name="productname" id="productname" value="'.$row['product_name'].'" hidden>
        //   <input type="text" name="cusname" id="cusname" value="'.$row['TI_NAME'].$row['PS_FNAME']."  ".$row['PS_LNAME'].'" hidden>
        //   <input type="hidden" name="ifempty" id="ifempty" value="'.$total_Camount.'">
        //   <p>ผู้ซื้อ '.$row['TI_NAME'].$row['PS_FNAME']."  ".$row['PS_LNAME'].'</p>
        //   <p>สินค้า '.$row['product_name'].'</p>
        //   <p>*** จำนวนเงินต่องวด <font color="black"><u>'.$Camount.'</u></font> บาท</p>';
          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'];
                  $output .='<p>เบี้ยปรับ <font color="black"><u>'.$row_SD['slowly_amnt'].'</u></font> บาท</p>';
                }else{
                  $total_Camount+=$due_amntTotal;
                  $output .='<p>ค้างชำระ <font color="black"><u>'.$due_amntTotal.'</u></font> บาท</p>';
                }
            }else{
              $output .='<p>ไม่มียอดค้างชำระ</p>';
            }
          }else{
            $output .='<p>ไม่มียอดค้างชำระ</p>';
          }
          // $output .='<p>*** ยอดทั้งหมด <font class="modal-pop-up">'.$total_Camount.'</font> บาท</p>';

          // $output .='<p>*** ยอดทั้งหมด <font class="modal-pop-up">'.$total_Camount.'</font> บาท</p>
          // ลูกค้าต้องการชำระ  <input type="text" name="price" id="price" onkeyup="chk()" size="8" onBlur="checkValue(this,this.defaultValue)" onFocus="clearValue(this,this.defaultValue)" value="กรุณากรอกจำนวนเงินที่ต้องการชำระ"><br>
          // <input type="submit" id="btsearchp" class="btn btn-info btn-lg" value="สร้างคิวอาร์โค้ด">';
        }





        // $data=array();'.$Camount.'
        // $data+=array("ct_no"=>$row['cnsvrefcode']);
        // $data+=array("ct_nos"=>$row['ct_no']);
        // $data+=array("product_name"=>$row['product_name']);
        // $data+=array("due_amnt"=>$row['due_amnt']);
        // $data+=array("cnsvrefcode"=>$row['cnsvrefcode']);
        // $data+=array("ps_name"=>$row['TI_NAME'].$row['PS_FNAME']."  ".$row['PS_LNAME']);
        // $data+=array("biller_id"=>"$billerid");
        // $data+=array("ref_1"=>"$ref1");
        // $data+=array("ref_2"=>"$ref2");
        // $data+=array("Camount"=>"$Camount");
        // echo json_encode($data);
      }
      $output .= '</form>';
        echo $output;
        //echo json_encode($data);
    }
?>
