添加经销商调整库存
parent
64ec251c1f
commit
0f9ce8f098
|
|
@ -0,0 +1,40 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Admin\Actions\Grid;
|
||||||
|
|
||||||
|
use App\Admin\Forms\DealerEditProduct as DealerEditProductForm;
|
||||||
|
use App\Admin\Renderable\DealerUserProductSimpleTable;
|
||||||
|
use Dcat\Admin\Grid\RowAction;
|
||||||
|
use Dcat\Admin\Widgets\Modal;
|
||||||
|
|
||||||
|
class DealerEditProduct extends RowAction
|
||||||
|
{
|
||||||
|
public function title()
|
||||||
|
{
|
||||||
|
if ($this->title) {
|
||||||
|
return $this->title;
|
||||||
|
}
|
||||||
|
return '<i class="feather grid-action-icon icon-package"></i> 修改库存 ';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,6 +4,7 @@ namespace App\Admin\Controllers;
|
||||||
|
|
||||||
use App\Admin\Actions\Grid\DealerBonds;
|
use App\Admin\Actions\Grid\DealerBonds;
|
||||||
use App\Admin\Actions\Grid\DealerEditLvl;
|
use App\Admin\Actions\Grid\DealerEditLvl;
|
||||||
|
use App\Admin\Actions\Grid\DealerEditProduct;
|
||||||
use App\Admin\Renderable\DealerEarningSimpleTable;
|
use App\Admin\Renderable\DealerEarningSimpleTable;
|
||||||
use App\Admin\Renderable\DealerUserProductLogSimpleTable;
|
use App\Admin\Renderable\DealerUserProductLogSimpleTable;
|
||||||
use App\Admin\Renderable\DealerWalletLogSimpleTable;
|
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')) {
|
if ($actions->row->lvl->value >= DealerLvl::Special->value && Admin::user()->can('dcat.admin.dealers.bonds')) {
|
||||||
$actions->append(new DealerBonds());
|
$actions->append(new DealerBonds());
|
||||||
}
|
}
|
||||||
|
$actions->append((new DealerEditProduct())->setKey($actions->row->user_id));
|
||||||
});
|
});
|
||||||
|
|
||||||
$grid->filter(function (Grid\Filter $filter) {
|
$grid->filter(function (Grid\Filter $filter) {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,102 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Admin\Forms;
|
||||||
|
|
||||||
|
use App\Exceptions\BizException;
|
||||||
|
use App\Models\DealerProduct;
|
||||||
|
use App\Models\DealerUserProduct;
|
||||||
|
use App\Models\DealerUserProductLog;
|
||||||
|
use Dcat\Admin\Contracts\LazyRenderable;
|
||||||
|
use Dcat\Admin\Traits\LazyWidget;
|
||||||
|
use Dcat\Admin\Widgets\Form;
|
||||||
|
use Illuminate\Database\QueryException;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
class DealerEditProduct extends Form implements LazyRenderable
|
||||||
|
{
|
||||||
|
use LazyWidget;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Model|Authenticatable|HasPermissions|null $user
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function authorize($user): bool
|
||||||
|
{
|
||||||
|
return $user->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('调整库存');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -19,7 +19,10 @@ class DealerUserProductLogSimpleTable extends LazyRenderable
|
||||||
$grid->column('product.name', '商品名称');
|
$grid->column('product.name', '商品名称');
|
||||||
$grid->column('remark', '备注');
|
$grid->column('remark', '备注');
|
||||||
$grid->column('qty', '变动数量')->display(function () {
|
$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', '创建时间');
|
$grid->column('created_at', '创建时间');
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ class DealerUserProductSimpleTable extends LazyRenderable
|
||||||
{
|
{
|
||||||
public function grid(): Grid
|
public function grid(): Grid
|
||||||
{
|
{
|
||||||
$userId = $this->payload['id']??0;
|
$userId = $this->payload['id'] ?? 0;
|
||||||
$builder = DealerUserProduct::query();
|
$builder = DealerUserProduct::query();
|
||||||
$builder->with(['product'])->where('user_id', $userId);
|
$builder->with(['product'])->where('user_id', $userId);
|
||||||
return Grid::make($builder, function (Grid $grid) {
|
return Grid::make($builder, function (Grid $grid) {
|
||||||
|
|
@ -18,10 +18,6 @@ class DealerUserProductSimpleTable extends LazyRenderable
|
||||||
$grid->column('product.cover', '商品封面')->image(80, 80);
|
$grid->column('product.cover', '商品封面')->image(80, 80);
|
||||||
$grid->column('stock', '剩余库存');
|
$grid->column('stock', '剩余库存');
|
||||||
$grid->column('created_at', '创建时间');
|
$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->withBorder();
|
||||||
$grid->model()->orderBy('created_at', 'desc');
|
$grid->model()->orderBy('created_at', 'desc');
|
||||||
$grid->disableRefreshButton();
|
$grid->disableRefreshButton();
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,8 @@ class DealerUserProductLog extends Model
|
||||||
public const TYPE_ORDER_IN = 1; //采购加库存
|
public const TYPE_ORDER_IN = 1; //采购加库存
|
||||||
public const TYPE_ORDER_OUT = 2; //发货扣库存
|
public const TYPE_ORDER_OUT = 2; //发货扣库存
|
||||||
public const TYPE_OFFLINE_OUT = 3;//线下去库存
|
public const TYPE_OFFLINE_OUT = 3;//线下去库存
|
||||||
|
public const TYPE_ADMIN_IN = 4;//后台添加库存
|
||||||
|
public const TYPE_ADMIN_OUT = 5;//后台扣减库存
|
||||||
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'user_id',
|
'user_id',
|
||||||
|
|
|
||||||
|
|
@ -313,6 +313,7 @@ class AdminPermissionSeeder extends Seeder
|
||||||
'children'=>[
|
'children'=>[
|
||||||
'edit_lvl'=>['name' =>'修改经销商等级'],
|
'edit_lvl'=>['name' =>'修改经销商等级'],
|
||||||
'bonds'=>['name' =>'填写保证金'],
|
'bonds'=>['name' =>'填写保证金'],
|
||||||
|
'edit_product'=>['name' =>'调整库存'],
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
'dealer_products'=>[
|
'dealer_products'=>[
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue