<?php error_reporting(0); ?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>EHONGMD | ยืนยันโอนค่าใช้จ่ายสาขา</title>
  <link rel="shortcut icon" href="dist/img/logo_1.png" />
  <?php
    include '_plugin.php';
    include '_connect.php';
  ?>
</head>
<body class="hold-transition skin-red sidebar-mini">
<div class="wrapper">
<?php
include '_header.php';
include '_menu.php';
?>
  <div class="content-wrapper">
    <section class="content">
      <div class="row">
      <!-- ค้นหา -->
      <!-- ตาราง -->
      <script src="bower_components/jquery/dist/jquery.min.js"></script>
      <?php
      // $thai_month_arr=array(
      //     "0"=>"",
      //     "01"=>"มกราคม",
      //     "02"=>"กุมภาพันธ์",
      //     "03"=>"มีนาคม",
      //     "04"=>"เมษายน",
      //     "05"=>"พฤษภาคม",
      //     "06"=>"มิถุนายน",
      //     "07"=>"กรกฎาคม",
      //     "08"=>"สิงหาคม",
      //     "09"=>"กันยายน",
      //     "10"=>"ตุลาคม",
      //     "11"=>"พฤศจิกายน",
      //     "12"=>"ธันวาคม"
      // );
      if (isset($_POST['submit'])) {
        $date_1 = $_POST['date1'];
        $date_2 = $_POST['date2'];
        $status = $_POST['status'];
        $br_id = $_POST['br_id'];
        if ($status == "C") {
          $ss = "C";
          $sn = "อนุมัติแล้ว";
        }
        if ($status == "S") {
          $ss = "S";
          $sn = "ยืนยันโอนเงิน";
        }
      }
      else {
        date_default_timezone_set('Asia/Bangkok');
        $date_1 = date('Y-m-d');
        $dm = date("t",strtotime("$date_1"));
        $date_2 = date('Y-m-').$dm;
        $status = "";
        $ss = "";
        $sn = "เลือก";
      }
      // echo $status;
      ?>
      <div class="col-md-12">
        <div class="box box-success">
          <div class="box-header with-border">
            <h3 class="box-title">EHONGMD | ยืนยันโอนค่าใช้จ่ายสาขา</h3>
          </div>
          <!-- target="_blank" lettersum.php-->
          <form role="form" action="" method="POST" enctype="multipart/form-data" >
            <div class="box-body">
              <div class="row">
                <div class="col-xs-2">
                  <p>จากวันที่</p>
                  <input type="date" class="form-control" placeholder="" name="date1" value="<?php echo $date_1; ?>">
                </div>
                <div class="col-xs-2">
                  <p>ถึงวันที่</p>
                  <input type="date" class="form-control" placeholder="" name="date2" value="<?php echo $date_2; ?>">
                </div>
                <div class="col-xs-2">
                  <p>สาขา</p>
                  <select name="br_id" id="" class="form-control">
                    <?php
                      $sql = "SELECT * FROM EHONGDB.dbo.BRANCH WHERE BR_ID NOT IN ('%') AND BR_ID = '$br_id'";
                      $select=$conn->prepare($sql);
                      $select->execute();
                      while($fetch=$select->fetch()){
                    ?>
                      <option value="<?php echo $fetch['BR_ID']; ?>"><?php echo $fetch['BR_ID']." : ".$fetch['BR_NAME']; ?></option>
                    <?php } ?>
                    <option value="">ทั้งหมด</option>
                    <?php
                      $sql = "SELECT * FROM EHONGDB.dbo.BRANCH WHERE BR_ID NOT IN ('%')";
                      $select=$conn->prepare($sql);
                      $select->execute();
                      while($fetch=$select->fetch()) {
                    ?>
                      <option value="<?php echo $fetch['BR_ID']; ?>"><?php echo $fetch['BR_ID']." : ".$fetch['BR_NAME']; ?></option>
                    <?php } ?>
                   </select>
                 </div>
                 <div class="col-xs-2">
                   <p>อนุมัติ / ยืนยันโอน</p>
                   <select name="status" id="" class="form-control">
                     <option value="<?php echo $ss; ?>"><?php echo $sn; ?></option>
                     <option value="C">อนุมัติแล้ว</option>
           					 <option value="S">ยืนยันโอนเงิน</option>
                    </select>
                  </div>
              </div>
            </div>
            <div class="box-footer">
              <input type="submit" name="submit" id="" class="btn btn-success" value="ตรวจสอบ">
              <!-- <button class="btn btn-success"><a style="color:#FFF;" href="transport_index_5.php" target="_blank">พิมพ์ใบรับรองแทนใบเสร็จรับเงิน FC</a></button> -->
            </div>
          </form>

        </div>
      </div>

      <?php
      if (isset($_POST['submit'])) {
        $stdate = $_POST['date1'];
        $fndate = $_POST['date2'];
        $status = $_POST['status'];
        $br_id = $_POST['br_id'];
        if($status=="N") $str=" (bill_date between '$stdate' and '$fndate') and (status is null or status='N')";
        if($status=="C") $str=" (bill_date between '$stdate' and '$fndate') and (status='C')";
        if($status=="S") $str=" (status='S' and (trans_date between '$stdate' and '$fndate'))";
        if($status=="V") $str=" (status='V' and (trans_date between '$stdate' and '$fndate'))";
        if ($br_id!="") {
          $br_id_1 = "AND a.br_id IN ('0001')";
        }
        else {
          $br_id_1 = "";
        }
        $st1 = substr($stdate,0,4)+543; $st2 = substr($stdate,5,2); $st3 = substr($stdate,8,2);
        $date11 = $st3."/".$st2."/".$st1;
        $sl1 = substr($fndate,0,4)+543; $sl2 = substr($fndate,5,2); $sl3 = substr($fndate,8,2);
        $date22 = $sl3."/".$sl2."/".$sl1;
      ?>
      <div class="col-md-12">
        <div class="box box-success">
          <div class="box-header">
            <h3 class="box-title"><p class="text-success">ยืนยันโอนค่าใช้จ่ายสาขา</p></h3>
            <p class="text-red">จากวันที่ <?php echo $date11; ?> ถึงวันที่ <?php echo $date22; ?> </p>
          </div>
          <div class="box-body">
            <div class="form-group">
              <!-- example31 -->
              <div style="overflow-x:auto;">
                <table id="example" class="table table-striped table-bordered" style="width:100%;">
                  <thead style="white-space: nowrap">
                    <tr >
                      <th rowspan="" style="background-color:black; color:#fff; text-align:center;">NO.</th>
                      <th rowspan="" style="background-color:black; color:#fff; text-align:center;">สาขา</th>
                      <th rowspan="" style="background-color:black; color:#fff; text-align:center;">เลขที่บัญชี</th>
                      <th rowspan="" style="background-color:yellow; color:#000; text-align:right;">จำนวนเงิน</th>
                      <th rowspan="" style="background-color:black; color:#fff; text-align:center;">บัญชี</th>
                    </tr>
                  </thead>
                  <tbody style="white-space: nowrap">
                    <?php
                    $sql = "SELECT
                    br_id,br_name,baccno,bankname,bill_no,bill_date,pre_amnt,trans_amnt,trans_div,status,trans_auditno,trans_date,trans_time,s_total
                    FROM EHONGDB.dbo.BRANCH a
                    left outer join(select br_id as xbr_id,bill_no,bill_date,pre_amnt,trans_amnt,trans_div,status,trans_auditno,trans_date,trans_time,s_total FROM EHONGDB.dbo.ACC_PAYELSEINCCASH where $str) c on (a.br_id=c.xbr_id)
                    where (bill_no is not null) $br_id_1";
                    // echo $sql;
                    // echo $str;
                    $select=$conn->prepare($sql);
                    $select->execute();
                    $item = 1;
                    $sum = 0;
                    while($fetch=$select->fetch()){
                    ?>
                    <tr>
                      <td style="background-color:; color:; text-align:;"><?php echo number_format($item); ?>.</td>
                      <td style="background-color:; color:; text-align:;"><?php echo $fetch['br_id'].":".$fetch['br_name']; ?></td>
                      <td style="background-color:; color:; text-align:;"><?php echo $fetch['baccno']; ?></td>
                      <th style="background-color:; color:blue; text-align:right;"><?php echo number_format($fetch['trans_amnt']); ?></th>
                      <td style="background-color:; color:; text-align:;"><?php echo $fetch['bankname']; ?></td>
                    </tr>
                    <?php
                      $sum += $fetch['trans_amnt'];
                      $item++;
                      }
                    ?>
                  </tbody>
                  <tfoot>
                    <tr>
                      <th colspan="3" style="background-color:black; color:#fff; text-align:center;">รวม</th>
                      <th style="background-color:yellow; color:#000; text-align:right;"><?php echo number_format($sum); ?></th>
                      <th style="background-color:black; color:#fff; text-align:right;"></th>
                    </tr>
                  </tfoot>
              </table>
            </div>
            </div>
          </div>
        </div>
      </div>
      <?php } ?>
      </div>
    </section>
  </div>
</div>

<?php
// include '_footer.php';
// include '_form.php';
?>
<div class="control-sidebar-bg"></div>
<?php
  include '_script_2.php';
  include 'scroll0.php';
?>
</body>
</html>
