<?php

require_once(dirname(dirname(__FILE__)) . '/app.php');
need_manager();


$count = Table::Count('user');
list($pagesize, $offset, $pagestring) = pagestring($count, 20);

$user = DB::LimitQuery('user',array(
	'size' => $pagesize,
	'offset' => $offset,
));
include template("header");
$sql = array();
$flow = array();
echo "<h2>对账</h2>
    <table>
    <thead>
    <tr><th>用户名</th><th>充值</th><th>退款</th><th>提现</th><th>下单</th><th>收入</th><th>支出</th><th>余额（用户）</th><th>财务下单-财务退款（实际下单）</th></tr></thead>";
foreach($user as $one){
    
    $sql[0] = "select count(id) as num,sum(`money`) as money from flow
            where action='store' and user_id=".$one['id'];
    $sql[1] = "select count(id) as num,sum(`money`) as money from flow
            where action='refund' and user_id=".$one['id'];
    $sql[2] = "select count(id) as num,sum(`money`) as money from flow
            where action='withdraw' and user_id=".$one['id'];
    $sql[3] = "select count(id) as num,sum(`money`) as money from flow
            where action='order' and user_id=".$one['id'];
    $sql[4] = "select count(id) as num,sum(money) as money from flow
            where direction='income' and user_id=".$one['id'];
    $sql[5] = "select count(id) as num,sum(money) as money from flow
            where direction='expense' and user_id=".$one['id'];

    
    for($i=0;$i<count($sql);$i++){
        $re = DB::GetQueryResult($sql[$i],true);
        (is_null($re['money'])) ? $flow[$i]['money']=0 :$flow[$i]['money'] = $re['money'];
        $flow[$i]['num'] = $re['num'];
    }

    
    $sql1 = "select sum(`sum`) as money from order_info
            where state!='unpay' and user_id=".$one['id'];
    $order = DB::GetQueryResult($sql1,true);
    (count($order)==0)? $order=0 :$order = $order['money'];
    
    $sql2 = "select username,sum(money) as money from user where id=".$one['id'];
    $money = DB::GetQueryResult($sql2,true);
    ;echo '    <tr id="user_'; echo $one['id']; ;echo '" onmouseover="cart_item_over(\'user_'; echo $one['id']; ;echo '\')" onmouseout="cart_item_out(\'user_'; echo $one['id']; ;echo '\')">
    ';
    echo "<td>".$money['username']."</td><td>[".$flow[0]['num']."]".$flow[0]['money']."</td><td>[".$flow[1]['num']."]".$flow[1]['money']."
        </td><td>[".$flow[2]['num']."]".$flow[2]['money']."</td><td>[".$flow[3]['num']."]".$flow[3]['money']."</td><td>[".$flow[4]['num']."]".$flow[4]['money']."
            </td><td>[".$flow[5]['num']."]".$flow[5]['money']."</td><td>".($flow[4]['money']-$flow[5]['money'])."(".$money['money'].")</td><td>".($flow[3]['money']-$flow[1]['money'])."(".$order.")</td></tr>";
}
echo "<tr><td colspan='9'>".$pagestring."</td></tr></table>";
 include template("footer");
?>
