data['shipping_type']) && $form->data['shipping_type']==ShippingTypeEnum::PICKUP){ return $item; } // 获取用户收货地址信息 if (empty($extra['address'])) { $address = UserAddress::getOne([['id' => $form['address_id']], ['user_id' => $form->process_extra_data['valet_order_origin_user_id'] ?? $form['user_id']]], true); $extra['address'] = $address; } else { $address = $extra['address']; } // 收货地址 if(empty($address)){ $address['province_id'] = $address['city_id'] = $address['district_id'] = $address['town_id'] = $address['community_id'] = 0; $address['province'] = $address['city'] = $address['district'] = $address['town'] = $address['community'] = $address['detail'] = $address['name'] = $address['mobile'] =''; } // 收货地址 $item['mch_order_info']['province'] = $address['province_id']; $item['mch_order_info']['city'] = $address['city_id']; $item['mch_order_info']['area'] = $address['district_id']; $item['mch_order_info']['town'] = $address['town_id']; $item['mch_order_info']['community'] = $address['community_id']; $item['mch_order_info']['region_name'] = $address['province'] .' '. $address['city'] .' '. $address['district'].' '.$address['town'].' '.$address['community']; $item['mch_order_info']['address'] = $address['detail']; $item['mch_order_info']['receiver_name'] = $address['name']; $item['mch_order_info']['mobile'] = $address['mobile']; $shipping_fee = 0;// 订单总运费初始化 $group_order_fee = 0;// 单个商户订单运费 $is_free = false;// 是否免邮 $compute_shipping = [];// 运费计算,配合运费规则compute_type == 2使用 // 获取包邮地区 $feeShippingRules = FreeShippingRules::getFreeShippingRulesArea($item['mch_order_info']['mall_id'], $item['mch_order_info']['mch_id']); // 获取运费模板 $freightRules = FreightRules::getFreightRulesArea($item['mch_order_info']['mall_id'], $item['mch_order_info']['mch_id']); // 包邮计算 foreach ($feeShippingRules as $fee) { if ($fee['rule_type'] == 1) { // 如果包邮金额<=购买商品金额 || 包邮件数<=购买商品件数,并且地区符合收货地址则免邮 if (($fee['money'] <= $item['mch_order_info']['goods_price'] || $fee['num'] <= $order_goods_num) && self::isInArea($address, $fee['district_ids'])) { $is_free = true; continue; } } else { // 如果包邮金额<=购买商品金额 && 包邮件数<=购买商品件数,并且地区符合收货地址则免邮 if (($fee['money'] <= $item['mch_order_info']['goods_price'] && $fee['num'] <= $order_goods_num) && self::isInArea($address, $fee['district_ids'])) { $is_free = true; continue; } } } // 不免邮处理 if ($is_free == false) { list($shipping_fee, $group_order_fee) = self::getFreightRules($item, $form, $freightRules, $address, $compute_shipping); // 按同件商品运费计算 if ($compute_shipping) { $shop_fee_assignment = self::shopFeeAssignment($compute_shipping); $shipping_fee += $shop_fee_assignment['shipping_fee'] ?? 0; $group_order_fee += $shop_fee_assignment['group_order_fee'] ?? 0; } // 单个商户订单运费计算 $item['mch_order_info']['shipping_type'] = $form['data']['shipping_type'] ?? ShippingTypeEnum::LOGISTICS; $item['mch_order_info']['shipping_money'] = bcmul($group_order_fee, 1, 2); } // 运费赋值 $item['mch_order_info']['pay_money'] = $item['mch_order_info']['pay_money'] + $shipping_fee; $prev_shipping_fee = $form->marketing['deduct']['shipping_fee']['money'] ?? 0; $form->marketing['deduct']['shipping_fee'] = ['name' => '运费', 'money' => bcadd($prev_shipping_fee, $group_order_fee, 2)]; return $item; } private static function getFreightRules($item, $form, $freightRules, $address, &$compute_shipping) { // 阶梯价格情况下 存储哪个运费模板已经计算了价格(因为如果是开启了阶梯,并且是同一个模板来计算的话只拿一次价格) $groupOrderFee = 0; $shippingFee = 0; // 校验输入数据完整性 if (empty($item['goods']) || empty($form['sku'])) { return [$groupOrderFee, $shippingFee]; } // 获取所有商品ID $goodsIds = array_column($item['goods'], 'goods_id'); // 批量查询虚拟商品信息 $virtualGoodsIds = Goods::find() ->select('id') ->where([ 'status' => StatusEnum::ENABLED, 'goods_type' => GoodsTypeEnum::GoodsTypeVirtual, 'id' => $goodsIds ]) ->column(); $goodsAttr = GoodsAttr::find()->where(['goods_id' => $goodsIds, 'status' => StatusEnum::ENABLED])->indexBy('id')->asArray()->all(); $calculatedFreight = []; $freightValue = self::getFreightValue($item['goods'], $form, $freightRules, $goodsAttr); foreach ($item['goods'] as $good_id => $good) { // 跳过虚拟商品 if (in_array($good_id, $virtualGoodsIds)) { continue; } // 获取SKU属性 $formAttr = $form['sku'][$good_id]['goods']; if (empty($formAttr)) continue; foreach ($good['goods_details'] as $detail) { // 跳过非物流方式的商品 if (!in_array(ShippingTypeEnum::LOGISTICS, explode(',', $formAttr['freight_type']))) continue; // 免邮规则:数量达到免邮条件 if ($formAttr['free_shipping_num'] > 0 && $detail['num'] >= $formAttr['free_shipping_num']) continue; // 邮规则:金额达到免邮条件 if ($formAttr['free_shipping_money']> 0 && $detail['goods_price'] >= $formAttr['free_shipping_money']) continue; // 运费规则计算 if ($formAttr['freight_rules_type'] == 1) { // 运费模版 // 根据商品id获取对应的规则 $thisRules = $freightRules[$formAttr['freight_id']] ?? []; if (empty($thisRules)) { continue; // 规则无效时跳过 } $shippingList = self::shippingDateMinute($detail, $thisRules, $address, $goodsAttr, $formAttr, $good, $form, $freightValue, $calculatedFreight, $compute_shipping); if (!empty($shippingList)) { $groupOrderFee += $shippingList[0]; $shippingFee += $shippingList[1]; } } else { // 自定义运费 if (!empty($formAttr['shipping_fee'])) { $groupOrderFee += $formAttr['shipping_fee']; $shippingFee += $formAttr['shipping_fee']; } } } } return [$groupOrderFee, $shippingFee]; } /** * @param $detail * @param $thisRules * @param $address * @param $goodsAttr * @param $formAttr * @param $goods * @param $form * @param $freightValue * @param $calculatedFreight * @param $compute_shipping * @return array|int[] */ private static function shippingDateMinute($detail, $thisRules, $address, $goodsAttr, $formAttr, $goods, &$form, $freightValue, &$calculatedFreight, &$compute_shipping) { // 初始化运费变量 $groupOrderFee = 0; $shippingFee = 0; // 检查规则是否存在 if (empty($thisRules['rule']) || !is_array($thisRules['rule'])) { return [$shippingFee, $groupOrderFee]; } foreach ($thisRules['rule'] as $rule) { // 获取模版对应区域id $areArrIds = array_column($rule['list'], 'id'); // 判断当前收货地址是否在模版区域中 if (!self::isInArea($address, $areArrIds)) continue; // 判断是否开启阶梯 if (!empty($rule['is_enable_ladder_condition']) && !empty($rule['ladder_condition'])) { // 根据商品计算方式来决定使用哪个字段来确定唯一值 switch ($thisRules['compute_type']) { case 1: // 按不同商品计算 $calculatedFreightKey = $detail['goods_attr_id']; break; case 2: // 按同件商品计算 $calculatedFreightKey = $formAttr['freight_id']; break; } if (isset($calculatedFreightKey)) { if (!empty($calculatedFreight[$thisRules['compute_type']][$calculatedFreightKey])) { continue; } // 验证是否满足阶梯 $computeLadderCondition = self::computeLadderCondition( $freightValue, $detail, $formAttr, $thisRules['type'], $thisRules['compute_type'], $rule, $form ); if ($computeLadderCondition['is_success']) { $groupOrderFee += $computeLadderCondition['money']; $shippingFee += $computeLadderCondition['money']; $calculatedFreight[$thisRules['compute_type']][$calculatedFreightKey] = true; continue; } } } // 判断是否设置首重 if (empty($rule['first'])) { return [0, 0]; } // 计算首重金额 if ($thisRules['compute_type'] == 1) { if (($thisRules['type'] == 1 && $goodsAttr[$detail['goods_attr_id']]['weight'] > 0) || ($thisRules['type'] == 2)) { $firstPrice = $rule['firstPrice'] ?? 0; $groupOrderFee += $firstPrice; // 订单运费 $shippingFee += $firstPrice; // 首重运费 } } // 判断是否设置续重 if (empty($rule['second']) && $thisRules['compute_type'] != 2) continue; if ($thisRules['type'] == 1) { // 计重量 // 获取当前商品重量 $newNumWeight = $detail['num'] * $goodsAttr[$detail['goods_attr_id']]['weight']; } else { // 计件 $newNumWeight = $detail['num']; } if ($thisRules['compute_type'] == 2) { self::getShopFee($thisRules['id'], $thisRules['type'], $rule, $newNumWeight, $compute_shipping); continue; } $newRenewed = $newNumWeight - $rule['first']; $secondPrice = $rule['secondPrice'] ?? 0; if ($newRenewed <= 0 || !is_numeric($secondPrice)) continue; $renewedFee = (double)bcmul(ceil($newRenewed / $rule['second']), $secondPrice, 2); $groupOrderFee += $renewedFee; $shippingFee += $renewedFee; } return [$shippingFee, $groupOrderFee]; } /** * 判断收货地址是否在地区中 * @Author: lun * @DateTime: 2021/9/30 0030 14:45 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $address * @param $areaArr * @return bool */ private static function isInArea($address, $areaArr) { $return = false; if(in_array($address['province_id'], $areaArr) || in_array($address['city_id'], $areaArr) || in_array($address['district_id'], $areaArr)) $return = true; return $return; } /** * 运费模板为:按同件商品计算 * 目的:不同商品使用不同的模板情况下的混合计算,主要算出:首件/重 + 续件/重 运费费用 * @Author: lun * @DateTime: 2023/8/12 0012 17:11 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $rule_id * @param $rule * @param $goods_weight_or_num * @param array $data */ private static function getShopFee($rule_id, $type, $rule, $goods_weight_or_num, &$data = []) { $data[$rule_id]['total_weight_or_num'] = empty($data[$rule_id]['total_weight_or_num']) ? $goods_weight_or_num : $data[$rule_id]['total_weight_or_num'] + $goods_weight_or_num; if (empty($data[$rule_id]['first_fee'])) $data[$rule_id]['first_fee'] = $rule['firstPrice'];// 首重/件 费用 if (empty($data[$rule_id]['group_first_fee'])) $data[$rule_id]['group_first_fee'] = $rule['firstPrice'];// 首重/件 费用 if (empty($data[$rule_id]['first_weight_or_num'])) $data[$rule_id]['first_weight_or_num'] = $rule['first'];// 同模板商品首重 if (empty($data[$rule_id]['renew_price'])) $data[$rule_id]['renew_price'] = 0;// 续重/件 费用 $surplus_weight_or_num = $data[$rule_id]['total_weight_or_num'] - $data[$rule_id]['first_weight_or_num'];// 总重量/总数量 - 首重/件 = 续重/件 // 续重/件运费 = (剩余重量 / 续件重量)[向上取整] * 续重费用 [向下取整] if($surplus_weight_or_num > 0 && isset($rule['second']) && $rule['second'] > 0) { if ($type == 2) {// 按件计费 $fee = $rule['secondPrice'] * floor($surplus_weight_or_num / $rule['second']); } else {// 按重计费 $fee = floor(ceil($surplus_weight_or_num / $rule['second']) * $rule['secondPrice']); } $data[$rule_id]['renew_price'] = $fee;// 每次都覆盖之前的值,为了重新算出最后的续重/件费用 } } /** * 将计算好的不同运费模板运费加起来 * @Author: lun * @DateTime: 2023/8/12 0012 17:13 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $shopFeeList * @return array */ private static function shopFeeAssignment($shopFeeList) { $shop_fee = 0; $group_order_fee = 0; foreach ($shopFeeList as $item) { $shop_fee += $item['first_fee'] + $item['renew_price']; $group_order_fee += $item['group_first_fee'] + $item['renew_price']; } return ['shipping_fee' => $shop_fee, 'group_order_fee' => $group_order_fee]; } /** * 计算阶梯价格 * * @param array $freightValue * @param array $good * @param int $type * @param int $computeType * @param array $rule * @param $form * * @return array */ private static function computeLadderCondition( array $freightValue, array $detail, array $formAttr, int $type, int $computeType, array $rule, $form ): array { $res = ['is_success' => false, 'money' => 0]; !isset($freightValue[$computeType]) && $freightValue[$computeType] = []; // 如果是按重,则需要转为kg if ($type == 1) { foreach ($freightValue[$computeType] as &$value) { $value = bcdiv($value, 1000, 2); } } switch ($computeType) { case 1: // 按不同商品计算 $goodsId = $detail['goods_attr_id']; $resValue = $freightValue[$computeType][$goodsId] ?? 0; break; case 2: // 按同件商品计算 $freightId = $formAttr['freight_id']; $resValue = $freightValue[$computeType][$freightId] ?? 0; break; default: return $res; } $checkLadderConditionRes = self::checkLadderCondition($rule['ladder_condition'], $resValue); if ($checkLadderConditionRes['res']) { $res['is_success'] = true; $res['money'] = $checkLadderConditionRes['money']; } return $res; } /** * @param array $ladderCondition * @param $value * * @return array */ protected static function checkLadderCondition(array $ladderCondition, $value): array { // 根据$ladderCondition的condition_value字段按从大到小排序 usort($ladderCondition, function ($a, $b) { return $b['condition_value'] <=> $a['condition_value']; }); foreach ($ladderCondition as $condition) { if ($value >= $condition['condition_value']) { return ['res' => true, 'money' => $condition['money']]; } } return ['res' => false]; } /** * 按计费方式和计算方式将值归类 * * @param $goods * @param $form * @param $freightRules * * @return array */ private static function getFreightValue($goods, $form, $freightRules, $goodsAttr): array { $freightValue = []; // 把相同运费模板的商品合并,根据选择的计费方式 // 如果是按相同商品计算时,就需要保存选择了当前模板的商品 foreach ($goods as $good_id => $good) { $freightId = $form['sku'][$good_id]['goods']['freight_id']; // 获取SKU属性 $formAttr = $form['sku'][$good_id]['goods']; if (empty($formAttr)) continue; // 过滤掉没有自定义运费模版商品 if ($formAttr['freight_rules_type'] != 1) continue; // 根据商品id获取对应的规则 $thisRules = $freightRules[$freightId] ?? []; if (empty($thisRules)) { continue; } // 因为同一个商品不同规格是按照不同商品计算 foreach ($good['goods_details'] as $detail) { if ($thisRules['type'] == 1) { // 按重计费 $new_num_weight = $goodsAttr[$detail['goods_attr_id']]['weight'] * $detail['num']; } else { // 按件计费 $new_num_weight = $detail['num']; } if (empty($new_num_weight)) continue; switch ($thisRules['compute_type']) { case 1: // 按照不同商品计算(取商品规格计算) !isset($freightValue[$thisRules['compute_type']][$detail['goods_attr_id']]) && $freightValue[$thisRules['compute_type']][$detail['goods_attr_id']] = 0; $freightValue[$thisRules['compute_type']][$detail['goods_attr_id']] += $new_num_weight; break; case 2: // 按同件商品计算 (取商品模版) !isset($freightValue[$thisRules['compute_type']][$freightId]) && $freightValue[$thisRules['compute_type']][$freightId] = 0; $freightValue[$thisRules['compute_type']][$freightId] += $new_num_weight; break; } } } return $freightValue; } }