<?php

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

if(!$_GET){
    Session::Set('error',"没有选择订单！");
    Utility::Redirect(WEB_ROOT."/order/index.php");
}


$order_number = $_GET['order_number'];

if($_POST){
    $item = $_POST['item'];
    
    
    foreach($item as $k=>$v){ 
        
        $item_one = Table::Fetch('order_item',$k);
        
        if(!isset($v['quantity']) || $v['quantity']>$item_one['quantity']){
            Session::Set('error','数量设置错误！已支付订单，新数量只能小于原订单数量！'.$v['quantity']);
            redirect(WEB_ROOT."/manage/order_item.php?order_number=".$order_number);
        }
        $update = array();
        $update['quantity'] = $v['quantity'];
        if($v['quantity']==0)$v['refund']="on";
        if( isset($v['refund']) && $item_one['state']!='refund' ) $update['state'] = 'refunding'; 
        Table::UpdateCache('order_item', $k, $update);
    }
    $order_number = $item_one['order_number'];
    OOrder::update($order_number);

}


$condition['order_number'] = $order_number;
$count = Table::Count('order_item', $condition);
list($pagesize, $offset, $pagestring) = pagestring($count, 50);
$sql = "select a.*,b.name as category_name,b.zone
        from order_item as a,category as b
        where a.category_id=b.id and a.order_number='".$order_number."'
        order by id ASC
        limit ".$offset.",".$pagesize;
$order_item = DB::GetQueryResult($sql, FALSE);

$order_info = Table::Fetch('order_info',$order_number,'order_number');
$state = $INI['sys']['order'];
if(count($order_item)==0){
    Session::Set('error','没有任何订单信息！');
}

include template('manage_order_item');
?>
