www.gusucode.com > citySHOP B2C商城系统 进销存 2.0.6码程序 > upload/cart.php

    <?php
define('NOROBOT', true);
include './include/common.inc.php';
$act = isset($act) ? trim($act) : '';

$pagetitle = '我的购物车';
$headlinks = showlinks('css','style');
$jslinks = showlinks('js','common');
$cart = isset($_MCOOKIE['cart'])?unserialize(stripslashes($_MCOOKIE['cart'])):array();

if (empty($cart)) {
    showmessage('对不起,您还没有挑选商品,不能结算。快去挑选你心爱的商品吧!', $indexname);
}
$gdsub = array('num' => 0, 'price' => 0, 'weight' => 0, 'gids' => '0');
$cartgoods = array();
$real_total = $hg_total = 0;
foreach($cart as $gid=>$gd){
		include (cacheload('goods'));
  		$goods = $_MCACHE['goods'];
  		if($goods['discount']<10){
  			$gd['t'] = '5';//特价
  		}
  		$photo = unserialize($goods['photos']);
  		$gd['n'] = submitcheck('clearingsubmit')?$gnums[$gid]:$gd['n'];
  		$cart[$gid]['n'] = $gd['n'];
  		$gdsub['num'] += $gd['n'];
		$gdsub['price'] += $gd['p']*$gd['n'];

		$real_total += $gd['p']*$gd['n'];

		$hg_total += $gd['p']*$gd['n'];

  		$gdsub['weight'] += ($gd['n'] * $goods['weight']);
  		$gdsub['gids'] .= ','.$gid;
  		$goods['t'] = $gd['t'];
  		$goods['u'] = $gd['u'];
  		$goods['zprice'] = $gd['p'];
  		$goods['gnum'] = $gd['n'];
  		$goods['phead'] = '';
  		$goods['u1'] = isset($gd['u1'])?$gd['u1']:'';
  		switch($gd['t']){
  			case '1':
  			$goods['phead'] = '<span class="cred">(搭配购买)</span>';
  			break;
  			case '2':
  			$goods['phead'] = '<span class="cred">(自动购买)</span>';
  			break;
  			case '3':
  			$goods['phead'] = '<span class="cred">('.$gd['u1'].'元自选礼包)</span>';
		    break;
  			case '4':
  			$goods['phead'] = '<span class="cred">(促销自选礼包)</span>';
  			break;
  			case '6':
  			$goods['phead'] = '<span class="cred">(换购)</span>';
  			break;
  		}
  		$cartgoods[] = $goods;
 } 	
		 
 if(submitcheck('clearingsubmit')){
 	dsetcookie('cart',serialize($cart),2592000);
 }
 $hg_total = round($hg_total,2);
 $real_total = number_format($real_total,2,'.','');
 $gdsub['price'] = number_format($gdsub['price'],2,'.','');
 $gdsub['weight'] = ceil($gdsub['weight']);
if ($act == 'clearcart') {
    dsetcookie('cart');
    showmessage('购物车内商品已成功清除!');
} elseif ($act == 'clearing') {
    $pagetitle = '填写核对订单信息';

    if (!$mall_uid) {
        showmessage('您好,请先登陆再进行结算!', 'login.php');
    }

    $query = $db->query("SELECT * FROM {$tablepre}members m LEFT JOIN {$tablepre}address a ON m.uid=a.uid AND a.default=1 WHERE m.uid='$mall_uid'");
    $member = $db->fetch_array($query);

    if ($member['area']) {
        $query = $db->query("SELECT * FROM {$tablepre}places WHERE placeid IN ($member[province],$member[city],$member[area])");
        while ($arr = $db->fetch_array($query)) {
            $place[$arr['placeid']] = $arr['name'];
        }
    }
    $recievemode = false;
    if (empty($member['realname']) || empty($member['area']) || empty($member['address']) || (empty($member['homephone']) && empty($member['mobilphone'])) ||
        empty($member['email'])) {
        $recievemode = true;
    }
}elseif($act == 'delfromcart'){
	$gid = intval($gdid);
	if(!$gid){
		showmessage('undefined_action');
	}
	if(in_array($cart[$gid]['t'],array('3','4'))){
		$pkid = $cart[$gid]['u'];
		foreach($cart as $ggid => $ct){
			if($pkid==$ct['u']){
				unset($cart[$ggid]);
			}
		}
	}elseif($cart[$gid]['t']=='0'){
		unset($cart[$gid]);
		foreach($cart as $ggid => $ct){
			if($ct['t']=='1' && $ct['u']==$gid){
				unset($cart[$ggid]);
			}
		}
	}else{
		unset($cart[$gid]);
	}
	foreach($cart as $ggid => $ct){
		if($ct['t']=='6'){
			unset($cart[$ggid]);
		}
	}
	dsetcookie('cart',serialize($cart),2592000);
	showmessage('成功从购物车移除!','cart.php');    
}elseif($act == 'addtocart'){
	$tp = isset($tp)?intval($tp):'0';
	if($gdid<1){
		showmessage('商品编号错误!',$returnpage);
	}
	foreach($cart as $ggid => $ct){
		if($ggid==$gdid){
			showmessage('该商品已经在订单里了。','cart.php');
		}
	}
	if($tp=='6'){
		$tp6 = 0;
		foreach($cart as $gid=>$gd){
			if($gd['t']=='6'){
				$tp6++;
			}
		}
		if($tp6>=$db->result_first("SELECT randomnum FROM {$tablepre}packages WHERE packid='$pkid'")){
			showmessage('对不起,换购区可购商品数量已满!','cart.php');
		}
		$cart[$gdid] = array('n'=>'1','p'=>$price,'t'=>$tp,'u'=>intval($pkid));
	}else{
		$cart[$gdid] = array('n'=>'1','p'=>$price,'t'=>$tp,'u'=>'0');
	}
	dsetcookie('cart',serialize($cart),2592000);
	showmessage('已成功添加到购物车!','cart.php');
}elseif ($act == 'submit') {
	/**
	 * 检测是否有有非法方式的订单
	 */
	 /*
	 $error_mode = false;
	 $error_str = '';
	 foreach ($cartgoods as $gd) {
		switch($gd['t']){
			case '0':	//普通方式购买
	  			
	  			if($gd['zprice']!=number_format($gd['eprice']*$gd['discount']/10,2)){
	  				$error_mode = true;
	  				$error_str = '1000';
	  			}
	  			break;
	  		case '1':	//搭配购买
	  			if(!in_array($gd['gid'],explode(',',$gdsub['gids']))){
	  				$error_mode = true;
	  			}
	  			if($db->result_first("SELECT pg.newprice FROM {$tablepre}packagegoods pg,{$tablepre}packages p WHERE p.maingid='$gd[u]' AND p.packid=pg.packid AND pg.gid='$gd[gid]'")!=$gd['zprice']){
	  				$error_mode = true;
	  				$error_str = '1001';
	  			}
			    break;
			case '2':	//自动购买
				$darr = $db->fetch_array($db->query("SELECT * FROM {$tablepre}autobuy WHERE gid='$gd[gid]'"));
				if($gd['zprice']!=number_format($gd['eprice']*$darr['discount']/10,2) || $gd['gnum']<$darr['limitnum']){
					$error_mode = true;
					$error_str = '1002';
				}
				break;
			case '3':	//固定价格自选礼包
				$darr = $db->fetch_array($db->query("SELECT * FROM {$tablepre}packages WHERE packid='$gd[u]'"));
				if($gd['u1']!=$darr['subprice'] || $gd['zprice']!=number_format($darr['subprice']/$darr['randomnum'],2)){
					$error_mode = true;
					$error_str = '1003';
				}
				break;	    
			case '4':	//促销自选礼包
				if($db->result_first("SELECT pg.newprice FROM {$tablepre}packagegoods pg,{$tablepre}packages p WHERE p.packid='$gd[u]' AND p.packid=pg.packid AND pg.gid='$gd[gid]'")!=$gd['zprice']){
	  				$error_mode = true;
	  				$error_str = '1004';
	  			}
				break;	
			case '6':	//换购
				$darr = $db->fetch_array($db->query("SELECT pg.newprice,p.subprice FROM {$tablepre}packagegoods pg,{$tablepre}packages p WHERE p.packid='$gd[u]' AND p.packid=pg.packid AND pg.gid='$gd[gid]'"));
				if($darr['newprice']!=$gd['zprice'] || $gdsub['price']<$darr['subprice']){
	  				$error_mode = true;
	  				$error_str = '1006';
	  			}
				break;    	
		}
	 }
     
	 if($error_mode){
	 	showmessage('订单异常:'.$error_str.',通常情况是之前保存的购物车内的商品与目前商品有冲突,您可清除购物车重新添加可解决问题。<br />是否清除购物车? <a href="cart.php?act=clearcart">立即清除>></a>','KEEP');
	 }
     */
  	/**
  	  * 提交订单
  	  */ 
    $pagetitle = '成功提交订单';
    
	$member = $db->fetch_array($db->query("SELECT * FROM {$tablepre}members m,{$tablepre}address a WHERE m.uid='$mall_uid' AND a.uid=m.uid AND a.default=1 "));
	if($defaddr!=$member['adid']){
		$db->query("UPDATE {$tablepre}address SET `default`='0' WHERE uid='$mall_uid'");
		$db->query("UPDATE {$tablepre}address SET `default`='1' WHERE adid='$defaddr'");
		$member = $db->fetch_array($db->query("SELECT * FROM {$tablepre}members m,{$tablepre}address a WHERE m.uid='$mall_uid' AND a.uid=m.uid AND a.default=1 "));
	}
    if (submitcheck('postsubmit')) {
    	if($serpay>0){
    		$gdsub['price'] += $serpay;
    	}
    	
    	/**
    	 * 加入最新动态
    	 */
    	$getcredits = $gdsub['price']*$credits_set['money_to_credit'];
		$actmsg = "<span class=\"cred\">$mall_user</span> 提交了价值 <span class=\"cred\">$gdsub[price]</span> 元的订单,可获得 <span class=\"cred\">$getcredits</span> 积分!"; 
    	addaction($mall_uid,$actmsg);
    	
        $db->query("INSERT INTO `{$tablepre}orders` (`adid`,`payway` , `sendway` , `gettime`  , `uid` , `remark` , `dgmoney` , `yfmoney` , `yhmoney` )
VALUES ('$member[adid]','$member[payway]', '$member[sendway]', '$timestamp', '$mall_uid', '$remark', '$gdsub[price]', '$freight', '0.00');");
        $oid = $db->insert_id();
        foreach ($cartgoods as $gd) {
			$db->query("INSERT INTO {$tablepre}ordersgoods (`oid`,`gid`,`buynum`,`buytype`,`param`,`param1`,`price`) VALUES ('$oid','$gd[gid]','$gd[gnum]','$gd[t]','$gd[u]','$gd[u1]','$gd[zprice]')");
        }
        /**
	  	 * 检测使用余额和优惠码支付
	  	 */
	  	 $complete = false;
		 $usecodepay = isset($usecodepay)?intval($usecodepay):0;
		 $useleftpay = isset($useleftpay)?intval($useleftpay):0;
	  	 if($usecodepay && preg_match("/^[A-Z]{2}[\d]{6}$/",$codevar)){
			$codeworth = $db->result_first("SELECT p.xprice FROM {$tablepre}codes c,{$tablepre}promcode p WHERE p.promid=c.promid AND c.code='$codevar' AND c.stats='0' AND p.stats='1'");
			$complete = payaction($oid,$timestamp.'_'.$oid,0,0,'code',$codevar);
	  	 }
   		 if($useleftpay){
	  	 	$complete = payaction($oid,$timestamp.'_'.$oid,0,$leftmoney,'leftpay','余额支付');
	  	 } 
        
        dsetcookie('cart');
        $orderbh = $order_start_number + $oid;
        $totalpay = number_format($gdsub['price']+$freight, 2,'.','');
        if(!$complete){
        	$codepay = $db->result_first("SELECT sum(worth) FROM {$tablepre}codes WHERE oid='$oid'");
			$cashpay = $db->result_first("SELECT sum(paymoney) FROM {$tablepre}payments WHERE oid='$oid' AND del='0'");
        	$needpay = number_format($totalpay-($codepay+$cashpay),2,'.','');
        }
    } else {
        showmessage('undefined_action');
    }


} else {	
	$hg_total = str_replace(',','',$hg_total);
	$query = $db->query("SELECT * FROM {$tablepre}packages WHERE $hg_total>=subprice AND display='2' AND stats='1' ORDER BY subprice DESC LIMIT 0,1");
	$packids = '0';
	$botprice = 0;
	$packs = array();
	if($arr = $db->fetch_array($query)){
		$botprice = $arr['subprice'];
		$packids .= ','.$arr['packid'];
		$packs[] = $arr;
		if($arr = $db->fetch_array($db->query("SELECT * FROM {$tablepre}packages WHERE $hg_total<subprice AND display='2' AND stats='1' ORDER BY subprice ASC LIMIT 0,1"))){
			$packids .= ','.$arr['packid'];
			$packs[] = $arr;	
		}
		
	}else{
		$query = $db->query("SELECT * FROM {$tablepre}packages WHERE display='2' AND stats='1' ORDER BY subprice ASC LIMIT 0,2");
		while($arr = $db->fetch_array($query)){
			$packids .= ','.$arr['packid'];
			$packs[] = $arr;
		}
	}

	$cusnum = count($packs);
	$query = $db->query("SELECT pg.packid,g.eprice,g.gid,g.subject,g.photos,pg.newprice FROM {$tablepre}goods g,{$tablepre}packagegoods pg WHERE g.gid=pg.gid AND pg.packid IN ($packids)");
	$cusbuyarr = array();
	while($arr = $db->fetch_array($query)){
		$arr['photos'] = unserialize($arr['photos']);
		$arr['photo'] = $arr['photos'][0];
		$cusbuyarr[$arr['packid']][] = $arr;
	}
	
    $attr = getattrs($gdsub['gids']);
    if ($mall_uid) {
        $query = $db->query("SELECT f.*,g.subject,g.eprice,g.discount FROM {$tablepre}favorites f,{$tablepre}goods g WHERE f.uid='$mall_uid' AND f.gid=g.gid ORDER BY f.fid DESC LIMIT 0,20");
        $favorites = array();
        while ($arr = $db->fetch_array($query)) {
            $arr['bh'] = $goods_start_number + $arr['gid'];
            $arr['zprice'] = number_format($arr['eprice'] * ($arr['discount'] / 10), 2,'.','');
            $favorites[] = $arr;
        }
    }
}

include (template('cart'));

?>