CouponManagementController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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->disableIdFilter();
  42. $filter->like('name','优惠券名称');
  43. $filter->equal('type','优惠券类型')->select([
  44. 1 => '满减券',
  45. 2 => '折扣券'
  46. ]);
  47. $filter->equal('usable_type','优惠券类型')->select([
  48. 1 => '全部产品通用',
  49. 2 => '部分产品可用'
  50. ]);
  51. $filter->equal('position_type','发放平台')->select([
  52. 1 => '领券中心',
  53. 2 => '后台发放'
  54. ]);
  55. $filter->timestampBetween('start_time', "有效期开始时间")->datetime();
  56. $filter->timestampBetween('end_time', "有效期结束时间")->datetime();
  57. });
  58. $grid->model()->orderBy('id','desc');
  59. $grid->column('id', __('Id'));
  60. $grid->column('name', __('劵名称'));
  61. $grid->column('title', __('劵标题'));
  62. $grid->column('icon', __('券图标'))->lightbox(['width' =>'', 'height' => 30]);
  63. $grid->column('type', __('类型'))->using([1=>'满减券',2=>'折扣券']);
  64. $grid->column('usable_type', __('可用类型'))->using([1=>'全部产品通用',2=>'部分产品可用']);
  65. $grid->column('CouponType', __('可用产品'))->pluck('product_type','id')->display(function ($i){
  66. $name = [];
  67. foreach ($i as $value)
  68. {
  69. switch ($value){
  70. case 1:
  71. array_push($name,'电话咨询');
  72. break;
  73. case 2:
  74. array_push($name,'图文咨询');
  75. break;
  76. case 3:
  77. array_push($name,'门诊咨询');
  78. break;
  79. case 4:
  80. array_push($name,'疫苗接种');
  81. break;
  82. case 5:
  83. array_push($name,'儿保预约');
  84. break;
  85. case 6:
  86. array_push($name,'服务包');
  87. break;
  88. case 7:
  89. array_push($name,'充值');
  90. break;
  91. }
  92. }
  93. return $name;
  94. })->label('info');
  95. $grid->column('money', __('满减券的优惠钱数'))->display(function ($money){
  96. return $money/100;
  97. });
  98. $grid->column('discount', __('折扣'));
  99. $grid->column('position_type', __('发放平台'))->using([1=>'领券中心',2=>'后台发放']);
  100. $grid->column('num', __('数量'));
  101. $grid->column('expire_type', __('过期时间'))->using([1=>'领取N天过期',2=>'设置固定时间']);
  102. $grid->column('effective_days', __('有效天数'));
  103. $grid->column('start_time', __('有效期开始时间'))->display(function ($time){
  104. if ($time ==0)
  105. return '';
  106. return date('Y-m-d H:i:s',$time);
  107. });
  108. $grid->column('end_time', __('有效期结束时间'))->display(function ($time){
  109. if ($time ==0)
  110. return '';
  111. return date('Y-m-d H:i:s',$time);
  112. });
  113. return $grid;
  114. }
  115. /**
  116. * Make a show builder.
  117. *
  118. * @param mixed $id
  119. * @return Show
  120. */
  121. protected function detail($id)
  122. {
  123. $show = new Show(Coupon::findOrFail($id));
  124. $show->field('id', __('Id'));
  125. $show->field('name', __('Name'));
  126. $show->field('title', __('Title'));
  127. $show->field('desc', __('Desc'));
  128. $show->field('rules', __('Rules'));
  129. $show->field('icon', __('Icon'));
  130. $show->field('type', __('Type'));
  131. $show->field('usable_type', __('Usable type'));
  132. $show->field('money', __('Money'));
  133. $show->field('discount', __('Discount'));
  134. $show->field('min_consume_amount', __('Min consume amount'));
  135. $show->field('max_reduce_amount', __('Max reduce amount'));
  136. $show->field('expire_type', __('Expire type'));
  137. $show->field('effective_days', __('Effective days'));
  138. $show->field('start_time', __('Start time'));
  139. $show->field('end_time', __('End time'));
  140. $show->field('created_at', __('Created at'));
  141. $show->field('updated_at', __('Updated at'));
  142. return $show;
  143. }
  144. /**
  145. * Make a form builder.
  146. *
  147. * @return Form
  148. */
  149. protected function form()
  150. {
  151. $form = new Form(new Coupon());
  152. $form->hidden('id', __('id'));
  153. $form->editing(function ($f){
  154. $f->model()->max_reduce_amount /=100;
  155. $f->model()->min_consume_amount /=100;
  156. $f->model()->money /=100;
  157. });
  158. $type = request('type');
  159. if (isset($type))
  160. {
  161. admin_toastr('最低消费金额不能小于满减的优惠钱数', 'error');
  162. }
  163. $form->text('name', __('优惠券名称'));
  164. $form->text('title', __('券标题'));
  165. $form->text('desc', __('使用说明'));
  166. $form->text('rules', __('使用规则'));
  167. // dd($form->toArray());
  168. $form->image('icon', __('券图标'))->help('图标大小48px * 48px');
  169. $form->select('type', '类型')
  170. ->options([
  171. 1 => '满减券',
  172. 2 => '折扣券',
  173. ])->when(1, function (Form $form) {
  174. $form->text('money', '满减券的优惠钱数');
  175. $form->text('min_consume_amount', '最低消费金额');
  176. })->when(2, function (Form $form) {
  177. $form->text('discount', '折扣');
  178. $form->text('max_reduce_amount', '折扣券的最大抵扣金额');
  179. });
  180. $form->select('position_type',__('发放平台'))->options([1=>'领券中心',2=>'后台发放']);
  181. $form->number('num', __('数量'));
  182. if ($form->isCreating())
  183. {
  184. $form->select('usable_type',__('可用类型'))
  185. ->options([
  186. 1=>'全部产品通用',
  187. 2=>'部分产品可用'
  188. ])->when(2,function (Form $form){
  189. $form->hasMany('CouponType','可用类型',function (Form\NestedForm $form){
  190. $form->select('product_type','可用类型')->options([
  191. 1=>'电话咨询',
  192. 2=>'图文咨询',
  193. 3=>'门诊咨询',
  194. 4=>'疫苗接种',
  195. 5=>'儿保预约',
  196. 6=>'服务包',
  197. 7=>'充值',
  198. ]);
  199. });
  200. });
  201. }else
  202. {
  203. $form->select('usable_type',__('可用类型'))
  204. ->options([
  205. 1=>'全部产品通用',
  206. 2=>'部分产品可用'
  207. ]);
  208. }
  209. $form->select('expire_type', '过期类型')
  210. ->options([
  211. 1 => '领取N天过期',
  212. 2 => '设置固定过期时间',
  213. ])->when(1, function (Form $form) {
  214. $form->text('effective_days', __('有效天数'));
  215. })->when(2, function (Form $form) {
  216. $form->datetime('start_time', __('有效期开始时间'));
  217. $form->datetime('end_time', __('有效期结束时间'));
  218. $form->saving(function ($form){
  219. $form->start_time = strtotime($form->start_time);
  220. $form->end_time = strtotime($form->end_time);
  221. $form->money = $form->money*100;
  222. $form->min_consume_amount = $form->min_consume_amount*100;
  223. $form->max_reduce_amount = $form->max_reduce_amount*100;
  224. if ($form->money >$form->min_consume_amount)
  225. {
  226. if ($form->isEditing)
  227. {
  228. $arr = request()->route()->parameters;
  229. return redirect('admin/coupons/'.$arr['coupon'].'/edit?type=1');
  230. }else
  231. {
  232. return redirect('admin/coupons/create?type=1');
  233. }
  234. }
  235. });
  236. });
  237. return $form;
  238. }
  239. }