You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
415 lines
17 KiB
415 lines
17 KiB
<?php
|
|
|
|
namespace App\AdminSupplier\Controllers;
|
|
|
|
use App\AdminSupplier\Actions\Grid\ExportToExcelButton;
|
|
use App\AdminSupplier\Actions\Grid\ImportFromExcelButton;
|
|
use App\AdminSupplier\Extensions\Form\Spec;
|
|
use App\AdminSupplier\Forms\ExcelImport;
|
|
use App\AdminSupplier\Repositories\Product;
|
|
use App\Common\OrderStatus;
|
|
use App\Common\ProductStatus;
|
|
use App\Jobs\ProductSpecSync;
|
|
use App\Models\AgentProductSpec;
|
|
use App\Models\Category;
|
|
use App\Models\DiyForm;
|
|
use App\Models\Order;
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Form;
|
|
use Dcat\Admin\Form\NestedForm;
|
|
use Dcat\Admin\Grid;
|
|
use Dcat\Admin\Layout\Content;
|
|
use Dcat\Admin\Show;
|
|
use Dcat\Admin\Http\Controllers\AdminController;
|
|
use Dcat\Admin\Widgets\Card;
|
|
|
|
class ProductController extends AdminController
|
|
{
|
|
protected $title = '产品';
|
|
|
|
public function import(Content $content): Content
|
|
{
|
|
return $content->title('从Excel导入产品')->body(new Card(new ExcelImport()));
|
|
}
|
|
|
|
/**
|
|
* Make a grid builder.
|
|
*
|
|
* @return Grid
|
|
*/
|
|
protected function grid()
|
|
{
|
|
return Grid::make(new Product(['category:id,name']), function (Grid $grid) {
|
|
$grid->model()->where('supplier_id', Admin::user()->id);
|
|
|
|
$grid->tools(new ImportFromExcelButton());
|
|
$grid->tools(new ExportToExcelButton());
|
|
// $grid->export(new ProductToExcelExporter);
|
|
|
|
$category_id = request()->input('cid');
|
|
if ($category_id) {
|
|
$grid->model()->whereIn('category_id', [$category_id, ...$this->get_category_child_ids($category_id)]);
|
|
}
|
|
|
|
$grid->column('id')->sortable();
|
|
$grid->column('type')->using(admin_trans('product.options.publish_type'));
|
|
$grid->column('category.name', '产品分类');
|
|
$grid->column('title')->limit(15);
|
|
$grid->column('picture')->image('', 60, 60);
|
|
$grid->column('price');
|
|
$grid->column('original_price');
|
|
$grid->column('stock');
|
|
$grid->column('sale');
|
|
$grid->column('status')->help('切换开关可改变上下架状态')
|
|
->if(fn() => in_array($this->status, [ProductStatus::SOLD_OUT, ProductStatus::ON_SALE]))
|
|
->using([ProductStatus::SOLD_OUT => 0, ProductStatus::ON_SALE => 1])
|
|
->switch()
|
|
->else()
|
|
->using(ProductStatus::array())
|
|
->dot([
|
|
ProductStatus::ON_SALE => 'success',
|
|
ProductStatus::UNAUDITED => '',
|
|
ProductStatus::REFUSE => 'danger',
|
|
ProductStatus::SOLD_OUT => 'warning',
|
|
], 'primary');
|
|
$grid->column('single_deposit');
|
|
$grid->column('verify_mobile','核销员手机');
|
|
$grid->column('created_at');
|
|
$grid->column('updated_at');
|
|
|
|
$grid->filter(function (Grid\Filter $filter) {
|
|
$filter->panel();
|
|
|
|
$filter->equal('id')->width(2);
|
|
$filter->like('title')->width(3);
|
|
$filter->equal('status')->select(ProductStatus::array())->width(2);
|
|
|
|
$options = array_slice(Category::selectOptions(fn($query) => $query->where('agent_id', 0)), 1, null, true);
|
|
$filter->equal('cid', '产品分类')->ignore()->select($options)->width(3);
|
|
$filter->equal('type')->select(admin_trans('product.options.publish_type'))->width(2);
|
|
});
|
|
});
|
|
}
|
|
|
|
//递归获取指定分类下的所有子分类
|
|
private function get_category_child_ids($category_id): array
|
|
{
|
|
static $category = null;
|
|
if ($category === null) {
|
|
$category = Category::where('agent_id', 0)->get()->toArray();
|
|
}
|
|
|
|
$child = [];
|
|
foreach ($category as $cat) {
|
|
if ($cat['pid'] == $category_id) {
|
|
$child[] = $cat['id'];
|
|
$child = array_merge($child, $this->get_category_child_ids($cat['id']));
|
|
}
|
|
}
|
|
|
|
return $child;
|
|
}
|
|
|
|
/**
|
|
* Make a show builder.
|
|
*
|
|
* @param mixed $id
|
|
*
|
|
* @return Show
|
|
*/
|
|
protected function detail($id)
|
|
{
|
|
return Show::make($id, new Product(), function (Show $show) {
|
|
//不允许查看非自己的数据
|
|
if ($show->model()->supplier_id != Admin::user()->id) {
|
|
Admin::exit('数据不存在');
|
|
}
|
|
|
|
$show->field('id');
|
|
$show->field('supplier_id');
|
|
$show->field('category_id');
|
|
$show->field('title');
|
|
$show->field('price');
|
|
$show->field('original_price');
|
|
$show->field('pictures')->image('', 80, 80);
|
|
$show->field('stock');
|
|
$show->field('sale');
|
|
$show->field('single_deposit');
|
|
$show->field('status');
|
|
$show->field('know')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
|
|
$show->field('content')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
|
|
$show->field('verify_mobile','核销员手机');
|
|
$show->field('created_at');
|
|
$show->field('updated_at');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form()
|
|
{
|
|
Admin::user()->publish_type = json_decode(Admin::user()->publish_type, true);
|
|
// Form::extend('spec', Spec::class);
|
|
return Form::make(new Product(['spec']), function (Form $form) {
|
|
// $form->spec('spec', '产品规格')->required();
|
|
//不允许编辑非自己数据
|
|
if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
|
|
return $form->response()->error('数据不存在');
|
|
}
|
|
|
|
$form->display('id');
|
|
|
|
$options = Category::selectOptions(fn($query) => $query->where('agent_id', 0)->whereIn('publish_type', Admin::user()->publish_type));
|
|
$form->select('category_id')
|
|
->options(array_slice($options, 1, null, true))
|
|
->required();
|
|
|
|
$cat2publish_type = json_encode(Category::where('agent_id', 0)->pluck('publish_type', 'id'));
|
|
Admin::script(<<<JS
|
|
var PublishType = $cat2publish_type;
|
|
$('select[name="category_id"]').change(function () {
|
|
var SelectId = $(this).val();
|
|
$('input[name="type"][value="'+PublishType[SelectId]+'"]').click();
|
|
});
|
|
JS);
|
|
|
|
//信息收集表单
|
|
$options = DiyForm::where(['merchant_id' => Admin::user()->id, 'type' => 1])->pluck('name', 'id');
|
|
if ($options->isEmpty()) {
|
|
$form->select('diy_form_id', '信息收集表单')
|
|
->help('提示:信息收集表单为空,请前往“<a href="' . admin_url('diy_form/create') . '">信息收集表单</a>”处新增')
|
|
->options($options)->required();
|
|
} else {
|
|
$form->select('diy_form_id', '信息收集表单')->options($options)->required();
|
|
}
|
|
|
|
$form->text('title')->required();
|
|
$form->hasMany('spec', function (NestedForm $form) {
|
|
$form->hidden('id');
|
|
$form->text('name', '规格')->required()->readonly();
|
|
$form->date('date', '日期')->required();
|
|
$form->text('stock')->required();
|
|
$form->text('original_price')->required();
|
|
$form->text('price')->required();
|
|
$form->text('cost_price')->required();
|
|
|
|
Admin::style('.field_date{width:100px!important;}
|
|
.has-many-spec .col-md-12{padding:0;}
|
|
.has-many-spec .add.btn{display:none;}
|
|
.has-many-spec .input-group-prepend{display:none;}
|
|
.has-many-spec .form-group{margin-bottom:0;}
|
|
.has-many-spec .input-group>.form-control:not(:first-child){border-radius:.25rem;}');
|
|
|
|
Admin::script(file_get_contents(resource_path('js/supplier-batch-add-spec.js')));
|
|
})->useTable()->required();
|
|
if ($form->isEditing() && in_array($form->model()->status, [ProductStatus::SOLD_OUT, ProductStatus::ON_SALE])) {
|
|
$form->radio('status')->options([1 => '上架', -2 => '下架'])->default(1);
|
|
}
|
|
$form->multipleImage('pictures')->required()->removable(false)->uniqueName();
|
|
$form->editor('know');
|
|
$form->editor('content')->required();
|
|
$form->mobile('verify_mobile')->required();
|
|
|
|
//扩展字段
|
|
$publish_type = array_intersect_key(
|
|
admin_trans('product.options.publish_type'),
|
|
array_flip(Admin::user()->publish_type)
|
|
);
|
|
|
|
//0:旅游线路、1:酒店、2:景区、3:餐厅、4:车队、5:单项
|
|
$form->radio('type')
|
|
->options($publish_type)->disable($form->isEditing())
|
|
->default(current(Admin::user()->publish_type))
|
|
->when(0, function (Form $form) { //旅游线路
|
|
if ($form->isEditing() && $form->model()->type != 0) {
|
|
return;
|
|
}
|
|
$form->text('extends.field_0_departure_place', '出发地');
|
|
$form->map('extends.field_0_departure_place_latitude', 'extends.field_0_departure_place_longitude', '出发地位置');
|
|
|
|
$form->text('extends.field_0_destination', '目的地');
|
|
$form->map('extends.field_0_destination_latitude', 'extends.field_0_destination_longitude', '目的地位置');
|
|
|
|
$form->table('extends.field_0_project', '包含项目', function (NestedForm $table) {
|
|
$table->text('name', '字段1');
|
|
$table->text('num', '字段2');
|
|
$table->text('price', '字段3');
|
|
})->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
|
|
|
|
$form->dateRange('extends.field_0_date.start', 'extends.field_0_date.end', '行程时间');
|
|
})->when(1, function (Form $form) { //酒店
|
|
if ($form->isEditing() && $form->model()->type != 1) {
|
|
return;
|
|
}
|
|
$default = [
|
|
['tag' => '行李寄存'], ['tag' => '24小时前台'], ['tag' => '前台保险柜'], ['tag' => '唤醒服务'],
|
|
['tag' => '早餐'], ['tag' => '送餐服务'], ['tag' => '电梯'], ['tag' => '空调'],
|
|
['tag' => '新风系统'], ['tag' => '24小时热水'], ['tag' => '吹风机'], ['tag' => '加湿器'],
|
|
['tag' => '自动售货机'], ['tag' => '健身房'], ['tag' => '桌球室'], ['tag' => '洗衣服务']
|
|
];
|
|
$form->table('extends.field_1_tags', '酒店设施', function (NestedForm $table) {
|
|
$table->text('tag', '包含项目')->placeholder('如:24小时热水、干洗服务等');
|
|
})->default($default)->help('首次创建时,系统会默认填充基本服务,请根据本酒店情况进行删减或新增');
|
|
|
|
$form->text('extends.field_1_name', '酒店名');
|
|
$form->text('extends.field_1_address', '地址');
|
|
$form->map('extends.field_1_latitude', 'extends.field_1_longitude', '位置');
|
|
})->when(2, function (Form $form) { //景区
|
|
if ($form->isEditing() && $form->model()->type != 2) {
|
|
return;
|
|
}
|
|
$form->table('extends.field_2_open_time', '开放时间', function (NestedForm $table) {
|
|
$table->text('node', '字段1')->placeholder('如:周一至周五');
|
|
$table->text('summer', '字段2')->placeholder('如:08:00~19:00');
|
|
$table->text('winter', '字段3')->placeholder('如:08:00~18:00');
|
|
})->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
|
|
|
|
$form->table('extends.field_2_project', '包含项目', function (NestedForm $table) {
|
|
$table->text('name', '字段1');
|
|
$table->text('num', '字段2');
|
|
$table->text('price', '字段3');
|
|
})->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
|
|
|
|
$form->text('extends.field_2_name', '景区名');
|
|
$form->text('extends.field_2_address', '地址');
|
|
$form->map('extends.field_2_latitude', 'extends.field_2_longitude', '位置');
|
|
})->when(3, function (Form $form) { //餐厅
|
|
if ($form->isEditing() && $form->model()->type != 3) {
|
|
return;
|
|
}
|
|
$form->table('extends.field_3_open_time', '开放时间', function (NestedForm $table) {
|
|
$table->text('week', '字段1')->placeholder('如:周一至周五');
|
|
$table->text('section', '字段2')->placeholder('如:上午/下午');
|
|
$table->text('time', '字段3')->placeholder('如:08:00~18:00');
|
|
})->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
|
|
|
|
$form->table('extends.field_3_package', '包含套餐', function (NestedForm $table) {
|
|
$table->text('name', '字段1')->placeholder('如:清蒸鱿鱼');
|
|
$table->text('num', '字段2')->placeholder('如:1条');
|
|
$table->text('price', '字段3')->placeholder('如:99元');
|
|
})->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
|
|
|
|
$form->text('extends.field_3_name', '餐厅名');
|
|
$form->text('extends.field_3_address', '地址');
|
|
$form->map('extends.field_3_latitude', 'extends.field_3_longitude', '位置');
|
|
})->when(4, function (Form $form) { //车队
|
|
if ($form->isEditing() && $form->model()->type != 4) {
|
|
return;
|
|
}
|
|
$form->text('extends.field_4_address', '地址');
|
|
$form->map('extends.field_4_latitude', 'extends.field_4_longitude', '位置');
|
|
})->when(5, function (Form $form) { //单项
|
|
if ($form->isEditing() && $form->model()->type != 5) {
|
|
return;
|
|
}
|
|
$form->text('extends.field_5_address', '地址');
|
|
$form->map('extends.field_5_latitude', 'extends.field_5_longitude', '位置');
|
|
});
|
|
})->saving(function (Form $form) {
|
|
//不允许编辑非自己数据
|
|
if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
|
|
return $form->response()->error('数据不存在');
|
|
}
|
|
|
|
if ($form->isCreating()) {
|
|
if (is_null($form->type) || !Admin::user()->publish_type || !in_array($form->type, Admin::user()->publish_type)) {
|
|
return $form->response()->error('对不起,你没有此类产品的发布、编辑权限');
|
|
}
|
|
} else if ($form->isEditing()) {
|
|
$form->type = $form->model()->type; //type不允许编辑
|
|
|
|
//如果存在未核销的订单不允许编辑
|
|
$exists = \App\Models\Order::where('product_id', $form->model()->id)
|
|
->whereIn('status', [OrderStatus::PAID, OrderStatus::PAID_RETAINAGE, OrderStatus::OFFLINE_PAID, OrderStatus::REFUSED_REFUND])
|
|
->exists();
|
|
if ($exists) {
|
|
return $form->response()->error('该产品还有未核销的订单,不允许编辑');
|
|
}
|
|
}
|
|
|
|
//不允许编辑的字段,忽略字段不起作用?
|
|
$form->ignore(['id', 'supplier_id', 'sale', 'created_at', 'updated_at', 'deleted_at']);
|
|
|
|
//null字段转为''
|
|
foreach ($form->input() as $k => $v) {
|
|
if (is_null($v)) {
|
|
$form->$k = '';
|
|
}
|
|
}
|
|
|
|
//用户可编辑的状态
|
|
$user_status = [ProductStatus::SOLD_OUT, ProductStatus::ON_SALE];
|
|
|
|
//列表切换上下架按钮
|
|
if ($form->isEditing() && !is_null($form->status) && is_null($form->title)) {
|
|
if (empty($form->model()->spec->toArray())) {
|
|
return $form->response()->error('请先设置产品规格')->refresh();
|
|
} else if (in_array($form->model()->status, $user_status)) {
|
|
$form->status = $form->status == 1 ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT;
|
|
$form->model()->update(['status' => $form->status]);
|
|
return $form->response()->success('更新成功')->refresh();
|
|
}
|
|
}
|
|
|
|
//规格处理
|
|
if (!$form->spec || !$spec = array_filter($form->spec, fn($v) => !$v['_remove_'])) {
|
|
return $form->response()->error('请输入产品规格');
|
|
}
|
|
|
|
$form->hidden(['stock', 'original_price', 'price', 'longitude', 'latitude', 'address']);
|
|
|
|
//处理库存、市场价、销售价
|
|
$form->stock = array_sum(array_column($spec, 'stock'));
|
|
$form->original_price = min(array_column($spec, 'original_price'));
|
|
$form->price = min(array_column($spec, 'price'));
|
|
|
|
//经度,纬度,地址
|
|
if ($form->type == 0) { //旅游线路用出发地保存
|
|
$form->longitude = $form->extends['field_0_departure_place_longitude'] ?? 0;
|
|
$form->latitude = $form->extends['field_0_departure_place_latitude'] ?? 0;
|
|
$form->address = $form->extends['field_0_departure_place'] ?? '';
|
|
} else {
|
|
$form->longitude = $form->extends['field_'.$form->type.'_longitude'] ?? 0;
|
|
$form->latitude = $form->extends['field_'.$form->type.'_latitude'] ?? 0;
|
|
$form->address = $form->extends['field_'.$form->type.'_address'] ?? '';
|
|
}
|
|
|
|
//特殊字段处理
|
|
if ($form->isCreating()) {
|
|
$form->hidden(['status', 'supplier_id']); //表单没有的字段,必须加上这句才能重置值
|
|
$form->supplier_id = Admin::user()->id;
|
|
$form->status = ProductStatus::UNAUDITED;
|
|
} else if ($form->isEditing()) {
|
|
//如果原来是下架或上架状态才允许修改
|
|
if (in_array($form->status, $user_status) && in_array($form->model()->status, $user_status)) {
|
|
$form->status = $form->status == ProductStatus::ON_SALE ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT;
|
|
} else {
|
|
$form->deleteInput('status');
|
|
}
|
|
}
|
|
})->saved(function (Form $form, $result) {
|
|
if ($form->isEditing() && $result) {
|
|
$delete_specs = array_filter($form->spec, fn($v) => $v['_remove_'] !== null); //删除的规格
|
|
|
|
//同步删除代理商规格
|
|
AgentProductSpec::whereIn('product_spec_id', array_keys($delete_specs))->delete();
|
|
|
|
//最新规格信息同步到代理商
|
|
ProductSpecSync::dispatch($form->getKey());
|
|
}
|
|
})->deleting(function (Form $form) {
|
|
//不允许删除非自己的数据
|
|
if (array_filter($form->model()->toArray(), fn($v) => $v['supplier_id'] != Admin::user()->id)) {
|
|
return $form->response()->error('数据不存在');
|
|
}
|
|
|
|
$no_status = [OrderStatus::CANCEL, OrderStatus::REFUNDED, OrderStatus::SUCCESS];
|
|
if (Order::where('product_id', $form->getKey())->whereNotIn('status', $no_status)->exists()) {
|
|
return $form->response()->error('该产品存在未完成的订单,不允许删除');
|
|
}
|
|
});
|
|
}
|
|
}
|