<?php

/**

*  Created by PhpStorm.

*  User: Administrator

*  Date: 14-4-13

*  Time: 下午2:41

*/

/*

*  购物流程页面:

*  商城的核心部分*/

define('ACC',true);

require('./include/init.php');

//获取地址栏上的动作,判断用户的动作

if(!isset($_GET['act'])){

$act = 'buy';

}else{

$act = $_GET['act'];

}

//获取购物车实例

$cart = CartTool::getCart();

$goods = new GoodsModel();

//

if($act == 'buy'){

$goods_id = isset($_GET['goods_id'])?$_GET['goods_id']+0:0;

$num = isset($_GET['num'])?$_GET['num']+0:1;

if($goods_id ){// 把商品放入购物车

$g = $goods->find($goods_id);

if(!empty($g)){//判断有此商品

/*判断此商品是否已被删除,已在回收站,

是否已经下架*/

if($g['is_delete'] ==1 ||  $g['is_on_sale'] == 0){

$msg = '此商品已被删除';

include(ROOT.'view/front/msg.html');

exit;

}

/*库存是否足够*/

/*先把商品加入到购物车*/

$cart->addItem($goods_id,$g['goods_name'],$g['shop_price'],$num);

/*判断*/

$items = $cart->getAll();

if(empty($items)){ //如果购物车为空,返回首页

header('location:index.php');

exit;

}

if($items[$goods_id]['num']  >$g['goods_number']){

/*库存不足,把刚才加入购物车的商品撤回*/

$cart->decNum($goods_id,$num);

$msg = '此商品库存不足';

include(ROOT.'view/front/msg.html');

exit;

}

}

$items = $goods->getCartGoods($items);

$total = $cart->getPrice();

$market_total = 0.0;

foreach($items as $v){

$market_total +=  $v['market_price']*$v['num'];

}

$discount = $market_total - $total;

$rate =round(($discount/$market_total)*100,2);

include(ROOT.'view/front/jiesuan.html');

}

}else if($act == 'clear'){

$cart->clear();

$msg = '购物车已清空';

include(ROOT.'view/front/msg.html');

exit;

}