<?php

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




$condition = array();
$condition['user_id'] = $login_user_id;
$condition1 = '';
if($_GET){
    if(isset($_GET['state'])){
        $condition['state'] = $_GET['state'];
        $condition1 = "and order_info.state='".$_GET['state']."'";
    }
}

$count = Table::Count('flow',$condition);
list($pagesize, $offset, $pagestring) = pagestring($count, 20);
$sql = "SELECT user.username as username,admin.username adminname,flow.*
        FROM user as user,user as admin,flow
        WHERE user.id=flow.user_id and admin.id=flow.admin_id and flow.user_id=".$login_user_id." ".$condition1."
        ORDER BY flow.create_time DESC
        LIMIT ".$offset.",".$pagesize;

$flow = DB::GetQueryResult($sql,false);

$sql = "SELECT sum( money ) as money
        FROM flow
        WHERE direction = 'income'
        AND user_id =".$login_user_id."
        UNION ALL
        SELECT sum( money ) as money
        FROM flow
        WHERE direction = 'expense'
        AND user_id =".$login_user_id;
$sum = DB::GetQueryResult($sql,false);

if($count==0){
    Session::Set('error','没有任何账务信息！');
}

include template('account_flow');
?>
