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 1/2] =?UTF-8?q?=E8=B0=83=E8=AF=95=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E9=80=80=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 7ef4bdac..6472fa03 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 525ebac1..5b436418 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 e373c780..c793e904 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 a3c407b1..c1bcba2e 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 00000000..4d5d2752 --- /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 00000000..ab719445 --- /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 00000000..6e5b0ad5 --- /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 00000000..02cfe708 --- /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 00000000..4286e223 --- /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 00000000..a95986a5 --- /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 From 3ff4f3b5b0a39c620106eeabcbbdcf16c2407b44 Mon Sep 17 00:00:00 2001 From: lewis <604446095@qq.com> Date: Mon, 6 Jan 2025 17:15:58 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=B0=83=E6=95=B4=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E9=80=80=E6=AC=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/logic/StoreFinanceFlowLogic.php | 20 +++----- app/common/service/RefundOrderService.php | 58 +++++++++++++++++----- 2 files changed, 52 insertions(+), 26 deletions(-) diff --git a/app/common/logic/StoreFinanceFlowLogic.php b/app/common/logic/StoreFinanceFlowLogic.php index e1fabed7..e5c3d4c8 100644 --- a/app/common/logic/StoreFinanceFlowLogic.php +++ b/app/common/logic/StoreFinanceFlowLogic.php @@ -148,24 +148,16 @@ class StoreFinanceFlowLogic extends BaseLogic { $store = SystemStore::where('id', $store_id)->find(); $capitalFlowDao = new CapitalFlowLogic($store, 'store'); - if ($money > 0) { - //判断是否是押金 - if($store['paid_deposit']<$store['security_deposit']){ - StoreFinanceFlow::where(['order_id' => $order_id, 'financial_type' => 11])->update(['status' => 1,'number'=>$money]); - StoreFinanceFlow::where(['order_id' => $order_id, 'financial_type' => 2])->update(['status' => 1,'number'=>0]); - - $capitalFlowDao->storeIncome('store_paid_deposit_add', 'order', $order_id, $money,'','paid_deposit'); - SystemStore::where('id', $store_id)->inc('paid_deposit', $money)->update(); - }else{ - $capitalFlowDao->storeIncome('store_money_add', 'order', $order_id, $money,'','store_money'); - SystemStore::where('id', $store_id)->inc('store_money', $money)->update(); - } - } - if ($deposit > 0) { + if ($deposit > 0 && $store['paid_deposit'] < $store['security_deposit']) { StoreFinanceFlow::where(['order_id' => $order_id, 'financial_type' => 11])->update(['status' => 1]); $capitalFlowDao->storeIncome('store_paid_deposit_add', 'order', $order_id, $deposit,'','paid_deposit'); SystemStore::where('id', $store_id)->inc('paid_deposit', $deposit)->update(); } + if ($money > 0) { + StoreFinanceFlow::where(['order_id' => $order_id, 'financial_type' => 2])->update(['status' => 1]); + $capitalFlowDao->storeIncome('store_money_add', 'order', $order_id, $money,'','store_money'); + SystemStore::where('id', $store_id)->inc('store_money', $money)->update(); + } $find = StoreFinanceFlow::where(['order_id' => $order_id, 'financial_pm' => 1, 'financial_type' => 16, 'status' => 0])->find(); StoreFinanceFlow::where(['order_id' => $order_id, 'financial_type' => 16])->update(['status' => 1]); if ($find) { diff --git a/app/common/service/RefundOrderService.php b/app/common/service/RefundOrderService.php index 4d5d2752..83fe6044 100644 --- a/app/common/service/RefundOrderService.php +++ b/app/common/service/RefundOrderService.php @@ -3,12 +3,15 @@ namespace app\common\service; use app\common\enum\OrderEnum; +use app\common\logic\CapitalFlowLogic; use app\common\logic\CommissionnLogic; use app\common\model\store_branch_product\StoreBranchProduct; use app\common\model\store_finance_flow\StoreFinanceFlow; use app\common\model\store_finance_flow_product\StoreFinanceFlowProduct; use app\common\model\store_order\StoreOrder; use app\common\model\store_order_cart_info\StoreOrderCartInfo; +use app\common\model\system_store\SystemStore; +use app\common\model\user\User; use app\common\service\pay\PayTool; use Exception; use support\exception\BusinessException; @@ -28,21 +31,17 @@ class RefundOrderService 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('订单商品状态异常'); - } + if ($order->status == 2 && $order->is_writeoff == 1) { + $this->refundStoreMoney($order); + $this->refundCommission($order); } $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); + $this->resetStoreFinanceFlow($order, $orderCartProducts); Db::commit(); } catch (Exception $e) { Db::rollback(); @@ -59,9 +58,7 @@ class RefundOrderService { $amount = '0.00'; foreach ($orderCartProducts as $orderCartProduct) { - if ($orderCartProduct['is_pay'] == 1 && $orderCartProduct['status'] == StoreOrderCartInfo::StatusWait) { - $amount = bcadd($amount, $orderCartProduct['total_price'], 2); - } + $amount = bcadd($amount, $orderCartProduct['total_price'], 2); } return $amount; } @@ -143,7 +140,7 @@ class RefundOrderService * @throws DbException * @throws ModelNotFoundException */ - public function refundCommission($order, $storeOrderProducts): void + public function resetStoreFinanceFlow($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())]); @@ -154,4 +151,41 @@ class RefundOrderService CommissionnLogic::setStore($order, $village_uid, $brigade_uid, $transaction_id); } + /** + * 扣除店铺余额和押金 + * @param $order + * @return void + * @throws DataNotFoundException + * @throws DbException + * @throws ModelNotFoundException + */ + public function refundStoreMoney($order): void + { + $margin = StoreFinanceFlow::where('order_id', $order['id'])->where('financial_type', OrderEnum::ORDER_MARGIN)->value('number'); + $income = StoreFinanceFlow::where('order_id', $order['id'])->where('financial_type', OrderEnum::MERCHANT_ORDER_OBTAINS)->value('number'); + if (empty($margin) && empty($income)) { + return; + } + $store = SystemStore::where('id', $order['store_id'])->find(); + if ($margin > 0) { + $store->paid_deposit = bcsub($store->paid_deposit, $margin, 2); + } + if ($income > 0) { + $store->store_money = bcsub($store->store_money, $income, 2); + } + $store->save(); + } + + public function refundCommission($order) + { + $list = StoreFinanceFlow::where(['order_id' => $order['id'], 'financial_pm' => 1, 'status' => 0])->where('other_uid', '>', 0)->select(); + foreach ($list as $v) { + $v->save(['status' => 1]); + $find = User::where('id', $v['other_uid'])->find(); + $capitalFlowDao = new CapitalFlowLogic($find); + $capitalFlowDao->userExpense('system_balance_sub', 'order', $order['id'], $v['number']); + $find->dec('now_money', $v['number'])->update(); + } + } + } \ No newline at end of file