92 lines
2.7 KiB
PHP
92 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Forms;
|
|
|
|
use App\Admin\Services\OrderService;
|
|
use App\Exceptions\BizException;
|
|
use App\Models\Order;
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Contracts\LazyRenderable;
|
|
use Dcat\Admin\Traits\LazyWidget;
|
|
use Dcat\Admin\Widgets\Form;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
|
|
class OrderReduce extends Form implements LazyRenderable
|
|
{
|
|
use LazyWidget;
|
|
|
|
/**
|
|
* 权限判断,如不需要可以删除此方法
|
|
*
|
|
* @param Model|Authenticatable|HasPermissions|null $user
|
|
*
|
|
* @return bool
|
|
*/
|
|
protected function authorize($user): bool
|
|
{
|
|
return $user->can('dcat.admin.orders.reduce');
|
|
}
|
|
|
|
/**
|
|
* Handle the form request.
|
|
*
|
|
* @param array $input
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function handle(array $input)
|
|
{
|
|
$orderId = $this->payload['id'] ?? 0;
|
|
$order = Order::findOrFail($orderId);
|
|
$reduceAmount = $input['reduce_amount']??0;
|
|
//获取调整价格;
|
|
if ($reduceAmount > $order->total_amount) {
|
|
throw new BizException('订单价格无法上浮');
|
|
}
|
|
if ($reduceAmount == $order->total_amount) {
|
|
throw new BizException('调整价格和订单原价格一致,无需调整');
|
|
}
|
|
$orderService = new OrderService();
|
|
|
|
$reduced = $order->total_amount-$reduceAmount;
|
|
//判断是否在当前操作人调价权限范围内
|
|
$adminUser = Admin::user();
|
|
if (! $adminUser->isAdministrator() && ! $adminUser->inReduceRange($reduced)) {
|
|
throw new BizException('当前调整价格超出权限范围,请重新确认价格。');
|
|
}
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
$orderService->adminReduceOrder($order, $reduceAmount);
|
|
DB::commit();
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
report($th);
|
|
throw new BizException('操作失败:'.$th->getMessage());
|
|
}
|
|
return $this->response()
|
|
->success(__('admin.update_succeeded'))
|
|
->refresh();
|
|
}
|
|
|
|
/**
|
|
* Build a form here.
|
|
*/
|
|
public function form()
|
|
{
|
|
$orderId = $this->payload['id'] ?? 0;
|
|
$order = Order::findOrFail($orderId);
|
|
|
|
$this->currency('reduce_amount', '调整价格')->symbol('¥')->default(0)->customFormat(function ($V) use ($order) {
|
|
return bcdiv($order->total_amount, 100, 2);
|
|
})->saving(function ($reduceAmount) {
|
|
return bcmul($reduceAmount, 100);
|
|
});
|
|
|
|
$this->confirm('是否确认调价?', '该操作不可逆,确认后将更改订单支付价格。');
|
|
|
|
$this->disableResetButton();
|
|
}
|
|
}
|