<?php

/**
 * ECSHOP 销售明细列表程序
 * ============================================================================
 * 版权所有 (C) 2005-2007 北京亿商互动科技发展有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * @author:     liry <liry123@hotmail.com>
 * @version:    v2.1
 * ---------------------------------------------
 * $Author: paulgao $
 * $Date: 2007-05-08 16:55:21 +0800 (星期二, 08 五月 2007) $
 * $Id: sale_list.php 8513 2007-05-08 08:55:21Z paulgao $
*/

define('IN_ECS', true);

require('includes/init.php');
require_once('../languages/' .$_CFG['lang']. '/admin/statistic.php');
$smarty->assign('lang', $_LANG);

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

/* 权限判断 */
admin_priv('sale_order_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);
}

/* 查询数据的条件 */
$goods_sales_list = array();
$where = " WHERE og.order_id = oi.order_id".
         " AND oi.order_status = '".OS_CONFIRMED."' AND oi.pay_status > '".PS_UNPAYED."'".
         " AND oi.shipping_status > '".SS_UNSHIPPED."'".
         " AND oi.order_time >= '$start_date' AND oi.order_time <= '$end_date'";

$sql = 'SELECT og.goods_id, og.goods_sn, og.goods_name, og.goods_number AS goods_num, og.goods_price '.
       'AS sales_price, oi.order_time AS sales_time, oi.order_id, oi.order_sn '.
       "FROM " . $ecs->table('order_goods')." AS og, ".$ecs->table('order_info')." AS oi ".
       $where. " ORDER BY sales_time DESC, goods_num DESC";

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

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

/*------------------------------------------------------ */
//--商品明细列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
    /* 赋值到模板 */
    $smarty->assign('goods_sales_list', $goods_sales_list);
    $smarty->assign('start_date',       $start_date);
    $smarty->assign('end_date',         $end_date);

    $smarty->assign('ur_here',      $_LANG['sale_list']);
    $smarty->assign('action_link',  array('text' => $_LANG['down_sales'],
    'href'=>'sale_list.php?act=down_sales_list&start_date='.$start_date.'&end_date='.$end_date));

    /* 显示页面 */
    assign_query_info();
    $smarty->display('sale_list.htm');
}

/*------------------------------------------------------ */
//--Excel文件下载
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'down_sales_list')
{
    include_once(ROOT_PATH.'includes/iconv/cls_iconv.php');
    $iconv = new Chinese(ROOT_PATH);

    $file_name = $_REQUEST['start_date'].'_'.$_REQUEST['end_date'] . '_sale';

    header("Content-type: application/vnd.ms-excel; charset=GB2312");
    header("Content-Disposition: attachment; filename=$file_name.xls");

    /* 文件标题 */
    echo $iconv->convert('UTF8', 'GB2312', $_REQUEST['start_date']. $_LANG['to'] .$_REQUEST['end_date'] .
    $_LANG['sales_list']) . "\t\n";

    /* 商品名称,订单号,商品数量,销售价格,销售日期 */
    echo $iconv->convert('UTF8', 'GB2312', $_LANG['goods_name']) . "\t";
    echo $iconv->convert('UTF8', 'GB2312', $_LANG['order_sn']) . "\t";
    echo $iconv->convert('UTF8', 'GB2312', $_LANG['amount']) . "\t";
    echo $iconv->convert('UTF8', 'GB2312', $_LANG['sell_price']) . "\t";
    echo $iconv->convert('UTF8', 'GB2312', $_LANG['sell_date']) . "\t\n";

    foreach ($goods_sales_list AS $key => $value)
    {
        echo $iconv->convert('UTF8', 'GB2312', $value['goods_name']) . "\t";
        echo $iconv->convert('UTF8', 'GB2312', '[ '.$value['order_sn'].' ]') . "\t";
        echo $iconv->convert('UTF8', 'GB2312', $value['goods_num']) . "\t";
        echo $iconv->convert('UTF8', 'GB2312', $value['sales_price']) . "\t";
        echo $iconv->convert('UTF8', 'GB2312', $value['sales_time']) . "\t";
        echo "\n";
    }
}

?>