/** * @param $province string 省份的id * @param $weight int 货物的重量 * @param $goodsSum float 商品总价格 * @return array() * @brief 配送方式计算管理模块 */ public static function getDelivery($province, $weight = 0, $goodsSum = 0) { $data = array(); //获得配送方式表的对象 $delivery = new IModel('delivery'); //获取配送方式列表 $where = 'is_delete = 0 and status = 1'; $list = $delivery->query($where, '*', 'sort', 'asc'); //循环各个配送方式 foreach ($list as $value) { //设置首重和次重 self::$firstWeight = $value['first_weight']; self::$secondWeight = $value['second_weight']; $data[$value['id']]['id'] = $value['id']; $data[$value['id']]['name'] = $value['name']; $data[$value['id']]['type'] = $value['type']; $data[$value['id']]['description'] = $value['description']; $data[$value['id']]['if_delivery'] = '0'; //当配送方式是统一配置的时候,不进行区分地区价格 if ($value['price_type'] == 0) { $data[$value['id']]['price'] = self::getFeeByWeight($weight, $value['first_price'], $value['second_price']); } else { $matchKey = ''; $flag = false; //每项都是以';'隔开的省份ID $area_groupid = unserialize($value['area_groupid']); foreach ($area_groupid as $key => $result) { //匹配到了特殊的省份运费价格 if (strpos($result, ';' . $province . ';') !== false) { $matchKey = $key; $flag = true; break; } } //匹配到了特殊的省份运费价格 if ($flag) { //获取当前省份特殊的运费价格 $firstprice = unserialize($value['firstprice']); $secondprice = unserialize($value['secondprice']); $data[$value['id']]['price'] = self::getFeeByWeight($weight, $firstprice[$matchKey], $secondprice[$matchKey]); } else { //判断是否设置默认费用了 if ($value['open_default'] == 1) { $data[$value['id']]['price'] = self::getFeeByWeight($weight, $value['first_price'], $value['second_price']); } else { $data[$value['id']]['price'] = '0'; $data[$value['id']]['if_delivery'] = '1'; } } } //计算保价 if ($value['is_save_price'] == 1) { $tempProtectPrice = $goodsSum * ($value['save_rate'] * 0.01); $data[$value['id']]['protect_price'] = $tempProtectPrice <= $value['low_price'] ? $value['low_price'] : $tempProtectPrice; } else { $data[$value['id']]['protect_price'] = 0; } } return $data; }
/** * @brief 配送方式计算管理模块 * @param $province int 省份的ID * @param $delivery_id int 配送方式ID * @param $goods_id array 商品ID * @param $product_id array 货品ID * @param $num array 商品数量 * @return array( * if_delivery => 0:支持配送;1:不支持配送; * price => 运费; * protect_price => 保价; * seller_id => array(price => 运费,protect_price => 保价,org_price => 原始运费) * ) */ public static function getDelivery($province, $delivery_id, $goods_id, $product_id = 0, $num = 1) { //获取默认的配送方式信息 $delivery = new IModel('delivery'); $deliveryDefaultRow = $delivery->getObj('is_delete = 0 and status = 1 and id = ' . $delivery_id); if (!$deliveryDefaultRow) { return "配送方式不存在"; } //最终返回结果 $result = array('name' => $deliveryDefaultRow['name'], 'description' => $deliveryDefaultRow['description'], 'if_delivery' => 0, 'org_price' => 0, 'price' => 0, 'protect_price' => 0); //读取全部商品,array('goodsSum' => 商品总价,'weight' => 商品总重量) $sellerGoods = array(); $goods_id = is_array($goods_id) ? $goods_id : array($goods_id); $product_id = is_array($product_id) ? $product_id : array($product_id); $num = is_array($num) ? $num : array($num); $goodsArray = array(); $productArray = array(); foreach ($goods_id as $key => $gid) { $pid = $product_id[$key]; $gnum = $num[$key]; if ($pid > 0) { $productArray[$pid] = $gnum; $goodsRow = Api::run("getProductInfo", array('#id#', $pid)); if (!$goodsRow) { throw new IException("计算商品运费货品ID【" . $pid . "】信息不存在"); } } else { $goodsArray[$gid] = $gnum; $goodsRow = Api::run("getGoodsInfo", array('#id#', $gid)); if (!$goodsRow) { throw new IException("计算商品运费商品ID【" . $gid . "】信息不存在"); } } if (!isset($sellerGoods[$goodsRow['seller_id']])) { $sellerGoods[$goodsRow['seller_id']] = array('goodsSum' => 0, 'weight' => 0); } $sellerGoods[$goodsRow['seller_id']]['weight'] += $goodsRow['weight'] * $gnum; $sellerGoods[$goodsRow['seller_id']]['goodsSum'] += $goodsRow['sell_price'] * $gnum; } //获取促销规则是否免运费 $countSumObj = new CountSum(self::$user_id); $cartObj = new Cart(); $countSumResult = $countSumObj->goodsCount($cartObj->cartFormat(array("goods" => $goodsArray, "product" => $productArray))); //根据商家不同计算运费 $deliveryExtendDB = new IModel('delivery_extend'); foreach ($sellerGoods as $seller_id => $data) { $weight = $data['weight']; //计算运费 $goodsSum = $data['goodsSum']; //计算保价 //使用商家配置的物流运费 $deliverySellerRow = $deliveryExtendDB->getObj('delivery_id = ' . $delivery_id . ' and seller_id = ' . $seller_id); $deliveryRow = $deliverySellerRow ? $deliverySellerRow : $deliveryDefaultRow; //设置首重和次重 self::$firstWeight = $deliveryRow['first_weight']; self::$secondWeight = $deliveryRow['second_weight']; $deliveryRow['if_delivery'] = '0'; //当配送方式是统一配置的时候,不进行区分地区价格 if ($deliveryRow['price_type'] == 0) { $deliveryRow['price'] = self::getFeeByWeight($weight, $deliveryRow['first_price'], $deliveryRow['second_price']); } else { $matchKey = ''; $flag = false; //每项都是以';'隔开的省份ID $area_groupid = unserialize($deliveryRow['area_groupid']); foreach ($area_groupid as $key => $item) { //匹配到了特殊的省份运费价格 if (strpos($item, ';' . $province . ';') !== false) { $matchKey = $key; $flag = true; break; } } //匹配到了特殊的省份运费价格 if ($flag) { //获取当前省份特殊的运费价格 $firstprice = unserialize($deliveryRow['firstprice']); $secondprice = unserialize($deliveryRow['secondprice']); $deliveryRow['price'] = self::getFeeByWeight($weight, $firstprice[$matchKey], $secondprice[$matchKey]); } else { //判断是否设置默认费用了 if ($deliveryRow['open_default'] == 1) { $deliveryRow['price'] = self::getFeeByWeight($weight, $deliveryRow['first_price'], $deliveryRow['second_price']); } else { $deliveryRow['price'] = '0'; $deliveryRow['if_delivery'] = '1'; } } } $deliveryRow['org_price'] = $deliveryRow['price']; //促销规则满足免运费 if (isset($countSumResult['freeFreight']) && in_array($seller_id, $countSumResult['freeFreight'])) { $deliveryRow['price'] = 0; } //计算保价 if ($deliveryRow['is_save_price'] == 1) { $tempProtectPrice = $goodsSum * ($deliveryRow['save_rate'] * 0.01); $deliveryRow['protect_price'] = $tempProtectPrice <= $deliveryRow['low_price'] ? $deliveryRow['low_price'] : $tempProtectPrice; } else { $deliveryRow['protect_price'] = 0; } //无法送达 if ($deliveryRow['if_delivery'] == 1) { return $deliveryRow; } //更新最终数据 $result['org_price'] += $deliveryRow['org_price']; $result['price'] += $deliveryRow['price']; $result['protect_price'] += $deliveryRow['protect_price']; } return $result; }