<?php
include("configs/convertdate.conf");
 $dm=date("t");
   if(strlen($nmonth)<2) $nmonth="0".$nmonth; else $nmonth=$nmonth;
 $myear=$nyear-543;
// $my=date("Y");
 //$myear=date("Y")+543;
   $date1="01/".$nmonth."/".$nyear;
   $stdate=thaidatetodb($date1);
  // echo $date1;
    $dm=date("t",strtotime($stdate));
   $date2=$dm."/".$nmonth."/".$nyear;
   $fndate=thaidatetodb($date2);
   $imonth=(int) $nmonth;

$style1="style=\"border-right-style:solid;border-bottom-style:solid;border-width:1\"";
/*
$database->Execute("update REG_BILLDATA set acpdate=b.ACPDATE,acpuser=b.ACPUSER,acptime=b.acptime,acphint=b.acphint,acpstatus=b.acpstatus from REG_BILLDATA a inner join (select * from reg_bill where ACPDATE is not null) b on(a.BILL_NO=b.BILL_NO) where a.acpdate is null");
$database->Execute("update REG_BILLDATADOCUMENT set acpdate=b.ACPDATE,acpuser=b.ACPUSER,acptime=b.acptime,acphint=b.acphint,acpstatus=b.acpstatus from REG_BILLDATADOCUMENT a inner join (select * from reg_bill where ACPDATE is not null) b on(a.BILL_NO=b.BILL_NO) where a.acpdate is null");
*/
$ndy=(int)date("d");
$database->Execute("IF OBJECT_ID('regemp.dbo.reg_bill_".$myear."_".$nmonth."', 'U') IS NOT NULL drop table regemp.dbo.reg_bill_".$myear."_".$nmonth);
$database->Execute("select *,getdate() as updatetime into regemp.dbo.reg_bill_".$myear."_".$nmonth." from reg_bill where (month(chkdate2)=$imonth and year(chkdate2)=$myear)");

$database->Execute("IF OBJECT_ID('regemp.dbo.reg_billdata_".$myear."_".$nmonth."', 'U') IS NOT NULL drop table regemp.dbo.reg_billdata_".$myear."_".$nmonth);
$database->Execute("select *,getdate() as updatetime into regemp.dbo.reg_billdata_".$myear."_".$nmonth." from reg_billdata where (month(chkdate2)=$imonth and year(chkdate2)=$myear)");

$database->Execute("IF OBJECT_ID('regemp.dbo.reg_billdata_berg_".$myear."_".$nmonth."', 'U') IS NOT NULL drop table regemp.dbo.reg_billdata_berg_".$myear."_".$nmonth);
$database->Execute("select *,getdate() as updatetime into regemp.dbo.reg_billdata_berg_".$myear."_".$nmonth." from reg_billdata where (month(chk_berg)=$imonth and year(chk_berg)=$myear)");
?>
<script type="text/javascript" src="script/sorttable.js"></script>
<script language="javascript">
function showlink(v1,v2,v3,v4){
   window.open("printproc.php?sid=reg&fid=rmainbody3det&dueday="+v1+"&xflg="+v2+"&fee_no="+v3+"&pr_code="+v4+"&nmonth="+form1.nmonth.value+"&nyear="+form1.nyear.value+"&br_id="+form1.br_id.value);
};
function showlink0(v1,v2,v3,v4){

   window.open("printproc.php?sid=reg&fid=rmainbody22det&dueday="+v1+"&xflg="+v2+"&zflg="+v3+"&nmonth="+form1.nmonth.value+"&nyear="+form1.nyear.value+"&br_id="+form1.br_id.value);

};

function showlinkx(v1,v2,v3,v4){

   window.open("printproc.php?sid=reg&fid=rmainbody3xdet&dueday="+v1+"&xflg="+v2+"&fee_no="+v3+"&nmonth="+form1.nmonth.value+"&nyear="+form1.nyear.value+"&br_id="+form1.br_id.value);

};

function showlinky(v1,v2,v3,v4){

   window.open("printproc.php?sid=reg&fid=rmainbody3ydet&dueday="+v1+"&xflg="+v2+"&zflg="+v3+"&nmonth="+form1.nmonth.value+"&nyear="+form1.nyear.value+"&br_id="+form1.br_id.value);

};

function padselect(idno){
 elemntLength=parseInt(form1.elements.length);
 for(i=0;i<elemntLength;i++){
   s=form1.elements[i].id;
   s=s.substr(s,6,1);
   if(s == "boxed["){
   s.className="";
   };
   };
//		alert(idno)
   orow =document.getElementById("boxed["+idno+"]");
//		alert(orow.style.display)
   orow.style.bgcolor="red";
};
</script>
<form name="form1" method="post">
<input type="hidden" name="trk_type" value="<?=$trk_type?>">
<input type="hidden" name="nmonth" value="<?=$nmonth?>">
<input type="hidden" name="nyear" value="<?=$nyear?>">
<input type="hidden" name="dueday" value="<?=$dueday?>">
<input type="hidden" name="m" value="<?=$nmonth?>">
<input type="hidden" name="y" value="<?=$nyear?>">
<input type="hidden" id="br_id" name="br_id" value="<?=$br_id?>">
<input type="hidden" name="zone_code" value="<?=$zone_code?>">
<input type="hidden" name="group_no" value="<?=$group_no?>">
<input type="hidden" name="date1" value="<?=$stdate?>">
<input type="hidden" name="date2" value="<?=$fndate?>">
<input type="hidden" name="opt" value="<?=$opt?>">
<input type="hidden" name="gopt" value="<?=$gopt?>">
<input type="hidden" name="flag">
<input type="hidden" name="paystatus" value="<?=$paystatus?>">
<input type="hidden" name="lastpayday" value="<?=$lastpayday?>">
<input type="hidden" name="npycnt" value="<?=$npycnt?>">
<input type="hidden" name="xtype" value="<?=$xtype?>">
</form>

<table width="100%" cellpadding="3" cellspacing="0">
<thead class="fixedHeader">
       <tr>
         <td bgcolor="#AFD8D8" <?=$style1?> align="center" rowspan="2"><b>รายการ</b></td>
         <td bgcolor="#AFD8D8" <?=$style1?> align="center" colspan="<?=$dm?>"><b>ฝ่ายทะเบียน</b></td>
         <td bgcolor="#AFD8D8" <?=$style1?> align="center" rowspan="2"><b>รวม</b></td>
       </tr>
       <tr>
<?
$sumcol1=array();
 $sumcol2=array();
  $sumcol3=array();
   $sumcol4=array();
    $sumcol5=array();
for($i=1;$i<=$dm;$i++){
$sumcol1[$i]=0;
 $sumcol2[$i]=0;
  $sumcol3[$i]=0;
   $sumcol4[$i]=0;
    $sumcol5[$i]=0;
?>
         <td bgcolor="#AFD8D8" width="50" align="center" <?=$style1?>><strong><u><font color="#0000FF"><?=$i?></font></u></strong></td>
 <?};?>
       </tr>
</thead>
     <!-- สีเขียว-->
<?
 $sumrow1=0;
 $sumrow2=0;
  $sumrow3=0;
   $sumrow4=0;
    $sumrow5=0;
  $bgcolor="bgcolor=\"#D7FFD7\"";
  $cnt1=0;
  $cnt2=0;
  $cnt3=0;
  $perc=0;
 //$clstr="(ct_no is not null) and (en_fwstatus between '10' and '14') and (st_npy=0) and (due_no between 1 and 6) ";

     $clstr.=" (bill_no is not null) ";
   if($br_id!="") $clstr.=" and (br_id like '$br_id')";
   if($fee_no!="") $clstr.=" and (fee_no like '$fee_no')";
   // and (bill_date between '".$stdate."' and '".$fndate."')
   $clstr.="  and ((fee_no between '00' and '09') or (fee_no in ('14','15','17'))) ";
 ?>
     <?

// ฝ่ายทะเบียน ค้างตรวจรับ มีปัญหา
   $sql="select day(chkdate2) as dx,count(bill_no) as cnt from regemp.dbo.reg_bill_".$myear."_".$nmonth." where  (status<>'V') and (chkdate2 is null ) and (acpdate is not null) and (chk_berg is null)  group by day(chkdate2) order by day(chkdate2)";
  // echo $sql;
   $rs=$database->getAll($sql);
   $xrowz0=array();
     foreach($rs as $row){
   $xrowz0[$row[dx]]=$row[cnt];
   };
   // ฝ่ายทะเบียน ค้างตรวจรับ
   $sql="select day(chkdate2) as dx,count(bill_no) as cnt from regemp.dbo.reg_bill_".$myear."_".$nmonth." where  (status<>'V') and (chkdate2 is not null and chkstatus2='C') and (acpdate is null)  and (xt_no is not null) group by day(chkdate2) order by day(chkdate2)";
  // echo $sql;
   $rs=$database->getAll($sql);
   $xrowx0=array();
     foreach($rs as $row){
   $xrowx0[$row[dx]]=$row[cnt];
   };
   // ฝ่ายทะเบียน ตรวจรับ
   $sql="select day(chkdate2) as dx,count(bill_no) as cnt from regemp.dbo.reg_bill_".$myear."_".$nmonth." where  (status<>'V')  and (chkdate2 is not null and chkstatus2='C') and (acpdate is not null)  and (xt_no is not null) and bill_no in (select bill_no from reg_billdata where (chk_berg is null) and (chkdate2 is not null and chkstatus2='C') and ((fee_no between '00' and '09') or (fee_no in ('14','15','17'))) group by bill_no) group by day(chkdate2) order by day(chkdate2)";
   //echo $sql;
   $rs=$database->getAll($sql);
   $xrowx1=array();
     foreach($rs as $row){
   $xrowx1[$row[dx]]=$row[cnt];
   };


       // ฝ่ายทะเบียน ค้างดำเนินการ
   $sql="select day(chkdate2) as dx,count(bill_no) as cnt from regemp.dbo.reg_bill_".$myear."_".$nmonth." where  (status<>'V')   and (chkdate2 is not null and chkstatus2='C')  and bill_no in (select bill_no from reg_billdata where (chk_berg is null) and (chkdate2 is not null and chkstatus2='C') and ((fee_no between '00' and '09') or (fee_no in ('14','15','17'))) group by bill_no) group by day(chkdate2) order by day(chkdate2)";
   //echo $sql."<br>";
   $rs=$database->getAll($sql);
   $xrowx2=array();
     foreach($rs as $row){
   $xrowx2[$row[dx]]=$row[cnt];
   };

 $sql="select day(chkdate2) as dx,count(bill_no) as cnt from regemp.dbo.reg_billdata_".$myear."_".$nmonth." where $clstr and  (status<>'V') and (chkdate2 is not null and chkstatus2='C')  and (acpdate is not null) group by day(chkdate2) order by day(chkdate2)";
   //echo $sql;
   $rs=$database->getAll($sql);
   $xrow0=array();
     foreach($rs as $row){
   $xrow0[$row[dx]]=$row[cnt];
   };
    // ค้างเบิกดำเนินการ
   $sql="select day(chkdate2) as dx,count(bill_no) as cnt from regemp.dbo.reg_billdata_".$myear."_".$nmonth." where $clstr and (status<>'V')  and (chkdate2 is not null and chkstatus2='C') and (chk_berg is null) group by day(chkdate2) order by day(chkdate2)";
  // echo $sql;
   $rs=$database->getAll($sql);
   $xrow1=array();
     foreach($rs as $row){
   $xrow1[$row[dx]]=$row[cnt];
   };
   $sql="select fee_no,day(chkdate2) as dx,count(bill_no) as cnt from regemp.dbo.reg_billdata_".$myear."_".$nmonth."  where $clstr and (status<>'V') and (chkdate2 is not null and chkstatus2='C')   and (chk_berg is null) group by fee_no,day(chkdate2) order by fee_no,day(chkdate2)";
   //echo $sql;
   $rs=$database->getAll($sql);
   $xrow1x=array();
     foreach($rs as $row){
   $xrow1x[$row[fee_no]][$row[dx]]=$row[cnt];
   };
  //  ดำเนินการ
    $sql="select day(chkdate2) as dx,count(bill_no) as cnt from regemp.dbo.reg_billdata_".$myear."_".$nmonth."  where $clstr and (status<>'V') and (chkdate2 is not null and chkstatus2='C') and (acpdate is not null)  and (chk_berg is not null) group by day(chkdate2) order by day(chkdate2)";
   //echo $sql;
   $rs=$database->getAll($sql);
   $xrow2=array();
     foreach($rs as $row){
   $xrow2[$row[dx]]=$row[cnt];
   };

   //  ดำเนินการ - ตามวันเบิก
    $sql="select day(chk_berg) as dx,count(bill_no) as cnt from regemp.dbo.reg_billdata_berg_".$myear."_".$nmonth." where $clstr and (status<>'V') and (chkdate2 is not null and chkstatus2='C') and (acpdate is not null)  and (chk_berg is not null) group by day(chk_berg) order by day(chk_berg)";
  // echo $sql;
   $rs=$database->getAll($sql);
   $xrow2x=array();
     foreach($rs as $row){
   $xrow2x[$row[dx]]=$row[cnt];
   };

   // สาขา คืนงาน
    $sql="select fee_no,day(rt_date) as dx,count(bill_no) as cnt from regemp.dbo.reg_billdata_return_".$myear."_".$nmonth."  where $clstr and (status<>'V') and (rt_status='R') group by fee_no,day(rt_date) order by fee_no,day(rt_date)";
  // echo $sql;
   $rs=$database->getAll($sql);
   $xrow8=array();
     foreach($rs as $row){
   //$xrow8[$row[dx]]=$row[cnt];
   $xrow8[$row[fee_no]][$row[dx]]=$row[cnt];
   };

?>
<tr align="left"><td <?=$style1?> colspan="<?=$dm+1?>"><font size="+1" color="blue">ใบสั่งทำรับงานจากห้องหลักฐาน (เรียงตามวันส่งจากฝ่ายหลักฐาน)</td></tr>

<tr><td <?=$style1?> nowrap align="left"><font color="red">มาทางลัด มีปัญหา</td>
<?
$sumrowz0=0;
   for($i=1;$i<=$dm;$i++){

      $cntstr="";
      $dcntz0=$xrowz0[$i];
       if($dcntz0>0){ $cntstr="<a href=\"javascript:showlink0('$i','Z','','');\"><b><font color=\"red\">".number_format($dcntz0)."</font></b></a>";
      $sumcolz0[$i]=$sumcolz0[$i]+$dcntz0;
      $sumrowz0=$sumrowz0+$dcntz0;
     };
     ?>
     <td bgcolor="#ffdfdf" width="50" align="center" <?=$style1?>><?=$cntstr?></td>
     <?}?>
   <td  align="right"><b><a href="javascript:showlink0('','Z','','');"><font color="red"><?=number_format($sumrowz0)?></a></font></b></td>
   </tr>
   <tr><td <?=$style1?> nowrap align="left"><font color="red">ค้างตรวจรับจากห้องหลักฐาน</td>
<?
$sumrowx0=0;
   for($i=1;$i<=$dm;$i++){
      $cntstr="";
      $dcntx0=$xrowx0[$i];
       if($dcntx0>0){ $cntstr="<a href=\"javascript:showlink0('$i','W','','');\"><b><font color=\"red\">".number_format($dcntx0)."</font></b></a>";
      $sumcolx0[$i]=$sumcolx0[$i]+$dcntx0;
      $sumrowx0=$sumrowx0+$dcntx0;
     };
     ?>
     <td <?=$bgcolor?> width="50" align="center" <?=$style1?>><?=$cntstr?></td>
     <?}?>
   <td  align="right"><b><a href="javascript:showlink0('','W','','');"><font color="red"><?=number_format($sumrowx0)?></a></font></b></td>
   </tr>
<tr><td <?=$style1?> nowrap align="left"><font color="blue">ตรวจรับจากห้องหลักฐาน</td>
<?
$sumrowx1=0;
   for($i=1;$i<=$dm;$i++){

      $cntstr="";
      $dcntx1=$xrowx1[$i];
       if($dcntx1>0){ $cntstr="<a href=\"javascript:showlink0('$i','C','','');\"><b><font color=\"blue\">".number_format($dcntx1)."</font></b></a>";
      $sumcolx1[$i]=$sumcolx1[$i]+$dcntx1;
      $sumrowx1=$sumrowx1+$dcntx1;
     };
     ?>
     <td <?=$bgcolor?> width="50" align="center" <?=$style1?>><?=$cntstr?></td>
     <?}?>
   <td  align="right"><b><a href="javascript:showlink0('','C','','');"><font color="blue"><?=number_format($sumrowx1)?></a></font></b></td>
   </tr>

<tr><td <?=$style1?> nowrap align="left"><font color="red">ค้างดำเนินการx</td>
<?
$sumrowx2=0;
   for($i=1;$i<=$dm;$i++){

      $cntstr="";
      $dcntx2=$xrowx2[$i];
       if($dcntx2>0){ $cntstr="<a href=\"javascript:showlink0('$i','X1','','');\"><b><font color=\"red\">".number_format($dcntx2)."</font></b></a>";
      $sumcolx2[$i]=$sumcolx2[$i]+$dcntx2;
      $sumrowx2=$sumrowx2+$dcntx2;
     };
     ?>
     <td <?=$bgcolor?> width="50" align="center" <?=$style1?>><?=$cntstr?> </td>
     <?}?>
   <td  align="right"><b><a href="javascript:showlink0('','X1','','');"><font color="red"><?=number_format($sumrowx2)?></a></font></b></td>
   </tr>


<tr align="left"><td <?=$style1?> colspan="<?=$dm+1?>"><font size="+1" color="blue">รายการสั่งทำ (แยกตามรายการสั่งทำ)</td></tr>

<tr><td <?=$style1?> nowrap><font color="#000000">รายการพร้อมดำเนินการ</td>
<?
$sumrow0=0;
   for($i=1;$i<=$dm;$i++){
      $cntstr="";
      $dcnt0=$xrow0[$i];
       if($dcnt0>0){ $cntstr="<a href=\"javascript:showlink('$i','N1','','');\"><b><font color=\"#000000\">".number_format($dcnt0)."</font></b></a>";
      $sumcol0[$i]=$sumcol0[$i]+$dcnt0;
      $sumrow0=$sumrow0+$dcnt0;
     };
     ?>
     <td <?=$bgcolor?> width="50" align="center" <?=$style1?>><font color="green"><?=$cntstr?></font></td>
     <?}?>
   <td  align="right"><b><a href="javascript:showlink('','N1','','');"><font color="#000000"><?=number_format($sumrow0)?></a></font></b></td>
   </tr>

   <tr><td <?=$style1?>><b><font color="red">* ค้างเบิกดำเนินการ</td>
<?
$sumrow1=0;
   for($i=1;$i<=$dm;$i++){

      $cntstr="";
      $dcnt1=$xrow1[$i];
       if($dcnt1>0){ $cntstr="<a href=\"javascript:showlink('$i','N1','','');\"><b><font color=\"red\">".number_format($dcnt1)."</font></b></a>";
      $sumcol1[$i]=$sumcol1[$i]+$dcnt1;
      $sumrow1=$sumrow1+$dcnt1;
     };
     ?>
     <td width="50" align="center" <?=$style1?>><?=$cntstr?></td>
     <?}?>
   <td  align="right"><b><a href="javascript:showlink('','N1','','');"><font color="red"><?=number_format($sumrow1)?></a></font></b></td>
   </tr>
     <?
     $sql="select fee_no,fee_no+':'+fee_name as fee_name from reg_feedata where ((fee_no between '00' and '09') or (fee_no in ('14','15','17'))) order by fee_no";
     $xrs=$database->getAll($sql);
     foreach($xrs as $xrow){
     ?>
     <tr><td <?=$style1?> align="left" nowrap><font color="blue"><?=$xrow[fee_name]?></td>
<?
$sumrow1x=0;
   for($i=1;$i<=$dm;$i++){

      $cntstr="";
      $dcnt1x=$xrow1x[$xrow[fee_no]][$i];
       if($dcnt1x>0){ $cntstr="<a href=\"javascript:showlink('$i','N1','".$xrow[fee_no]."','');\"><b><font color=\"red\">".number_format($dcnt1x)."</font></b></a>";
      $sumcol1x[$i]=$sumcol1x[$i]+$dcnt1x;
      $sumrow1x=$sumrow1x+$dcnt1x;
     };
     ?>
     <td <?=$bgcolor?> width="50" align="center" <?=$style1?>><?=$cntstr?></td>
     <?}?>
   <td  align="right"><b><a href="javascript:showlink('','N1','<?=$xrow["fee_no"]?>','');"><font color="red"><?=number_format($sumrow1x)?></a></font></b></td>
   </tr>
   <?};?>
   <tr><td <?=$style1?> nowrap><font color="blue">รายการดำเนินการแล้ว (วันที่ส่งงานจากห้องหลักฐาน)</td>
<?
$sumrow2=0;
   for($i=1;$i<=$dm;$i++){

      $cntstr="";
      $dcnt2=$xrow2[$i];
       if($dcnt2>0){ $cntstr="<a href=\"javascript:showlink('$i','BG','','');\"><b><font color=\"blue\">".number_format($dcnt2)."</font></b></a>";
      $sumcol2[$i]=$sumcol2[$i]+$dcnt2;
      $sumrow2=$sumrow2+$dcnt2;
     };
     ?>
     <td <?=$bgcolor?> width="50" align="center" <?=$style1?>><?=$cntstr?></td>
     <?}?>
   <td  align="right"><b><a href="javascript:showlink('','BG','','');"><font color="blue"><?=number_format($sumrow2)?></a></font></b></td>
   </tr>
   <tr align="center"><td <?=$style1?> colspan="<?=$dm+1?>">&nbsp;</td></tr>
   <tr><td <?=$style1?> nowrap><b><font color="blue" size="+1">รายการเบิกแล้ว (เรียงวันที่เบิก)</font><b></td>
<?
$sumrow2x=0;
   for($i=1;$i<=$dm;$i++){

      $cntstr="";
      $dcnt2x=$xrow2x[$i];
       if($dcnt2x>0){ $cntstr="<a href=\"javascript:showlinky('$i','XT','','');\"><b><font color=\"blue\" size=\"+1\">".number_format($dcnt2x)."</font></b></a>";
      $sumcol2x[$i]=$sumcol2x[$i]+$dcnt2x;
      $sumrow2x=$sumrow2x+$dcnt2x;
     };
     ?>
     <td <?=$bgcolor?> width="50" align="center" <?=$style1?>><?=$cntstr?></td>
     <?}?>
   <td  align="right"><b><a href="javascript:showlinky('','XT','','');"><font color="blue"><?=number_format($sumrow2x)?></a></font></b></td>
   </tr>

 <tr align="center"><td <?=$style1?> colspan="<?=$dm+1?>">&nbsp;</td></tr>
   <tr><td <?=$style1?> nowrap><font color="red" size="+1">รายการคืนงาน เบิกทำใหม่</td>
   <?php for($i=1;$i<=$dm;$i++){

?>
         <td bgcolor="#AFD8D8" width="50" align="center" <?=$style1?>><strong><u><font color="#0000FF"><?=$i?></font></u></strong></td>
 <?};?>
   </tr>
   <?
     $sql="select fee_no,fee_no+':'+fee_name as fee_name from reg_feedata where ((fee_no between '00' and '09') or (fee_no in ('14','15','17'))) order by fee_no";
     $xrs=$database->getAll($sql);
     foreach($xrs as $xrow){
     ?>
     <tr><td <?=$style1?> align="left" nowrap><font color="blue"><?=$xrow[fee_name]?></td>
<?
$sumrow8=0;
   for($i=1;$i<=$dm;$i++){

      $cntstr="";
      $dcnt8=$xrow8[$xrow[fee_no]][$i];
       if($dcnt8>0){ $cntstr="<a href=\"javascript:showlinkx('$i','X','".$xrow[fee_no]."','');\"><b><font color=\"red\">".number_format($dcnt8)."</font></b></a>";
      $sumcol8[$i]=$sumcol8[$i]+$dcnt8;
      $sumrow8=$sumrow8+$dcnt8;
     };
     ?>
     <td <?=$bgcolor?> width="50" align="center" <?=$style1?>><?=$cntstr?></td>
     <?}?>
   <td  align="right"><b><a href="javascript:showlinkx('','X','<?=$xrow["fee_no"]?>','');"><font color="red"><?=number_format($sumrow8)?></a></font></b></td>
   </tr>
   <?};?>

</table>
