Browse Source

Merge remote-tracking branch 'origin/master'

Panda 1 month ago
parent
commit
de73dfdb95

+ 6 - 6
addons/shopro/controller/Pay.php

@@ -32,8 +32,8 @@ class Pay extends Common
         $order_sn = $this->request->post('order_sn');
         $payment = $this->request->post('payment');
         $openid = $this->request->post('openid', '');
-        /*$money = $this->request->post('money', 0);
-        $money = $money > 0 ? $money : 0;*/
+        $bean = $this->request->post('bean', 0); //善豆
+        $bean = $bean > 0 ? $bean : 0;
         $platform = $this->request->post('platform');
 
         list($order, $order_type) = $this->getOrderInstance($order_sn);
@@ -62,15 +62,15 @@ class Pay extends Common
         // pay 实例
         $payOper = new PayOper();
 
-        $money = false;
-        if ($money && $order_type == 'order') {
+
+        if ($bean && $order_type == 'order') {
             // 余额混合支付
-            $order = Db::transaction(function () use ($payOper, $order, $order_type, $money) {
+            $order = Db::transaction(function () use ($payOper, $order, $order_type, $bean) {
                 // 加锁读订单
                 $order = $order->lock(true)->find($order->id);
 
                 // 余额支付
-                $order = $payOper->money($order, $money, $order_type);
+                $order = $payOper->bean($order, $bean, $order_type);
 
                 return $order;
             });

+ 1 - 0
addons/shopro/service/Wallet.php

@@ -22,6 +22,7 @@ class Wallet
      */
     public static function change($user, $type, $amount, $event, $ext = [], $memo = '')
     {
+        error_stop('禁止使用的钱包方法');
         // 判断用户
         if (is_numeric($user)) {
             $user = UserModel::getById($user);

+ 38 - 1
addons/shopro/service/order/OrderCreate.php

@@ -17,6 +17,7 @@ use app\admin\model\shopro\user\Coupon;
 use app\admin\model\shopro\user\Address as UserAddress;
 use app\admin\model\shopro\user\Invoice as UserInvoice;
 use addons\shopro\service\StockSale;
+use app\common\model\Wallet;
 
 class OrderCreate
 {
@@ -74,6 +75,11 @@ class OrderCreate
     protected $money = 0;
 
     /**
+     * 善豆抵扣(余额和 微信|支付宝,混合支付时使用了)
+     */
+    protected $bean = 0;
+
+    /**
      * 发票配置
      */
     protected $invoiceConfig = [];
@@ -101,6 +107,7 @@ class OrderCreate
         'goods_original_amount' => '0',       // 商品原始总价
         'goods_old_amount' => '0',                // 商品不参与活动时的总价
         'goods_amount' => '0',                // 商品总价
+        'goods_bean_amount' => '0',                // 商品善豆可抵扣总价
         'coupon_discount_fee' => '0',                // 优惠券优惠金额
         'promo_discount_fee' => 0,          // 当前促销优惠总金额 (包含满包邮的邮费)
         'total_discount_fee' => 0,              // 当前订单,总优惠金额(优惠券 + 活动优惠)
@@ -156,6 +163,7 @@ class OrderCreate
         $this->remark = $params['remark'] ?? '';
 
         $this->money = (isset($params['money']) && $params['money'] > 0) ? $params['money'] : 0;
+        $this->bean  = (isset($params['bean']) && $params['bean'] > 0) ? $params['bean'] : 0;
 
         // 获取商品信息
         $this->goodsListInit();
@@ -289,10 +297,13 @@ class OrderCreate
      */
     public function calcAmount()
     {
+        //dump($this->goodsList);
         // 计算商品金额
         foreach ($this->goodsList as $key => &$buyInfo) {
             $goods = $buyInfo['goods'];
 
+            //dd(json_decode(json_encode($buyInfo),true));
+
             // 当前商品原始总价
             $current_goods_original_amount = bcmul($goods->original_price, $buyInfo['goods_num'], 2);
             $this->orderData['goods_original_amount'] = bcadd($this->orderData['goods_original_amount'], $current_goods_original_amount, 2);
@@ -301,6 +312,16 @@ class OrderCreate
             $current_goods_amount = bcmul($buyInfo['current_sku_price']->price, $buyInfo['goods_num'], 2);
             $this->orderData['goods_amount'] = bcadd($this->orderData['goods_amount'], $current_goods_amount, 2);
 
+            //当前商品的善豆抵扣价,商品没有就拿分类
+            $bean_rate = floatval($goods->bean_rate) > 0 ? $goods->bean_rate : 0;
+            if($bean_rate == 0){
+                $bean_rate = Db::name('shopro_category')->where('id',$goods->category_ids)->value('bean_rate');
+                $bean_rate = floatval($bean_rate) > 0 ? $bean_rate : 0;
+            }
+
+            $current_goods_bean_amount = bcdiv(bcmul($current_goods_amount,$bean_rate,2),100,2);
+            $this->orderData['goods_bean_amount'] = bcadd($this->orderData['goods_bean_amount'],$current_goods_bean_amount,2);
+
             // 当有活动时,计算作为普通商品时的商品总金额
             $current_goods_old_amount = $current_goods_amount;
             if ($this->activity['activity']) {
@@ -315,12 +336,14 @@ class OrderCreate
                 $this->orderData['score_amount'] = $this->orderData['score_amount'] + $current_score_amount;
             }
 
+
             // 当前商品总重量
             $current_weight = bcmul($buyInfo['current_sku_price']->weight, $buyInfo['goods_num'], 2);
 
             // 将计算好的属性记录下来,插入订单 item 表使用
             $buyInfo['goods_original_amount'] = $current_goods_original_amount;         // 当前商品原始总金额(原价 * 数量)
             $buyInfo['goods_amount'] = $current_goods_amount;                           // 当前商品总金额(价格 * 数量)
+            $buyInfo['goods_bean_amount'] = $current_goods_bean_amount;                           // 当前商品可抵扣最高总金额(当前商品总金额 * 比例)
             $buyInfo['score_amount'] = $current_score_amount;       // 商品所需积分(积分商城)
             $buyInfo['weight'] = $current_weight;       // 当前商品总重量
             $buyInfo['original_dispatch_amount'] = 0;        // 当前商品运费(未判断活动的,并且也未合并相同运费模板商品的原始运费)
@@ -949,12 +972,24 @@ class OrderCreate
             }
         }
 
-        $temp_remain_pay_fee = bcsub($this->orderData['pay_fee'], $this->money, 2);
+        //善豆抵扣不能高于善豆余额
+        $user_bean = (new Wallet())->getWalletBak($this->user->id,'bean');
+        $this->orderData['goods_bean_amount'] = $this->orderData['goods_bean_amount'] > $user_bean ? $user_bean : $this->orderData['goods_bean_amount'];
+
+        //$temp_remain_pay_fee = bcsub($this->orderData['pay_fee'], $this->money, 2);
+
+        $this->bean = $this->bean > $this->orderData['goods_bean_amount'] ? $this->orderData['goods_bean_amount'] : $this->bean;
+        $temp_remain_pay_fee = bcsub($this->orderData['pay_fee'], $this->bean, 2);
 
         $result = [
             'goods_original_amount' => $this->orderData['goods_original_amount'],
             'goods_old_amount' => $this->orderData['goods_old_amount'],
             'goods_amount' => $this->orderData['goods_amount'],
+
+            'goods_bean_amount' => $this->orderData['goods_bean_amount'],  //最大可抵扣善豆
+            'user_bean_amount'  => $user_bean,                                      //用户善豆余额
+            'min_bean_amount' => config('site.shopro_min_bean_amount'),               //最小可使用善豆数量
+
             'dispatch_amount' => $this->orderData['dispatch_amount'],
             'real_dispatch_amount' => $this->orderData['real_dispatch_amount'],
             'order_amount' => $this->orderData['order_amount'],
@@ -964,6 +999,7 @@ class OrderCreate
             'coupon_discount_fee' => $this->orderData['coupon_discount_fee'],
             'promo_discount_fee' => $this->orderData['promo_discount_fee'],     // 包含包邮的运费优惠
             'money' => $this->money,                                             // 余额支付部分
+            'bean'  => $this->bean,                                             // 善豆支付部分
             "invoice_amount" => $this->orderData[$this->invoiceConfig['amount_type']]       // 可开票金额
         ];
 
@@ -1093,6 +1129,7 @@ class OrderCreate
             $orderData['promo_types'] = join(',', $result['promo_types']);
             $orderData['goods_original_amount'] = $result['goods_original_amount'];
             $orderData['goods_amount'] = $result['goods_amount'];
+            $orderData['goods_bean_amount'] = $result['goods_bean_amount'];
             $orderData['dispatch_amount'] = $result['dispatch_amount'];
             $orderData['remark'] = $this->remark;
             $orderData['order_amount'] = $result['order_amount'];

+ 50 - 2
addons/shopro/service/pay/PayOper.php

@@ -8,6 +8,7 @@ use app\admin\model\shopro\user\User;
 use app\admin\model\shopro\order\Action;
 use think\helper\Str;
 use addons\shopro\service\Wallet as WalletService;
+use app\common\model\Wallet;
 
 
 class PayOper
@@ -90,11 +91,17 @@ class PayOper
         $money = $order->remain_pay_fee > $money ? $money : $order->remain_pay_fee;     // 混合支付不能超过订单应支付总金额
 
         // 扣除用户余额
-        WalletService::change($this->user, 'money', -$money, 'order_pay', [
+        /*WalletService::change($this->user, 'money', -$money, 'order_pay', [
             'order_id' => $order->id,
             'order_sn' => $order->order_sn,
             'order_type' => $order_type,
-        ]);
+        ]);*/
+
+        $walletService = new Wallet();
+        if (!$walletService->change($this->user->id, -$money, 'money', 31, '商城购物', 'shopro_order', $order->id)) {
+            Db::rollback();
+            error_stop($walletService->getMessage());
+        }
 
         // 添加支付记录
         $pay = $this->addPay($order, [
@@ -111,6 +118,47 @@ class PayOper
         return $this->checkAndPaid($order, $order_type, 'online', 'money');
     }
 
+    /**
+     * 善豆付款
+     *
+     * @param think\Model $order
+     * @param float $bean
+     * @param string $order_type
+     * @return think\Model
+     */
+    public function bean($order, $bean, $order_type = 'order')
+    {
+        // 余额支付金额,传入金额和剩余支付金额最大值
+        $bean = $order->remain_pay_fee > $bean ? $bean : $order->remain_pay_fee;     // 混合支付不能超过订单应支付总金额
+
+        // 扣除用户余额
+        /*WalletService::change($this->user, 'bean', -$bean, 'order_pay', [
+            'order_id' => $order->id,
+            'order_sn' => $order->order_sn,
+            'order_type' => $order_type,
+        ]);*/
+
+        $walletService = new Wallet();
+        if (!$walletService->change($this->user->id, -$bean, 'bean', 131, '商城购物抵扣', 'shopro_order', $order->id)) {
+            Db::rollback();
+            error_stop($walletService->getMessage());
+        }
+
+        // 添加支付记录
+        $pay = $this->addPay($order, [
+            'order_type' => $order_type,
+            'pay_type' => 'bean',
+            'pay_fee' => $bean,
+            'real_fee' => $bean,
+            'transaction_id' => null,
+            'payment_json' => [],
+            'status' => PayModel::PAY_STATUS_PAID
+        ]);
+
+        // 余额直接支付成功,更新订单剩余应付款金额,并检测订单状态
+        return $this->checkAndPaid($order, $order_type, 'online', 'bean');
+    }
+
 
 
     /**

+ 1 - 1
application/api/controller/Payios.php

@@ -436,7 +436,7 @@ class Payios extends Api
         $args = json_decode($order_info['args'],true);
         //先充值
         if($order_info['table_name'] == 'gold_recharge'){
-            $result = model('Wallet')->lockChangeAccountRemain($order_info['user_id'],'gold',$args['gold'],10, '金币充值','pay_order',$order_info['id']);
+            $result = model('Wallet')->lockChangeAccountRemain($order_info['user_id'],$args['gold'],'gold',10, '金币充值','pay_order',$order_info['id']);
             if($result['status']===false)
             {
                 filePut($prefix.'逻辑添加金币失败');

+ 1 - 1
application/api/controller/Task.php

@@ -138,7 +138,7 @@ class Task extends Api
         Db::startTrans();
         try{
             // 增加用户金币
-            $res1 = model('wallet')->lockChangeAccountRemain($this->auth->id,'bean',$taskInfo["exp"],102,$taskInfo['name'],'task_log',$tasklogInfo['id']);
+            $res1 = model('wallet')->lockChangeAccountRemain($this->auth->id,$taskInfo["exp"],'bean',102,$taskInfo['name'],'task_log',$tasklogInfo['id']);
             if($res1['status'] === false){
                 Db::rollback();
                 $this->error($res1['msg']);

+ 1 - 1
application/api/controller/Usersign.php

@@ -94,7 +94,7 @@ class Usersign extends Api
         }
 
         //加钱
-        $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'bean',$data['goldnum'],101,'签到奖励','user_sign',$log_id);
+        $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$data['goldnum'],'bean',101,'签到奖励','user_sign',$log_id);
         if($rs['status'] === false){
             Db::rollback();
             $this->error($rs['msg']);

+ 5 - 3
application/common/model/Wallet.php

@@ -21,6 +21,8 @@ class Wallet extends BaseModel
         //善豆
         101 => '签到', //bean +
         102 => '完成任务', //bean +
+        131 => '商城订单支付抵扣', //bean -
+        132 => '商城订单退款', //bean +
     ];
     // 操作钱包余额类型
     const money_type = [
@@ -84,7 +86,7 @@ class Wallet extends BaseModel
      *
      * @param floatval $number 金额(正数进账,负数出账)
      * @param $accountType 货币类型,money,score
-     * @param $logtype 日志的类型
+     * @param $log_type 日志的类型
      * @param $remark  备注
      * @param $user_id  用户id
      * @param $table  来源表
@@ -96,7 +98,7 @@ class Wallet extends BaseModel
      * @return array[log_table]
      * @return array[log_id]
      */
-    public function lockChangeAccountRemain($user_id, $accountType = 'money', $number, $logtype = '', $remark = '', $table = '', $table_id = 0, $isAdmin = false)
+    public function lockChangeAccountRemain($user_id, $number, $accountType = 'money',  $log_type = '', $remark = '', $table = '', $table_id = 0)
     {
         //初始化
         $result = array(
@@ -144,7 +146,7 @@ class Wallet extends BaseModel
                 //钱币记录
                 $data             = array();
                 $data['user_id']  = $user_id;
-                $data['log_type'] = $logtype;
+                $data['log_type'] = $log_type;
 //                $data['money_type'] = $accountType;
                 $data['before']       = $wallet[$accountType];
                 $data['change_value'] = $number;

+ 2 - 0
application/extra/site.php

@@ -33,6 +33,7 @@ return array (
     'explain' => '说明配置',
     'keyword' => '关键词',
     'live' => '直播配置',
+    'wallet' => '财务配置',
   ),
   'mail_type' => '1',
   'mail_smtp_host' => 'smtp.qq.com',
@@ -70,4 +71,5 @@ return array (
   'examine_bg_image' => '/uploads/20241211/811f53e60d4eb8f976373b6f4e22d107.png',
   'keyword_filter' => '毛泽东|邓小平|江泽民|胡锦涛|共产党|习大大|大法|微信',
   'live_notice' => '欢迎来到直播间直播间内严禁出现违法违规、低俗色情、吸烟酗酒、人身伤害等内容。请大家注意财产安全,理性消费,谨防网络诈骗。',
+  'shopro_min_bean_amount' => '0.01',
 );