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