<?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: guest_stats.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'] = 'list';
}
else
{
    $_REQUEST['act'] = trim($_REQUEST['act']);
}

/*------------------------------------------------------ */
//-- 客户统计列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
    /* 权限判断 */
    admin_priv('client_flow_stats');

    /* 取得会员总数 */
    $users      =& init_users();
    $user_num   = $users->get_user_count();

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

    /* 有过订单的会员数 */
    $sql = 'SELECT COUNT(DISTINCT user_id) FROM ' .$ecs->table('order_info').
           " WHERE user_id > 0 AND order_status = '" .OS_CONFIRMED. "' AND pay_status > '" .PS_UNPAYED. "'".
           " AND shipping_status > '" .SS_UNSHIPPED. "'";
    $have_order_usernum = $db->getOne($sql);

    /* 会员订单总数和订单总购物额 */
    $user_all_order = array();
    $sql = "SELECT COUNT(*) AS order_num, " . $total_fee.
           "FROM " .$ecs->table('order_info').
           " WHERE user_id > 0 AND order_status = '".OS_CONFIRMED."' AND pay_status > '" .PS_UNPAYED. "'".
           " AND shipping_status > '" .SS_UNSHIPPED. "'";
    $user_all_order = $db->getRow($sql);
    $user_all_order['turnover'] = floatval($user_all_order['turnover']);

    /* 匿名会员订单总数和总购物额 */
    $guest_all_order = array();
    $sql = "SELECT COUNT(*) AS order_num, " . $total_fee.
           "FROM " .$ecs->table('order_info').
           " WHERE user_id = 0 AND order_status = '" .OS_CONFIRMED. "' AND pay_status > '" .PS_UNPAYED. "'".
           " AND shipping_status > '" .SS_UNSHIPPED. "'";
    $guest_all_order = $db->getRow($sql);

    /* 匿名会员平均订单额: 购物总额/订单数 */
    $guest_order_amount = ($guest_all_order['order_num'] > 0) ? floatval($guest_all_order['turnover'] / $guest_all_order['order_num']) : '0.00';

    /* 赋值到模板 */
    $smarty->assign('user_num',            $user_num);                    // 会员总数
    $smarty->assign('have_order_usernum',  $have_order_usernum);          // 有过订单的会员数
    $smarty->assign('user_order_turnover', $user_all_order['order_num']); // 会员总订单数
    $smarty->assign('user_all_turnover',   price_format($user_all_order['turnover']));  //会员购物总额
    $smarty->assign('guest_all_turnover',  price_format($guest_all_order['turnover'])); //匿名会员购物总额
    $smarty->assign('guest_order_num',     $guest_all_order['order_num']);              //匿名会员订单总数

    /* 每会员订单数 */
    $smarty->assign('ave_user_ordernum',  $user_num > 0 ? sprintf("%0.2f", $user_all_order['order_num'] / $user_num) : 0);

    /* 每会员购物额 */
    $smarty->assign('ave_user_turnover',  $user_num > 0 ? price_format($user_all_order['turnover'] / $user_num) : 0);

    /* 注册会员购买率 */
    $smarty->assign('user_ratio', sprintf("%0.2f", ($user_num > 0 ? $have_order_usernum / $user_num : 0) * 100));

     /* 匿名会员平均订单额 */
    $smarty->assign('guest_order_amount', $guest_all_order['order_num'] > 0 ? price_format($guest_all_order['turnover'] / $guest_all_order['order_num']) : 0);

    $smarty->assign('all_order',          $user_all_order);    //所有订单总数以及所有购物总额
    $smarty->assign('ur_here',            $_LANG['report_guest']);
    $smarty->assign('lang',               $_LANG);

    assign_query_info();
    $smarty->display('guest_stats.htm');
}

?>