CouponManagementController.php 8.9 KB

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