diff --git a/app/Admin/Actions/Grid/DealerOrderRefuse.php b/app/Admin/Actions/Grid/DealerOrderRefuse.php
index f768c386..1b186e2a 100644
--- a/app/Admin/Actions/Grid/DealerOrderRefuse.php
+++ b/app/Admin/Actions/Grid/DealerOrderRefuse.php
@@ -2,73 +2,20 @@
namespace App\Admin\Actions\Grid;
-use App\Enums\DealerOrderStatus;
-use App\Exceptions\BizException;
-use App\Models\DealerOrder;
+use App\Admin\Forms\DealerOrderRefuse as DealerOrderRefuseForm;
use Dcat\Admin\Grid\RowAction;
-use Illuminate\Http\Request;
-use Illuminate\Support\Facades\DB;
-use Throwable;
+use Dcat\Admin\Widgets\Modal;
class DealerOrderRefuse extends RowAction
{
- public function title()
+ public function render()
{
- if ($this->title) {
- return $this->title;
- }
+ $form = DealerOrderRefuseForm::make()->payload(['id' => $this->getKey()]);
- return ' 拒绝收款 ';
- }
-
- /**
- * @param Model|Authenticatable|HasPermissions|null $user
- *
- * @return bool
- */
- protected function authorize($user): bool
- {
- return $user->can('dcat.admin.dealer_orders.paid');
- }
-
- /**
- * Handle the action request.
- *
- * @param Request $request
- *
- * @return Response
- */
- public function handle(Request $request)
- {
- try {
- DB::beginTransaction();
-
- $order = DealerOrder::lockForUpdate()->findOrFail($this->getKey());
-
- if (! $order->isPay()) {
- throw new BizException('无法收款:订单状态异常,请刷新后再试');
- }
-
- $order->update([
- 'status' => DealerOrderStatus::Paying,
- 'pay_time' => null,
- ]);
-
- DB::commit();
- } catch (Throwable $th) {
- DB::rollBack();
- report($th);
- return $this->response()->error('操作失败,'.$th->getMessage())->refresh();
- }
-
- return $this->response()->success('操作成功')->refresh();
- }
-
- /**
- * @return string|array|void
- */
- public function confirm()
- {
- return ['确认未收到此订单货款?', '确认后将无法逆操作'];
+ return Modal::make()
+ ->lg()
+ ->title('拒绝收款')
+ ->body($form)
+ ->button(' 拒绝收款 ');
}
}
diff --git a/app/Admin/Forms/DealerOrderRefuse.php b/app/Admin/Forms/DealerOrderRefuse.php
new file mode 100644
index 00000000..9eef199f
--- /dev/null
+++ b/app/Admin/Forms/DealerOrderRefuse.php
@@ -0,0 +1,80 @@
+payload['id'] ?? null;
+
+ DB::beginTransaction();
+
+ $order = DealerOrder::lockForUpdate()->findOrFail($id);
+
+ if (! $order->isPay()) {
+ throw new BizException('无法收款:订单状态异常,请刷新后再试');
+ }
+
+ $order->update([
+ 'status' => DealerOrderStatus::Paying,
+ 'pay_time' => null,
+ ]);
+
+ $order->refuseLogs()->create([
+ 'reason' => $input['reason'],
+ ]);
+
+ DB::commit();
+ } catch (Throwable $e) {
+ DB::rollBack();
+
+ report($e);
+
+ return $this->response()->error('操作失败,'.$e->getMessage())->refresh();
+ }
+
+ return $this
+ ->response()
+ ->success('操作成功')
+ ->refresh();
+ }
+
+ /**
+ * Build a form here.
+ */
+ public function form()
+ {
+ $this->textarea('reason', '原因')->rules('required|max:255');
+ }
+
+ /**
+ * The data of the form.
+ *
+ * @return array
+ */
+ public function default()
+ {
+ return [
+ 'reason' => '',
+ ];
+ }
+}
diff --git a/app/Endpoint/Api/Http/Controllers/Dealer/OrderController.php b/app/Endpoint/Api/Http/Controllers/Dealer/OrderController.php
index 86bffd74..89deefdd 100644
--- a/app/Endpoint/Api/Http/Controllers/Dealer/OrderController.php
+++ b/app/Endpoint/Api/Http/Controllers/Dealer/OrderController.php
@@ -158,7 +158,12 @@ class OrderController extends Controller
throw new BizException('订单未找到');
}
- return OrderResource::make($order);
+ $refuseLog = $order->isPendinged() ? $order->refuseLogs()->latest('id')->first() : null;
+
+ $data = OrderResource::make($order)->toArray($request);
+ $data['refuse_reason'] = $refuseLog?->reason;
+
+ return response()->json($data);
}
/**
diff --git a/app/Models/DealerOrder.php b/app/Models/DealerOrder.php
index 8af27bef..3fa31eda 100644
--- a/app/Models/DealerOrder.php
+++ b/app/Models/DealerOrder.php
@@ -164,6 +164,11 @@ class DealerOrder extends Model
return $this->belongsTo(User::class, 'consignor_id');
}
+ public function refuseLogs()
+ {
+ return $this->hasMany(DealerOrderRefuseLog::class, 'order_id');
+ }
+
public function isUser($userId)
{
return $this->user_id == $userId;
diff --git a/app/Models/DealerOrderRefuseLog.php b/app/Models/DealerOrderRefuseLog.php
new file mode 100644
index 00000000..6a475d69
--- /dev/null
+++ b/app/Models/DealerOrderRefuseLog.php
@@ -0,0 +1,16 @@
+id();
+ $table->unsignedBigInteger('order_id');
+ $table->string('reason')->nullable()->comment('原因');
+ $table->timestamps();
+
+ $table->index('order_id');
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::dropIfExists('dealer_order_refuse_logs');
+ }
+}