Linux中国 Linux中国门户站!
设为主页 设为主页
收藏本站 收藏本站
 
当前位置 :首页 ->Linux技术 ->系统管理 ->正文

一个网上理财站点的设计(十五)

来源:Linux-cn.com 作者:Webmaster 时间:2007-05-05 点击: [收藏] [投稿]

  在今天的这个专题内,我们将完成现金流量表,在这张报表中,我们希望能够显示某一时间段内收入、支出、转入、转出以及开户余额的分收支项目的汇总情况,对这五种情况我们分别使用了五段SQL语句来计算,在这时,我们意识到这个数据库设计实际上还存在一个缺陷,就是对于初始化产生的开户余额,由于我们没有在in_ex_condition中添加相应记录,所以处理起来有点复杂。如果我们对于这种情况,在in_ex_condition中添加了相应记录,我们的处理会简单得多,这五段SQL语句完全可以由一段SQL语句来完成,从这个例子我们可以看出,数据库设计的一个小的疏忽,不仅会给我们下面的编程带来不必要的难度,更重要的是会降低程序执行的效率及速度,虽然我们仍然能完成相应功能。

  下面是这张报表的源程序:(report_3.php)


<?php
include ("cookie_check.php");
class report_3 {
  var $type; //类型
  var $in_ex_type;//收支类目
  var $money;//金额
  // 本方法生成某用户的收支情况明细表
  function add_item ($user, $date_begin,$date_end) {
    $sum_1=0;
    $sum_2=0;
    $query = "select in_ex_type.name as in_ex_type,sum(money*exchange_rate) 
    as mymoney from
 in_ex_condition,account,currency,in_ex_type where date>='".$date_begin."'
 and date<='".$date_end."' and
 account.currency=currency.code and in_ex_condition.account=account.code
 and in_ex_condition.user='".$user."' 
and in_ex_condition.in_ex_type= in_ex_type.code and in_ex_type.type=0 
and in_ex_type<>44 group by in_ex_type order by in_ex_type.code"; 
    $res = mysql_query($query); 
    $row = @mysql_fetch_array($res);
    $j=0;
    if ($row)
      {$sum=0;
       $i=1;
       $this->type[$i]='收入';
       $j=1;
      }
    while ($row)
      {$i++;
       $this->in_ex_type[$i] = $row['in_ex_type'];
       $this->money[$i] = $row['mymoney'];
       $sum=$sum+$row['mymoney'];
       $row = @mysql_fetch_array($res);
      }
    if ($j==1)
      {$sum_1=$sum_1+$sum;
       $i++;
       $this->money[$i] = $sum;
      }
    $query = "select in_ex_type.name as in_ex_type,sum(money*exchange_rate) 
    as mymoney from in_ex_condition,account,currency,in_ex_type 
where date>='".$date_begin."' and date<='".$date_end."' and
account.currency=currency.code and in_ex_condition.account=account.code and
 in_ex_condition.user='".$user."' and in_ex_condition.in_ex_type= 
 in_ex_type.code and in_ex_type.type=1 and in_ex_type<>1 group 
 by in_ex_type
 order by in_ex_type.code"; 
    $res = mysql_query($query); 
    $row = @mysql_fetch_array($res);
    $j=0;
    if ($row)
      {$sum=0;
       $i++;
       $this->type[$i]='支出';
       $j=1;
      }
    while ($row)
      {$i++;
       $this->in_ex_type[$i] = $row['in_ex_type'];
       $this->money[$i] = $row['mymoney'];
       $sum=$sum+$row['mymoney'];
       $row = @mysql_fetch_array($res);
      }
    if ($j==1)
      {$sum_2=$sum_2+$sum;
       $i++;
       $this->money[$i] = $sum;
      }
    $query = "select in_ex_type.name as in_ex_type,sum(money*exchange_rate)
    as mymoney from in_ex_condition,account,currency,in_ex_type 
where date>='".$date_begin."' and date<='".$date_end."' and 
account.currency=currency.code and in_ex_condition.account=account.code and 
in_ex_condition.user='".$user."' and in_ex_condition.in_ex_type= 
in_ex_type.code and in_ex_type=44 group by in_ex_type"; 
    $res = mysql_query($query); 
    $row = @mysql_fetch_array($res);
    if ($row)
      {$i++;
       $this->type[$i]='转入';
       $i++;
       $this->in_ex_type[$i] = $row['in_ex_type'];
       $this->money[$i] = $row['mymoney'];
       $i++;
       $this->money[$i] = $row['mymoney'];
       $sum_1=$sum_1+$row['mymoney'];
      }
    $query = "select in_ex_type.name as in_ex_type,sum(money*exchange_rate) 
    as mymoney from in_ex_condition,account,currency,in_ex_type 
where date>='".$date_begin."' and date<='".$date_end."' and 
account.currency=currency.code and in_ex_condition.account=account.code and 
in_ex_condition.user='".$user."' and in_ex_condition.in_ex_type= 
in_ex_type.code and in_ex_type=1 group by in_ex_type"; 
    $res = mysql_query($query); 
    $row = @mysql_fetch_array($res);
    if ($row)
      {$i++;
       $this->type[$i]='转出';
       $i++;
       $this->in_ex_type[$i] = $row['in_ex_type'];
       $this->money[$i] = $row['mymoney'];
       $i++;
       $this->money[$i] = $row['mymoney'];
       $sum_2=$sum_2+$row['mymoney'];
      }
    $query="select code,money_open from account where 
    date_open>='".$date_begin."' and date_open<='".$date_end."' 
    and user='".$user."'";
    $res = mysql_query($query); 
    $money_open=0;
    $row = @mysql_fetch_array($res);
    while ($row)
      {$query="select in_ex_type,money,balance from in_ex_condition
      where date>='".$date_begin."' and user='".$user."' and 
      account=".$row['code']." order by date";
       $res1 = mysql_query($query); 
       $row1 = @mysql_fetch_array($res1);
       if (!$row1) $money_open=$money_open+$row['money_open']; 
       else
         {$query="select type from in_ex_type where code=".$row1['in_ex_type'];
          $res2 = mysql_query($query); 
          $row2 = @mysql_fetch_array($res2);
          if ($row2['type']==1)
            $money_open=$money_open+$row1['balance']+$row1['money'];
          else
            $money_open=$money_open+$row1['balance']-$row1['money'];
         }
       $row = @mysql_fetch_array($res);
      }
    if ($money_open!=0)
      {$i++;
       $this->type[$i]='开户';
       $i++;
       $this->in_ex_type[$i] = '开户余额';
       $this->money[$i] = $money_open;
       $i++;
       $this->money[$i] = $money_open;
       $sum_1=$sum_1+$money_open;
      }
    $i++;
    $this->in_ex_type[$i] = '总流入';
    $this->money[$i] = $sum_1;
    $i++;
    $this->in_ex_type[$i] = '总流出';
    $this->money[$i] = $sum_2;
    $i++;
    $this->in_ex_type[$i] = '差额';
    $this->money[$i] = $sum_1-$sum_2;
    return $i;
  }
}
?>
<HTML>
<TITLE> 网上理财:收支情况明细表 </TITLE>
<BODY>
<P ALIGN=CENTER><FONT FACE="隶书" SIZE="7" COLOR="#0000FF">
网上理财</FONT><BR>
<P ALIGN=LEFT>
<FONT FACE="宋体" SIZE="3" COLOR="#0000FF"><a href=index.php 
STYLE=Text-Decoration:none>首页</a>>><a href=report_3.php
STYLE=Text-Decoration:none>现金流量表</a>
<P ALIGN=CENTER><FONT FACE="隶书" SIZE="6" COLOR="#0000FF">
现金流量表</FONT><BR>
<FORM NAME="report_3" ACTION="report_3.php" METHOD="POST">
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD BGCOLOR="#FFFFFF" width=700>
<Font Face=宋体 Size=2 Color="#0000FF" >从<INPUT TYPE=TEXT
NAME="date_begin" SIZE="10" MAXLENGTH="10" value='<?php
if (!$date_begin)
{$today=getdate();
$date_begin=sprintf ("%04s-01-01",$today['year']);}
echo $date_begin
?>' onchange=javascript:document.report_3.submit()>到<INPUT 
TYPE=TEXT NAME="date_end" SIZE="10" MAXLENGTH="10" value='<?php
if (!$date_end)
{$today=getdate();
$date_end=sprintf ("%04s-%02s-%02s",$today['year']
,$today['mon'],$today['mday']);}
echo $date_end
?>' onchange=javascript:document.report_3.submit()>
</FONT>
</TD><TD BGCOLOR="#FFFFFF" width=100>
</TD></TR>
</TABLE>
</FORM><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0"
align=center>
<TR>
<TD BGCOLOR="#FFFFFF" ALIGN="left" VALIGN="MIDDLE" width=150>
<Font Face=宋体 Size=4 Color="#0000FF" >
 </FONT>
</TD><TD BGCOLOR="#FFFFFF" ALIGN="left" width=100><Font 
Face=宋体 Size=4 Color="#0000FF" >
收支类目</FONT>
</TD><TD BGCOLOR="#FFFFFF" ALIGN="left" width=150>
<Font Face=宋体 Size=4 Color="#0000FF" >
金额_折算人民币</FONT>
</TD></TR>
<?php
$report_3= new report_3;
$k=$report_3->add_item($cookie_user,$date_begin,$date_end);
if (!$limit) $limit=0;
$i=1;
while ($i<=15 and ($i+$limit)<=$k)
{echo "<TR>";
echo "<TD BGCOLOR='#FFFFFF' ALIGN=left>";
echo "<Font Face=宋体 Size=3 Color='#0000FF' >";
echo $report_3->type[$limit+$i];
echo "</FONT>";
echo "</TD><TD BGCOLOR='#FFFFFF' ALIGN=left>";
echo "<Font Face=宋体 Size=2 Color='#0000FF' >";
echo $report_3->in_ex_type[$limit+$i];
echo "</FONT>";
echo "</TD><TD BGCOLOR='#FFFFFF' ALIGN=right>";
if ($report_3->in_ex_type[$limit+$i])
  echo "<Font Face=宋体 Size=2 Color='#0000FF' >";
else
  echo "<Font Face=宋体 Size=2 Color='#000000' >";
if ($report_3->money[$limit+$i])
printf ("%.2f",$report_3->money[$limit+$i]);
echo "</FONT>";
echo "</TD></TR>";
$i++;}
echo "</TABLE>";
echo "<P ALIGN=RIGHT>";
$total=floor(($k-1)/15)*15; 
$limit_pre=$limit-15;
$limit_next=$limit+15;
echo "<Font Face=宋体 Size=3 Color='#0000FF' >";
echo "第";
echo $limit/15+1;
echo "页 共";
echo $total/15+1;
echo "页 ";
if ($limit!=0)
echo "<a href='report_3.php?limit=0
&date_begin=.$date_begin.&date_end=.$date_end.' 
STYLE='Text-Decoration:none'>";
echo "首页 ";
if ($limit!=0)
echo "</a>";
if ($limit<$total)
echo "<a href='report_3.php?limit=.$total.
&date_begin=.$date_begin.&date_end=.$date_end.' 
STYLE='Text-Decoration:none'>";
echo "尾页 ";
if ($limit!=$total)
echo "</a>";
if ($limit_pre>=0)
echo "<a href='report_3.php?limit=.$limit_pre.
&date_begin=.$date_begin.&date_end=.$date_end.' 
STYLE='Text-Decoration:none'>";
echo "前页 ";
if ($limit_pre>=0) echo "</a>";
if ($limit_next<=$total)
echo "<a href='report_3.php?limit=.$limit_next.
&date_begin=.$date_begin.&date_end=.$date_end.'
STYLE='Text-Decoration:none'>";
echo "后页 ";
if ($limit_next<=$total)
echo "</a>";
echo "</FONT>";
?>
</BODY>
</HTML>


 如果您对本文有任何疑问或者建议,请到讨论区发表您的意见: >> 论坛入口 <<



上一篇:一个网上理财站点的设计(二)   下一篇:什么是防火墙

文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【我要投稿】 【论坛讨论
更多相关文章