114 lines
4.1 KiB
PHP
114 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Forms;
|
|
|
|
use App\Exceptions\BizException;
|
|
use App\Models\AfterSale;
|
|
use App\Services\AfterSaleService;
|
|
use Dcat\Admin\Contracts\LazyRenderable;
|
|
use Dcat\Admin\Traits\LazyWidget;
|
|
use Dcat\Admin\Widgets\Form;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
|
|
class AfterSaleVerify extends Form implements LazyRenderable
|
|
{
|
|
use LazyWidget;
|
|
|
|
/**
|
|
* 权限判断,如不需要可以删除此方法
|
|
*
|
|
* @param Model|Authenticatable|HasPermissions|null $user
|
|
*
|
|
* @return bool
|
|
*/
|
|
protected function authorize($user): bool
|
|
{
|
|
return $user->can('dcat.admin.after_sales.verify');
|
|
}
|
|
|
|
/**
|
|
* Handle the form request.
|
|
*
|
|
* @param array $input
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function handle(array $input)
|
|
{
|
|
$afterSaleService = new AfterSaleService();
|
|
try {
|
|
DB::beginTransaction();
|
|
$afterSale = AfterSale::where('state', AfterSale::STATE_VERIFY)->findOrFail($this->payload['id']);
|
|
if ($input['state'] == 3) {//审核通过
|
|
$amount = Arr::get($input, 'amount', 0);
|
|
$afterSale->load(['order', 'order.afterSales']);
|
|
$finishedAfterSale = $afterSale->order->afterSales->filter(function ($items) {
|
|
if ($items->state == AfterSale::STATE_FINISH && in_array($items->type, [AfterSale::TYPE_REFUND_AND_RETURN, AfterSale::TYPE_REFUND, Aftersale::TYPE_CHANGE])) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
});
|
|
if ($amount > ($afterSale->orderProduct->total_amount - $finishedAfterSale->sum('amount'))) {
|
|
throw new BizException('退款金额不能大于商品实付金额');
|
|
}
|
|
$afterSaleService->verify($afterSale, $input['remarks3'], (int) Arr::get($input, 'amount', 0));
|
|
} elseif ($input['state'] == 1) {//需要补充资料
|
|
$afterSaleService->backApply($afterSale, $input['remarks1']);
|
|
}
|
|
DB::commit();
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
report($th);
|
|
return $this->response()->error('操作失败:'.$th->getMessage());
|
|
}
|
|
|
|
return $this->response()
|
|
->success(__('admin.update_succeeded'))
|
|
->refresh();
|
|
}
|
|
|
|
/**
|
|
* Build a form here.
|
|
*/
|
|
public function form()
|
|
{
|
|
$id = $this->payload['id'] ?? 0;
|
|
$afterSale = AfterSale::findOrFail($id);
|
|
if (in_array($afterSale->type, [1, 2])) {
|
|
$this->currency('amount')->customFormat(function () use ($afterSale) {
|
|
return bcdiv($afterSale->amount, 100, 2);
|
|
})->saving(function ($amount) {
|
|
return bcmul($amount, 100);
|
|
})->symbol('¥');
|
|
}
|
|
|
|
$this->radio('state')
|
|
->when(3, function (Form $form) use ($afterSale) {
|
|
$defaultRemarks = '';
|
|
switch ($afterSale->type) {
|
|
case 1:
|
|
$defaultRemarks = '同意退款退货, 请确认退款金额,并填入回寄单号';//需要用户确认并填入回寄单号
|
|
break;
|
|
case 2:
|
|
$defaultRemarks = '同意退款,请确认退款金额。';
|
|
break;
|
|
case 3:
|
|
$defaultRemarks = '同意换货,请填入回寄单号';//需要用户确认并填入回寄单号
|
|
break;
|
|
case 4:
|
|
$defaultRemarks = '同意补货';//需要用户收到补货后确认,可后台关闭
|
|
}
|
|
$this->text('remarks3')->value($defaultRemarks);
|
|
})
|
|
->when(1, function (Form $form) {
|
|
$this->text('remarks1')->value('需要补充资料');
|
|
})->options([
|
|
3=>'审核通过',
|
|
1=>'需要补充资料',
|
|
])->default(3);
|
|
}
|
|
}
|