242 lines
9.1 KiB
PHP
242 lines
9.1 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use App\Admin\Actions\Grid\SkuList;
|
|
use App\Admin\Renderable\ProductSkuTable;
|
|
use App\Admin\Repositories\ProductSpu;
|
|
use App\Exceptions\BizException;
|
|
use App\Models\ProductBuynote;
|
|
use App\Models\ProductFeature;
|
|
use App\Models\ProductGroup;
|
|
use App\Models\ProductCategory;
|
|
use App\Models\ProductSpu as ProductSpuModel;
|
|
use App\Models\ShippingTemplate;
|
|
use Carbon\Carbon;
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Form;
|
|
use Dcat\Admin\Grid;
|
|
use Dcat\Admin\Http\Controllers\AdminController;
|
|
use Dcat\Admin\Layout\Content;
|
|
use Dcat\Admin\Show;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class ProductSpuController extends AdminController
|
|
{
|
|
/**
|
|
* Make a grid builder.
|
|
*
|
|
* @return Grid
|
|
*/
|
|
protected function grid()
|
|
{
|
|
return Grid::make(new ProductSpu(), function (Grid $grid) {
|
|
$grid->column('id')->sortable();
|
|
$grid->column('name');
|
|
$grid->column('subtitle');
|
|
// $grid->column('cover')->image(80, 80);
|
|
$grid->column('sell_price')->display(function ($value) {
|
|
return bcdiv($value, 100, 2);
|
|
})->prepend('¥');
|
|
$grid->column('market_price')->display(function ($value) {
|
|
return bcdiv($value, 100, 2);
|
|
})->prepend('¥');
|
|
$grid->column('cost_price')->display(function ($value) {
|
|
return bcdiv($value, 100, 2);
|
|
})->prepend('¥');
|
|
$grid->column('vip_price')->display(function ($value) {
|
|
if (is_null($value)) {
|
|
return '未设置';
|
|
}
|
|
return '¥'.bcdiv($value, 100, 2);
|
|
});
|
|
$grid->column('weight');
|
|
$grid->column('sales_value');
|
|
$grid->column('created_at')->sortable();
|
|
|
|
//排序
|
|
$grid->model()->orderBy('created_at', 'desc');
|
|
|
|
/** 操作 **/
|
|
//新增
|
|
if (Admin::user()->can('dcat.admin.product_spus.create')) {
|
|
$grid->disableCreateButton(false);
|
|
}
|
|
//修改
|
|
// $grid->showQuickEditButton(Admin::user()->can('dcat.admin.product_spus.edit'));
|
|
//删除以及自定义操作
|
|
$grid->actions(function (Grid\Displayers\Actions $actions) {
|
|
$actions->disableEdit(Admin::user()->cannot('dcat.admin.product_spus.edit'));
|
|
$actions->disableDelete(Admin::user()->cannot('dcat.admin.product_spus.destroy'));
|
|
if (Admin::user()->can('dcat.admin.product_spus.sku_list')) {
|
|
$actions->prepend('<a href="'.admin_route('product_spus.sku_list', ['spu' =>$actions->row]).'">
|
|
<i class="feather icon-list grid-action-icon"></i> '.__('admin.list').
|
|
'</a>');
|
|
// $actions->prepend(new SkuList());
|
|
}
|
|
});
|
|
|
|
$grid->tools(function (Grid\Tools $tools) {
|
|
// Excel导入
|
|
$tools->append(new \App\Admin\Actions\Modal\ProductImport());
|
|
$tools->append(new \App\Admin\Actions\Grid\ProductDownloadTemplate());
|
|
});
|
|
|
|
/** 查询 **/
|
|
$grid->filter(function (Grid\Filter $filter) {
|
|
$filter->panel();
|
|
$filter->like('name')->width(3);
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a show builder.
|
|
*
|
|
* @param mixed $id
|
|
*
|
|
* @return Show
|
|
*/
|
|
protected function detail($id)
|
|
{
|
|
return Show::make($id, new ProductSpu(), function (Show $show) {
|
|
$show->field('id');
|
|
$show->field('name');
|
|
$show->field('subtitle');
|
|
$show->field('cover');
|
|
$show->field('images');
|
|
$show->field('description');
|
|
$show->field('sell_price');
|
|
$show->field('market_price');
|
|
$show->field('cost_price');
|
|
$show->field('vip_price');
|
|
$show->field('media');
|
|
$show->field('weight');
|
|
$show->field('attrs');
|
|
$show->field('created_at');
|
|
$show->field('updated_at');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form()
|
|
{
|
|
$builder = ProductSpu::with(['features']);
|
|
return Form::make($builder, function (Form $form) {
|
|
$form->display('id');
|
|
|
|
if ($form->isCreating()) {
|
|
// $form->select('one_category')->options(admin_route('api.product_categories'))->load('two_category', admin_route('api.product_categories'));
|
|
// $form->select('two_category')->load('category_id', admin_route('api.product_categories'));
|
|
$form->select('category_id')->options(admin_route('api.product_categories', ['level' => 2]))->required();
|
|
}
|
|
$form->text('name')->required();
|
|
$form->text('subtitle');
|
|
$form->divider();
|
|
$form->image('cover')
|
|
->move('product-spus/cover/'.Carbon::now()->toDateString())
|
|
->saveFullUrl()
|
|
->removable(false)
|
|
->retainable()
|
|
->autoUpload();
|
|
$form->multipleImage('images')
|
|
->move('product-spus/'.Carbon::now()->toDateString())
|
|
->saveFullUrl()
|
|
->removable(false)
|
|
->retainable()
|
|
->autoUpload();
|
|
$form->file('media')->chunked()
|
|
->accept('mp4', 'mp4/*')
|
|
->move('prduct-spu-medias/'.Carbon::now()->toDateString())
|
|
->maxSize(204800)//默认最大200M
|
|
->saveFullUrl()
|
|
->removable(false)
|
|
->retainable()
|
|
->autoSave(false)
|
|
->autoUpload();
|
|
$form->multipleSelect('features')->options(ProductFeature::all()->pluck('name', 'id'))->customFormat(function ($v) {
|
|
if (! $v) {
|
|
return [];
|
|
}
|
|
// 从数据库中查出的二维数组中转化成ID
|
|
return array_column($v, 'id');
|
|
});
|
|
$form->editor('description');
|
|
$form->select('buynote_id')->options(ProductBuynote::all()->pluck('name', 'id'));
|
|
$form->number('weight')->min(0)->default(0);
|
|
$form->select('shipping_template_id')->options(ShippingTemplate::all()->pluck('name', 'id'))->required();
|
|
$form->number('stock')->min(0)->default(0);
|
|
$form->divider();
|
|
$form->currency('sell_price')->symbol('¥')->default(0)->customFormat(function ($sellPrice) {
|
|
return bcdiv($sellPrice, 100, 2);
|
|
})->saving(function ($sellPrice) {
|
|
return bcmul($sellPrice, 100);
|
|
});
|
|
$form->currency('market_price')->symbol('¥')->default(0)->customFormat(function ($marketPrice) {
|
|
return bcdiv($marketPrice, 100, 2);
|
|
})->saving(function ($marketPrice) {
|
|
return bcmul($marketPrice, 100);
|
|
});
|
|
$form->currency('cost_price')->symbol('¥')->default(0)->customFormat(function ($costPrice) {
|
|
return bcdiv($costPrice, 100, 2);
|
|
})->saving(function ($costPrice) {
|
|
return bcmul($costPrice, 100);
|
|
});
|
|
$form->currency('vip_price')->symbol('¥')->customFormat(function ($vipPrice) {
|
|
if (is_null($vipPrice)) {
|
|
return null;
|
|
}
|
|
return bcdiv($vipPrice, 100, 2);
|
|
})->saving(function ($vipPrice) {
|
|
if (is_null($vipPrice)) {
|
|
return null;
|
|
}
|
|
return bcmul($vipPrice, 100);
|
|
});
|
|
$form->currency('sales_value')->symbol('@')->default(0)->saving(function ($salesValue) {
|
|
if (is_null($salesValue)) {
|
|
return 0;
|
|
}
|
|
return $salesValue;
|
|
});
|
|
$form->divider();
|
|
$form->select('attr_group')->options(ProductGroup::all()->pluck('name', 'id'));
|
|
$form->selectAttr('attrs')->listen('attr_group');
|
|
if ($form->isCreating()) {
|
|
$form->selectSpec('specs')->listen('attr_group');
|
|
}
|
|
|
|
$form->ignore(['one_category', 'two_category', 'attr_group']);
|
|
$form->display('created_at');
|
|
$form->display('updated_at');
|
|
});
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
//如果含有子sku
|
|
$spu = ProductSpuModel::findOrFail($id);
|
|
if ($spu->hasSku()) {
|
|
throw new BizException(__('product-spu.options.deny_message'));
|
|
}
|
|
|
|
return parent::destroy($id);
|
|
}
|
|
|
|
/**
|
|
* Undocumented function
|
|
*
|
|
* @return void
|
|
*/
|
|
public function skuList(Content $content, ProductSpuModel $spu)
|
|
{
|
|
return $content->header(__('product-spu.labels.ProductSpu'))
|
|
->description($spu->name)
|
|
->body(ProductSkuTable::grid($spu->id));
|
|
}
|
|
}
|