CouponManagementController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\Models\Coupon;
  6. use Encore\Admin\Controllers\AdminController;
  7. use Encore\Admin\Form;
  8. use Encore\Admin\Grid;
  9. use Encore\Admin\Show;
  10. class CouponManagementController extends AdminController
  11. {
  12. /**
  13. * Title for current resource.
  14. *
  15. * @var string
  16. */
  17. protected $title = '优惠券管理';
  18. /**
  19. * Make a grid builder.
  20. *
  21. * @return Grid
  22. */
  23. protected function grid()
  24. {
  25. $grid = new Grid(new Coupon());
  26. $grid->actions(function ($actions) {
  27. $actions->disableView();
  28. $actions->add(new CouponDetails());
  29. if($actions->row->position_type == 2 )
  30. {
  31. $actions->add(new CouponDistribution());
  32. }
  33. });
  34. $grid->column('id', __('Id'));
  35. $grid->column('name', __('劵名称'));
  36. $grid->column('title', __('劵标题'));
  37. $grid->column('icon', __('券图标'))->image();
  38. $grid->column('type', __('类型'))->using([1=>'满减券',2=>'折扣券']);
  39. $grid->column('usable_type', __('可用类型'))->using([1=>'全部产品通用',2=>'部分产品可用']);
  40. $grid->column('money', __('满减券的优惠钱数'))->display(function ($money){
  41. return $money/100;
  42. });
  43. $grid->column('discount', __('折扣'));
  44. $grid->column('position_type', __('发放平台'))->using([1=>'领券中心',2=>'后台发放']);
  45. $grid->column('num', __('数量'));
  46. $grid->column('effective_days', __('有效天数'));
  47. $grid->column('start_time', __('有效期开始时间'))->display(function ($time){
  48. return date('Y-m-d H:i:s',$time);
  49. });
  50. $grid->column('end_time', __('有效期结束时间'))->display(function ($time){
  51. return date('Y-m-d H:i:s',$time);
  52. });
  53. return $grid;
  54. }
  55. /**
  56. * Make a show builder.
  57. *
  58. * @param mixed $id
  59. * @return Show
  60. */
  61. protected function detail($id)
  62. {
  63. $show = new Show(Coupon::findOrFail($id));
  64. $show->field('id', __('Id'));
  65. $show->field('name', __('Name'));
  66. $show->field('title', __('Title'));
  67. $show->field('desc', __('Desc'));
  68. $show->field('rules', __('Rules'));
  69. $show->field('icon', __('Icon'));
  70. $show->field('type', __('Type'));
  71. $show->field('usable_type', __('Usable type'));
  72. $show->field('money', __('Money'));
  73. $show->field('discount', __('Discount'));
  74. $show->field('min_consume_amount', __('Min consume amount'));
  75. $show->field('max_reduce_amount', __('Max reduce amount'));
  76. $show->field('expire_type', __('Expire type'));
  77. $show->field('effective_days', __('Effective days'));
  78. $show->field('start_time', __('Start time'));
  79. $show->field('end_time', __('End time'));
  80. $show->field('created_at', __('Created at'));
  81. $show->field('updated_at', __('Updated at'));
  82. return $show;
  83. }
  84. /**
  85. * Make a form builder.
  86. *
  87. * @return Form
  88. */
  89. protected function form()
  90. {
  91. $form = new Form(new Coupon());
  92. $form->text('name', __('优惠券名称'));
  93. $form->text('title', __('券标题'));
  94. $form->text('desc', __('使用说明'));
  95. $form->text('rules', __('使用规则'));
  96. $form->image('icon', __('券图标'));
  97. $form->select('type', '类型')
  98. ->options([
  99. 1 => '满减券',
  100. 2 => '折扣券',
  101. ])->when(1, function (Form $form) {
  102. $form->text('money', '满减券的优惠钱数');
  103. $form->text('min_consume_amount', '最低消费金额');
  104. })->when(2, function (Form $form) {
  105. $form->text('discount', '折扣');
  106. $form->text('max_reduce_amount', '折扣券的最大抵扣金额');
  107. });
  108. $form->select('position_type',__('发放平台'))->options([1=>'领券中心',2=>'后台发放']);
  109. $form->number('num', __('数量'));
  110. $form->select('usable_type',__('可用类型'))->options([1=>'全部产品通用',2=>'部分产品可用']);
  111. $form->select('expire_type', '过期类型')
  112. ->options([
  113. 1 => '领取N天过期',
  114. 2 => '设置固定过期时间',
  115. ])->when(1, function (Form $form) {
  116. $form->text('effective_days', __('有效天数'));
  117. })->when(2, function (Form $form) {
  118. $form->date('start_time', __('有效期开始时间'))->required();
  119. $form->date('end_time', __('有效期结束时间'))->required()->rules(
  120. 'date|after:start_time',['after' => '结束时间小于开始时间']
  121. );
  122. $form->saving(function ($form){
  123. $form->start_time = strtotime($form->start_time);
  124. $form->end_time = strtotime($form->end_time);
  125. });
  126. });
  127. return $form;
  128. }
  129. }