      <?php
  $admin_id = $_SESSION['admin_id'];
    // $rs = $db->findAll('tbiv')->execute();
        function DateThai($strDate){
      $strYear = date("Y",strtotime($strDate))+543;
      $strMonth= date("n",strtotime($strDate));
      $strDay= date("j",strtotime($strDate));
      $strHour= date("H",strtotime($strDate));
      $strMinute= date("i",strtotime($strDate));
      $strSeconds= date("s",strtotime($strDate));
      $strMonthCut = Array("","ม.ค.","ก.พ.","มี.ค.","เม.ย.","พ.ค.","มิ.ย.","ก.ค.","ส.ค.","ก.ย.","ต.ค.","พ.ย.","ธ.ค.");
      $strMonthThai=$strMonthCut[$strMonth];
      return "$strDay $strMonthThai $strYear";
    }
      function DateGoBack($DateGo,$DateBack){
      $strYear = date("Y",strtotime($DateBack))+543;
      $strMonth= date("n",strtotime($DateBack));
      $strDay = date("j",strtotime($DateBack));
      $strDay2 = date("j",strtotime($DateGo));
      // $strMonthCut = Array("","มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม");
      $strMonthCut = Array("","ม.ค.","ก.พ.","มี.ค.","เม.ย.","พ.ค.","มิ.ย.","ก.ค.","ส.ค.","ก.ย.","ต.ค.","พ.ย.","ธ.ค.");
      $strMonthThai=$strMonthCut[$strMonth];
      return "$strDay2 - $strDay $strMonthThai $strYear";
    }

   $today = date("Y-m-d");
   if ($_SESSION['admin_status'] == 0 || $_SESSION['admin_status'] == 2) {
    $book_t = $db->findByAttributes('tbiv',array('iv_date =' => $today))->execute();
    $rs = $db->findAll('tbiv')->execute();
   }else{
    // $book_t = $db->findByAttributes('tbiv',array('iv_date =' => $today))->execute();
  if($_SESSION['admin_id'] == 49){
    $book_t = $db->findByAttributes('tbiv',array('iv_date =' => $today, 'iv_admin =' => 'เฟิร์น'))->execute();
    $book_t2 = $db->findByAttributes('tbiv',array('iv_date =' => $today, 'iv_admin =' => $admin_id))->execute();
    $book_ad = $db->findByAttributes('tbiv',array('admin_nickname =' => $admin_id))->execute();
    $rs = $db->findByAttributes('tbiv',array('iv_admin =' => 'เฟิร์น'))->execute();
    $rs2 = $db->findByAttributes('tbiv',array('iv_admin =' => 'ตาตั้ม'))->execute();
    // $rs2 = $db->findByAttributes('tbiv',array('iv_admin =' => $admin_id))->execute();
    }else if($_SESSION['admin_id'] == 51){
    $book_t = $db->findByAttributes('tbiv',array('iv_date =' => $today, 'iv_admin =' => 'หนุงหนิง'))->execute();
    $book_t2 = $db->findByAttributes('tbiv',array('iv_date =' => $today, 'iv_admin =' => $admin_id))->execute();
    $book_ad = $db->findByAttributes('tbiv',array('admin_nickname =' => $admin_id))->execute();
    $rs = $db->findByAttributes('tbiv',array('iv_admin =' => 'หนุงหนิง'))->execute();
    $rs2 = $db->findByAttributes('tbiv',array('iv_admin =' => $admin_id))->execute();
    }else if($_SESSION['admin_id'] == 52){
    $book_t = $db->findByAttributes('tbiv',array('iv_date =' => $today, 'iv_admin =' => 'Patra'))->execute();
    $book_t2 = $db->findByAttributes('tbiv',array('iv_date =' => $today, 'iv_admin =' => $admin_id))->execute();
    $book_ad = $db->findByAttributes('tbiv',array('admin_nickname =' => $admin_id))->execute();
    $rs = $db->findByAttributes('tbiv',array('iv_admin =' => 'Patra'))->execute();
    $rs2 = $db->findByAttributes('tbiv',array('iv_admin =' => $admin_id))->execute();
    }else{
    $book_ad = $db->findByAttributes('tbiv',array('admin_id =' => $admin_id))->execute();
    $rs = $db->findByAttributes('tbiv',array('admin_id =' => $admin_id))->execute();
   }

   }


  
?>      

<section class="panel" style="color:red;display:none;">
              <header class="panel-heading">
                  <!-- <a href="#" class="fa fa-caret-down"></a>
                  <a href="#" class="fa fa-times"></a -->
        <a href="?url=iv_form" data-link="?url=iv_form" class="btn btn-success with-shadow-success" style="position: absolute;right: 16px;top: 10px;">เพิ่ม INVOICE ใหม่</a>
              <h1 class="panel-title">ใบจองที่จะออกเดินทาง</h1>
              </header>
              <div class="panel-body">
                <table class="table table-bordered table-striped mb-none" id="datatable-tabletoolss">
                  <thead>
                    <tr>
                      <th>NO</th>
                      <th>Programtour</th>
                      <th>SALE</th>
                      <th>INVOICE</th>
                      <th>ใบเสร็จ</th>
                      <th>STATUS</th>
                      <th>Actions</th>
                    </tr>
                  </thead>
                  <tbody>
                <?php
                // $rs = $db->findAll('tbiv')->execute(); 
                      $i=0;
                        while($dbarr= mysql_fetch_array($book_t)){ 
                    echo "<tr>";  $i++ ;
                    echo "    <td class='center'>$i</td>";
                    echo "    <td> $dbarr[iv_codepro] $dbarr[iv_namepro]</td>";
                    echo "    <td class='center'>$dbarr[iv_admin]</td>";
                            echo "<td class='center'>
                            <a href='https://www.imaginetourservice.com/ivnew.php?id=$dbarr[iv_id]' target='blank' class='btn btn-edit-color'><i class='halflings-icon file white'></i>IM$dbarr[iv_id]</a>
                           <a href='https://www.imaginetourservice.com/ivnew_en.php?id=$dbarr[iv_id]' target='blank' class='btn btn-edit-color'><i class='halflings-icon file white'></i>(ENG)</a>
                            </td>";
                            echo " <td class='center'>
                            <a class='btn btn-primary' href='?url=receipt_form&iv=$dbarr[iv_id]'>
                            <i class='fa fa-file'></i>  </a>";
                            echo "</td>";
                            echo " <td class='center'>
                            <a class='btn btn-danger' href='?url=iv_edit&iv=$dbarr[iv_id]'>
                            <i class='fa fa-edit fa-x'></i>  </a>";
                            echo "</td></tr>";  
                }//mysql_close($link);
                    ?> 
                  </tbody>
                </table>
              </div>
            </section>

            <section class="panel" style="color:red;">
              <header class="panel-heading">
                  <!-- <a href="#" class="fa fa-caret-down"></a>
                  <a href="#" class="fa fa-times"></a -->
                <h1 class="panel-title">เลือก INVOICE</h1>
              </header>
                <header class="panel-heading">
             <div class="row">
              <!-- <div class="col-md-1"> <label>ผู้ขาย :</label></div> -->
<!--           <div class="col-md-2">
              <label>สถานะ :</label><br>
             <label data-target="#tophead" class="btn btn-success"> <input type="checkbox" class="filter-checkbox-iv" value="ปกติ"/> ปกติ </label>
             <label data-target="#tophead" class="btn btn-dark"> <input type="checkbox" class="filter-checkbox-iv" value="ยกเลิก"/>  ยกเลิก </label>
            </div>
              <div class="col-md-3">
                <label>ผู้ขาย :</label>
                <select class="form-control status-dropdown">
                <option value="">All</option>
                <option value="เฟิร์น">เฟิร์น</option>
                <option value="หนุงหนิง">หนุงหนิง</option>
              </select>
            </div>
           <div class="col-md-3">
                <label>เดือนที่เดินทาง :</label>
                <select class="form-control datego-dropdown">
                <option value="">All</option>
                <option value="ม.ค.">ม.ค.</option>
                <option value="ก.พ.">ก.พ.</option>
                <option value="มี.ค.">มี.ค.</option>
                <option value="เม.ย.">เม.ย.</option>
                <option value="เม.ย.">พ.ค.</option>
                <option value="เม.ย.">มิ.ย.</option>
                <option value="เม.ย.">มิ.ย.</option>
                <option value="ก.ค.">ก.ค.</option>
                <option value="ส.ค.">ส.ค.</option>
                <option value="ก.ย.">ก.ย.</option>
                <option value="ต.ค.">ต.ค.</option>
                <option value="พ.ย.">พ.ย.</option>
                 <option value="ธ.ค.">ธ.ค.</option>
              </select>
            </div>
            </div> -->
              </header>

              <div class="panel-body">
                <table class="table table-bordered table-striped mb-none" id="datatable-iv">
                  <thead>
                    <tr>
                      <th>NO</th>
                      <th style="width:160px;">INVOICE</th>
                      <th style="width:160px;">REF</th>
                      <th style="width:300px;">ชื่อลูกค้า</th>
                      <th style="width:100px;">Code</th>
                      <th style="width:400px;">Programtour</th>
                      <th style="width:50px;text-align: center;">SALE</th>
                      <th>PAX</th>
                      <th>ราคา</th>
<!--                       <th>ส่วนลด/ราคาเพิ่ม</th>
                      <th>จ่ายแล้ว</th>
                      <th>ราคารวม</th>
                      <th>เหลือที่ต้องชำระ</th> -->
                      <th>STATUS</th>
                      <th>Actions</th>
                     <!--  <th>TOTAL<br>COM</th> -->
                    </tr>
                  </thead>
                  <tbody>
                <?php
                // $rs = $db->findAll('tbiv')->execute(); 
                      $i=0;
                        while($dbarr= mysql_fetch_array($rs)){ 
                        // if($dbarr[iv_admin] == 'เฟิร์น'){

                        // }

$book_ccc = $db->findByAttributes('tbpass',array('pass_iv =' => $dbarr['iv_id'] ))->execute();
$ca_book = 0;
$payin = $db->findByPK('tbpayment','payment_iv',$dbarr['iv_id'])->executeRow();
while($c = mysql_fetch_assoc($book_ccc)){
    $ca_book++;
  }
                $file = $dbarr[programe_file];
                if ($dbarr['iv_status_2'] == '0') {
                  echo "<tr>";
                }else{
                 echo "<tr style='background: #8080804a;'>";
                } $i++ ;
                    echo "    <td class='center'>$i</td>";
                            echo "<td class='center'>
                            <a href='https://www.imaginetourservice.com/ivnew.php?id=$dbarr[iv_id]' target='blank' class='btn btn-edit-color' style='font-size:1.2rem;'><i class='halflings-icon file white'></i>IM$dbarr[iv_id]</a>
                            </td>";

                    echo "<td class='center'>";

                    if(!empty($dbarr['iv_ref1'])){
                    echo "<a href='https://www.imaginetourservice.com/ivnew.php?id=$dbarr[iv_ref1]' target='blank' class='btn btn-edit-color' style='font-size:1.2rem;'><i class='halflings-icon file white'></i>IM$dbarr[iv_ref1]</a>";
                     }
                    if(!empty($dbarr['iv_ref2'])){
                    echo "<a href='https://www.imaginetourservice.com/ivnew.php?id=$dbarr[iv_ref2]' target='blank' class='btn btn-edit-color' style='font-size:1.2rem;'><i class='halflings-icon file white'></i>IM$dbarr[iv_ref2]</a>";
                     }
                    if(!empty($dbarr['iv_ref3'])){
                    echo "<a href='https://www.imaginetourservice.com/ivnew.php?id=$dbarr[iv_ref3]' target='blank' class='btn btn-edit-color' style='font-size:1.2rem;'><i class='halflings-icon file white'></i>IM$dbarr[iv_ref3]</a>";
                     }


                            
                    echo "</td>";
     

                    echo "    <td class='text-left' style='line-height:21pt;'>$dbarr[cus_name] <br> TEL: $dbarr[cus_tel]</td>";
                    echo "    <td> $dbarr[iv_codepro]</td>";
                    echo "    <td style='line-height:21pt;'> $dbarr[iv_namepro]</td>";
                    echo "    <td class='center'>$dbarr[iv_admin]</td>";
                    // echo "    <td>".DateGoBack($dbarr[pro_datego],$dbarr[pro_dateback])."</td>";
                            echo "    <td class='center'><i class='fa fa-user' aria-hidden='true'></i> $dbarr[adult_num]</td>";
                            // echo " <td class='center'>
                            // <a class='btn btn-primary' href='?url=receipt_form&iv=$dbarr[iv_id]'>
                            // <i class='fa fa-file'></i>  </a>";
                          //   echo "</td>";
                          // if ($ca_book != '0') {
                          //  echo " <td class='center'>
                          //   <a class='btn btn-danger' href='?url=pass_add&iv=$dbarr[iv_id]' style='font-size:0.9rem;'>
                          //   <i class='fa fa-users' aria-hidden='true'></i> PASSENGER <br> ".$ca_book." / $dbarr[adult_num]</a>";
                          //   echo "</td>"; 
                          // }else{
                          //  echo " <td class='center'>
                          //   <a class='btn btn-dark' href='?url=pass_add&iv=$dbarr[iv_id]' style='font-size:0.9rem;'>
                          //   <i class='fa fa-users' aria-hidden='true'></i> PASSENGER <br> ".$ca_book." / $dbarr[adult_num]</a>";
                          //   echo "</td>"; 
                          // }
                        $sum_ad= $dbarr['adult_price'] * $dbarr['adult_num'];
                        $sum_c= $dbarr['c_price'] * $dbarr['c_num'];
                        $sum_nb= $dbarr['nb_price'] * $dbarr['nb_num'];
                        $sum_s= $dbarr['s_price'] * $dbarr['s_num'];


$custom= $dbarr['custom1_price'] * $dbarr['custom1_num'];
$custom2= $dbarr['custom2_price'] * $dbarr['custom2_num'];

$custom3= $dbarr['custom3_price'] * $dbarr['custom3_num'];
$custom4= $dbarr['custom4_price'] * $dbarr['custom4_num'];

                        $total_add=$custom + $custom2 + $custom3 + $custom4;

                        $sum_dis= $dbarr['discount_price'] * $dbarr['discount_sum'];
                        $sum_dis2= $dbarr['cus_de_price'] * $dbarr['cus_de_num'];
                        $sum_dis3= $dbarr['cus_de_price2'] * $dbarr['cus_de_num2'];
                        $total_dis= $sum_dis + $sum_dis2 + $sum_dis3;
                        $total=$sum_ad + $sum_c + $sum_nb + $sum_s + $custom + $custom2 + $custom3 + $custom4 - $sum_dis - $sum_dis2 - $sum_dis3;

// $gp = $db->findByPK('tbreceipt,tbpayment','receipt_iv',$dbarr['iv_id'])->execute();

                        $gp = $db->findByPK('tbpayment','payment_iv',$dbarr['iv_id'])->execute();
                        $pay = 0;
                        while($gpm = mysql_fetch_array($gp)){
                        $re_custom = $db->findByPK('tbreceipt','receipt_pay',$gpm['payment_id'])->executeRow();
                        $pay += $re_custom['receipt_total'];
                        }

                        $balance = $total-$pay;

                            echo "<td style='line-height:21pt;text-align:center;'> ".number_format($total)."</td>";
                            // echo "<td style='line-height:21pt;color:red;text-align:center;'> (-".number_format($total_dis).") <br>
                            // <span style='color:#000;'>+".number_format($total_add)."</span></td>";
                            // echo "<td style='line-height:21pt;text-align:center;' class='text-primary'> ".number_format($pay)."</td>";
                            // echo "<td style='line-height:21pt;text-align:center;'> ".number_format($pay)."</td>";
                            // echo "<td style='line-height:21pt;text-align:center;'  class='text-danger'> ".number_format($balance)."</td>";

// <a class='btn btn-primary' href='?url=iv_list&iv=$dbarr[iv_id]'><i class='fa fa-search'></i></a>

                               if ($payin['payment_iv'] == $dbarr['iv_id']) {
                            echo "<td style='line-height:21pt;text-align:center;'  class='text-danger'>
                             <a class='btn btn-primary' href='?url=receipt_form&iv=$dbarr[iv_id]'  style='font-size:0.8rem;display:block;'>มีการขำระเงิน</a>
                             </td>";
                            }else{
                            echo "<td style='line-height:21pt;text-align:center;'  class='text-danger'>
                             <a class='btn btn-dark' href='?url=receipt_form&iv=$dbarr[iv_id]' style='font-size:0.8rem;display:block;'>ยังไม่มีการชำระ</a>
                             </td>";
                            }

                            echo "<td style='line-height:21pt;text-align:center;'  class='text-danger'>
                             <a class='btn btn-primary' style='display:block;' href='?url=receipt_form&iv=$dbarr[iv_id]'>
                            <i class='fa fa-file'></i>  </a>
                             </td></tr>";
                            // echo " <td class='center'>
                            // <a class='btn btn-edit-color' data-toggle='modal' data-target='#Modal$dbarr[iv_id]' href='#' style='padding:5px;'>
                            // L/WS </a>
                            // <a class='btn btn-danger d-none' href='?url=iv_edit&iv=$dbarr[iv_id]'>
                            // <i class='fa fa-edit fa-x'></i>  </a>
                            // ";
                            // echo "</td></tr>"; 
                  
                        
          
                }//mysql_close($link);
           //             while($dbarr= mysql_fetch_array($rs2)){ 
           //             // if($dbarr[iv_admin] == 'เฟิร์น'){

           //             // }
                // $file = $dbarr[programe_file];
           //         echo "<tr>";  $i++ ;
           //         echo "    <td class='center'>$i</td>";
           //                  echo "<td class='center'>
           //                  <a href='https://www.imaginetourservice.com/ivnew.php?id=$dbarr[iv_id]' target='blank' class='btn btn-edit-color' style='font-size:1.2rem;'><i class='halflings-icon file white'></i>IM$dbarr[iv_id]</a>
           //                 <a href='https://www.imaginetourservice.com/ivnew_en.php?id=$dbarr[iv_id]' target='blank' class='btn btn-edit-color' style='font-size:1.2rem;'><i class='halflings-icon file white'></i>(ENG)</a>
           //                  </td>";
           //          echo "    <td> $dbarr[iv_codepro] ( ".datethai($dbarr[pro_datego])." - ".datethai($dbarr[pro_dateback])." )</td>";
           //         echo "    <td> $dbarr[iv_codepro] $dbarr[iv_namepro]</td>";
           //         echo "    <td class='center'>$dbarr[iv_admin]</td>";
           //          echo "    <td class='center'>".datethai($dbarr[iv_date])."</td>";
           //          echo "    <td class='text-left'>$dbarr[cus_name] <br> TEL: $dbarr[cus_tel]</td>";
           //                  echo "    <td class='center'><i class='fa fa-user' aria-hidden='true'></i> $dbarr[adult_num]</td>";
           //                  echo " <td class='center'>
           //                  <a class='btn btn-primary' href='?url=receipt_form&iv=$dbarr[iv_id]'>
           //                  <i class='fa fa-file'></i>  </a>";
           //                  echo "</td>";
           //                  echo " <td class='center'>
           //                  <a class='btn btn-dark' href='?url=pass_add&iv=$dbarr[iv_id]' style='font-size:0.9rem;'>
           //                   <i class='fa fa-users' aria-hidden='true'></i> PASSENGER</a>";
           //                  echo "</td>";    
           //                  echo " <td class='center'>
           //                  <a class='btn btn-danger' href='?url=iv_edit&iv=$dbarr[iv_id]'>
           //                  <i class='fa fa-edit fa-x'></i>  </a>";
           //                  echo "</td></tr>"; 
                  
                        
          
              //  }
       
                    ?> 
                  </tbody>
                </table>
              </div>
            </section>
