public function direct_count($id, $type, $buy_num = 1, $promo = '', $active_id = '') { /*开启促销活动*/ if ($promo && $active_id) { //开启促销活动 $activeObject = new Active($promo, $active_id, $this->user_id, $id, $type, $buy_num); $activeResult = $activeObject->checkValid(); if ($activeResult === true) { $typeRow = $activeObject->originalGoodsInfo; $disPrice = $activeObject->activePrice; //设置优惠价格,如果不存在则优惠价等于商品原价 $typeRow['reduce'] = $typeRow['sell_price'] - $disPrice; $typeRow['count'] = $buy_num; $typeRow['sum'] = $disPrice * $buy_num; //拼接返回数据 $result = array('final_sum' => $typeRow['sum'], 'promotion' => array(), 'proReduce' => 0, 'sum' => $typeRow['sell_price'] * $buy_num, 'goodsList' => array($typeRow), 'count' => $buy_num, 'reduce' => $typeRow['reduce'] * $buy_num, 'weight' => $typeRow['weight'] * $buy_num, 'point' => $typeRow['point'] * $buy_num, 'exp' => $typeRow['exp'] * $buy_num, 'freeFreight' => false); return $result; } else { //报错信息 return $activeResult; } } else { $buyInfo = array($type => array('id' => array($id), 'data' => array($id => array('count' => $buy_num)), 'count' => $buy_num)); return $this->goodsCount($buyInfo); } }
/** * @brief 计算商品价格 * @param Array $buyInfo ,购物车格式 * @promo string 活动类型 团购,抢购 * @active_id int 活动ID * @return array or bool */ public function goodsCount($buyInfo, $promo = '', $active_id = '') { $this->sum = 0; //原始总额(优惠前) $this->final_sum = 0; //应付总额(优惠后) $this->weight = 0; //总重量 $this->reduce = 0; //减少总额 $this->count = 0; //总数量 $this->promotion = array(); //促销活动规则文本 $this->proReduce = 0; //促销活动规则优惠额 $this->point = 0; //增加积分 $this->exp = 0; //增加经验 $this->freeFreight = array(); //商家免运费 $this->tax = 0; //商品税金 $this->seller = array(); //商家商品总额统计, 商家ID => 商品金额 $user_id = $this->user_id; $group_id = $this->group_id; $goodsList = array(); $productList = array(); //活动购买情况 if ($promo && $active_id) { $ac_type = isset($buyInfo['goods']) ? "goods" : "product"; $ac_id = current($buyInfo[$ac_type]['id']); $ac_buy_num = $buyInfo[$ac_type]['data'][$ac_id]['count']; //开启促销活动 $activeObject = new Active($promo, $active_id, $user_id, $ac_id, $ac_type, $ac_buy_num); $activeResult = $activeObject->checkValid(); if ($activeResult === true) { $typeRow = $activeObject->originalGoodsInfo; $disPrice = $activeObject->activePrice; //设置优惠价格,如果不存在则优惠价等于商品原价 $typeRow['reduce'] = $typeRow['sell_price'] - $disPrice; $typeRow['count'] = $ac_buy_num; $current_sum_all = $typeRow['sell_price'] * $ac_buy_num; $current_reduce_all = $typeRow['reduce'] * $ac_buy_num; $typeRow['sum'] = $current_sum_all - $current_reduce_all; if (!isset($this->seller[$typeRow['seller_id']])) { $this->seller[$typeRow['seller_id']] = 0; } $this->seller[$typeRow['seller_id']] += $typeRow['sum']; //全局统计 $this->weight += $typeRow['weight'] * $ac_buy_num; $this->point += $typeRow['point'] * $ac_buy_num; $this->exp += $typeRow['exp'] * $ac_buy_num; $this->sum += $current_sum_all; $this->reduce += $current_reduce_all; $this->count += $ac_buy_num; $this->tax += self::getGoodsTax($typeRow['sum'], $typeRow['seller_id']); $typeRow == "goods" ? $goodsList[] = $typeRow : ($productList[] = $typeRow); } else { //报错信息 return $activeResult; } } else { /*开始计算goods和product的优惠信息 , 会根据条件分析出执行以下哪一种情况: *(1)查看此商品(货品)是否已经根据不同会员组设定了优惠价格; *(2)当前用户是否属于某个用户组中的成员,并且此用户组享受折扣率; *(3)优惠价等于商品(货品)原价; */ //获取商品或货品数据 /*Goods 拼装商品优惠价的数据*/ if (isset($buyInfo['goods']['id']) && $buyInfo['goods']['id']) { //购物车中的商品数据 $goodsIdStr = join(',', $buyInfo['goods']['id']); $goodsObj = new IModel('goods as go'); $goodsList = $goodsObj->query('go.id in (' . $goodsIdStr . ')', 'go.name,go.id as goods_id,go.img,go.sell_price,go.point,go.weight,go.store_nums,go.exp,go.goods_no,0 as product_id,go.seller_id'); //开始优惠情况判断 foreach ($goodsList as $key => $val) { //检查库存 if ($buyInfo['goods']['data'][$val['goods_id']]['count'] <= 0 || $buyInfo['goods']['data'][$val['goods_id']]['count'] > $val['store_nums']) { return "商品:" . $val['name'] . "购买数量超出库存,请重新调整购买数量"; } $groupPrice = $this->getGroupPrice($val['goods_id'], 'goods'); $goodsList[$key]['reduce'] = $groupPrice === null ? 0 : $val['sell_price'] - $groupPrice; $goodsList[$key]['count'] = $buyInfo['goods']['data'][$val['goods_id']]['count']; $current_sum_all = $goodsList[$key]['sell_price'] * $goodsList[$key]['count']; $current_reduce_all = $goodsList[$key]['reduce'] * $goodsList[$key]['count']; $goodsList[$key]['sum'] = $current_sum_all - $current_reduce_all; if (!isset($this->seller[$val['seller_id']])) { $this->seller[$val['seller_id']] = 0; } $this->seller[$val['seller_id']] += $goodsList[$key]['sum']; //全局统计 $this->weight += $val['weight'] * $goodsList[$key]['count']; $this->point += $val['point'] * $goodsList[$key]['count']; $this->exp += $val['exp'] * $goodsList[$key]['count']; $this->sum += $current_sum_all; $this->reduce += $current_reduce_all; $this->count += $goodsList[$key]['count']; $this->tax += self::getGoodsTax($goodsList[$key]['sum'], $val['seller_id']); } } /*Product 拼装商品优惠价的数据*/ if (isset($buyInfo['product']['id']) && $buyInfo['product']['id']) { //购物车中的货品数据 $productIdStr = join(',', $buyInfo['product']['id']); $productObj = new IQuery('products as pro,goods as go'); $productObj->where = 'pro.id in (' . $productIdStr . ') and go.id = pro.goods_id'; $productObj->fields = 'pro.sell_price,pro.weight,pro.id as product_id,pro.spec_array,pro.goods_id,pro.store_nums,pro.products_no as goods_no,go.name,go.point,go.exp,go.img,go.seller_id'; $productList = $productObj->find(); //开始优惠情况判断 foreach ($productList as $key => $val) { //检查库存 if ($buyInfo['product']['data'][$val['product_id']]['count'] <= 0 || $buyInfo['product']['data'][$val['product_id']]['count'] > $val['store_nums']) { return "货品:" . $val['name'] . "购买数量超出库存,请重新调整购买数量"; } $groupPrice = $this->getGroupPrice($val['product_id'], 'product'); $productList[$key]['reduce'] = $groupPrice === null ? 0 : $val['sell_price'] - $groupPrice; $productList[$key]['count'] = $buyInfo['product']['data'][$val['product_id']]['count']; $current_sum_all = $productList[$key]['sell_price'] * $productList[$key]['count']; $current_reduce_all = $productList[$key]['reduce'] * $productList[$key]['count']; $productList[$key]['sum'] = $current_sum_all - $current_reduce_all; if (!isset($this->seller[$val['seller_id']])) { $this->seller[$val['seller_id']] = 0; } $this->seller[$val['seller_id']] += $productList[$key]['sum']; //全局统计 $this->weight += $val['weight'] * $productList[$key]['count']; $this->point += $val['point'] * $productList[$key]['count']; $this->exp += $val['exp'] * $productList[$key]['count']; $this->sum += $current_sum_all; $this->reduce += $current_reduce_all; $this->count += $productList[$key]['count']; $this->tax += self::getGoodsTax($productList[$key]['sum'], $val['seller_id']); } } //总金额满足的促销规则 if ($user_id) { //计算每个商家促销规则 foreach ($this->seller as $seller_id => $sum) { $proObj = new ProRule($sum, $seller_id); $proObj->setUserGroup($group_id); if ($proObj->isFreeFreight() == true) { $this->freeFreight[] = $seller_id; } $this->promotion = array_merge($proObj->getInfo(), $this->promotion); $this->proReduce += $sum - $proObj->getSum(); } } } $this->final_sum = $this->sum - $this->reduce - $this->proReduce; return array('final_sum' => $this->final_sum, 'promotion' => $this->promotion, 'proReduce' => $this->proReduce, 'sum' => $this->sum, 'goodsList' => array_merge($goodsList, $productList), 'count' => $this->count, 'reduce' => $this->reduce, 'weight' => $this->weight, 'point' => $this->point, 'exp' => $this->exp, 'tax' => $this->tax, 'freeFreight' => $this->freeFreight, 'seller' => $this->seller); }