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

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