93 lines
2.3 KiB
PHP
93 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Forms;
|
|
|
|
use App\Exceptions\BizException;
|
|
use App\Models\Order;
|
|
use App\Services\OrderService;
|
|
use Dcat\Admin\Contracts\LazyRenderable;
|
|
use Dcat\Admin\Traits\LazyWidget;
|
|
use Dcat\Admin\Widgets\Form;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
|
|
class OrderPackage 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.create_package');
|
|
}
|
|
|
|
/**
|
|
* Handle the form request.
|
|
*
|
|
* @param array $input
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function handle(array $input)
|
|
{
|
|
$orderId = $this->payload['id'] ?? 0;
|
|
|
|
$order = Order::findOrFail($orderId);
|
|
try {
|
|
DB::beginTransaction();
|
|
$orderService = new OrderService();
|
|
$orderService->createPackage($order, $input);
|
|
DB::commit();
|
|
} catch (BizException $e) {
|
|
DB::rollBack();
|
|
report($e);
|
|
throw new BizException('操作失败:'.$e->getMessage());
|
|
} 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->text('shipping_company')->required();
|
|
$this->text('shipping_number')->required();
|
|
$this->hasMany('packages', function (Form $form) use ($order) {
|
|
$form->select('order_product_id')->options($order->products()->where('after_sale_state', '<>', 1)->pluck('name', 'id'));
|
|
$form->number('quantity')->min(1);
|
|
});
|
|
|
|
$this->disableResetButton();
|
|
}
|
|
|
|
/**
|
|
* The data of the form.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function default()
|
|
{
|
|
return [
|
|
// 'name' => 'John Doe',
|
|
// 'email' => 'John.Doe@gmail.com',
|
|
];
|
|
}
|
|
}
|