 <!DOCTYPE html>
 <html>
 <head>
   <meta charset="utf-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <title>EHONGMD</title>
   <?php
   include '_plugin.php';
   include '_connect.php';
   ?>
   <script src="bower_components/jquery/dist/jquery.min.js"></script>
 </head>
 <body class="hold-transition skin-green sidebar-mini">
  <script language="JavaScript">
  	function ClickCheckAll(vol)
  	{
  		var i=1;
  		for(i=1;i<=document.frmMain.hdnCount.value;i++)
  		{
  			if(vol.checked == true)
  			{
  				eval("document.frmMain.chkPrint"+i+".checked=true");
  			}
  			else
  			{
  				eval("document.frmMain.chkPrint"+i+".checked=false");
  			}
  		}
  	}
  	function onPrint()
  	{
  		if(confirm('ต้องการที่จะพิมพ์ทั้งหมดหรือไม่?')==true)
  		{
  			return true;
  		}
  		else
  		{
  			return false;
  		}
  	}
</script>

 <div class="">
 <?php
 include '_header.php';
 // include '_menu.php';
 ?>
   <div class="">
     <section class="content">
       <div class="">
       <!-- ค้นหา -->
       <?php
       $month = $_GET['M'];
       $year = $_GET['Y'];
       $year2 = $_GET['Y']+543;
       $month2 =  $month;
       if ($_GET['D'] == '') {
         $day = $_GET['D1'];
         $day1 = $year."-".$month2."-".'01';
         $day2 = $year."-".$month2."-".$day;
         $code = "a.CHK_BERG BETWEEN '$day1' AND '$day2'";
       } else {
         $day = $_GET['D'];
         $day1 = $year."-".$month2."-".$day;
         $code = "a.CHK_BERG = '$day1'";
       }
       ?>
       <?php
       include '_connect.php';
       $_month_name = array("01"=>"มกราคม",  "02"=>"กุมภาพันธ์",  "03"=>"มีนาคม",
          "04"=>"เมษายน",  "05"=>"พฤษภาคม",  "06"=>"มิถุนายน",
          "07"=>"กรกฎาคม",  "08"=>"สิงหาคม",  "09"=>"กันยายน",
          "10"=>"ตุลาคม", "11"=>"พฤศจิกายน",  "12"=>"ธันวาคม");
       $m = date('m');

       $now_month = date("$year-$month-01");
       $mk_time = strtotime($now_month);
       $day_no = date("t",$mk_time);
       $wan_no = date("w",$mk_time);
       $box_day = $day_no+$wan_no;
       $rows_week = ceil($box_day/7);
       $total_box=$rows_week*7;
       $timeDate = strtotime($year.'-'.$month."-01"); //เปลี่ยนวันที่เป็น timestamp
       $lastDay = date("t", $timeDate); //จำนวนวันของเดือน
       // include 'reg_search_1.php'
       ?>
       <!-- ตาราง -->
       <div class="col-md-12">
         <div class="box box-success">
           <div class="">
             <form name="frmMain" action="reg_print_1.php?M1=<?php echo $month2; ?>&Y1=<?php echo $year; ?>" target="_blank" method="POST" OnSubmit="return onPrint();">
             <h3 class="box-title"><p class="text-success">รายการการบันทึกใบเสร็จ</p></h3>
             <p><?php echo $day." เดือน ".$_month_name[$month2]." ".$year2; ?> &nbsp;&nbsp;&nbsp;
                <input type="submit" class="btn btn-warning" name="btnDelete" value="ดูใบแทนรับเงิน"> </p>
           <!-- </div> -->
           <div class="box-body">
             <div class="form-group" style="overflow-x:auto;">
               <table id="" class="table-bordered table-striped" width="140%" border="0">
                 <!-- <thead> -->
                 <tr>
                   <td width=""> <div align="center"><b>No.</div></td>
                   <td width=""> <div align="center"><b><input name="CheckAll" type="checkbox" id="CheckAll" value="Y" onClick="ClickCheckAll(this);"></div></td>
                   <td width=""> <div align="center"><b>วันที่เบิก</div></td>
                   <td width=""> <div align="center"><b>#เบิก</div></td>
                   <td width=""> <div align="center"><b>เลขที่ใบเบิก</div></td>
                   <td width=""> <div align="center"><b>ชื่อลูกค้า</div></td>
                   <td width=""> <div align="center"><b>รายการเบิก</div></td>
                   <td width=""> <div align="center"><b>บริษัทที่เบิก</div></td>
                   <td width=""> <div align="center"><b>ยี่ห้อ</div></td>
                   <td width=""> <div align="center"><b>เลขที่เครื่อง</div></td>
                   <td width=""> <div align="center"><b>ชื่อผู้เบิก</div></td>
                   <td width=""> <div align="center"><b>ค่าใช้จ่ายในการเบิก</div></td>
                   <td width=""> <div align="center"><b>ค่าใช้จ่ายที่มีใบเสร็จ (บาท)</div></td>
                   <td width=""> <div align="center"><b>ค่าใช้จ่ายที่จ่ายให้ขนส่ง</div></td>
                   <td width=""> <div align="center"><b>ชื่อเจ้าหน้าที่ให้ขนส่ง 1</div></td>
                   <td width=""> <div align="center"><b>มูลค่าที่ได้รับ (ค่าตรวจ)</div></td>
                   <td width=""> <div align="center"><b>ชื่อเจ้าหน้าที่ให้ขนส่ง 2</div></td>
                   <td width=""> <div align="center"><b>มูลค่าที่ได้รับ (ค่าบริการ)</div></td>
                   <td width=""> <div align="center"><b>จังหวัด</div></td>
                   <td width=""> <div align="center"><b>ผลต่าง</div></td>
                   <td width=""> <div align="center"><b>หมายเหตุ</div></td>
                 </tr>
                 <!-- </thead> -->
                 <!-- <tbody> -->
                   <tr>
                     <?php
                     // $sql = "SELECT
                     // regemp.dbo.reg_billdata_berg_".$year."_".$month2.".*
                     // , EHONGDB.dbo.PROV.*, CONFIG.dbo.REG_BILLDATATRANSPORT.*
                     // , [EHONGDB].[dbo].[REG_BILL].*
                     // FROM regemp.dbo.reg_billdata_berg_".$year."_".$month2."
                     // LEFT JOIN EHONGDB.dbo.PROV ON reg_billdata_berg_".$year."_".$month2.".BG_PROV = PROV.PR_CODE
                     // LEFT JOIN CONFIG.dbo.REG_BILLDATATRANSPORT ON reg_billdata_berg_".$year."_".$month2.".BILL_NO = REG_BILLDATATRANSPORT.REG_NO
                     // RIGHT JOIN [EHONGDB].[dbo].[REG_BILL] ON reg_billdata_berg_".$year."_".$month2.".BILL_NO = REG_BILL.BILL_NO
                     // WHERE $code
                     // ORDER BY PROV.PR_CODE,reg_billdata_berg_".$year."_".$month2.".CHK_BERG";
                      // AND (a.FEE_NO=c.REG_STATUS)
                     $sql = "SELECT
                     a.*, a.CHK_BERG as cb_date, a.BG_PROV as prov_no1,
                     b.*,
                     c.*,
                     d.*,
                     e.*
                     FROM regemp.dbo.reg_billdata_berg_".$year."_".$month2." a
                     LEFT OUTER JOIN (SELECT * FROM [EHONGDB].[dbo].[PROV]) b ON (a.BG_PROV = b.PR_CODE)
                     LEFT OUTER JOIN (SELECT * FROM [CONFIG].[dbo].[REG_BILLDATA_V1]) c ON (a.BILL_NO=c.REG_NO)
                     LEFT OUTER JOIN (SELECT * FROM [EHONGDB].[dbo].[REG_BILL]) d ON (a.BILL_NO=d.BILL_NO)
                     LEFT OUTER JOIN (SELECT * FROM [CONFIG].[dbo].[tsp_transport]) e ON (a.BG_PROV=e.tsp_no)
                     WHERE $code and (a.status<>'V') and (a.chkdate2 is not null and a.chkstatus2='C') and (a.acpdate is not null) and (a.chk_berg is not null)
                     ORDER BY b.PR_CODE,a.CHK_BERG";
                     $select=$conn->prepare($sql);
                     $select->execute();
                     // echo $sql;

                     // $sql = "SELECT [EHONGDB].[dbo].[REG_BILLDATADOCUMENT].*, [EHONGDB].[dbo].[REG_BILL].*, EHONGDB.dbo.REG_FEEDATA.*
                     // FROM [EHONGDB].[dbo].[REG_BILLDATADOCUMENT]
                     // FULL OUTER JOIN [EHONGDB].[dbo].[REG_BILL] ON REG_BILLDATADOCUMENT.BILL_NO = REG_BILL.BILL_NO
                     // FULL OUTER JOIN EHONGDB.dbo.REG_FEEDATA ON REG_BILLDATADOCUMENT.FEE_NO = REG_FEEDATA.FEE_NO
                     // WHERE $code ORDER BY REG_BILLDATADOCUMENT.CHK_BERG";
                     // $select=$conn->prepare($sql);
                     // $select->execute();
                     // echo $sql."<br>";




                     $FEE_COST = 0;
                     $REG_BILL1= 0;
                     $REG_BILL2= 0;
                     $REG_BILL3 = 0;
                     $REG_BILL4 = 0;

                     $item = 1;
                     $i = 0;
                     while($fetch=$select->fetch())
                     {
                       $i++;
                       $CHK_BERG1 = substr($fetch['cb_date'],0,4)+543; $CHK_BERG2 = substr($fetch['cb_date'],5,2); $CHK_BERG3 = substr($fetch['cb_date'],8,2);
                       $CHK_BERG = $CHK_BERG3." ". $_month_name[$CHK_BERG2]." ".$CHK_BERG1;
                       // $CHK = $fetch['BILL_NO'].$fetch['FEE_NO'];
                       // $CHK1 = $fetch['REG_STATUS'];
                       // $CHK_BERG = '';
                       // echo $fetch['ddoo'];
                       // 00 จดทะเบียนใหม่(สด)ไฟแนนซ์
                       if ($fetch['FEE_NO'] == '00') { $SUM1 = '340.00'; $SUM2 = '80.00';	$SUM3 = '100.00';	$SUM4 = $SUM2+$SUM3;
                         // echo $SUM1." ".$SUM2." ".$SUM3." ".$SUM4;
                       }
                       // 01 จดทะเบียนใหม่(สด)
                       if ($fetch['FEE_NO'] == '01') {
                         if ($fetch['FEE_COST'] = '1250.0') { $SUM1 = $fetch['FEE_COST']; $SUM2 = '0.00'; $SUM3 = '0.00'; $SUM4 = '0.00';
                         }
                         else { $SUM1 = '340.00'; $SUM2 = '80.00'; $SUM3 = '100.00'; $SUM4 = $SUM2+$SUM3;
                         }
                       }
                       // 02 จดทะเบียนใหม่(ผ่อน) มี 2 อัน ทำถึงอันนี้นะ
                       if ($fetch['FEE_NO'] == '02' && $fetch['FEE_COST'] == '495.0') { $SUM1 = '315.00'; $SUM2 = '80.00'; $SUM3 = '100.00'; $SUM4 = $SUM2+$SUM3;
                       }
                       else {
                         $SUM1 = $fetch['FEE_COST']; $SUM2 = '0.00'; $SUM3 = '0.00'; $SUM4 = '0.00';
                       }
                       if ($fetch['FEE_NO'] == '02' && $fetch['FEE_COST'] == '75.0') { $SUM1 = '25.00'; $SUM2 = '0.00'; $SUM3 = '50.00'; $SUM4 = $SUM2+$SUM3;
                       }
                       // 03 โอนทะเบียน
                       if ($fetch['FEE_NO'] == '03') { $SUM1 = '125.00'; $SUM2 = '100.00'; $SUM3 = '80.00'; $SUM4 = $SUM2+$SUM3;
                       }
                       if ($fetch['FEE_NO'] == '03' && $fetch['FEE_COST'] != '305.0') { $SUM1 = $fetch['FEE_COST']; $SUM2 = '0.00'; $SUM3 = '0.00'; $SUM4 = '0.00';
                       }
                       // 04 ต่อภาษีปีละ
                       if ($fetch['FEE_NO'] == '04') { $SUM1 = $fetch['FEE_COST']; $SUM2 = '0.00'; $SUM3 = '0.00'; $SUM4 = '0.00';
                       }
                       // 05 แจ้งป้ายวงกลมหาย
                       if ($fetch['FEE_NO'] == '05') { $SUM1 = '25.00'; $SUM2 = '0.00';	$SUM3 = '50.00';	$SUM4 = $SUM2+$SUM3;
                       }
                       // 07 แจ้งป้ายแข็งหาย
                       if ($fetch['FEE_NO'] == '07') { $SUM1 = '105.00'; $SUM2 = '0.00';	$SUM3 = '70.00';	$SUM4 = $SUM2+$SUM3;
                       }
                       // 14 เปลี่ยนสีรถ
                       if ($fetch['FEE_NO'] == '14') { $SUM1 = '25.00'; $SUM2 = '100.00';	$SUM3 = '25.00';	$SUM4 = $SUM2+$SUM3;
                       }
                       // 17 ชื่อผู้ครอบครอง
                       if ($fetch['FEE_NO'] == '17') { $SUM1 = '25.00'; $SUM2 = '100.00';	$SUM3 = '25.00';	$SUM4 = $SUM2+$SUM3;
                       }
                     ?>
                     <td style="text-align: center;"><?=$item."."?></td>
                     <td><center><input type="checkbox" name="chkPrint[]" id="chkPrint<?php echo $i;?>" value="<?php echo $fetch["BILL_NO"]." ".$fetch['FEE_NO'];?>"></center></td>
                     <td><center>
                       <!-- <?php
                       if ($CHK == $CHK1) { ?>
                         <i class="fa fa-check" aria-hidden="true"></i>
                       <?php } ?> -->
                       <!-- <input type="checkbox" name="" value=""> -->
                       <?php echo $CHK_BERG; ?>
                     </center></td>
                     <!-- href="reg_print_1.php?BILL_NO=<?php echo $fetch['BILL_NO'];?>&FEE_NO=<?php echo $fetch['FEE_NO']; ?>" target="_blank" -->
                     <td><center><a><?php echo $fetch['BILL_NO']; ?></a></center></td>
                     <td><center><?php echo $fetch['REG_STATUS']; ?></center></td>
                     <td><center><?php echo $fetch['PS_NAME']; ?></center></td>
                     <td><center><?php echo $fetch['FEE_NO'].":".$fetch['FEE_NAME']?></center></td>
                     <td><center><?php echo $fetch['CROP_NAME']; ?></center></td>
                     <td><center><?php echo $fetch['XBRAND_NAME']; ?></center></td>
                     <td><center><?php echo $fetch['SERIAL_NO']; ?></center></td>
                     <td><center><?php echo $fetch['tsp_open']; ?></center></td>
                     <td><center><p class="text-blue"><?php echo $fetch['FEE_COST']; ?></p></center></td>
                     <td><center><p class="text-blue"><?php echo number_format($SUM1,2); ?></p></center></td>
                     <td><center><p class="text-blue"><?php echo number_format($SUM4,2); ?></p></center></td>
                     <td><center><?php echo $fetch['tsp_registrar1']; ?></center></td>
                     <td><center><p class="text-blue"><?php echo number_format($SUM2,2); ?></center></p></td>
                     <td><center><?php echo $fetch['tsp_registrar']; ?></center></td>
                     <td><center><p class="text-blue"><?php echo number_format($SUM3,2); ?></p></center></td>
                     <td><center><?php echo $fetch['prov_no1'].":".$fetch['PR_NAME'];?></center></td>
                     <td><center></center></td>
                     <td><center></center></td>
                   </tr>
                   <?php
                   $FEE_COST += str_replace( ',', '', $fetch['FEE_COST']);
                   $REG_BILL1+= str_replace( ',', '', $SUM1);
                   $REG_BILL4 += str_replace( ',', '', $SUM4);
                   $REG_BILL2 += str_replace( ',', '', $SUM2);
                   $REG_BILL3 += str_replace( ',', '', $SUM3);
                     $item++; }
                   ?>
                   <!-- </tbody> -->
                   <!-- <tbody> -->
                   <tr>
                     <td><center><p class="text-blue">รวม</p></center></td>
                     <td><center></center></td>
                     <td><center></center></td>
                     <td><center></center></td>
                     <td><center></center></td>
                     <td><center></center></td>
                     <td><center></center></td>
                     <td><center></center></td>
                     <td><center></center></td>
                     <td><center></center></td>
                     <td><center></center></td>
                     <td><center><p class="text-blue"><?php echo number_format($FEE_COST,2); ?></p></center></td>
                     <td><center><p class="text-blue"><?php echo number_format($REG_BILL1,2); ?></p></center></td>
                     <td><center><p class="text-blue"><?php echo number_format($REG_BILL4,2); ?></p></center></td>
                     <td><center></center></td>
                     <td><center><p class="text-blue"><?php echo number_format($REG_BILL2,2); ?></p></center></td>
                     <td><center></td>
                     <td><center><p class="text-blue"><?php echo number_format($REG_BILL3,2); ?></p></center></center></td>
                     <td><center></center></td>
                     <td><center></center></td>
                     <td><center></center></td>
                   </tr>
                   <tr>
                     <td width=""> <div align="center"><b>No.</div></td>
                     <td width=""> <div align="center"><b></div></td>
                     <td width=""> <div align="center"><b>วันที่เบิก</div></td>
                     <td width=""> <div align="center"><b>#เบิก</div></td>
                     <td width=""> <div align="center"><b>เลขที่ใบเบิก</div></td>
                     <td width=""> <div align="center"><b>ชื่อลูกค้า</div></td>
                     <td width=""> <div align="center"><b>รายการเบิก</div></td>
                     <td width=""> <div align="center"><b>บริษัทที่เบิก</div></td>
                     <td width=""> <div align="center"><b>ยี่ห้อ</div></td>
                     <td width=""> <div align="center"><b>เลขที่เครื่อง</div></td>
                     <td width=""> <div align="center"><b>ชื่อผู้เบิก</div></td>
                     <td width=""> <div align="center"><b>ค่าใช้จ่ายในการเบิก</div></td>
                     <td width=""> <div align="center"><b>ค่าใช้จ่ายที่มีใบเสร็จ (บาท)</div></td>
                     <td width=""> <div align="center"><b>ค่าใช้จ่ายที่จ่ายให้ขนส่ง</div></td>
                     <td width=""> <div align="center"><b>ชื่อเจ้าหน้าที่ให้ขนส่ง 1</div></td>
                     <td width=""> <div align="center"><b>มูลค่าที่ได้รับ</div></td>
                     <td width=""> <div align="center"><b>ชื่อเจ้าหน้าที่ให้ขนส่ง 2</div></td>
                     <td width=""> <div align="center"><b>มูลค่าที่ได้รับ</div></td>
                     <td width=""> <div align="center"><b>จังหวัด</div></td>
                     <td width=""> <div align="center"><b>ผลต่าง</div></td>
                     <td width=""> <div align="center"><b>หมายเหตุ</div></td>
                   </tr>
                 <!-- </tbody> -->
               </table>
               <input type="hidden" name="hdnCount" value="<?php echo $i;?>">
               </form>
               <br>
             </div>
           </div>
         </div>
       </div>

       </div>
     </section>
   </div>
 </div>

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