<?php
session_start();
require_once("../common/init.php");
require_once("security.inc.php");

$keyword = $_POST['keyword'];
if($_POST['iStatus'] != '')
{
	$iStatus = $_POST['iStatus'];
}
else
{
	$iStatus = $_GET['iStatus'];
}

$fromYear = $_POST['fromYear'];
$fromMonth = $_POST['fromMonth'];
$fromDay = $_POST['fromDay'];
$toYear = $_POST['toYear'];
$toMonth = $_POST['toMonth'];
$toDay = $_POST['toDay'];
$page = $_GET['page'];
/************* 查询财务信息 ******************/
$wheres = " AND UserName='".$_SESSION['User_S']."'";
$keyword=trim($keyword);
if($keyword){
	$wheres .= " AND(Amount like '%$keyword%' or About like '%$keyword%' )";
}
if($ProductID){
	$wheres .= " AND ProductID='$ProductID'";
}
if($fIDProd){
	$wheres .= " AND fIDProd='$fIDProd'";
}
if($iStatus!=""){
	 $wheres .= " AND iStatus='$iStatus'";
}
//按日期搜索
if($fromYear){
   if(!$fromMonth)$fromMonth=1;
   if(!$fromDay)$fromDay=1;
   $from=$fromYear."-".$fromMonth."-".$fromDay;
   $from=date("Y-m-d",strtotime($from));
   if($toYear){
	  if(!$toMonth)$toMonth=12;
	  if(!$toDay)$toDay=31;
	  $to=$toYear."-".$toMonth."-".$toDay;
	  $to=date("Y-m-d",strtotime($to));
   }
   if($to){
	  $wheres=" AND (dtCreate between '$from' and  '$to' OR SUBSTRING(dtCreate,1,10)='$to')";
   }elseif($from){
	  $wheres=" AND (dtCreate >='$from')";
   }
}
//得到总记录数
$sql="select count(*) from `order` where 1 $wheres";
$rs=$db->query($sql);
$pageSize=30;
$totalRecord=$db->result($rs,0,0);
$totalPage=ceil($totalRecord/$pageSize);
if(!$page)$page=1;
$offset=($page-1)*$pageSize;
//查询
$sql = "select * from `order` where 1 $wheres order by OrderID DESC limit $offset,$pageSize";
$result = $db->query($sql);
$pageList = GetPageList($_SERVER[PHP_SELF],"page","15",$totalPage,$page);
$pageAmount = 0;
$pagePAmount = 0;
while($FinI=$db->fetch_array($result)){
	  $FinI['bg'] = $bg=="#FFFFFF"?"#EEEEEE":"#FFFFFF";
	  $OrderItems[] = $FinI;
}
$pagers = "共<font color='red'> $totalRecord </font>条记录,".
          "共<font color='red'> $totalPage </font>页,".
		  "当前是第<font color='red'> $page </font>页";
if($pageList) $pagers .= "$pageList[0] <br> $pageList[1]";
/************* end of 查询财务信息 *******************/
/************* 初始化页面 ****************************/
for($y=2001;$y<=date("Y");$y++){
   if($fromYear==$y)$sel="selected";
   else $sel="";
   $option_fromYear .= "<option value='$y' $sel >$y</option>";
}

for($m=1;$m<=12;$m++){
   if($fromMonth==$m)$sel="selected";
   else $sel="";
   $option_fromMonth .= "<option value='$m' $sel >$m</option>";
}

for($d=1;$d<=31;$d++){
   if($fromDay==$d)$sel="selected";
   else $sel="";
   $option_fromDay .= "<option value='$d' $sel >$d</option>";
}

for($y=2001;$y<=date("Y");$y++){
   if($toYear==$y)$sel="selected";
   else $sel="";
   $option_toYear .= "<option value='$y' $sel >$y</option>";
}

for($m=1;$m<=12;$m++){
   if($toMonth==$m)$sel="selected";
   else $sel="";
   $option_toMonth .= "<option value='$m' $sel >$m</option>";
}

for($d=1;$d<=31;$d++){
   if($toDay==$d)$sel="selected";
   else $sel="";
   $option_toDay .= "<option value='$d' $sel >$d</option>";
}

$option_iStatus = "<option value='0' ".($iStatus=='0'?'selected':'').">未生效</option><option value='1' ".($iStatus==1?'selected':'')." >已生效</option>";
$option_fType = "<option value='1' ".($fType==1?'selected':'').">支出记录</option><option value='-1' ".($fType==-1?'selected':'')." >入帐记录</option>";
/************* end of 初始化页面 *********************/
//输出

$smarty->assign("option_fromYear",$option_fromYear);
$smarty->assign("option_fromMonth",$option_fromMonth);
$smarty->assign("option_fromDay",$option_fromDay);
$smarty->assign("option_toYear",$option_toYear);
$smarty->assign("option_toMonth",$option_toMonth);
$smarty->assign("option_toDay",$option_toDay);
$smarty->assign("option_iStatus",$option_iStatus);
$smarty->assign("option_fType",$option_fType);
$smarty->assign("OrderItems",$OrderItems);
$smarty->assign("pagers",$pagers);
$smarty->display('user_order_list.html');
$db->close();
?>