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

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