<?php


class OCart{
    
    public static function addItem($id,$quantity){
        global $INI;
        global $login_user_id;
        global $login_user;
        $item = Table::Fetch('item',$id);
        $category = Table::Fetch('category',$item['category_id']);
        $tb = new Table($INI['db']['profix'] . "Cart");
        $tb->item_id = $id;
        $tb->user_id = $login_user_id;
        $tb->create_time = time();
        $tb->quantity = $quantity;
        $tb->provider_id = $item['provider_id'];
        $tb->category_id = $item['category_id'];
        $tb->origin = $item['origin'];
        $tb->item_number = $item['item_number'];
        $tb->size = $item['size'];
        $tb->weight=$item['weight'];

        
        
        
        $user_level = $INI['sys']['level'][$login_user['level']];
        $curr_discount = $category['user_discount']+$INI['sys']['level'][$login_user['level']]/100;
        if($curr_discount<=$category['cost_discount']) $curr_discount = $category['cost_discount'] + 0.01; 
        $tb->discount = $curr_discount;


        return $tb->Insert('item_id','category_id','provider_id','user_id','create_time','quantity','item_number','size','origin','discount','weight');
    }
    
    public static function getProvider(){
        global $INI;
        global $login_user_id;
        $provider = array();
        $sql = "SELECT provider.* FROM `cart`,`provider` WHERE provider.id = cart.provider_id and cart.user_id=".$login_user_id." group by cart.provider_id order by provider.id DESC";
        $provider = DB::GetQueryResult($sql,false);
        return $provider;
    }
    public static function getProvince(){
        global $INI;
        global $login_user_id;
        $province = array();
        $sql = "SELECT provider.id as provider_id,provider.province FROM `cart`,`provider` WHERE provider.id = cart.provider_id and cart.user_id=".$login_user_id." group by provider.province order by provider.id DESC";
        $province = DB::GetQueryResult($sql,false);
        return $province;
    }
    
    public static function delItem($id){
        global $INI;
        Table::Delete($INI['db']['prefix'] . "Cart",$id);
        return true;
    }
    
    public static function sum(){
        global $INI;
        global $login_user_id;
        $sum = array();
        
        $ceil = "";
        
        if(mceil(1.3)==2) $ceil = 'ceil';
        $sql = "select c.provider_id,p.province,p.name,p.channel,sum(c.weight*c.quantity) as sum_weight,sum(quantity*".$ceil."(origin*discount)) as sum_money from cart as c,provider as p ";
        $sql .="where c.user_id=".$login_user_id." and p.id=c.provider_id group by c.provider_id order by c.provider_id ASC";
        
        $sql1 = "select provider_id,0 as province,0 as name,0 as channel,sum(weight*quantity) as sum_weight,sum(quantity*".$ceil."(origin*discount)) as sum_money from cart where user_id=".$login_user_id;
        $sum = DB::GetQueryResult($sql,false);
        $sum1 = DB::GetQueryResult($sql1);
        array_push($sum,$sum1);
        return $sum;
    }
    public static function clear(){
        global $INI;
        global $login_user_id;
        return Table::Delete($INI['db']['prefix']."Cart",$login_user_id, 'user_id');
    }
    public static function modiQuantity($id,$quantity){
        global $INI;
        global $login_user_id;
        return Table::UpdateCache($INI['db']['prefix']."Cart", $id, array( 'quantity' => $quantity));
    }
    public static function hasItem($item_id){
        global $INI;
        global $login_user_id;
        $condition['user_id'] = $login_user_id;
        $condition['item_id'] = $item_id;
        $item = DB::LimitQuery('cart', array(
            'condition' => $condition,
            'size' =>1
        ));
        if(count($item)>0) return $item[0];
        return false;
    }
}
function cart_ut1(){
    include_once(dirname(dirname(dirname(__FILE__)))."/app.php");
    global $INI;
    global $login_user_id;
    $login_user_id = 1;
    
    
    
    
    OCart::modiQuantity(8,8);
}

function cart_ut2(){
    include_once(dirname(dirname(dirname(__FILE__)))."/app.php");
    global $INI;
    global $login_user_id;
    $login_user_id = 1;
    echo "<pre>";
    
    echo count(OCart::hasItem(1555));
    echo "</pre>";
}

?>
