From 0f9ce8f0986c0b81500e95868bd2c84a5ae1b8fc Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Fri, 11 Feb 2022 11:13:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BB=8F=E9=94=80=E5=95=86?= =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=BA=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/Actions/Grid/DealerEditProduct.php | 40 +++++++ app/Admin/Controllers/DealerController.php | 2 + app/Admin/Forms/DealerEditProduct.php | 102 ++++++++++++++++++ .../DealerUserProductLogSimpleTable.php | 5 +- .../DealerUserProductSimpleTable.php | 6 +- app/Models/DealerUserProductLog.php | 2 + database/seeders/AdminPermissionSeeder.php | 1 + 7 files changed, 152 insertions(+), 6 deletions(-) create mode 100644 app/Admin/Actions/Grid/DealerEditProduct.php create mode 100644 app/Admin/Forms/DealerEditProduct.php diff --git a/app/Admin/Actions/Grid/DealerEditProduct.php b/app/Admin/Actions/Grid/DealerEditProduct.php new file mode 100644 index 00000000..86a583b4 --- /dev/null +++ b/app/Admin/Actions/Grid/DealerEditProduct.php @@ -0,0 +1,40 @@ +title) { + return $this->title; + } + return ' 修改库存  '; + } + + /** + * @param Model|Authenticatable|HasPermissions|null $user + * + * @return bool + */ + protected function authorize($user): bool + { + return $user->can('dcat.admin.dealers.edit_product'); + } + + public function render() + { + $form = DealerEditProductForm::make()->payload(['id'=>$this->primaryKey]); + // $grid = DealerUserProductSimpleTable::make(['id'=>$this->primaryKey]); + return Modal::make() + ->lg() + ->title($this->title()) + ->body($form) + ->button($this->title()); + } +} diff --git a/app/Admin/Controllers/DealerController.php b/app/Admin/Controllers/DealerController.php index 30617675..42c0251c 100644 --- a/app/Admin/Controllers/DealerController.php +++ b/app/Admin/Controllers/DealerController.php @@ -4,6 +4,7 @@ namespace App\Admin\Controllers; use App\Admin\Actions\Grid\DealerBonds; use App\Admin\Actions\Grid\DealerEditLvl; +use App\Admin\Actions\Grid\DealerEditProduct; use App\Admin\Renderable\DealerEarningSimpleTable; use App\Admin\Renderable\DealerUserProductLogSimpleTable; use App\Admin\Renderable\DealerWalletLogSimpleTable; @@ -81,6 +82,7 @@ class DealerController extends AdminController if ($actions->row->lvl->value >= DealerLvl::Special->value && Admin::user()->can('dcat.admin.dealers.bonds')) { $actions->append(new DealerBonds()); } + $actions->append((new DealerEditProduct())->setKey($actions->row->user_id)); }); $grid->filter(function (Grid\Filter $filter) { diff --git a/app/Admin/Forms/DealerEditProduct.php b/app/Admin/Forms/DealerEditProduct.php new file mode 100644 index 00000000..c0016985 --- /dev/null +++ b/app/Admin/Forms/DealerEditProduct.php @@ -0,0 +1,102 @@ +can('dcat.admin.dealers.edit_product'); + } + + /** + * Handle the form request. + * + * @param array $input + * + * @return mixed + */ + public function handle(array $input) + { + $id = $this->payload['id'] ?? 0; + try { + DB::beginTransaction(); + $product = DealerUserProduct::where([ + 'user_id' => $id, + 'product_id'=>$input['product_id'], + ])->first(); + switch ($input['type']) { + case DealerUserProductLog::TYPE_ADMIN_IN: + if (!$product) { + $product = DealerUserProduct::create([ + 'user_id' => $id, + 'product_id'=>$input['product_id'], + ]); + } + $product->increment('stock', $input['qty']); + break; + case DealerUserProductLog::TYPE_ADMIN_OUT: + if (!$product) { + throw new BizException('库存不足'); + } + $product->decrement('stock', $input['qty']); + break; + } + DealerUserProductLog::create([ + 'user_id'=> $id, + 'product_id'=> $input['product_id'], + 'type' => $input['type'], + 'qty'=>$input['qty'], + 'remark'=>'后台改动:'.$input['remark'] ?? null, + ]); + DB::commit(); + } catch (QueryException $e) { + DB::rollBack(); + if (strpos($e->getMessage(), 'Numeric value out of range') !== false) { + $e = new BizException('库存不足'); + } + throw $e; + } 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() + { + $this->radio('type', '操作')->options([ + DealerUserProductLog::TYPE_ADMIN_IN =>'添加', + DealerUserProductLog::TYPE_ADMIN_OUT=>'扣减', + ])->required(); + $this->select('product_id', '商品')->options(DealerProduct::all()->pluck('name', 'id')) + ->required(); + $this->number('qty', '数量')->default(1)->min(1); + $this->text('remark', '备注')->default('调整库存'); + } +} diff --git a/app/Admin/Renderable/DealerUserProductLogSimpleTable.php b/app/Admin/Renderable/DealerUserProductLogSimpleTable.php index 9b3ca026..a0500170 100644 --- a/app/Admin/Renderable/DealerUserProductLogSimpleTable.php +++ b/app/Admin/Renderable/DealerUserProductLogSimpleTable.php @@ -19,7 +19,10 @@ class DealerUserProductLogSimpleTable extends LazyRenderable $grid->column('product.name', '商品名称'); $grid->column('remark', '备注'); $grid->column('qty', '变动数量')->display(function () { - return ($this->type == DealerUserProductLog::TYPE_ORDER_IN ? '+' : '-').$this->qty.$this->product?->unit; + return (in_array($this->type, [ + DealerUserProductLog::TYPE_ORDER_IN, + DealerUserProductLog::TYPE_ADMIN_IN, + ]) ? '+' : '-').$this->qty.$this->product?->unit; }); $grid->column('created_at', '创建时间'); diff --git a/app/Admin/Renderable/DealerUserProductSimpleTable.php b/app/Admin/Renderable/DealerUserProductSimpleTable.php index b04f216d..dc8e9386 100644 --- a/app/Admin/Renderable/DealerUserProductSimpleTable.php +++ b/app/Admin/Renderable/DealerUserProductSimpleTable.php @@ -10,7 +10,7 @@ class DealerUserProductSimpleTable extends LazyRenderable { public function grid(): Grid { - $userId = $this->payload['id']??0; + $userId = $this->payload['id'] ?? 0; $builder = DealerUserProduct::query(); $builder->with(['product'])->where('user_id', $userId); return Grid::make($builder, function (Grid $grid) { @@ -18,10 +18,6 @@ class DealerUserProductSimpleTable extends LazyRenderable $grid->column('product.cover', '商品封面')->image(80, 80); $grid->column('stock', '剩余库存'); $grid->column('created_at', '创建时间'); - $grid->column('logs', '库存记录')->display('查看')->modal(function ($modal) { - $modal->title('库存记录'); - return DealerUserProductLogSimpleTable::make(['product_id'=>$this->product_id, 'user_id'=> $this->user_id]); - }); // $grid->withBorder(); $grid->model()->orderBy('created_at', 'desc'); $grid->disableRefreshButton(); diff --git a/app/Models/DealerUserProductLog.php b/app/Models/DealerUserProductLog.php index 8aed53cc..59e5eef8 100644 --- a/app/Models/DealerUserProductLog.php +++ b/app/Models/DealerUserProductLog.php @@ -16,6 +16,8 @@ class DealerUserProductLog extends Model public const TYPE_ORDER_IN = 1; //采购加库存 public const TYPE_ORDER_OUT = 2; //发货扣库存 public const TYPE_OFFLINE_OUT = 3;//线下去库存 + public const TYPE_ADMIN_IN = 4;//后台添加库存 + public const TYPE_ADMIN_OUT = 5;//后台扣减库存 protected $fillable = [ 'user_id', diff --git a/database/seeders/AdminPermissionSeeder.php b/database/seeders/AdminPermissionSeeder.php index 31b99dea..7d479f8e 100644 --- a/database/seeders/AdminPermissionSeeder.php +++ b/database/seeders/AdminPermissionSeeder.php @@ -313,6 +313,7 @@ class AdminPermissionSeeder extends Seeder 'children'=>[ 'edit_lvl'=>['name' =>'修改经销商等级'], 'bonds'=>['name' =>'填写保证金'], + 'edit_product'=>['name' =>'调整库存'], ], ], 'dealer_products'=>[