From 12314cdf424aa82331fc88cd1d4a90e2e95d7ccb Mon Sep 17 00:00:00 2001 From: "DESKTOP-GMUNQ1B\\Administrator" <604446095@qq.com> Date: Sat, 14 Dec 2024 14:46:35 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E8=AF=95=E8=AE=A2=E5=8D=95=E9=80=80?= =?UTF-8?q?=E6=AC=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/order/OrderController.php | 13 +- app/api/lists/order/OrderList.php | 2 +- app/common/model/store_order/StoreOrder.php | 9 + .../StoreOrderCartInfo.php | 4 + app/common/service/RefundOrderService.php | 157 ++++++++++++++++++ app/common/service/pay/BalancePay.php | 25 +++ app/common/service/pay/CashPay.php | 29 ++++ app/common/service/pay/PayTool.php | 32 ++++ app/common/service/pay/PurchaseFundPay.php | 25 +++ app/common/service/pay/WeChatPay.php | 40 +++++ 10 files changed, 325 insertions(+), 11 deletions(-) create mode 100644 app/common/service/RefundOrderService.php create mode 100644 app/common/service/pay/BalancePay.php create mode 100644 app/common/service/pay/CashPay.php create mode 100644 app/common/service/pay/PayTool.php create mode 100644 app/common/service/pay/PurchaseFundPay.php create mode 100644 app/common/service/pay/WeChatPay.php diff --git a/app/api/controller/order/OrderController.php b/app/api/controller/order/OrderController.php index 7ef4bdac0..6472fa032 100644 --- a/app/api/controller/order/OrderController.php +++ b/app/api/controller/order/OrderController.php @@ -18,6 +18,7 @@ use app\common\model\system_store\SystemStoreStaff; use app\common\model\user\User; use app\common\model\user\UserAddress; use app\common\service\Curl; +use app\common\service\RefundOrderService; use Exception; use support\Log; use think\facade\Db; @@ -464,19 +465,11 @@ class OrderController extends BaseApiController /** * 订单退款申请 */ - public function apply_refund() + public function apply_refund(RefundOrderService $refundOrderService) { $params = (new OrderValidate())->post()->goCheck('add'); $uid = $this->userId; - //拆单逻辑 - // $res = OrderLogic::dealRefund($uid, $params); - $detail = StoreOrder::where('id', $params['id'])->where('uid',$uid)->where('status','<>',2)->where('paid',1)->find(); - if ($detail) { - $res = StoreOrderLogic::refund($detail, ['order_id' => $detail['order_id']]); - if ($res != false) { - return $this->success($res); - } - } + $refundOrderService->refund($uid, $params); return $this->success('申请成功'); } diff --git a/app/api/lists/order/OrderList.php b/app/api/lists/order/OrderList.php index 525ebac18..5b436418c 100644 --- a/app/api/lists/order/OrderList.php +++ b/app/api/lists/order/OrderList.php @@ -51,7 +51,7 @@ class OrderList extends BaseAdminDataLists implements ListsSearchInterface ->select() ->each(function ($item) { $item['goods_list'] = StoreOrderCartInfo::where('oid', $item['id']) - ->field('product_id,cart_num,verify_code,is_writeoff,writeoff_time,cart_info')->limit(3)->select() + ->field('id,product_id,cart_num,verify_code,is_writeoff,writeoff_time,cart_info,status')->select() ->each(function ($v) use ($item) { $v['store_name'] = ''; $v['image'] = ''; diff --git a/app/common/model/store_order/StoreOrder.php b/app/common/model/store_order/StoreOrder.php index e373c780f..c793e9041 100644 --- a/app/common/model/store_order/StoreOrder.php +++ b/app/common/model/store_order/StoreOrder.php @@ -123,4 +123,13 @@ class StoreOrder extends BaseModel Log::error('store_order:更新后'.$e->getMessage()); } } + + public function allowRefund(): bool + { + if (in_array($this->status, [0, 1]) && $this->paid == 1) { + return true; + } + return false; + } + } diff --git a/app/common/model/store_order_cart_info/StoreOrderCartInfo.php b/app/common/model/store_order_cart_info/StoreOrderCartInfo.php index a3c407b10..c1bcba2e1 100644 --- a/app/common/model/store_order_cart_info/StoreOrderCartInfo.php +++ b/app/common/model/store_order_cart_info/StoreOrderCartInfo.php @@ -16,6 +16,10 @@ class StoreOrderCartInfo extends BaseModel protected $json = ['cart_info']; protected $jsonAssoc = true; + const StatusWait = 0; //待核销 + const StatusWriteOff = 1; //核销完成 + const StatusRefund = 2; //退款 + public function goodsName() { return $this->hasOne(StoreBranchProduct::class,'id','product_id')->bind(['store_name','image','unit','price']); diff --git a/app/common/service/RefundOrderService.php b/app/common/service/RefundOrderService.php new file mode 100644 index 000000000..4d5d2752b --- /dev/null +++ b/app/common/service/RefundOrderService.php @@ -0,0 +1,157 @@ +where('uid', $userId)->find(); + if (empty($order)) { + throw new BusinessException('订单不存在'); + } + if (!$order->allowRefund()) { + throw new BusinessException('订单不能退款'); + } + $orderCartProducts = StoreOrderCartInfo::where('oid', $order['id'])->whereIn('id', $params['old_cart_id'])->select(); + foreach ($orderCartProducts as $orderCartProduct) { + if ($orderCartProduct['is_pay'] != 1 || $orderCartProduct['status'] != StoreOrderCartInfo::StatusWait) { + throw new BusinessException('订单商品状态异常'); + } + } + $refundAmount = $this->calculate($orderCartProducts); + $this->refundMoney($order, $refundAmount); + $this->updateProductStock($orderCartProducts); + $this->updateOrderProductStatus($order, $params['old_cart_id']); + $this->updateOrderStatus($order, $refundAmount, count($params['old_cart_id'])); + $this->refundCommission($order, $orderCartProducts); + Db::commit(); + } catch (Exception $e) { + Db::rollback(); + throw new BusinessException($e->getMessage()); + } + } + + /** + * 计算退款金额 + * @param $orderCartProducts + * @return string + */ + public function calculate($orderCartProducts): string + { + $amount = '0.00'; + foreach ($orderCartProducts as $orderCartProduct) { + if ($orderCartProduct['is_pay'] == 1 && $orderCartProduct['status'] == StoreOrderCartInfo::StatusWait) { + $amount = bcadd($amount, $orderCartProduct['total_price'], 2); + } + } + return $amount; + } + + /** + * 资金原路退回 + * @param $order + * @param $refundAmount + * @return void + */ + public function refundMoney($order, $refundAmount): void + { + $payTool = PayTool::getInstance($order['pay_type']); + $payTool->refund($refundAmount, $order); + } + + /** + * 更新商品库存 + * @param $orderCartProducts + * @throws Exception + */ + public function updateProductStock($orderCartProducts): void + { + $updateData = []; + foreach ($orderCartProducts as $product) { + $storeBranchProductId = StoreBranchProduct::where('store_id', $product['store_id']) + ->where('product_id', $product['product_id']) + ->withTrashed()->value('id'); + if ($storeBranchProductId) { + $updateData[] = [ + 'id' => $storeBranchProductId, + 'stock' => ['inc', $product['cart_num']], + 'sales' => ['dec', $product['cart_num']], + ]; + } + } + (new StoreBranchProduct())->saveAll($updateData); + } + + /** + * 更新订单商品状态 + * @param $order + * @param $orderCartIds + * @return void + * @throws DbException + */ + public function updateOrderProductStatus($order, $orderCartIds): void + { + StoreOrderCartInfo::whereIn('id', $orderCartIds)->where('oid', $order['id'])->update(['is_pay' => -1, 'status' => OrderEnum::REFUND_STATUS_FINISH]); + } + + /** + * 更新订单状态 + * @param $order + * @param $refundAmount + * @param $refundNum + * @return void + * @throws DbException + */ + public function updateOrderStatus($order, $refundAmount, $refundNum): void + { + $orderProductCount = StoreOrderCartInfo::where('oid', $order['id'])->count(); + if (($order['refund_num'] + $refundNum) == $orderProductCount) { + // 全部退款完成,订单状态改为已退款 + $order->refund_status = OrderEnum::REFUND_STATUS_FINISH; + $order->status = OrderEnum::RECEIVED_BACK; + } + $order->refund_price = bcadd($order['refund_price'], $refundAmount, 2); + $order->refund_num += $refundNum; + $order->save(); + } + + /** + * 退佣金 + * @param $order + * @param $storeOrderProducts + * @return void + * @throws DataNotFoundException + * @throws DbException + * @throws ModelNotFoundException + */ + public function refundCommission($order, $storeOrderProducts): void + { + $productIds = array_unique(array_column($storeOrderProducts->toArray(), 'product_id')); + StoreFinanceFlowProduct::where('oid', $order['id'])->whereIn('product_id', $productIds)->update(['number' => 0, 'update_time' => strtotime(time())]); + $village_uid = StoreFinanceFlow::where('order_id', $order['id'])->where('financial_type', 14)->value('other_uid'); + $brigade_uid = StoreFinanceFlow::where('order_id', $order['id'])->where('financial_type', 15)->value('other_uid'); + $transaction_id = StoreFinanceFlow::where('order_id', $order['id'])->value('transaction_id'); + StoreFinanceFlow::where('order_id', $order['id'])->update(['delete_time' => time()]); + CommissionnLogic::setStore($order, $village_uid, $brigade_uid, $transaction_id); + } + +} \ No newline at end of file diff --git a/app/common/service/pay/BalancePay.php b/app/common/service/pay/BalancePay.php new file mode 100644 index 000000000..ab719445b --- /dev/null +++ b/app/common/service/pay/BalancePay.php @@ -0,0 +1,25 @@ +findOrEmpty(); + $capitalFlowDao = new CapitalFlowLogic($user); + $capitalFlowDao->userIncome('now_money_refund', 'system_back', $order['id'], $amount); + $user->now_money = bcadd($user['now_money'], $amount, 2); + $user->save(); + } + +} \ No newline at end of file diff --git a/app/common/service/pay/CashPay.php b/app/common/service/pay/CashPay.php new file mode 100644 index 000000000..6e5b0ad5e --- /dev/null +++ b/app/common/service/pay/CashPay.php @@ -0,0 +1,29 @@ +store_id = $order['store_id'] ?? 0; + $model->cash_price = $order->pay_price; + $model->receivable = $order->pay_price; + $model->remark = '退款'; + $model->type = 1; + $model->status = YesNoEnum::YES; + $model->save(); + } + +} \ No newline at end of file diff --git a/app/common/service/pay/PayTool.php b/app/common/service/pay/PayTool.php new file mode 100644 index 000000000..02cfe7087 --- /dev/null +++ b/app/common/service/pay/PayTool.php @@ -0,0 +1,32 @@ + new WeChatPay($params), + PayEnum::BALANCE_PAY => new BalancePay($params), + PayEnum::PURCHASE_FUNDS => new PurchaseFundPay($params), + default => new CashPay(), + }; + } + + abstract function refund($amount, $order); + +} \ No newline at end of file diff --git a/app/common/service/pay/PurchaseFundPay.php b/app/common/service/pay/PurchaseFundPay.php new file mode 100644 index 000000000..4286e2233 --- /dev/null +++ b/app/common/service/pay/PurchaseFundPay.php @@ -0,0 +1,25 @@ +findOrEmpty(); + $capitalFlowDao = new CapitalFlowLogic($user); + $capitalFlowDao->userIncome('purchase_refund', 'system_back', $order['id'], $amount, '', 1); + $user->purchase_funds = bcadd($user['purchase_funds'], $amount, 2); + $user->save(); + } + +} \ No newline at end of file diff --git a/app/common/service/pay/WeChatPay.php b/app/common/service/pay/WeChatPay.php new file mode 100644 index 000000000..a95986a5e --- /dev/null +++ b/app/common/service/pay/WeChatPay.php @@ -0,0 +1,40 @@ + $order['order_id'], + 'out_refund_no' => 'BO' . time(), + 'amount' => [ + 'refund' => $totalFee, + 'total' => $order['pay_price'], + 'currency' => 'CNY', + ], + ]; + $res = $wechat->wechat->refund($order); + if ($res['status'] == 'PROCESSING') { + return true; + } + return false; + } catch (\Exception $e) { + \support\Log::info($e->extra['message'] ?? $e->getMessage()); + throw new BusinessException($e->extra['message'] ?? $e->getMessage()); + } + } + +} \ No newline at end of file