Jelajahi Sumber

明细支出

gorden 1 bulan lalu
induk
melakukan
32e054a151

+ 9 - 0
app/admin/controller/finance/WithdrawalListController.php

@@ -15,6 +15,7 @@ use support\Db;
 use support\exception\BusinessException;
 use support\Request;
 use support\Response;
+use Webman\Event\Event;
 
 class WithdrawalListController extends Curd
 {
@@ -177,6 +178,14 @@ class WithdrawalListController extends Curd
             _syslog('提现', '修改状态成功');
 
             Db::commit();
+
+            if ($status == 'ACTIVED') {
+                // 入收支记录
+                $params['account_list_id'] = $memberAccountListId;
+                $params['type'] = 'withdraw';
+                Event::dispatch('statistics.inout.out',$params);
+            }
+
             return json_success('success');
         } catch (BusinessException $e) {
 

+ 6 - 0
app/admin/controller/order/RefundController.php

@@ -21,6 +21,7 @@ use support\Log;
 use support\Request;
 use support\Response;
 use Tinywan\Jwt\JwtToken;
+use Webman\Event\Event;
 use Yansongda\Pay\Pay;
 
 class RefundController extends Curd
@@ -418,6 +419,11 @@ class RefundController extends Curd
 
             Db::commit();
 
+            // 退款入收支
+            $params['orderId'] = $orderId;
+            $params['type'] = 'refund';
+            Event::dispatch('statistics.inout.out',$params);
+
             return json_success('success');
         } catch (BusinessException $e) {
             Db::rollBack();

+ 155 - 5
app/event/statistics/InOutEvent.php

@@ -3,10 +3,12 @@
 namespace app\event\statistics;
 
 use app\model\DataInout;
+use app\model\MemberAccountList;
 use app\model\Order;
 use app\model\OrderSheet;
 use app\model\PayDetail;
 use app\model\SysDept;
+use support\exception\BusinessException;
 use support\Log;
 
 class InOutEvent
@@ -25,6 +27,14 @@ class InOutEvent
         {"label": "退款", "value": "退款"}
      */
 
+    /**
+     * @Desc 收入
+     * @Author Gorden
+     * @Date 2024/10/12 15:23
+     *
+     * @param $params
+     * @return void
+     */
     public function dataIn($params)
     {
         try {
@@ -37,7 +47,7 @@ class InOutEvent
                 ->get()
                 ->toArray();
             $payDetails = PayDetail::where('join_pay_order_id', $order->order_groupby)
-                ->whereJsonContains('join_pay_object_json->order_id',$params['orderId'])
+                ->whereJsonContains('join_pay_object_json->order_id', $params['orderId'])
                 ->where('pay_status', 'SUCCESS')
                 ->get()
                 ->toArray();
@@ -86,7 +96,7 @@ class InOutEvent
             $data['inout_name'] = implode(',', $data['inout_name']);
 
             $payJson = [
-                'trade'=>'',
+                'trade' => '',
                 'if_union' => count($payDetails) > 1 ? 'Y' : 'N',
                 'union_order_id' => count($payDetails) > 1 ? $params['orderId'] : ''
             ];
@@ -105,9 +115,9 @@ class InOutEvent
                 }
                 if (!empty($payDetail['pay_json_response'])) {
                     $payJsonResponse = json_decode($payDetail['pay_json_response'], true);
-                    if (isset($payJsonResponse['transaction_id'])){
+                    if (isset($payJsonResponse['transaction_id'])) {
                         $payJson['trade'] = $payJsonResponse['transaction_id'];
-                    }elseif (isset($payJsonResponse['trade_no'])){
+                    } elseif (isset($payJsonResponse['trade_no'])) {
                         $payJson['trade'] = $payJsonResponse['trade_no'];
                     }
                 }
@@ -122,9 +132,149 @@ class InOutEvent
         }
     }
 
-    public function dataOut()
+    public function dataOut($params)
+    {
+        try {
+            if ($params['type'] == 'refund') {
+                $data = $this->orderRefund($params);
+            } elseif ($params['type'] == 'withdraw') {
+                $data = $this->commissionWithdraw($params);
+            } else {
+                throw new BusinessException("不支持的支出类型");
+            }
+
+            $this->dataSave($data);
+        } catch (BusinessException $e) {
+            Log::error("统计支出失败:" . $e->getMessage(), $params);
+        } catch (\Exception $e) {
+            Log::error("统计支出失败:" . $e->getMessage(), $params);
+        }
+    }
+
+    public function orderRefund($params)
     {
+        try {
+            $order = Order::where('order_id', $params['orderId'])->first();
+            $sheets = OrderSheet::with([
+                'goods' => function ($query) {
+                    $query->select('goods_id', 'goods_name', 'goods_classify');
+                }
+            ])->where('join_sheet_order_id', $params['orderId'])
+                ->get()
+                ->toArray();
+            $payDetail = PayDetail::where('join_pay_order_id', $order->order_groupby)
+                ->whereJsonContains('join_pay_object_json->order_id', $params['orderId'])
+                ->where('pay_category', 'REFUND')
+                ->where('pay_status', 'SUCCESS')
+                ->first();
+            if (!$payDetail) {
+                throw new BusinessException("支付数据异常");
+            }
+            $payDetail = $payDetail->toArray();
 
+            $data = [
+                'dept_id' => 0,
+                'member_id' => $order->join_order_member_id,
+                'inout_classify' => 'OUT',
+                'inout_category' => '退款',
+
+            ];
+            // 部门id
+            if (!empty($order->order_config_json)) {
+                $orderConfigJson = json_decode($order->order_config_json, true);
+                if (!empty($orderConfigJson['dept'])) {
+                    $data['dept_id'] = $orderConfigJson['dept'];
+                } elseif (!empty($orderConfigJson['premises'])) {
+                    $data['dept_id'] = SysDept::where('dept_name', $orderConfigJson['premises'])->value('dept_id');
+                }
+            }
+            $inoutObjectJson = [];
+            // 产品
+            foreach ($sheets as $sheet) {
+                $inoutObjectJson['goods'][] = [
+                    'goods_id' => $sheet['goods']['goods_id'] ?? '',
+                    'goods_name' => $sheet['goods']['goods_name'] ?? ''
+                ];
+                $data['inout_name'][] = $sheet['goods']['goods_name'] ?? '';
+                if (!isset($inoutObjectJson['order'])) {
+                    $inoutObjectJson['order'][$params['orderId']] = [
+                        'pay' => $payDetail['pay_amount'],
+                        'classify' => $order->order_classify,
+                        'discount' => $order->order_discount_json ?? json_decode($order->order_discount_json)
+                    ];
+                }
+                $inoutObjectJson['order'][$params['orderId']]['goods'][] = [
+                    'goods_id' => $sheet['goods']['goods_id'] ?? '',
+                    'goods_name' => $sheet['goods']['goods_name'] ?? '',
+                    'order_sheet_num' => $sheet['order_sheet_amount'],
+                    'order_sheet_pay' => $sheet['order_sheet_pay']
+                ];
+                $inoutObjectJson['classify'][] = $sheet['goods']['goods_classify'] ?? '';
+            }
+            $inoutObjectJson['ordergroup'] = $order->order_groupby;
+            $data['inout_object_json'] = json_encode($inoutObjectJson);
+            $data['inout_name'] = implode(',', $data['inout_name']);
+
+            $payJson = [
+                'trade' => '',
+                'if_union' => 'N',
+                'union_order_id' => ''
+            ];
+            if (in_array($payDetail['pay_prepayid'], ['WXPAY', 'ALIPAY', 'OFFLINE_WXPAY', 'OFFLINE_ALIPAY', 'MONEY'])) {
+                $data['inout_attr'] = 'MONEY';
+            } else {
+                $data['inout_attr'] = 'ACCOUNT';
+            }
+            $data['inout_amount'] = $payDetail['pay_amount'];
+            $prepayId = explode('-', $payDetail['pay_prepayid']);
+            if (count($prepayId) == 1) {
+                $data['pay_type'] = $payDetail['pay_prepayid'];
+            } elseif (count($prepayId) > 1) {
+                $data['pay_type'] = $prepayId[1];
+            }
+            if (!empty($payDetail['pay_json_response'])) {
+                $payJsonResponse = json_decode($payDetail['pay_json_response'], true);
+                if (isset($payJsonResponse['transaction_id'])) {
+                    $payJson['trade'] = $payJsonResponse['transaction_id'];
+                } elseif (isset($payJsonResponse['trade_no'])) {
+                    $payJson['trade'] = $payJsonResponse['trade_no'];
+                }
+            }
+            $data['pay_json'] = json_encode($payJson);
+
+            return $data;
+        } catch (BusinessException $e) {
+            throw new BusinessException($e->getMessage());
+        } catch (\Exception $e) {
+            throw new BusinessException("退款支出处理失败");
+        }
+    }
+
+    public function commissionWithdraw($params)
+    {
+        try {
+            $memberAccountList = MemberAccountList::where('member_account_list_id', $params['account_list_id'])->first();
+            $data = [
+                'dept_id' => 0,
+                'member_id' => $memberAccountList->join_member_account_list_member_id,
+                'inout_object_json' => $memberAccountList->member_account_list_json,
+                'inout_attr' => 'MONEY',
+                'inout_classify' => 'OUT',
+                'inout_category' => '提现',
+                'inout_name' => '提现',
+                'inout_amount' => $memberAccountList->member_account_list_amount,
+                'pay_type' => 'UNIONPAY',
+                'pay_json' => json_encode([
+                    'trade' => '',
+                    'if_union' => 'N',
+                    'union_order_id' => ''
+                ]),
+            ];
+
+            return $data;
+        } catch (\Exception $e) {
+            throw new BusinessException("提现支出处理失败");
+        }
     }
 
     public function dataSave($data)

+ 3 - 0
config/event.php

@@ -36,5 +36,8 @@ return [
     ],
     'statistics.inout.in' => [
         [\app\event\statistics\InOutEvent::class, 'dataIn']
+    ],
+    'statistics.inout.out' => [
+        [\app\event\statistics\InOutEvent::class, 'dataOut']
     ]
 ];