CouponManagementController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App\Admin\Controllers\CouponManagement;
  3. use App\Admin\Actions\backstage\Coupon\CouponDetails;
  4. use App\Admin\Actions\backstage\Coupon\CouponDistribution;
  5. use App\Admin\Actions\backstage\Coupon\CouponDistributions;
  6. use App\Models\Coupon;
  7. use Encore\Admin\Controllers\AdminController;
  8. use Encore\Admin\Form;
  9. use Encore\Admin\Grid;
  10. use Encore\Admin\Show;
  11. class CouponManagementController extends AdminController
  12. {
  13. /**
  14. * Title for current resource.
  15. *
  16. * @var string
  17. */
  18. protected $title = '优惠券管理';
  19. /**
  20. * Make a grid builder.
  21. *
  22. * @return Grid
  23. */
  24. protected function grid()
  25. {
  26. $grid = new Grid(new Coupon());
  27. $grid->tools(function (Grid\Tools $tools) {
  28. $tools->append(new CouponDistributions());
  29. });
  30. $grid->actions(function ($actions) {
  31. $actions->disableView();
  32. $actions->add(new CouponDetails());
  33. });
  34. $grid->filter(function($filter){
  35. $filter->timestampBetween('start_time', "有效期开始时间")->datetime();
  36. $filter->timestampBetween('end_time', "有效期结束时间")->datetime();
  37. });
  38. $grid->column('id', __('Id'));
  39. $grid->column('name', __('劵名称'));
  40. $grid->column('title', __('劵标题'));
  41. $grid->column('icon', __('券图标'))->image('',100,100);
  42. $grid->column('type', __('类型'))->using([1=>'满减券',2=>'折扣券']);
  43. $grid->column('usable_type', __('可用类型'))->using([1=>'全部产品通用',2=>'部分产品可用']);
  44. $grid->column('money', __('满减券的优惠钱数'))->display(function ($money){
  45. return $money/100;
  46. });
  47. $grid->column('discount', __('折扣'));
  48. $grid->column('position_type', __('发放平台'))->using([1=>'领券中心',2=>'后台发放']);
  49. $grid->column('num', __('数量'));
  50. $grid->column('effective_days', __('有效天数'));
  51. $grid->column('start_time', __('有效期开始时间'))->display(function ($time){
  52. if ($time ==0)
  53. return '';
  54. return date('Y-m-d H:i:s',$time);
  55. });
  56. $grid->column('end_time', __('有效期结束时间'))->display(function ($time){
  57. if ($time ==0)
  58. return '';
  59. return date('Y-m-d H:i:s',$time);
  60. });
  61. return $grid;
  62. }
  63. /**
  64. * Make a show builder.
  65. *
  66. * @param mixed $id
  67. * @return Show
  68. */
  69. protected function detail($id)
  70. {
  71. $show = new Show(Coupon::findOrFail($id));
  72. $show->field('id', __('Id'));
  73. $show->field('name', __('Name'));
  74. $show->field('title', __('Title'));
  75. $show->field('desc', __('Desc'));
  76. $show->field('rules', __('Rules'));
  77. $show->field('icon', __('Icon'));
  78. $show->field('type', __('Type'));
  79. $show->field('usable_type', __('Usable type'));
  80. $show->field('money', __('Money'));
  81. $show->field('discount', __('Discount'));
  82. $show->field('min_consume_amount', __('Min consume amount'));
  83. $show->field('max_reduce_amount', __('Max reduce amount'));
  84. $show->field('expire_type', __('Expire type'));
  85. $show->field('effective_days', __('Effective days'));
  86. $show->field('start_time', __('Start time'));
  87. $show->field('end_time', __('End time'));
  88. $show->field('created_at', __('Created at'));
  89. $show->field('updated_at', __('Updated at'));
  90. return $show;
  91. }
  92. /**
  93. * Make a form builder.
  94. *
  95. * @return Form
  96. */
  97. protected function form()
  98. {
  99. $form = new Form(new Coupon());
  100. $form->text('name', __('优惠券名称'));
  101. $form->text('title', __('券标题'));
  102. $form->text('desc', __('使用说明'));
  103. $form->text('rules', __('使用规则'));
  104. $form->image('icon', __('券图标'))->help('图标大小48px * 48px');
  105. $form->select('type', '类型')
  106. ->options([
  107. 1 => '满减券',
  108. 2 => '折扣券',
  109. ])->when(1, function (Form $form) {
  110. $form->text('money', '满减券的优惠钱数');
  111. $form->text('min_consume_amount', '最低消费金额');
  112. })->when(2, function (Form $form) {
  113. $form->text('discount', '折扣');
  114. $form->text('max_reduce_amount', '折扣券的最大抵扣金额');
  115. });
  116. $form->select('position_type',__('发放平台'))->options([1=>'领券中心',2=>'后台发放']);
  117. $form->number('num', __('数量'));
  118. $form->select('usable_type',__('可用类型'))
  119. ->options([
  120. 1=>'全部产品通用',
  121. 2=>'部分产品可用'
  122. ])->when(2,function (Form $form){
  123. $form->hasMany('CouponType','可用类型',function (Form\NestedForm $form){
  124. $form->select('product_type','可用类型')->options([
  125. 1=>'电话咨询',
  126. 2=>'图文咨询',
  127. 3=>'门诊咨询',
  128. 4=>'疫苗接种',
  129. 5=>'儿保预约',
  130. 6=>'服务包',
  131. 7=>'充值',
  132. ]);
  133. });
  134. });
  135. $form->select('expire_type', '过期类型')
  136. ->options([
  137. 1 => '领取N天过期',
  138. 2 => '设置固定过期时间',
  139. ])->when(1, function (Form $form) {
  140. $form->text('effective_days', __('有效天数'));
  141. })->when(2, function (Form $form) {
  142. $form->date('start_time', __('有效期开始时间'));
  143. $form->date('end_time', __('有效期结束时间'));
  144. $form->saving(function ($form){
  145. $form->start_time = strtotime($form->start_time);
  146. $form->end_time = strtotime($form->end_time);
  147. });
  148. });
  149. return $form;
  150. }
  151. }