系统订单拒绝收款
parent
07ac327982
commit
0e53eb73d3
|
|
@ -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 '<i class="feather icon-x-circle grid-action-icon"></i> 拒绝收款 ';
|
||||
}
|
||||
|
||||
/**
|
||||
* @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('<i class="feather icon-x-circle grid-action-icon"></i> 拒绝收款 ');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,80 @@
|
|||
<?php
|
||||
|
||||
namespace App\Admin\Forms;
|
||||
|
||||
use App\Enums\DealerOrderStatus;
|
||||
use App\Exceptions\BizException;
|
||||
use App\Models\DealerOrder;
|
||||
use Dcat\Admin\Traits\LazyWidget;
|
||||
use Dcat\Admin\Widgets\Form;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Throwable;
|
||||
|
||||
class DealerOrderRefuse extends Form
|
||||
{
|
||||
use LazyWidget;
|
||||
|
||||
/**
|
||||
* Handle the form request.
|
||||
*
|
||||
* @param array $input
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle(array $input)
|
||||
{
|
||||
try {
|
||||
$id = $this->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' => '',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class DealerOrderRefuseLog extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'order_id',
|
||||
'reason',
|
||||
];
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateDealerOrderRefuseLogsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('dealer_order_refuse_logs', function (Blueprint $table) {
|
||||
$table->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');
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue