Parcourir la source

Merge branch 'master' of http://git.liuniu946.com/Kirin/zccy

zxhxx il y a 3 ans
Parent
commit
86fcbac0df

+ 1 - 0
app/admin/controller/system/SystemStore.php

@@ -147,6 +147,7 @@ class SystemStore extends AuthController
             ['sh_start_distance', 0],
             ['sh_step', 0],
             ['sh_step_money', 0],
+            ['sharing', 0],
         ]);
         SystemStoreModel::beginTrans();
         try {

+ 9 - 0
app/admin/view/system/system_store/add.php

@@ -139,6 +139,14 @@
                                     </i-Col>
                                 </Row>
                             </Form-Item>
+                            <Form-Item>
+                                <Row>
+                                    <i-Col span="13">
+                                        <span>订单分帐:</span>
+                                        <i-Input placeholder="订单分帐" v-model="form.sharing" style="width: 80%" type="text"></i-Input>
+                                    </i-Col>
+                                </Row>
+                            </Form-Item>
                             <Form-Item>
                                 <Row>
                                     <i-Col span="13">
@@ -212,6 +220,7 @@
                         sh_start_distance:storeData.sh_start_distance || 0,
                         sh_step:storeData.sh_step || 0,
                         sh_step_money:storeData.sh_step_money || 0,
+                        sharing:storeData.sharing || 0,
                     },
                     visible:false,
                 }

+ 71 - 0
app/models/store/StoreOrder.php

@@ -9,6 +9,7 @@ namespace app\models\store;
 
 use app\admin\model\system\ShippingTemplatesFree;
 use app\admin\model\system\ShippingTemplatesRegion;
+use app\admin\model\system\SystemAdmin;
 use app\admin\model\system\SystemStorePoint;
 use crmeb\basic\BaseModel;
 use think\db\exception\DataNotFoundException;
@@ -19,6 +20,7 @@ use crmeb\traits\ModelTrait;
 use think\facade\Log;
 use app\models\system\SystemStore;
 use app\models\routine\RoutineTemplate;
+use think\Model;
 use app\models\user\{User, UserAddress, UserBill, UserSpread, WechatUser};
 use crmeb\services\{MiniProgramService,
     OtherSMSService,
@@ -2259,4 +2261,73 @@ class StoreOrder extends BaseModel
         return compact('list', 'count');
     }
 
+
+    /**
+     * 处理奖励
+     */
+    public static function treatmentAward($order_id)
+    {
+        $order = StoreOrder::get($order_id);
+        if (!$order) return self::setErrorInfo('找不到订单');
+        if (!$order['transaction_id']) return true;
+        $store = SystemStorePoint::get($order['store_id']);
+        if (!$store) return true;
+
+        $store_leader = SystemAdmin::where('role', sys_config('default_store_admin', 7))
+            ->where('store_id', $store['id'])->where('is_del', 0)->where('status', 1)
+            ->find();
+        if (!$store_leader) return true;
+        $user = User::where('admin_id', $store_leader['id'])->find();
+        if (!$user) return true;
+        $p_user_info = WechatUser::get($user['uid']);
+
+        // 选择发起人的订单作为分账的账单
+        $transaction_id = $order['transaction_id']; // 微信订单号
+        $out_order_no = $order['order_id'];
+
+        // 计算发起人和参与者的分账金额
+        $price = $order['pay_price'] - $order['pay_postage_sh'] - $order['pay_postage'];
+        $p_amount = bcmul($price, $store['sharing'] / 100, 2);  //bcmul:2个任意精度数字乘法计算
+        $p_separate_account_amount = floor($p_amount * 100); // 发起人分账金额,以分为单位
+        if ($p_separate_account_amount <= 0) return true;
+
+        $receivers[] = array(
+            'type' => 'PERSONAL_OPENID',
+            'account' => $p_user_info['routine_openid'],
+            'amount' => $p_separate_account_amount,
+            'description' => '门店订单'
+        );
+
+        // 记录发起人分账开始的信息
+        $p_separate_account_record = new StoreSeparateAccountRecord();
+        $p_separate_account_record['money'] = $p_amount;
+        $p_separate_account_record['order_id'] = $order_id;  // OK
+        $p_separate_account_record['uid'] = $user['uid'];
+        $p_separate_account_record['state'] = 0; // 默认为失败
+        $p_separate_account_record['notice'] = "分账失败";
+        $p_separate_account_record['store_id'] = $order['store_id'];
+        $p_separate_account_record['start_time'] = time();
+        $p_separate_account_record['end_time'] = 0;
+        $p_separate_account_record->save();
+
+        // 添加发起人为分账接受方
+        MiniProgramService::addReceiver($p_user_info['routine_openid']); // 添加发起人为分账接受方
+
+        if ($transaction_id) {
+            $res = MiniProgramService::multi_profit_sharing($transaction_id, $out_order_no, $receivers);
+            $res = json_decode($res, true);
+
+            $separateAccount = StoreSeparateAccountRecord::where('order_id', $order_id)->select();
+            if ($res['return_code'] == 'SUCCESS') {
+                foreach ($separateAccount as $key => $value) {
+                    $value['state'] = 1;
+                    $value['notice'] = "分账成功";
+                    $value['end_time'] = time();
+                    $value->save();
+                }
+                MiniProgramService::profit_sharing_finish($order['transaction_id'], $order['order_id']);
+            }
+        }
+        return true;
+    }
 }

+ 24 - 0
app/models/store/StoreSeparateAccountRecord.php

@@ -0,0 +1,24 @@
+<?php
+
+namespace app\models\store;
+
+use crmeb\services\UtilService;
+use crmeb\traits\ModelTrait;
+use crmeb\basic\BaseModel;
+
+class StoreSeparateAccountRecord extends BaseModel
+{
+	/**
+     * 数据表主键
+     * @var string
+     */
+    protected $pk = 'id';
+
+    /**
+     * 模型名称
+     * @var string
+     */
+    protected $name = 'store_separate_account_record';
+
+    use ModelTrait;
+}

+ 6 - 3
crmeb/repositories/OrderRepository.php

@@ -106,11 +106,12 @@ class OrderRepository
         $res1 = StoreOrder::gainUserIntegral($order);
 //        $res2 = User::backOrderBrokerage($order);
         $res2 = User::sendBackOrderBrokerage($order);
+        $res3 = StoreOrder::treatmentAward($order['id']);
         StoreOrder::orderTakeAfter($order);
         StoreOrder::where('id', $order['id'])->update(['arrive_time' => time()]);
         //满赠优惠券
         WechatUser::userTakeOrderGiveCoupon($uid, $order['total_price']);
-        if (!($res1 && $res2)) exception('收货失败!');
+        if (!($res1 && $res2 && $res3)) exception('收货失败!');
     }
 
     /**
@@ -124,9 +125,10 @@ class OrderRepository
         $res1 = AdminStoreOrder::gainUserIntegral($order);
 //        $res2 = User::backOrderBrokerage($order);
         $res2 = User::sendBackOrderBrokerage($order);
+        $res3 = StoreOrder::treatmentAward($order['id']);
         AdminStoreOrder::orderTakeAfter($order);
         StoreOrder::where('id', $order['id'])->update(['arrive_time' => time()]);
-        if (!($res1 && $res2)) exception('收货失败!');
+        if (!($res1 && $res2 && $res3)) exception('收货失败!');
     }
 
     /**
@@ -139,9 +141,10 @@ class OrderRepository
 
         $res1 = AdminStoreOrder::gainUserIntegral($order, false);
         $res2 = User::sendBackOrderBrokerage($order);
+        $res3 = StoreOrder::treatmentAward($order['id']);
         AdminStoreOrder::orderTakeAfter($order);
         StoreOrder::where('id', $order['id'])->update(['arrive_time' => time()]);
-        if (!($res1 && $res2)) exception('收货失败!');
+        if (!($res1 && $res2 && $res3)) exception('收货失败!');
     }
 
 

+ 30 - 0
crmeb/services/MiniProgramService.php

@@ -7,6 +7,7 @@
 
 namespace crmeb\services;
 
+use app\models\store\StoreOrder;
 use crmeb\repositories\PaymentRepositories;
 use crmeb\utils\Hook;
 use EasyWeChat\Foundation\Application;
@@ -332,6 +333,7 @@ class MiniProgramService
                     if (($count = strpos($notify->out_trade_no, '_')) !== false) {
                         $notify->out_trade_no = substr($notify->out_trade_no, $count + 1);
                     }
+                    StoreOrder::edit(['transaction_id' => $notify->transaction_id], $notify->out_trade_no, 'order_id');
                     (new Hook(PaymentRepositories::class, 'wechat'))->listen($notify->attach, $notify->out_trade_no);
                 }
                 return false;
@@ -354,4 +356,32 @@ class MiniProgramService
     }
 
 
+    /**
+     * 添加分账接收方
+     */
+    public static function addReceiver($account)
+    {
+        $receiver = array(
+            'type' => 'PERSONAL_OPENID',
+            'account' => $account, // openid
+            'relation_type' => 'USER',
+        );
+        return self::paymentService()->addReceiver($receiver);
+    }
+
+    /**
+     * 多次分账
+     */
+    public static function multi_profit_sharing($orderNo, $sharingNo, $receivers)
+    {
+        return self::paymentService()->multiProfitSharing($orderNo, $sharingNo, $receivers);
+    }
+
+    /**
+     * 完结分账
+     */
+    public static function profit_sharing_finish($orderNo, $sharingNo)
+    {
+        return self::paymentService()->profitSharingFinish($orderNo, $sharingNo);
+    }
 }

+ 65 - 0
vendor/overtrue/wechat/src/Payment/API.php

@@ -74,6 +74,10 @@ class API extends AbstractAPI
     const API_QUERY_REFUND = '/pay/refundquery';
     const API_DOWNLOAD_BILL = '/pay/downloadbill';
     const API_REPORT = '/payitil/report';
+    const API_PROFITSHARING = '/secapi/pay/profitsharing';
+    const API_MULTI_PROFIT_SHARING = '/secapi/pay/multiprofitsharing';
+    const API_PROFIT_SHARING_ADD_RECEIVER = '/secapi/pay/profitsharingaddreceiver';
+    const API_PROFIT_SHARING_FINISH = '/secapi/pay/profitsharingfinish';
 
     const API_URL_SHORTEN = 'https://api.mch.weixin.qq.com/tools/shorturl';
     const API_AUTH_CODE_TO_OPENID = 'https://api.mch.weixin.qq.com/tools/authcodetoopenid';
@@ -561,4 +565,65 @@ class API extends AbstractAPI
     {
         return $this->cache ?: $this->cache = new FilesystemCache(sys_get_temp_dir());
     }
+
+    /**
+     * @param $orderNo
+     * @param $sharingNo
+     * @param $receivers
+     * @param string $type
+     * @return Collection
+     */
+    public function profit_sharing(
+        $orderNo,
+        $sharingNo,
+        $receivers,
+        $type = self::TRANSACTION_ID
+    )
+    {
+        $params = [
+            $type => $orderNo,
+            'out_order_no' => $sharingNo,
+            'receivers' => json_encode($receivers)
+        ];
+        return $this->safeRequest($this->wrapApi(self::API_PROFITSHARING), $params, 'post','hash_hmac');
+    }
+
+    public function addReceiver(
+        $receiver
+    )
+    {
+        $params = [
+            'receiver' => json_encode($receiver)
+        ];
+        return $this->safeRequest($this->wrapApi(self::API_PROFIT_SHARING_ADD_RECEIVER), $params, 'post', 'hash_hmac');
+    }
+
+    public function multiProfitSharing(
+        $orderNo,
+        $sharingNo,
+        $receivers,
+        $type = self::TRANSACTION_ID
+    )
+    {
+        $params = [
+            $type => $orderNo,
+            'out_order_no' => $sharingNo,
+            'receivers' => json_encode($receivers)
+        ];
+        return $this->safeRequest($this->wrapApi(self::API_MULTI_PROFIT_SHARING), $params, 'post', 'hash_hmac');
+    }
+
+    public function profitSharingFinish(
+        $orderNo,
+        $sharingNo,
+        $type = self::TRANSACTION_ID
+    )
+    {
+        $params = [
+            $type => $orderNo,
+            'out_order_no' => $sharingNo,
+            'description' => '分账已完成'
+        ];
+        return $this->safeRequest($this->wrapApi(self::API_PROFIT_SHARING_FINISH), $params, 'post', 'hash_hmac');
+    }
 }