134 lines
5.4 KiB
PHP
134 lines
5.4 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']);
|
||
//处理同一个商品多次售后问题;//退款退货、退款、换货
|
||
if (in_array($afterSale->type, [AfterSale::TYPE_REFUND_AND_RETURN, AfterSale::TYPE_REFUND, Aftersale::TYPE_CHANGE])) {
|
||
$finishedAfterSale = $afterSale->order->afterSales->filter(function ($items) use ($afterSale) {
|
||
if ($afterSale->order_product_id == $items->order_product_id//同一个售后商品
|
||
&& $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('退款金额不能大于商品实付金额');
|
||
}
|
||
}
|
||
$num = (int) Arr::get($input, 'num', 0) ?: $afterSale->num;
|
||
if ($num > $afterSale->orderProduct->quantity) {
|
||
throw new BizException('调整数量不能大于下单数量');
|
||
}
|
||
//调整换货,补货时amount问题
|
||
$amount = (int) Arr::get($input, 'amount', 0) ?: $afterSale->amount;
|
||
if (in_array($afterSale->type, [Aftersale::TYPE_CHANGE, Aftersale::TYPE_FILL])) {
|
||
//如果调整数量, 则同时调整价格
|
||
if ($num != $afterSale->num) {
|
||
$amount = bcmul(bcdiv($afterSale->amount, $afterSale->num), $num);
|
||
}
|
||
}
|
||
$afterSaleService->verify($afterSale, $input['remarks3'], $amount, $num);
|
||
} 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);
|
||
//可调整数量
|
||
$this->number('num')->value($afterSale->num)->min(1)->max($afterSale->orderProduct->quantity); //获取当前商品可发起的最大数量
|
||
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);
|
||
}
|
||
}
|