| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- namespace addons\ValetOrder\common\logic\order\marketing;
- use common\enums\ShippingTypeEnum;
- use common\models\goods\FreeShippingRules;
- use common\models\goods\FreightRules;
- use common\models\user\UserAddress;
- /**
- * 运费减免计算
- * @Author bing
- * @DateTime 2021-09-27 19:07:32
- * @copyright: Copyright (c) 2020 广东七件事集团
- */
- class FreeShippingMarketing extends BaseMarketing{
- public function execute( $form){}
- /**
- * 计算运费
- * @Author: lun
- * @DateTime: 2021/10/6 0006 10:37
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $item
- * @param $form
- * @param $extra
- * @return mixed
- */
- public static function compute($item, &$form, &$extra)
- {
- $address = [
- 'province_id' => $form['province_id'],
- 'city_id' => $form['city_id'],
- 'district_id' => $form['district_id'],
- 'province' => $form['province'],
- 'city' => $form['city'],
- 'district' => $form['district'],
- 'town' => $form['town'] ?? '',
- 'town_id' => $form['town_id'] ?? 0,
- 'community' => $form['community'] ?? '',
- 'community_id' => $form['community_id'] ?? 0,
- 'detail' => $form['address'],
- 'name' => $form['name'],
- 'mobile' => $form['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'] ?? 0;
- $item['mch_order_info']['community'] = $address['community_id'] ?? 0;
- $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;// 是否免邮
- // 获取包邮地区
- $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['money'] <= $item['mch_order_info']['goods_price'] && self::isInArea($address, $fee['district_ids'])) {
- $is_free = true;
- continue;
- }
- }
- // 不免邮处理
- if ($is_free == false) {
- foreach ($item['goods'] as $good_id => $good) {
- $good_num = 0;// 第N个商品
- if ($good['num'] > 1) {
- $good_num+= $good['num'];
- } else {
- $good_num++;
- }
- // 物流方式
- if (!in_array(ShippingTypeEnum::LOGISTICS, explode(',', $form['sku'][$good_id]['goods']['freight_type']))) continue;
- // 计算满件包邮
- if ($form['sku'][$good_id]['goods']['free_shipping_num'] > 0 && $good['num'] >= $form['sku'][$good_id]['goods']['free_shipping_num']) continue;
- // 计算满元包邮
- if ($form['sku'][$good_id]['goods']['free_shipping_money'] > 0 && $good['goods_price'] >= $form['sku'][$good_id]['goods']['free_shipping_money']) continue;
- // 运费规则
- if ($form['sku'][$good_id]['goods']['freight_rules_type'] == 1) {
- $thisRules = $freightRules[$form['sku'][$good_id]['goods']['freight_id']];// 根据商品id获取对应的规则
- if(!empty($thisRules['rule'])){
- foreach ($thisRules['rule'] as $rule) {
- // 获取该模板的所有地区id
- $areaArr = array_column($rule['list'], 'id');
- // 判断收货地址是否在模板中
- if (self::isInArea($address, $areaArr)) {
- if ($thisRules['type'] == 1) {// 按重计费
- $new_num_weight = $form['sku'][$good_id]['weight'] * $good['num'];
- } else {// 按件计费
- $new_num_weight = $good['num'];
- }
- // 计算[首件/首重]收费
- if ($new_num_weight >= $rule['first']) {
- $group_order_fee += $rule['firstPrice'];
- $shipping_fee += $rule['firstPrice'];
- }
- if ($thisRules['type'] == 1) {// 计算续重收费
- $surplus_weight = $new_num_weight - $rule['first'];// 剩余重量 = 总重量 - 首重重量
- if ($surplus_weight > 0) {
- // 续重运费 = (剩余重量 / 续件重量)[向上取整] * 续重费用 [向下取整]
- if(isset($rule['second'])&&$rule['second']>0) {
- $fee = floor(ceil($surplus_weight / $rule['second']) * $rule['secondPrice']);
- $shipping_fee += $fee;
- $group_order_fee += $fee;
- }
- }
- } else {// 计算续件收费
- $surplus_num = $good_num - $rule['first'];// 减扣首件数量
- if($rule['second']>0){
- if ($surplus_num > 0 && $rule['second']>0 && $surplus_num % $rule['second'] == 0) {
- // 续费 * (减扣首件数量 / 续件数量)[向下取整]
- if(isset($rule['second'])&&$rule['second']>0){
- $fee = $rule['secondPrice'] * floor($surplus_num / $rule['second']);
- $shipping_fee += $fee;
- $group_order_fee += $fee;
- }
- }
- }
- }
- continue;// 已计算好运费则跳出循环
- }
- }
- }
- } else {
- // 自定义运费
- if ($form['sku'][$good_id]['goods']['shipping_fee'] >= 0) {
- $shipping_fee+= $form['sku'][$good_id]['goods']['shipping_fee'];
- $group_order_fee+= $form['sku'][$good_id]['goods']['shipping_fee'];
- }
- }
- }
- // 单个商户订单运费计算
- $item['mch_order_info']['shipping_type'] = $form['data']['shipping_type'] ?? ShippingTypeEnum::LOGISTICS;
- $item['mch_order_info']['shipping_money'] = $group_order_fee;
- }
- // 运费赋值
- $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' => $prev_shipping_fee + $shipping_fee];
- return $item;
- }
- /**
- * 判断收货地址是否在地区中
- * @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;
- }
- }
|