CouponManagementController.php 7.8 KB

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