diff --git a/app/Admin/Controllers/DealerOrderController.php b/app/Admin/Controllers/DealerOrderController.php index c23a503a..e8b3d784 100644 --- a/app/Admin/Controllers/DealerOrderController.php +++ b/app/Admin/Controllers/DealerOrderController.php @@ -10,6 +10,7 @@ use App\Admin\Actions\Show\DealerOrderRemark; use App\Admin\Repositories\DealerOrder; use App\Enums\DealerOrderStatus; use App\Models\DealerChannelSubsidyLog; +use App\Models\DealerOrder as DealerOrderModel; use App\Models\DealerOrderProduct; use Dcat\Admin\Admin; use Dcat\Admin\Form; @@ -48,6 +49,12 @@ class DealerOrderController extends AdminController $grid->column('total_amount')->prepend('¥'); $statusTexts = DealerOrderStatus::texts(); + $grid->column('pay_way')->using(DealerOrderModel::$payWayText)->label([ + 'wallet'=>'warning', + 'offline'=>'danger', + 'none'=>'#b3b9bf', + ]); + $grid->column('order_status')->display(function ($v) { return $this->order_status; })->using($statusTexts)->dot([ diff --git a/app/Endpoint/Api/Http/Controllers/Dealer/OrderController.php b/app/Endpoint/Api/Http/Controllers/Dealer/OrderController.php index 72f9e5b9..f77b69ad 100644 --- a/app/Endpoint/Api/Http/Controllers/Dealer/OrderController.php +++ b/app/Endpoint/Api/Http/Controllers/Dealer/OrderController.php @@ -138,8 +138,21 @@ class OrderController extends Controller $input = $request->validate([ 'pay_image' => ['bail', 'string'], + 'pay_way' => ['bail', 'string'], ]); - $orderService->payOrder($order, $input['pay_image'] ?? null); + try { + DB::beginTransaction(); + $orderService->payOrder($order, $input['pay_way'] ?? 'offline', $input['pay_image'] ?? null); + DB::commit(); + } catch (BizException $th) { + DB::rollBack(); + throw $th; + } catch (Throwable $th) { + DB::rollBack(); + report($th); + throw new BizException('操作失败,请刷新后再试'); + } + return response()->noContent(); } @@ -166,7 +179,40 @@ class OrderController extends Controller if (strpos($e->getMessage(), 'Numeric value out of range') !== false) { $e = new BizException('当前可发货库存不足'); } + throw $e; + } catch (Throwable $th) { + DB::rollBack(); + report($th); + throw new BizException('操作失败,请刷新后再试'); + } + return response()->noContent(); + } + /** + * 确认发货 + * + * @param [type] $id + * @param Request $request + * @param OrderService $orderService + * @return void + */ + public function shippingOrder($id, Request $request, OrderService $orderService) + { + $order = DealerOrder::findOrFail($id); + $userId = $request->user()->id; + //不是发货人 + if (!$order->isConsignor($userId)) { + throw new BizException('订单未找到'); + } + try { + DB::beginTransaction(); + $orderService->shippingOrder($order);//确认发货 + DB::commit(); + } catch (QueryException $e) { + DB::rollBack(); + if (strpos($e->getMessage(), 'Numeric value out of range') !== false) { + $e = new BizException('当前可发货库存不足'); + } throw $e; } catch (Throwable $th) { DB::rollBack(); diff --git a/app/Endpoint/Api/routes.php b/app/Endpoint/Api/routes.php index 7da84fbf..0f1412c0 100644 --- a/app/Endpoint/Api/routes.php +++ b/app/Endpoint/Api/routes.php @@ -250,8 +250,10 @@ Route::group([ Route::post('orders/{order}/confirm', [Dealer\OrderController::class, 'confirmOrder']); //确认打款 Route::post('orders/{order}/pay', [Dealer\OrderController::class, 'payOrder']); - //确认收款 + //确认收款+发货 Route::post('orders/{order}/paid', [Dealer\OrderController::class, 'paidOrder']); + //确认发货 + Route::post('orders/{order}/shipping', [Dealer\OrderController::class, 'shippingOrder']); //确认收货 Route::post('orders/{order}/shippinged', [Dealer\OrderController::class, 'shippingedOrder']); //取消订单 diff --git a/app/Enums/DealerWalletAction.php b/app/Enums/DealerWalletAction.php index 9619f5fc..8ab1f585 100644 --- a/app/Enums/DealerWalletAction.php +++ b/app/Enums/DealerWalletAction.php @@ -9,4 +9,6 @@ enum DealerWalletAction: int { case ChannelSubsidyIn = 4; case WithdrawBank = 5; case WithdrawFiled = 6; + case OrderPaid = 7; + case OrderIncome = 8; } diff --git a/app/Models/DealerOrder.php b/app/Models/DealerOrder.php index 6dd699e3..f53485d2 100644 --- a/app/Models/DealerOrder.php +++ b/app/Models/DealerOrder.php @@ -14,6 +14,9 @@ class DealerOrder extends Model use Filterable; use HasDateTimeFormatter; + public const PAY_WAY_WALLET = 'wallet'; // 余额 + public const PAY_WAY_OFFLINE = 'offline'; // 线下支付 + protected $attributes = [ 'status' => DealerOrderStatus::Pending, 'settle_state' => DealerOrderSettleState::Pending, @@ -40,6 +43,7 @@ class DealerOrder extends Model 'consignee_telephone', 'consignee_zone', 'consignee_address', + 'pay_way', 'pay_time', 'paied_time', 'shipping_time', @@ -48,6 +52,11 @@ class DealerOrder extends Model 'remark', ]; + public static $payWayText = [ + self::PAY_WAY_WALLET => '余额支付', + self::PAY_WAY_OFFLINE => '线下打款', + ]; + /** * 仅获取待结算的已付款订单 */ diff --git a/app/Services/Dealer/OrderService.php b/app/Services/Dealer/OrderService.php index 28e68407..32a8529e 100644 --- a/app/Services/Dealer/OrderService.php +++ b/app/Services/Dealer/OrderService.php @@ -4,6 +4,7 @@ namespace App\Services\Dealer; use App\Enums\DealerLvl; use App\Enums\DealerOrderStatus; +use App\Enums\DealerWalletAction; use App\Exceptions\BizException; use App\Models\DealerOrder; use App\Models\DealerOrderAllocateLog; @@ -136,17 +137,41 @@ class OrderService * * @return void */ - public function payOrder(DealerOrder $order, ?string $payImage) + public function payOrder(DealerOrder $order, string $payWay, ?string $payImage) { + if (empty($payWay)) { + throw new BizException('请选择付款方式'); + } if (!$order->isPendinged()) { throw new BizException('订单状态异常,请刷新后再试'); } - $order->update([ - 'status' => DealerOrderStatus::Confirming, - 'pay_image' => $payImage, - 'pay_info' => $order->getConsignorPayInfo() ?? null, - 'pay_time' => now(), - ]); + switch ($payWay) { + case DealerOrder::PAY_WAY_WALLET: + /** 付款以及完成确认收款动作 **/ + $walletService = new WalletService(); + //付款 + $walletService->changeBalance($order->user, 0 - $order->total_amount, DealerWalletAction::OrderPaid, '订单:'.$order->sn, $order); + $order->update([ + 'status'=>DealerOrderStatus::Confirming, + 'pay_time' => now(), + 'pay_way' => DealerOrder::PAY_WAY_WALLET, + ]); + //收款 + if ($order->consignor) { + $walletService->changeBalance($order->consignor, $order->total_amount, DealerWalletAction::OrderIncome, '订单:'.$order->sn, $order); + } + $this->paidOrder($order); + break; + case DealerOrder::PAY_WAY_OFFLINE: + $order->update([ + 'status' => DealerOrderStatus::Confirming, + 'pay_image' => $payImage, + 'pay_info' => $order->getConsignorPayInfo() ?? null, + 'pay_time' => now(), + 'pay_way' => DealerOrder::PAY_WAY_OFFLINE, + ]); + break; + } } /** diff --git a/database/migrations/2022_02_11_163430_add_pay_way_to_dealer_orders_table.php b/database/migrations/2022_02_11_163430_add_pay_way_to_dealer_orders_table.php new file mode 100644 index 00000000..f4684385 --- /dev/null +++ b/database/migrations/2022_02_11_163430_add_pay_way_to_dealer_orders_table.php @@ -0,0 +1,34 @@ +string('pay_way')->nullable()->comment('1线下打款,2余额'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('dealer_orders', function (Blueprint $table) { + // + $table->dropColumn('pay_way'); + }); + } +} diff --git a/resources/lang/zh_CN/dealer-order.php b/resources/lang/zh_CN/dealer-order.php index 2718e254..c42837db 100644 --- a/resources/lang/zh_CN/dealer-order.php +++ b/resources/lang/zh_CN/dealer-order.php @@ -33,6 +33,7 @@ return [ 'consignee_address' => '收货人详细地址', 'pay_info' => '收款信息', 'pay_image' => '打款凭证', + 'pay_way'=>'支付方式', 'pay_time' => '支付时间', 'paied_time' => '确认收款时间', 'shipping_time' => '发货时间',