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

746 lines
30 KiB

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