<?php  require('system/login.php');  ?>
<?php require_once('../Connections/BE2.php'); ?>
<?php
 
$currentPage = $_SERVER["PHP_SELF"];

 
$maxRows_Rt2 = 25;
$pageNum_Rt2 = 0;
if (isset($_GET['pageNum_Rt2'])) {
  $pageNum_Rt2 = $_GET['pageNum_Rt2'];
}
$startRow_Rt2 = $pageNum_Rt2 * $maxRows_Rt2;

$colname_Rt2 = "-1";
if (isset($_GET['keyword'])) {
  $colname_Rt2 = $_GET['keyword'];
}


mysql_select_db($database_BE2, $BE2);
$query_Rt1 = "SELECT distinct(SUBSTRING(ddate, 1, 7)) as mm FROM order_datamtmp where 1 order by ddate desc" ;
$Rt1 = mysql_query($query_Rt1, $BE2) or die(mysql_error());
$row_Rt1 = mysql_fetch_assoc($Rt1);

$mm = $row_Rt1['mm'];
if (isset($_GET['mm'])) {
  $mm = $_GET['mm'];
}


mysql_select_db($database_BE2, $BE2);

$query_Rt2 =  sprintf("SELECT a.coid,SUBSTRING(a.ddate, 1, 7) as ddate2,count(a.coid) as coid2,sum(a.dmoney) as dmoney,sum(a.dsky) as dsky,sum(a.dexpess) as dexpess,sum(a.p10) as p10,sum(a.p05) as p05,sum(a.p02) as p02,sum(a.p17) as p17,b.company_title FROM order_datamtmp as a,company as b where a.coid=b.coid AND a.ddate like '%s' group by a.coid,ddate2",$mm.'%') ; 

$query_limit_Rt2 = sprintf("%s LIMIT %d, %d", $query_Rt2, $startRow_Rt2, $maxRows_Rt2);
$Rt2 = mysql_query($query_limit_Rt2, $BE2) or die(mysql_error());
$row_Rt2 = mysql_fetch_assoc($Rt2);

// echo $query_Rt2;

if (isset($_GET['totalRows_Rt2'])) {
  $totalRows_Rt2 = $_GET['totalRows_Rt2'];
} else {
  $all_Rt2 = mysql_query($query_Rt2);
  $totalRows_Rt2 = mysql_num_rows($all_Rt2);
}
$totalPages_Rt2 = ceil($totalRows_Rt2/$maxRows_Rt2)-1;

$queryString_Rt2 = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_Rt2") == false && 
        stristr($param, "totalRows_Rt2") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_Rt2 = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_Rt2 = sprintf("&totalRows_Rt2=%d%s", $totalRows_Rt2, $queryString_Rt2);
 
?>
<script type="text/javascript">
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
</script>
  
  月份<select name="selmoon" id="selmoon" onchange="MM_jumpMenu('self',this,0)">
     <? 
	 
	  do{?>
         <option value="<?php printf("%s?act=percents2&mm=%s", $currentPage, $row_Rt1['mm']); ?>" <? if($mm==$row_Rt1['mm']){ ?>selected="selected" <? }?>><?= $row_Rt1['mm'] ;?></option>
      <?php } while ($row_Rt1 = mysql_fetch_assoc($Rt1)); ?>
     </select>
     
   
<a href="excel_precents2.php?sdate=<?= $mm; ?>"><button type="button" class="btn blue" > Excel 明細下載</button></a>
	 <? /* 
     <a href="excel_precent2.php?sdate=<?= $mm; ?>"><button type="button" class="btn blue" > 加總下載</button></a>
        */ ?>
 								

<table border="0" cellpadding="2" cellspacing="2" style="font-size:14px">
  <tr>
    <th height="28" nowrap>商品廠商</th>
    <th nowrap>月份</th>
    <th nowrap>訂單數量</th>
    <th nowrap>商品金額加總</th>
    <th nowrap>退貨款金額加總</th>
    <th nowrap>使用點數加總</th>
    <th nowrap>物流費用加總</th>
    <th nowrap>營業稅加總</th>
    <th nowrap>平台抽成加總</th>
    <th nowrap>金流費用加總</th>
    <th nowrap>服務稅額總金額</th>
    <th nowrap>本期結算金額</th>
    <th nowrap>結算狀態</th>
    <th nowrap>結算日期</th>
    <th nowrap>備註</th>

  </tr>
    <tr>
    <td height="3" colspan="15" background="images/dot3.gif"></td>
  </tr>
  <?php 
 
  $i=0;
    do { 
   $i++;
  ?>
    <tr onMouseOver="this.style.backgroundColor='#2172A1';" onMouseOut=this.style.backgroundColor="#fff">
      <td height="27" align="center"><?= $row_Rt2['company_title'] ?></td>
      <td height="27" align="center"><?= $row_Rt2['ddate2'] ?></td>
      <td align="center"><?= $row_Rt2['coid2'] ?></td>
      <td align="center"><?= $row_Rt2['dmoney'] ?></td>
      <td align="center">&nbsp;</td>
      <td align="center"><?= $row_Rt2['dsky'] ?></td>
      <td align="center" nowrap="nowrap"><?= $row_Rt2['dexpess'] ?></td>
      <td align="center"><?= $row_Rt2['p10'] ?></td>
      <td align="center"><?= $row_Rt2['p05'] ?></td>
      <td align="center"><?= $row_Rt2['p02'] ?></td>
      <td align="center"><?= $row_Rt2['p17'] ?></td>
      <td align="center">&nbsp;</td>
      <td align="center">&nbsp;</td>
      <td align="center">&nbsp;</td>
      <td align="center">&nbsp;</td>
 
    </tr>
  <tr>
    <td height="2" colspan="15" background="images/dot3.gif"></td>
  </tr>	
    <?php  } while ($row_Rt2 = mysql_fetch_assoc($Rt2)); ?>

  <tr>
   <td  colspan="15"  >
   
      <table width="697" border="0" align="left" cellpadding="0" cellspacing="0">
                <tr>
                  <td width="34%">總筆數：<?php echo $totalRows_Rt2 ?>筆 </td>
                  <td width="66%"><div id="font_s">
                      <p>
                        <?php if ($pageNum_Rt2 > 0) { // Show if not first page ?>
                        <a href="<?php printf("%s?pageNum_Rt2=%d%s", $currentPage, max(0, $pageNum_Rt2 - 1), $queryString_Rt2); ?>"><img src="images/First.gif" alt="上一頁" title="上一頁" width="18" height="13" border="0" /></a>
                        <?php } // Show if not first page ?>
                        第
                        <select name="jumpMenu" id="jumpMenu" onchange="MM_jumpMenu('self',this,0)">
                          <? for($i=0;$i<=$totalPages_Rt2;$i++){?>
                          <option value="<?php printf("%s?pageNum_Rt2=%d%s", $currentPage, $i, $queryString_Rt2); ?>" <? if($pageNum_Rt2==$i){ ?>selected="selected" <? }?>><? echo $i+1 ;?></option>
                          <? }?>
                        </select>
                        頁
                        <?php
	    //分頁程式
if($totalPages_Rt2<9){
		 $start=0;
		 $end=$totalPages_Rt2;
		}else{  
	    $start=$pageNum_Rt2-5;
		$end=$pageNum_Rt2+5;
        if ($start<0){
		 $start=0;
		 $end=10; } 
		 else { $end=$start+10 ;}
		 if ($end>$totalPages_Rt2){ 
		   $start = $end-$totalPages_Rt2-5;
		   $end=$totalPages_Rt2;
		   }
		 if ($end-$start>10){ 
		   $start = $end-10;
		   }  
		 }
		   
		for($i=$start;$i<=$end;$i++){
		if($i==$pageNum_Rt2){ echo "[".($i+1)."]";} else {?>
                        <a href="<?php printf("%s?pageNum_Rt2=%d%s", $currentPage, min($totalPages_Rt2, $i), $queryString_Rt2); ?>"> <? echo $i+1 ;?></a>
                        <?	}   }  ?>
                        <?php if ($pageNum_Rt2 < $totalPages_Rt2) { // Show if not last page ?>
                        <a href="<?php printf("%s?pageNum_Rt2=%d%s", $currentPage, min($totalPages_Rt2, $pageNum_Rt2 + 1), $queryString_Rt2); ?>"><img src="images/Next.gif" alt="下一頁" title="下一頁" width="14" height="13" border="0" /></a>
                        <?php } // Show if not last page ?>
                    </p>
                  </div></td>
                </tr>
    </table>  
 
   </td>
  </tr>
</table>
 
<?php
// mysql_free_result($Rt2);
?>
