海南旅游SAAS
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.

712 lines
29 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\AdminAgent\Controllers;
  3. use App\AdminAgent\Forms\LoadSupplierSpec;
  4. use App\AdminAgent\Renderable\SelectGuide;
  5. use App\AdminAgent\Renderable\SelectProduct;
  6. use App\AdminAgent\Repositories\AgentProduct;
  7. use App\Common\AgentType;
  8. use App\Common\ProductStatus;
  9. use App\Models\AgentProductItem;
  10. use App\Models\Category;
  11. use App\Models\Channel;
  12. use App\Models\DiyForm;
  13. use App\Models\Guide;
  14. use App\Models\Product;
  15. use App\Models\Supplier;
  16. use Dcat\Admin\Admin;
  17. use Dcat\Admin\Form;
  18. use Dcat\Admin\Form\NestedForm;
  19. use Dcat\Admin\Grid;
  20. use Dcat\Admin\Show;
  21. use Dcat\Admin\Http\Controllers\AdminController;
  22. class AgentProductController extends AdminController
  23. {
  24. /**
  25. * Make a grid builder.
  26. *
  27. * @return Grid
  28. */
  29. protected function grid()
  30. {
  31. //下架供应商产品状态不是上架或库存不足的产品
  32. \App\Models\AgentProduct::where('agent_id', Admin::user()->id)
  33. ->whereHas('agentProductItem', function ($query) {
  34. return $query->whereHas('product', function ($query) {
  35. return $query->where('stock', '<=', 0)->orWhere('status', '<>', ProductStatus::ON_SALE);
  36. });
  37. })
  38. ->orWhere('stock', '<=', 0)
  39. ->update(['status' => ProductStatus::SOLD_OUT]);
  40. return Grid::make(new AgentProduct(['product.supplier:id,company_name', 'category:id,name']), function (Grid $grid) {
  41. $agent_id = Admin::user()->id;
  42. $grid->model()->where('agent_id', $agent_id);
  43. $grid->column('id')->sortable();
  44. $grid->column('picture', '产品图片')->image('', 60, 60);
  45. $grid->column('title', '产品名称')->limit(15);
  46. $grid->column('price');
  47. $grid->column('original_price');
  48. $grid->column('sale');
  49. $grid->column('stock');
  50. $channels = Channel::where('agent_id', $agent_id)->pluck('name', 'id')->toArray();
  51. $grid->column('channel_id', '频道')
  52. ->display(function ($modal) use ($channels) {
  53. $data = array_flip(explode(',', $this->channel_id));
  54. return join(',',array_intersect_key($channels, $data));
  55. })
  56. ->limit(10);
  57. $grid->column('category.name', '分类');
  58. /*$grid->column('product_ids', '产品详情')
  59. ->display('查看')
  60. ->modal(function ($modal) {
  61. $titles = ['供应商', '产品标题', '产品图片', '市场价', '现价', '销量', '库存'];
  62. $pic = isset($this->product->picture)
  63. ? "<img data-action=\"preview-img\" src=\"{$this->product->picture}\" style=\"max-width:80px;max-height:200px;cursor:pointer\" class=\"img img-thumbnail\">"
  64. : '';
  65. $data = [[
  66. $this->product->supplier->company_name ?? '',
  67. $this->product->title ?? '',
  68. $pic,
  69. $this->product->original_price ?? '',
  70. $this->product->price ?? '',
  71. $this->product->sale ?? '',
  72. $this->product->stock ?? '',
  73. ]];
  74. return Table::make($titles, $data);
  75. });*/
  76. $grid->column('status')->help('切换开关可改变上下架状态')
  77. ->if(fn() => in_array($this->status, [ProductStatus::SOLD_OUT, ProductStatus::ON_SALE]))
  78. ->using([ProductStatus::SOLD_OUT => 0, ProductStatus::ON_SALE => 1])
  79. ->switch()
  80. ->else()
  81. ->using(ProductStatus::array())
  82. ->dot([
  83. ProductStatus::ON_SALE => 'success',
  84. ProductStatus::UNAUDITED => '',
  85. ProductStatus::REFUSE => 'danger',
  86. ProductStatus::SOLD_OUT => 'warning',
  87. ], 'primary');
  88. if (Admin::user()->type != AgentType::OPERATOR) {
  89. $grid->column('is_rec')->switch()->help('推荐后将在“我的”页面下方显示');
  90. }
  91. $grid->column('updated_at');
  92. $grid->filter(function (Grid\Filter $filter) {
  93. $filter->panel();
  94. $filter->model()->where('agent_id', Admin::user()->id);
  95. $filter->equal('id')->width(2);
  96. $filter->like('product.title', '产品标题')->width(3);
  97. $filter->equal('status')->select(ProductStatus::array())->width(2);
  98. $options = Supplier::where('status', 1)->pluck('company_name', 'id')->toArray();
  99. $filter->equal('product.supplier_Id', '供应商')->select($options)->width(2);
  100. });
  101. });
  102. }
  103. /**
  104. * Make a show builder.
  105. *
  106. * @param mixed $id
  107. *
  108. * @return Show
  109. */
  110. protected function detail($id)
  111. {
  112. return Show::make($id, new AgentProduct(['category:id,name', 'product.supplier:id,company_name', 'guide:id,name']), function (Show $show) {
  113. //不允许查看非自己的数据
  114. if ($show->model()->agent_id != Admin::user()->id) {
  115. Admin::exit('数据不存在');
  116. }
  117. $show->field('id');
  118. $show->field('product_id');
  119. $show->field('product.supplier.company_name', '供应商');
  120. $show->field('price');
  121. $show->field('original_price');
  122. $show->field('sale');
  123. $show->field('stock');
  124. $show->field('category.name', '分类');
  125. $show->field('status')->using(ProductStatus::array());
  126. if (Admin::user()->type == AgentType::OPERATOR) {
  127. $show->field('guide.name', '地接');
  128. }
  129. $show->field('title');
  130. $show->field('pictures')->image('', 80, 80);
  131. $show->field('know')->unescape();
  132. $show->field('content')->unescape();
  133. if (Admin::user()->type != AgentType::OPERATOR) {
  134. $show->field('is_rec')->using(['未推荐', '已推荐']);
  135. $show->field('channel_id')->as(fn($v) => join(',', Channel::whereIn('id', explode(',', $v))->pluck('name')->toArray()));
  136. $show->field('earnest');
  137. $show->field('earnest_timeout');
  138. $show->field('deposit');
  139. $show->field('deposit_timeout');
  140. }
  141. $show->field('created_at');
  142. $show->field('updated_at');
  143. });
  144. }
  145. /**
  146. * Make a form builder.
  147. *
  148. * @return Form
  149. */
  150. protected function form()
  151. {
  152. return Form::make(new AgentProduct(['product:id,title', 'spec.productSpec']), function (Form $form) {
  153. $agent_id = Admin::user()->id;
  154. //不允许查看非自己的数据
  155. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  156. return $form->response()->error('数据不存在');
  157. }
  158. // $form->display('id');
  159. //计调版旅行社不允许选择计调云产品
  160. if (Admin::user()->type == AgentType::OPERATOR) {
  161. $form->hidden('product_id')->value(0)->default(0);
  162. $form->hidden('type')->value(1)->default(1);
  163. // 组合销售
  164. $form->multipleSelectTable('product_ids', '供应商产品')
  165. ->help('请选择两个或两个以上的产品组合销售')
  166. ->title('选择产品')
  167. ->dialogWidth('80%;min-width:825px;')
  168. ->from(SelectProduct::make(['ids' => $form->model()->product_ids]))
  169. ->model(Product::class);
  170. // 自定义内容
  171. $form->text('title');
  172. $form->multipleImage('pictures')->removable(false)->uniqueName();
  173. $form->editor('know');
  174. $form->editor('content');
  175. } else {
  176. if ($form->isCreating()) {
  177. $form->radio('type')->options([0 => '从供应商进货', 3 => '自营产品'])->default(0)
  178. ->when(0, function (Form $form) {
  179. $form->selectTable('product_id', '供应商产品')
  180. ->title('选择产品')->default(0)
  181. ->dialogWidth('80%;min-width:825px;')
  182. ->from(SelectProduct::make(['ids' => $form->model()->product_ids]))
  183. ->model(Product::class)
  184. ->script("
  185. $(function () {
  186. $('input[name=\"product_id\"]').change(function () {
  187. $('.spec-sync').click();
  188. });
  189. });
  190. ");
  191. //关联供应商规格
  192. $this->related_spec($form);
  193. })->when(3, function (Form $form) {
  194. //自营产品表单
  195. $this->self_form($form);
  196. });
  197. } else {
  198. $form->hidden('product_id');
  199. if ($form->model()->type == 0) {
  200. $form->display('product.title');
  201. //关联供应商规格
  202. $this->related_spec($form);
  203. } else if ($form->model()->type == 3) {
  204. //自营产品表单
  205. $this->self_form($form);
  206. //再加这个hasMany才能正常保存
  207. $form->hasMany('spec', function (Form\NestedForm $form) {
  208. $form->hidden('name');
  209. $form->hidden('date');
  210. $form->hidden('stock');
  211. $form->hidden('original_price');
  212. $form->hidden('price');
  213. })->useTable()->script('$(function(){
  214. $(".has-many-spec").parent().parent().remove();
  215. });');
  216. }
  217. }
  218. $js = file_get_contents(resource_path('js/agent-spec-edit.js'));
  219. $class = str_replace('\\', '\\\\', LoadSupplierSpec::class);
  220. $js = str_replace(
  221. ['`{{url}}`', '`{{class}}`', '`{{agent_product_id}}`'],
  222. [route(admin_api_route_name('form')), $class, $form->model()->id ?? 0],
  223. $js
  224. );
  225. Admin::script($js);
  226. if ($form->isCreating()) {
  227. Admin::style('.has-many-spec .spec-sync{display:none;}');
  228. }
  229. }
  230. $form->number('sale')->min(0)->default(0);
  231. $options = Category::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
  232. $form->select('category_id')
  233. ->options(array_slice($options, 1, null, true))
  234. ->required();
  235. if ($form->isEditing() && in_array($form->model()->status, [ProductStatus::UNAUDITED, ProductStatus::REFUSE])) {
  236. $form->display('status')->customFormat(fn($v) => ProductStatus::array()[$form->model()->status]);
  237. } else {
  238. $form->radio('status')
  239. ->default(ProductStatus::ON_SALE)
  240. ->options([
  241. ProductStatus::ON_SALE => '上架',
  242. ProductStatus::SOLD_OUT => '下架',
  243. ])
  244. ->required();
  245. }
  246. //计调版旅行社可以选择地接
  247. if (Admin::user()->type == AgentType::OPERATOR) {
  248. $form->selectTable('guide_id', '地接人员')
  249. ->title('选择地接人员')
  250. ->dialogWidth('50%;min-width:600px;') //不起作用
  251. ->from(SelectGuide::make())
  252. ->model(Guide::class, 'id', 'name');
  253. } else {
  254. $form->switch('is_rec')->help('推荐后将在小程序“我的”页面下方显示');
  255. $options = Channel::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
  256. $form->multipleSelect('channel_id')->options(array_slice($options, 1, null, true));
  257. $form->decimal('earnest')->rules('required|numeric|min:0',[
  258. '*' => '金额为必填字段且必须大于0',
  259. ])->default(0);
  260. $form->number('earnest_timeout')->min(0)
  261. ->default(0)->help('单位:分钟。超过这个时间未支付尾款,订单将自动关闭,且定金不退');
  262. $form->decimal('deposit')->rules('required|numeric|min:0',[
  263. '*' => '金额为必填字段且必须大于0',
  264. ])->default(0);;
  265. $form->number('deposit_timeout')->min(0)
  266. ->default(0)->help('单位:分钟。超过这个时间未支付尾款,订单将自动关闭,且订金不退');
  267. }
  268. })->saving(function (Form $form) {
  269. //不允许修改非自己的数据
  270. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  271. return $form->response()->error('数据不存在');
  272. }
  273. if ($form->isEditing() && $form->product_id === null) {
  274. //推荐按钮开关
  275. if ($form->is_rec !== null) {
  276. $form->model()->is_rec = $form->is_rec ? 1 : 0;
  277. $form->model()->save();
  278. return $form->response()->success('更新成功!')->refresh();
  279. }
  280. if ($form->stock !== null || $form->status !== null) {
  281. $ids = explode(',', $form->model()->product_ids);
  282. $exists = Product::where([
  283. ['status', '<>', ProductStatus::ON_SALE],
  284. ])->whereIn('id', $ids)->exists();
  285. if ($exists) {
  286. return $form->response()->error('供应商产品已下架')->refresh();
  287. }
  288. //修改库存
  289. if ($form->stock !== null) {
  290. $form->model()->stock = $form->stock;
  291. $form->model()->save();
  292. return $form->response()->success('更新成功!');
  293. }
  294. //上下架状态按钮开关
  295. if ($form->status !== null) {
  296. //待审核和拒绝的状态不允许修改
  297. if (in_array($form->model()->status, [ProductStatus::UNAUDITED, ProductStatus::REFUSE])) {
  298. return $form->response()->error('产品待审核或审核拒绝,不允许修改!')->refresh();
  299. }
  300. //计调云产品处理
  301. if ($form->type == 2) {
  302. $cloud_product = AgentProduct::find($form->agent_cloud_pid);
  303. if (!$cloud_product || $cloud_product->status != ProductStatus::ON_SALE) {
  304. return $form->response()->error('你选择的计调云产品状态异常,上架失败!')->refresh();
  305. }
  306. }
  307. //库存不足,上架失败
  308. if ($form->model()->stock <= 0 && $form->status == 1) {
  309. return $form->response()->error('库存不足,上架失败,请先增加库存!')->refresh();
  310. }
  311. $form->model()->status = $form->status == 1 ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT;
  312. $form->model()->save();
  313. return $form->response()->success('更新成功!')->refresh();
  314. }
  315. }
  316. }
  317. if ($form->isCreating() && is_null($form->type)) {
  318. return $form->response()->error('请选择产品类型!');
  319. } else if ($form->isEditing()) {
  320. $form->type = $form->model()->type;
  321. }
  322. //自营产品规格
  323. if ($form->type == 3) {
  324. $form->spec = $form->spec_self;
  325. $form->deleteInput('spec_self');
  326. }
  327. $form->hidden(['stock', 'price']);
  328. if (!$form->spec) {
  329. return $form->response()->error('请选择产品,并设置产品规格');
  330. }
  331. $spec = array_filter($form->spec, fn($v) => !$v['_remove_']);
  332. if (!$spec) {
  333. return $form->response()->error('产品规格不能为空');
  334. }
  335. $form->stock = array_sum(array_column($spec, 'stock'));
  336. $form->original_price = min(array_column($spec, 'original_price'));
  337. $form->price = min(array_column($spec, 'price'));
  338. //单品销售
  339. if ($form->type == 0) {
  340. $form->hidden(['product_ids', 'title', 'pictures', 'know', 'content']);
  341. $form->product_id = (int)$form->product_id;
  342. if (!$form->product_id) {
  343. return $form->response()->error('请选择产品');
  344. }
  345. $form->product_ids = $form->product_id;
  346. //将供应商产品写入title,pictures,know,content
  347. $product = Product::find($form->product_id);
  348. if ($product->status != ProductStatus::ON_SALE) {
  349. return $form->response()->error('供应商产品 '. $product->title .' 已下架');
  350. /*} else if ($product->stock < $form->stock) {
  351. return $form->response()->error("供应商当前库存为{$product->stock},你设置的库存不能超过该数值");*/
  352. } else if ($form->price < $product->price) {
  353. return $form->response()->error("产品销售价不能小于供应商销售价{$product->price}");
  354. }
  355. $form->title = $product->title;
  356. $form->pictures = $product->pictures;
  357. $form->know = $product->know;
  358. $form->content = $product->content;
  359. } else if ($form->type == 3) {
  360. if (!$form->title || !$form->pictures || !$form->know || !$form->content) {
  361. return $form->response()->error('标题、产品图片、旅游须知、产品详情不能为空');
  362. } else if (!$form->diy_form_id) {
  363. return $form->response()->error('请选择信息收集表单');
  364. }
  365. } else {
  366. return $form->response()->error('不存在此销售方式');
  367. }
  368. $agent_id = Admin::user()->id;
  369. //处理特殊字段
  370. $form->hidden(['agent_id', 'status', 'longitude', 'latitude', 'address']); //表单没有的字段,必须加这句才能够重写
  371. $form->agent_id = $agent_id;
  372. $form->agent_cloud_pid = $form->type ==2 ? ($form->agent_cloud_pid ?? 0) : 0;
  373. if (array_key_exists('guide_id', $form->input())) {
  374. $form->guide_id = $form->guide_id ?? 0;
  375. }
  376. //产品模板
  377. if ($form->isEditing()) {
  378. $form->tpl_type = $form->model()->tpl_type;
  379. }
  380. //经度,纬度,地址
  381. if ($form->tpl_type == 0) { //旅游线路用出发地保存
  382. $form->longitude = $form->extends['field_0_departure_place_longitude'] ?? 0;
  383. $form->latitude = $form->extends['field_0_departure_place_latitude'] ?? 0;
  384. $form->address = $form->extends['field_0_departure_place'] ?? '';
  385. } else {
  386. $form->longitude = $form->extends['field_'.$form->tpl_type.'_longitude'] ?? 0;
  387. $form->latitude = $form->extends['field_'.$form->tpl_type.'_latitude'] ?? 0;
  388. $form->address = $form->extends['field_'.$form->tpl_type.'_address'] ?? '';
  389. }
  390. //组合销售需要审核,编辑时是否需要审核在saved里面判断
  391. if ($form->type == 1 || $form->type == 3) {
  392. if ($form->isCreating()) {
  393. $form->status = ProductStatus::UNAUDITED;
  394. } else if ($form->isEditing() && in_array($form->model()->status, [ProductStatus::UNAUDITED, ProductStatus::REFUSE])) {
  395. $form->deleteInput('status'); //待审核和拒绝的状态不允许修改
  396. }
  397. } else if (!is_null($form->status) && ($form->type == 0 || $form->type == 2)) {
  398. $form->status = $form->status == ProductStatus::ON_SALE ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT;
  399. }
  400. //订金
  401. if ($form->earnest <= 0 || $form->earnest_timeout <= 0) {
  402. $form->earnest = 0;
  403. $form->earnest_timeout = 0;
  404. } else if ($form->earnest > $form->price) {
  405. return $form->response()->error('订金不能大于商品价');
  406. }
  407. //定金
  408. if ($form->deposit <= 0 || $form->deposit_timeout <= 0) {
  409. $form->deposit = 0;
  410. $form->deposit_timeout = 0;
  411. } else if ($form->earnest > $form->price) {
  412. return $form->response()->error('定金不能大于商品价');
  413. }
  414. //不允许编辑的字段
  415. $form->ignore(['id', 'agent_id', 'created_at', 'updated_at', 'deleted_at']);
  416. //判断是否重复发布产品
  417. $where = [
  418. ['type', '=', $form->type],
  419. ['agent_id', '=', $agent_id],
  420. ['product_id', '=', $form->product_id],
  421. ['product_ids', '=', $form->product_ids],
  422. ];
  423. if ($form->isEditing()) {
  424. $where[] = ['id', '<>', $form->getKey()];
  425. }
  426. if ($id = $form->repository()->model()->where($where)->value('id')) {
  427. return $form->response()->error("你发布的产品ID {$id} 与本产品重复,请检查");
  428. }
  429. })->saved(function (Form $form) {
  430. /** 保存到组合产品明细,先查询出之前明细,再跟新的比较,若没有则删除,新的产品原来明细表没有的,则插入 **/
  431. $product_ids = explode(',', $form->product_ids);
  432. $agent_product_id = $form->getKey();
  433. $product = Product::whereIn('id', $product_ids)->orderBy('id')
  434. ->get(['id AS product_id', 'supplier_id'])->toArray();
  435. $insert_data = array_map(function ($v) use ($agent_product_id) {
  436. $v['agent_product_id'] = $agent_product_id;
  437. $v['agent_id'] = Admin::user()->id;
  438. return $v;
  439. }, $product);
  440. //组合产品编辑关键字段需要审核
  441. /*if ($form->isEditing() && $form->type == 1 && $form->model()->wasChanged(['title', 'pictures', 'know', 'content'])) {
  442. $form->model()->update(['status' => ProductStatus::UNAUDITED]);
  443. }*/
  444. if ($form->isCreating()) {
  445. AgentProductItem::insert($insert_data);
  446. } else if ($form->isEditing()) {
  447. //删除原来有,但现在没有的数据
  448. AgentProductItem::query()
  449. ->where('agent_product_id', $agent_product_id)
  450. ->whereNotIn('product_id', $product_ids)->delete();
  451. //插入原来没有,但是现在有的数据
  452. foreach ($insert_data as $v) {
  453. AgentProductItem::query()->firstOrCreate(
  454. ['agent_product_id' => $agent_product_id, 'product_id' => $v['product_id']],
  455. $v
  456. );
  457. }
  458. }
  459. //如果是计调云产品,且处于上架状态,同步信息到其它产品,否则下架所有关联的产品
  460. if ($form->is_cloud) {
  461. if ($form->status == ProductStatus::ON_SALE) {
  462. $data = [
  463. 'product_id' => $form->product_id,
  464. 'product_ids' => $form->product_ids,
  465. 'guide_id' => $form->guide_id,
  466. 'title' => $form->title,
  467. 'pictures' => explode(',', $form->pictures),
  468. 'know' => $form->know,
  469. 'content' => $form->content,
  470. ];
  471. } else {
  472. $data = ['status' => ProductStatus::SOLD_OUT];
  473. }
  474. \App\Models\AgentProduct::query()
  475. ->where(['agent_cloud_pid' => $form->getKey(), 'type' => 2])
  476. ->update($data);
  477. }
  478. })->deleting(function (Form $form) {
  479. //不允许删除非自己的数据
  480. if (array_filter($form->model()->toArray(), fn($v) => $v['agent_id'] != Admin::user()->id)) {
  481. return $form->response()->error('数据不存在');
  482. }
  483. //处理组合产品明细表
  484. $ids = array_column($form->model()->toArray(),'id');
  485. AgentProductItem::query()->whereIn('agent_product_id',$ids)->delete();
  486. });
  487. }
  488. /**
  489. * 关联供应商规格
  490. * @param Form $form
  491. */
  492. private function related_spec(&$form)
  493. {
  494. //关联供应商规格
  495. $form->hasMany('spec', function (Form\NestedForm $form) {
  496. // $form->hidden('id'); hasMany时,ID会自动生成
  497. $form->hidden('product_spec_id');
  498. $form->hidden('name')->saving(fn($v) => is_null($v) ? '' : $v); //必须加上这一句,自营产品规格才能正常保存
  499. $form->hidden('date'); //必须加上这一句,自营产品规格才能正常保存
  500. $form->text('supplier_name', '规格')->disable()->customFormat(fn() => $this->product_spec['name'] ?? '供应商已删除规格');
  501. $form->date('supplier_date', '日期')->disable()->customFormat(fn() => $this->product_spec['date'] ?? '供应商已删除规格');
  502. $form->text('supplier_stock', '供应商库存')->disable()->customFormat(fn() => $this->product_spec['stock'] ?? 0);
  503. $form->text('supplier_cost_price', '供应商成本价')->disable()->customFormat(fn() => $this->product_spec['cost_price'] ?? 0);
  504. $form->text('supplier_price', '建议销售价')->disable()->customFormat(fn() => $this->product_spec['price'] ?? 0);
  505. $form->text('stock', '您的库存')->required()
  506. //如果库存大于供应商库存,则取供应商库存
  507. ->customFormat(fn() => isset($this->product_spec['stock'], $this->stock) && $this->stock > $this->product_spec['stock'] ? $this->product_spec['stock'] : $this->stock);
  508. $form->text('original_price', '您的市场价')->required();
  509. $form->text('price', '您的销售价')->required()
  510. //如果代理商价格小于供应商建议售价,则取供应商售价
  511. ->customFormat(fn() => isset($this->product_spec['price'], $this->price) && $this->price < $this->product_spec['price'] ? $this->product_spec['price'] : $this->price);
  512. })->useTable();
  513. Admin::style('.has-many-spec .add.btn{display:none;}
  514. .has-many-spec .field_supplier_date{width:100px!important;}
  515. .has-many-spec .col-md-12{padding:0;}
  516. .has-many-spec .form-group{margin-bottom:0;}
  517. .has-many-spec .form-group .remove{margin-top:10px;}
  518. .has-many-spec .input-group-prepend{display:none;}
  519. .has-many-spec .input-group>.form-control:not(:first-child){border-radius:.25rem;}');
  520. }
  521. /**
  522. * 自营产品表单
  523. * @param Form $form
  524. */
  525. private function self_form(&$form)
  526. {
  527. //信息收集表单
  528. $options = DiyForm::where(['merchant_id' => Admin::user()->id, 'type' => 1])->pluck('name', 'id');
  529. if ($options->isEmpty()) {
  530. $form->select('diy_form_id', '信息收集表单')
  531. ->help('提示:信息收集表单为空,请前往“<a href="' . admin_url('diy_form/create') . '">信息收集表单</a>”处新增')
  532. ->options($options)->saving(fn($v) => is_null($v) ? 0 : $v);
  533. } else {
  534. $form->select('diy_form_id', '信息收集表单')->options($options)->saving(fn($v) => is_null($v) ? 0 : $v);
  535. }
  536. $form->text('title');
  537. $form->multipleImage('pictures')->removable(false)->uniqueName();
  538. $form->editor('know');
  539. $form->editor('content');
  540. //自营产品规格
  541. $form->hasMany('spec_self', '产品规格', function (Form\NestedForm $form) {
  542. $form->text('name', '规格')->readOnly();
  543. $form->date('date', '日期');
  544. $form->text('stock', '库存');
  545. $form->text('original_price', '市场价');
  546. $form->text('price', '销售价');
  547. })->useTable()->customFormat(fn() => $form->model()->spec);
  548. Admin::style('.has-many-spec_self .add.btn{display:none;}
  549. .has-many-spec_self .field_supplier_date{width:100px!important;}
  550. .has-many-spec_self .col-md-12{padding:0;}
  551. .has-many-spec_self .form-group{margin-bottom:0;}
  552. .has-many-spec_self .form-group .remove{margin-top:10px;}
  553. .has-many-spec_self .input-group-prepend{display:none;}
  554. .has-many-spec_self .input-group>.form-control:not(:first-child){border-radius:.25rem;}');
  555. //0:旅游线路、1:酒店、2:景区、3:餐厅、4:车队、5:单项
  556. $form->radio('tpl_type', '产品模板')->disable($form->isEditing())->default(0)
  557. ->options([0 => '旅游线路', 1 => '酒店', 2 => '景区', 3 => '餐厅', 4 => '车队', 5 => '单项'])
  558. ->when(0, function (Form $form) { //旅游线路
  559. if ($form->isEditing() && $form->model()->tpl_type != 0) {
  560. return;
  561. }
  562. $form->text('extends.field_0_departure_place', '出发地');
  563. $form->map('extends.field_0_departure_place_latitude', 'extends.field_0_departure_place_longitude', '出发地位置');
  564. $form->text('extends.field_0_destination', '目的地');
  565. $form->map('extends.field_0_destination_latitude', 'extends.field_0_destination_longitude', '目的地位置');
  566. $form->table('extends.field_0_project', '包含项目', function (NestedForm $table) {
  567. $table->text('name', '字段1');
  568. $table->text('num', '字段2');
  569. $table->text('price', '字段3');
  570. })->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
  571. $form->dateRange('extends.field_0_date.start', 'extends.field_0_date.end', '行程时间');
  572. })->when(1, function (Form $form) { //酒店
  573. if ($form->isEditing() && $form->model()->tpl_type != 1) {
  574. return;
  575. }
  576. $default = [
  577. ['tag' => '行李寄存'], ['tag' => '24小时前台'], ['tag' => '前台保险柜'], ['tag' => '唤醒服务'],
  578. ['tag' => '早餐'], ['tag' => '送餐服务'], ['tag' => '电梯'], ['tag' => '空调'],
  579. ['tag' => '新风系统'], ['tag' => '24小时热水'], ['tag' => '吹风机'], ['tag' => '加湿器'],
  580. ['tag' => '自动售货机'], ['tag' => '健身房'], ['tag' => '桌球室'], ['tag' => '洗衣服务']
  581. ];
  582. $form->table('extends.field_1_tags', '酒店设施', function (NestedForm $table) {
  583. $table->text('tag', '包含项目')->placeholder('如:24小时热水、干洗服务等');
  584. })->default($default)->help('首次创建时,系统会默认填充基本服务,请根据本酒店情况进行删减或新增');
  585. $form->text('extends.field_1_name', '酒店名');
  586. $form->text('extends.field_1_address', '地址');
  587. $form->map('extends.field_1_latitude', 'extends.field_1_longitude', '位置');
  588. })->when(2, function (Form $form) { //景区
  589. if ($form->isEditing() && $form->model()->tpl_type != 2) {
  590. return;
  591. }
  592. $form->table('extends.field_2_open_time', '开放时间', function (NestedForm $table) {
  593. $table->text('node', '字段1')->placeholder('如:周一至周五');
  594. $table->text('summer', '字段2')->placeholder('如:08:00~19:00');
  595. $table->text('winter', '字段3')->placeholder('如:08:00~18:00');
  596. })->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
  597. $form->table('extends.field_2_project', '包含项目', function (NestedForm $table) {
  598. $table->text('name', '字段1');
  599. $table->text('num', '字段2');
  600. $table->text('price', '字段3');
  601. })->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
  602. $form->text('extends.field_2_name', '景区名');
  603. $form->text('extends.field_2_address', '地址');
  604. $form->map('extends.field_2_latitude', 'extends.field_2_longitude', '位置');
  605. })->when(3, function (Form $form) { //餐厅
  606. if ($form->isEditing() && $form->model()->tpl_type != 3) {
  607. return;
  608. }
  609. $form->table('extends.field_3_open_time', '开放时间', function (NestedForm $table) {
  610. $table->text('week', '字段1')->placeholder('如:周一至周五');
  611. $table->text('section', '字段2')->placeholder('如:上午/下午');
  612. $table->text('time', '字段3')->placeholder('如:08:00~18:00');
  613. })->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
  614. $form->table('extends.field_3_package', '包含套餐', function (NestedForm $table) {
  615. $table->text('name', '字段1')->placeholder('如:清蒸鱿鱼');
  616. $table->text('num', '字段2')->placeholder('如:1条');
  617. $table->text('price', '字段3')->placeholder('如:99元');
  618. })->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
  619. $form->text('extends.field_3_name', '餐厅名');
  620. $form->text('extends.field_3_address', '地址');
  621. $form->map('extends.field_3_latitude', 'extends.field_3_longitude', '位置');
  622. })->when(4, function (Form $form) { //车队
  623. if ($form->isEditing() && $form->model()->tpl_type != 4) {
  624. return;
  625. }
  626. $form->text('extends.field_4_address', '地址');
  627. $form->map('extends.field_4_latitude', 'extends.field_4_longitude', '位置');
  628. })->when(5, function (Form $form) { //单项
  629. if ($form->isEditing() && $form->model()->tpl_type != 5) {
  630. return;
  631. }
  632. $form->text('extends.field_5_address', '地址');
  633. $form->map('extends.field_5_latitude', 'extends.field_5_longitude', '位置');
  634. });
  635. $form->mobile('verify_mobile');
  636. }
  637. }