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

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