CouponManagementController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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', __('券图标'))->lightbox(['width' =>'', 'height' => 30]);
  42. $grid->column('type', __('类型'))->using([1=>'满减券',2=>'折扣券']);
  43. $grid->column('usable_type', __('可用类型'))->using([1=>'全部产品通用',2=>'部分产品可用']);
  44. $grid->column('CouponType', __('可用产品'))->pluck('product_type','id')->display(function ($i){
  45. $name = [];
  46. foreach ($i as $value)
  47. {
  48. switch ($value){
  49. case 1:
  50. array_push($name,'电话咨询');
  51. break;
  52. case 2:
  53. array_push($name,'图文咨询');
  54. break;
  55. case 3:
  56. array_push($name,'门诊咨询');
  57. break;
  58. case 4:
  59. array_push($name,'疫苗接种');
  60. break;
  61. case 5:
  62. array_push($name,'儿保预约');
  63. break;
  64. case 6:
  65. array_push($name,'服务包');
  66. break;
  67. case 7:
  68. array_push($name,'充值');
  69. break;
  70. }
  71. }
  72. return $name;
  73. })->label('info');
  74. $grid->column('money', __('满减券的优惠钱数'))->display(function ($money){
  75. return $money/100;
  76. });
  77. $grid->column('discount', __('折扣'));
  78. $grid->column('position_type', __('发放平台'))->using([1=>'领券中心',2=>'后台发放']);
  79. $grid->column('num', __('数量'));
  80. $grid->column('effective_days', __('有效天数'));
  81. $grid->column('start_time', __('有效期开始时间'))->display(function ($time){
  82. if ($time ==0)
  83. return '';
  84. return date('Y-m-d H:i:s',$time);
  85. });
  86. $grid->column('end_time', __('有效期结束时间'))->display(function ($time){
  87. if ($time ==0)
  88. return '';
  89. return date('Y-m-d H:i:s',$time);
  90. });
  91. return $grid;
  92. }
  93. /**
  94. * Make a show builder.
  95. *
  96. * @param mixed $id
  97. * @return Show
  98. */
  99. protected function detail($id)
  100. {
  101. $show = new Show(Coupon::findOrFail($id));
  102. $show->field('id', __('Id'));
  103. $show->field('name', __('Name'));
  104. $show->field('title', __('Title'));
  105. $show->field('desc', __('Desc'));
  106. $show->field('rules', __('Rules'));
  107. $show->field('icon', __('Icon'));
  108. $show->field('type', __('Type'));
  109. $show->field('usable_type', __('Usable type'));
  110. $show->field('money', __('Money'));
  111. $show->field('discount', __('Discount'));
  112. $show->field('min_consume_amount', __('Min consume amount'));
  113. $show->field('max_reduce_amount', __('Max reduce amount'));
  114. $show->field('expire_type', __('Expire type'));
  115. $show->field('effective_days', __('Effective days'));
  116. $show->field('start_time', __('Start time'));
  117. $show->field('end_time', __('End time'));
  118. $show->field('created_at', __('Created at'));
  119. $show->field('updated_at', __('Updated at'));
  120. return $show;
  121. }
  122. /**
  123. * Make a form builder.
  124. *
  125. * @return Form
  126. */
  127. protected function form()
  128. {
  129. $form = new Form(new Coupon());
  130. $form->text('name', __('优惠券名称'));
  131. $form->text('title', __('券标题'));
  132. $form->text('desc', __('使用说明'));
  133. $form->text('rules', __('使用规则'));
  134. $form->image('icon', __('券图标'))->help('图标大小48px * 48px');
  135. $form->select('type', '类型')
  136. ->options([
  137. 1 => '满减券',
  138. 2 => '折扣券',
  139. ])->when(1, function (Form $form) {
  140. $form->text('money', '满减券的优惠钱数');
  141. $form->text('min_consume_amount', '最低消费金额');
  142. })->when(2, function (Form $form) {
  143. $form->text('discount', '折扣');
  144. $form->text('max_reduce_amount', '折扣券的最大抵扣金额');
  145. });
  146. $form->select('position_type',__('发放平台'))->options([1=>'领券中心',2=>'后台发放']);
  147. $form->number('num', __('数量'));
  148. $form->select('usable_type',__('可用类型'))
  149. ->options([
  150. 1=>'全部产品通用',
  151. 2=>'部分产品可用'
  152. ])->when(2,function (Form $form){
  153. $form->hasMany('CouponType','可用类型',function (Form\NestedForm $form){
  154. $form->select('product_type','可用类型')->options([
  155. 1=>'电话咨询',
  156. 2=>'图文咨询',
  157. 3=>'门诊咨询',
  158. 4=>'疫苗接种',
  159. 5=>'儿保预约',
  160. 6=>'服务包',
  161. 7=>'充值',
  162. ]);
  163. });
  164. });
  165. $form->select('expire_type', '过期类型')
  166. ->options([
  167. 1 => '领取N天过期',
  168. 2 => '设置固定过期时间',
  169. ])->when(1, function (Form $form) {
  170. $form->text('effective_days', __('有效天数'));
  171. })->when(2, function (Form $form) {
  172. $form->date('start_time', __('有效期开始时间'));
  173. $form->date('end_time', __('有效期结束时间'));
  174. $form->saving(function ($form){
  175. $form->start_time = strtotime($form->start_time);
  176. $form->end_time = strtotime($form->end_time);
  177. });
  178. });
  179. return $form;
  180. }
  181. }