CouponManagementController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. $f->model()->start_time = date('Y-m-d H:i:s',$f->model()->start_time);
  158. $f->model()->end_time = date('Y-m-d H:i:s',$f->model()->end_time);
  159. if ($f->model()->start_time == 0 || $f->model()->end_time)
  160. {
  161. $f->model()->end_time = date('Y-m-d H:i:s',time());
  162. $f->model()->start_time = date('Y-m-d H:i:s',time());
  163. }
  164. });
  165. $type = request('type');
  166. if (isset($type))
  167. {
  168. admin_toastr('最低消费金额不能小于满减的优惠钱数', 'error');
  169. }
  170. $form->text('name', __('优惠券名称'));
  171. $form->text('title', __('券标题'));
  172. $form->text('desc', __('使用说明'));
  173. $form->text('rules', __('使用规则'));
  174. $form->image('icon', __('券图标'))->help('图标大小48px * 48px');
  175. $form->select('type', '类型')
  176. ->options([
  177. 1 => '满减券',
  178. 2 => '折扣券',
  179. ])->when(1, function (Form $form) {
  180. $form->text('money', '满减券的优惠钱数');
  181. $form->text('min_consume_amount', '最低消费金额');
  182. })->when(2, function (Form $form) {
  183. $form->text('discount', '折扣');
  184. $form->text('max_reduce_amount', '折扣券的最大抵扣金额');
  185. });
  186. $form->select('position_type',__('发放平台'))->options([1=>'领券中心',2=>'后台发放']);
  187. $form->number('num', __('数量'));
  188. if ($form->isCreating())
  189. {
  190. $form->select('usable_type',__('可用类型'))
  191. ->options([
  192. 1=>'全部产品通用',
  193. 2=>'部分产品可用'
  194. ])->when(2,function (Form $form){
  195. $form->hasMany('CouponType','可用类型',function (Form\NestedForm $form){
  196. $form->select('product_type','可用类型')->options([
  197. 1=>'电话咨询',
  198. 2=>'图文咨询',
  199. 3=>'门诊咨询',
  200. 4=>'疫苗接种',
  201. 5=>'儿保预约',
  202. 6=>'服务包',
  203. 7=>'充值',
  204. ]);
  205. });
  206. });
  207. }else
  208. {
  209. $form->select('usable_type',__('可用类型'))
  210. ->options([
  211. 1=>'全部产品通用',
  212. 2=>'部分产品可用'
  213. ]);
  214. }
  215. $form->select('expire_type', '过期类型')
  216. ->options([
  217. 1 => '领取N天过期',
  218. 2 => '设置固定过期时间',
  219. ])->when(1, function (Form $form) {
  220. $form->text('effective_days', __('有效天数'));
  221. })->when(2, function (Form $form) {
  222. $form->datetime('start_time', __('有效期开始时间'));
  223. $form->datetime('end_time', __('有效期结束时间'));
  224. $form->saving(function ($form){
  225. $form->start_time = strtotime($form->start_time);
  226. $form->end_time = strtotime($form->end_time);
  227. $form->money = $form->money*100;
  228. $form->min_consume_amount = $form->min_consume_amount*100;
  229. $form->max_reduce_amount = $form->max_reduce_amount*100;
  230. if ($form->money >$form->min_consume_amount)
  231. {
  232. if ($form->isEditing)
  233. {
  234. $arr = request()->route()->parameters;
  235. return redirect('admin/coupons/'.$arr['coupon'].'/edit?type=1');
  236. }else
  237. {
  238. return redirect('admin/coupons/create?type=1');
  239. }
  240. }
  241. //如果过期类型是领取n天的话就把开始时间和结束时间置空
  242. if ($form->expire_type == 1)
  243. {
  244. $form->start_time = 0;
  245. $form->end_time = 0;
  246. }else{
  247. $form->effective_days = 0;
  248. }
  249. });
  250. });
  251. return $form;
  252. }
  253. }