<?php

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


if($_GET){
    if(isset($_GET['act']) && $_GET['act']=='send'){
        $express_no = $_POST['express_no'];
        $order_n = 0;
        foreach($express_no as $k=>$v){
            OOrder::send($k,$v);
            $order_n++;
        }
        Session::Set('success',$order_n.'个订单发货成功！');
    }

}



$condition1 = '';
$condition = array();
if(isset($_POST['order_number']) && !empty($_POST['order_number'])){
    $condition['order_number'] = $_POST['order_number'];
    $condition1 .= "and order_info.order_number='".$_POST['order_number']."' ";
}
if(isset($_POST['user_name']) && !empty($_POST['user_name'])){
    $condition['user_id'] = OUser::GetUserId($_POST['user_name']);
    $condition1 .= "and order_info.user_id='".$condition['user_id']."' ";
}
if(isset($_POST['real_name']) && !empty($_POST['real_name'])){
    $condition['realname'] = $_POST['real_name'];
    $condition1 .= "and order_info.realname='".$_POST['real_name']."' ";
}
if(isset($_POST['provider']) && !empty($_POST['provider'])){
    $condition['provider_id'] = $_POST['provider'];
    $condition1 .= "and order_info.provider_id='".$_POST['provider']."' ";
}

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

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




(mceil(1.2)==2)? $ceil="ceil" : $ceil="";
$sql = "SELECT user.username, order_info . * , provider.name AS provider_name, provider.channel, provider.brand, fare.name AS express_name,
        sum( ".$ceil."(order_item.origin*order_item.discount)*order_item.quantity - order_item.origin*order_item.cost*order_item.quantity ) AS mll
        FROM `user` , `order_info` , `provider` , `fare` , `order_item`
        WHERE fare.id = order_info.express_id
        AND user.id = order_info.user_id
        AND order_info.provider_id = provider.id
        AND order_info.order_number=order_item.order_number ".$condition1."
        GROUP BY order_item.order_number
        ORDER BY order_info.pay_time DESC
        LIMIT ".$offset.",".$pagesize;

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

$sql = "select provider.id,provider.name
        from provider,order_info
        where order_info.provider_id=provider.id ".$condition1."
        group by order_info.provider_id";
$provider = DB::GetQueryResult($sql,false);
if($count==0){
    Session::Set('error','没有任何订单信息！');
}

include template('manage_order');
?>
