<?php

/**
 * ECSHOP 会员排行统计程序
 * ============================================================================
 * 版权所有 (C) 2005-2007 北京亿商互动科技发展有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * @author:     liry <liry123@hotmail.com>
 * @version:    v2.1
 * ---------------------------------------------
 * $Author: paulgao $
 * $Date: 2007-04-19 18:11:35 +0800 (星期四, 19 四月 2007) $
 * $Id: users_order.php 8272 2007-04-19 10:11:35Z paulgao $
*/

define('IN_ECS', true);

require('includes/init.php');
require_once('../languages/' .$_CFG['lang']. '/admin/statistic.php');

/* act操作项的初始化 */
if (empty($_REQUEST['act']))
{
    $_REQUEST['act'] = 'order_num';
}
else
{
    $_REQUEST['act'] = trim($_REQUEST['act']);
}

$smarty->assign('lang', $_LANG);

/* 权限判断 */
admin_priv('client_flow_stats');

/* 时间参数 */
if (isset($_POST) && !empty($_POST))
{
    $start_date = trim($_POST['start_dateYear']. '-' .$_POST['start_dateMonth']. '-' .$_POST['start_dateDay']);
    $end_date   = trim($_POST['end_dateYear']. '-' .$_POST['end_dateMonth']. '-' .$_POST['end_dateDay']);
}
else
{
    $start_date = date('Y-m-d', time() - 86400 * 30);
    $end_date   = date('Y-m-d', time() + 24 * 3600);
}

/* 根据用户条件生成报表显示的数量*/
$show_num   = (!empty($_POST['show_num'])) ? intval($_POST['show_num']) : 15;

/*------------------------------------------------------ */
//--按订单数量排行
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'order_num')
{
    /* 取得会员排行数据 */
    $user_orderinfo = get_user_orderinfo($show_num, 'order_num', $start_date, $end_date);

    /* 赋值到模板 */
    $smarty->assign('ur_here',      $_LANG['report_users']);
    $smarty->assign('action_link',  array('text' => $_LANG['buy_sum_sort'],
    'href'=>'users_order.php?act=turnover'));

    $smarty->assign('user_orderinfo', $user_orderinfo);
    $smarty->assign('start_date',     $start_date);
    $smarty->assign('end_date',       $end_date);

    $smarty->assign('form_act',       'order_num');
    $smarty->assign('show_num',       $show_num);

    /* 页面显示 */
    assign_query_info();
    $smarty->display('users_order.htm');
}
/*------------------------------------------------------ */
//--按购物金额排行
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'turnover')
{
    /* 取得会员排行数据 */
    $user_orderinfo = get_user_orderinfo($show_num, 'turnover', $start_date, $end_date);

    /* 赋值到模板 */
    $smarty->assign('user_orderinfo', $user_orderinfo);
    $smarty->assign('start_date',     $start_date);
    $smarty->assign('end_date',       $end_date);

    $smarty->assign('form_act',       'turnover');
    $smarty->assign('show_num',       $show_num);

    $smarty->assign('ur_here',      $_LANG['report_users']);
    $smarty->assign('action_link',  array('text' => $_LANG['order_amount_sort'],
    'href'=>'users_order.php?act=order_num'));

    /* 显示页面 */
    assign_query_info();
    $smarty->display('users_order.htm');
}
/*------------------------------------------------------ */
//--会员排行需要的函数
/*------------------------------------------------------ */
/*
 * 取得会员订单量/购物额排名统计数据
 *
 * @param   int             $show_num        每页显示的数量
 * @param   timestamp       $start_date      开始时间
 * @param   timestamp       $end_date        结束时间
 * @return  array                            会员购物排行数据
 */
 function get_user_orderinfo($show_num, $order_by, $start_date, $end_date)
 {
    global $db, $ecs;

    $where = "WHERE u.user_id = o.user_id ".
             "AND u.user_id > 0 AND o.order_status ='" .OS_CONFIRMED. "' AND o.pay_status > '" .PS_UNPAYED. "' ".
             "AND o.shipping_status > '" .SS_UNSHIPPED. "' ";
    $limit = " LIMIT " .$show_num;

    if ($start_date)
    {
        $where .= "AND o.order_time >= '$start_date' ";
    }

    if ($end_date)
    {
        $where .= "AND o.order_time <= '$end_date' ";
    }

    /* 计算订单各种费用之和的语句 */
    $total_fee = " SUM(goods_amount + shipping_fee + insure_fee + pay_fee + pack_fee + card_fee) AS turnover ";

    if ($order_by == 'order_num')
    {
        /* 按订单数量来排序 */
        $sql = "SELECT u.user_id, u.user_name, COUNT(*) AS order_num, " .$total_fee.
               "FROM ".$ecs->table('users')." AS u, ".$ecs->table('order_info')." AS o " .$where .
               "GROUP BY u.user_id ORDER BY order_num DESC, turnover DESC" . $limit;
    }
    else
     {
        /* 按购物金额来排序 */
        $sql = "SELECT u.user_id, u.user_name, COUNT(*) AS order_num, " .$total_fee.
               "FROM ".$ecs->table('users')." AS u, ".$ecs->table('order_info')." AS o " .$where .
               "GROUP BY u.user_id ORDER BY turnover DESC, order_num DESC" . $limit;
     }

    $user_orderinfo = array();
    $res = $db->query($sql);

    while ($items = $db->fetchRow($res))
    {
        $items['turnover'] = price_format($items['turnover']);
        $user_orderinfo[] = $items;
    }

    return $user_orderinfo;
}

?>