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

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