Ver código fonte

运费计算

shish 11 meses atrás
pai
commit
4200ebc650
1 arquivos alterados com 96 adições e 46 exclusões
  1. 96 46
      app-mall/controllers/OrderController.php

+ 96 - 46
app-mall/controllers/OrderController.php

@@ -298,7 +298,7 @@ class OrderController extends BaseController
             if (isset($post['sendType']) && $post['sendType'] == 2) {
                 $hasMap = dict::getDict('hasMap');
                 if ($hasMap == 0) {
-                    util::fail('不能使用跑腿');
+                    util::fail('不能使用跑腿,请选其它方式');
                 }
                 $cargoName = '花材'; // 商品名称
                 $cargo = [
@@ -321,6 +321,13 @@ class OrderController extends BaseController
                 $showAddress = $post['showAddress'] ?? '';
                 $userAddress = $city . $dist . $address . "({$showAddress})";
                 $main = $this->main;
+
+                $shopLat = $shop->lat ?? '';
+                $shopLong = $shop->long ?? '';
+                if (empty($shopLat) || empty($shopLong)) {
+                    util::fail('商家门店地址定位未设置');
+                }
+
                 $wxStoreId = $main->wxStoreId ?? 0;
                 if (empty($wxStoreId)) {
                     util::fail('商家还没有开通跑腿功能');
@@ -341,7 +348,6 @@ class OrderController extends BaseController
                     util::fail('获取运费错误:' . $errMsg);
                 }
                 $fee = $result['est_fee'] ?? 0;
-                //距离,好像是米
                 $distance = $result['distance'] ?? 0;
                 $sendCost = bcdiv($fee, 100, 2);
                 $post['sendCost'] = $sendCost;
@@ -390,10 +396,14 @@ class OrderController extends BaseController
         if (empty($goodsId)) {
             util::fail('请选择商品');
         }
+        $mainId = $this->mainId;
         $goodsInfo = GoodsClass::getById($goodsId, true);
         if (empty($goodsInfo)) {
             util::fail('没有找到商品');
         }
+        if ($goodsInfo->mainId != $mainId) {
+            util::fail('不是你的商品');
+        }
         $stock = $goodsInfo->stock ?? 0;
         if ($stock <= 0) {
             util::fail('库存不足');
@@ -422,15 +432,29 @@ class OrderController extends BaseController
             $post['bookMobile'] = $bookMobile;
             $post['payWay'] = 0;
 
-            //计算运费
-            //$sendDistance = 0;
-            //$sendCost = 0;
             $shop = $this->shop;
+            $params = [];
+            $goodsData = $goodsInfo->attributes;
+            $ret = \bizHd\goods\classes\GoodsClass::getFinalPrice($goodsData, $shop, $custom, $params);
+            $unitPrice = $ret['price'] ?? 0;
+            if ($unitPrice <= 0) {
+                util::fail('没有价格,不能下单哈');
+            }
+            //计算总金额
+            $goodsPrice = $unitPrice * $goodsNum;
+
+            //计算运费
+            $sendDistance = 0;
+            $sendCost = 0;
+
             if ($sendType == dict::getDict('sendType', 'thirdSend')) {
+                if (dict::getDict('hasMap') == 0) {
+                    util::fail('不能使用跑腿,请选其它方式');
+                }
                 $lat = $post['lat'] ?? '';
                 $lng = $post['long'] ?? '';
-                if ((empty($lat) || empty($lng)) && dict::getDict('hasMap') == 1) { // 当 hasMap 为1时,经纬数据是必要的
-                    util::fail('缺少经纬数据');
+                if ((empty($lat) || empty($lng))) { // 当 hasMap 为1时,经纬数据是必要的
+                    util::fail('请填写正确地址');
                 }
                 $address = $post['address'] ?? '';
                 $floor = $post['floor'] ?? '';
@@ -440,53 +464,79 @@ class OrderController extends BaseController
                 $shopLat = $shop->lat ?? '';
                 $shopLong = $shop->long ?? '';
                 if (empty($shopLat) || empty($shopLong)) {
-                    //util::fail('请完成门店地址');
+                    util::fail('商家门店地址定位未设置');
+                }
+
+                $weight = $goodsInfo->weight ?? 0;
+                if ($weight <= 0) {
+                    util::fail('商品没有重量');
+                }
+                $totalWeight = bcmul($weight, $goodsNum, 2);
+                $cargoName = '鲜花'; // 商品名称
+                $cargo = [
+                    'cargo_name' => $cargoName, // 商品名称
+                    'cargo_weight' => intval($totalWeight * 1000), // 单位:克 -- 把千克转换为克 ---- 重量(用户未知)-- 大概
+                    'cargo_price' => intval($goodsPrice * 100), // 单位:分 -- 把元转换为分
+                    'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
+                    'cargo_num' => $goodsNum
+                ];
+                $customName = $custom->name;
+                $customMobile = $custom->mobile;
+                if (empty($post['long']) || empty($post['lat'])) {
+                    util::fail('位置错误');
+                }
+                $originalLng = doubleval($post['long']);
+                $originalLat = doubleval($post['lat']);
+                $city = $post['city'] ?? '';
+                $dist = $post['dist'] ?? '';
+                $address = $post['address'] ?? '';
+                $showAddress = $post['showAddress'] ?? '';
+                $userAddress = $city . $dist . $address . "({$showAddress})";
+                $main = $this->main;
+
+                $shopLat = $shop->lat ?? '';
+                $shopLong = $shop->long ?? '';
+                if (empty($shopLat) || empty($shopLong)) {
+                    util::fail('商家门店地址定位未设置');
+                }
+
+                $wxStoreId = $main->wxStoreId ?? 0;
+                if (empty($wxStoreId)) {
+                    util::fail('商家还没有开通跑腿功能');
+                }
+                $orderData = [
+                    'wx_store_id' => $wxStoreId,
+                    'user_name' => $customName,
+                    'user_phone' => $customMobile,
+                    'user_lng' => $originalLng,
+                    'user_lat' => $originalLat,
+                    'user_address' => $userAddress,
+                    'cargo' => $cargo,
+                ];
+                $result = IntraCityExpress::preAddOrder($orderData);
+                $errCode = $result['errcode'] ?? 1;
+                if ($errCode != 0) {
+                    $errMsg = $result['errmsg'] ?? '';
+                    util::fail('获取运费错误:' . $errMsg);
                 }
-                //花束重量默认1.5
-                $weight = 1.5;
-                //$disRespond = OrderClass::getDistanceFee($lat, $lng, $shopLat, $shopLong, $weight);
-                //$sendCost = $disRespond['fee'] ?? 0;
-                //$sendDistance = $disRespond['distance'] ?? 0;
-            }
-            $post['sendDistance'] = isset($post['sendDistance']) ? $post['sendDistance'] : 0;
-            $sendCost = isset($post['sendCost']) ? floatval($post['sendCost']) : 0;
+                $fee = $result['est_fee'] ?? 0;
+                $sendDistance = $result['distance'] ?? 0;
+                $sendCost = bcdiv($fee, 100, 2);
+                noticeUtil::push("运费:{$sendCost} 重量:{$totalWeight} 距离:{$sendDistance} 姓名:{$customName} 手机号 {$customMobile} 经纬 {$originalLng} {$originalLat} 金额:{$goodsPrice} 数量:{$goodsNum}", '15280215347');
+            }
+            $post['sendDistance'] = $sendDistance;
             $post['sendCost'] = $sendCost;
             $post['sjId'] = $this->sjId;
             $post['shopId'] = $this->shopId;
-            $mainId = $this->mainId;
             $post['mainId'] = $mainId;
             $post['userId'] = $this->userId;
-            //计算总金额
-            $unitPrice = $goodsInfo->price ?? 0;
-            if ($unitPrice <= 0) {
-                util::fail('没有价格,无法下单');
-            }
-            $rise = GoodsSettingClass::getRise($mainId);
-            //根据涨价设置进行自动涨价
-            if (!empty($rise)) {
-                $price = $unitPrice;
-                if ($rise['riseType'] == 0) {
-                    //百分比
-                    $newPrice = $price + sprintf("%.1f", ($price * $rise['riseAmount'] / 100));
-                } else {
-                    //金额
-                    $newPrice = $price + $rise['riseAmount'];
-                }
-                $unitPrice = $newPrice;
+            if ($goodsInfo->priceType == 0) {
+                util::fail('没有价格,不能下单');
             }
-
-            $goodsPrice = $unitPrice * $goodsNum;
-            $prePrice = stringUtil::calcAdd($sendCost, $goodsPrice);
+            $modifyPrice = stringUtil::calcAdd($sendCost, $goodsPrice);
             //非门店订单
             $post['store'] = 0;
-            $sourceType = 2;
-            $couponId = !empty($post['couponId']) ? $post['couponId'] : 0;
-            $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->userId]);
-            $actPrice = $discountData['price'];
-            $post['modifyPrice'] = $actPrice;
-            $post['discountType'] = $discountData['discountType'];
-            $post['discountAmount'] = $discountData['discountAmount'];
-
+            $post['modifyPrice'] = $modifyPrice;
             //非自取订单考虑配送时间
             $post['reachDate'] = !empty($post['reachDate']) ? $post['reachDate'] : date("Y-m-d");
             $sendType = $post['sendType'] ?? 0;
@@ -518,7 +568,7 @@ class OrderController extends BaseController
             $orderId = $return->id;
             $orderSn = $return->orderSn;
             $transaction->commit();
-            util::success(['orderSn' => $orderSn, 'totalPrice' => $actPrice, 'couponId' => $couponId, 'id' => $orderId]);
+            util::success(['orderSn' => $orderSn, 'totalPrice' => $actPrice, 'couponId' => 0, 'id' => $orderId]);
         } catch (Exception $e) {
             $transaction->rollBack();
             Yii::info("失败原因:" . $e->getMessage());