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

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