123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- namespace App\Admin\Controllers\CouponManagement;
- use App\Admin\Actions\backstage\Coupon\CouponDetails;
- use App\Admin\Actions\backstage\Coupon\CouponDistribution;
- use App\Admin\Actions\backstage\Coupon\CouponDistributions;
- use App\Admin\Actions\backstage\Coupon\UpdateCouponType;
- use App\Models\Coupon;
- use App\Models\CouponType;
- use Encore\Admin\Controllers\AdminController;
- use Encore\Admin\Form;
- use Encore\Admin\Grid;
- use Encore\Admin\Show;
- class CouponManagementController extends AdminController
- {
- /**
- * Title for current resource.
- *
- * @var string
- */
- protected $title = '优惠券管理';
- /**
- * Make a grid builder.
- *
- * @return Grid
- */
- protected function grid()
- {
- $grid = new Grid(new Coupon());
- $grid->tools(function (Grid\Tools $tools) {
- $tools->append(new CouponDistributions());
- });
- $grid->actions(function ($actions) {
- $actions->disableView();
- $actions->add(new CouponDetails());
- if ($actions->row->usable_type == 2)
- {
- $actions->add(new UpdateCouponType());
- }
- });
- $grid->filter(function($filter){
- $filter->disableIdFilter();
- $filter->like('name','优惠券名称');
- $filter->equal('type','优惠券类型')->select([
- 1 => '满减券',
- 2 => '折扣券'
- ]);
- $filter->equal('usable_type','优惠券类型')->select([
- 1 => '全部产品通用',
- 2 => '部分产品可用'
- ]);
- $filter->equal('position_type','发放平台')->select([
- 1 => '领券中心',
- 2 => '后台发放'
- ]);
- $filter->timestampBetween('start_time', "有效期开始时间")->datetime();
- $filter->timestampBetween('end_time', "有效期结束时间")->datetime();
- });
- $grid->model()->orderBy('id','desc');
- $grid->column('id', __('Id'));
- $grid->column('name', __('劵名称'));
- $grid->column('title', __('劵标题'));
- $grid->column('icon', __('券图标'))->lightbox(['width' =>'', 'height' => 30]);
- $grid->column('type', __('类型'))->using([1=>'满减券',2=>'折扣券']);
- $grid->column('usable_type', __('可用类型'))->using([1=>'全部产品通用',2=>'部分产品可用']);
- $grid->column('CouponType', __('可用产品'))->pluck('product_type','id')->display(function ($i){
- $name = [];
- foreach ($i as $value)
- {
- switch ($value){
- case 1:
- array_push($name,'电话咨询');
- break;
- case 2:
- array_push($name,'图文咨询');
- break;
- case 3:
- array_push($name,'门诊咨询');
- break;
- case 4:
- array_push($name,'疫苗接种');
- break;
- case 5:
- array_push($name,'儿保预约');
- break;
- case 6:
- array_push($name,'服务包');
- break;
- case 7:
- array_push($name,'充值');
- break;
- }
- }
- return $name;
- })->label('info');
- $grid->column('money', __('满减券的优惠钱数'))->display(function ($money){
- return $money/100;
- });
- $grid->column('discount', __('折扣'));
- $grid->column('position_type', __('发放平台'))->using([1=>'领券中心',2=>'后台发放']);
- $grid->column('num', __('数量'));
- $grid->column('expire_type', __('过期时间'))->using([1=>'领取N天过期',2=>'设置固定时间']);
- $grid->column('effective_days', __('有效天数'));
- $grid->column('start_time', __('有效期开始时间'))->display(function ($time){
- if ($time ==0)
- return '';
- return date('Y-m-d H:i:s',$time);
- });
- $grid->column('end_time', __('有效期结束时间'))->display(function ($time){
- if ($time ==0)
- return '';
- return date('Y-m-d H:i:s',$time);
- });
- return $grid;
- }
- /**
- * Make a show builder.
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- $show = new Show(Coupon::findOrFail($id));
- $show->field('id', __('Id'));
- $show->field('name', __('Name'));
- $show->field('title', __('Title'));
- $show->field('desc', __('Desc'));
- $show->field('rules', __('Rules'));
- $show->field('icon', __('Icon'));
- $show->field('type', __('Type'));
- $show->field('usable_type', __('Usable type'));
- $show->field('money', __('Money'));
- $show->field('discount', __('Discount'));
- $show->field('min_consume_amount', __('Min consume amount'));
- $show->field('max_reduce_amount', __('Max reduce amount'));
- $show->field('expire_type', __('Expire type'));
- $show->field('effective_days', __('Effective days'));
- $show->field('start_time', __('Start time'));
- $show->field('end_time', __('End time'));
- $show->field('created_at', __('Created at'));
- $show->field('updated_at', __('Updated at'));
- return $show;
- }
- /**
- * Make a form builder.
- *
- * @return Form
- */
- protected function form()
- {
- $form = new Form(new Coupon());
- $form->hidden('id', __('id'));
- $form->editing(function ($f){
- $f->model()->max_reduce_amount /=100;
- $f->model()->min_consume_amount /=100;
- $f->model()->money /=100;
- $f->model()->start_time = date('Y-m-d H:i:s',$f->model()->start_time);
- $f->model()->end_time = date('Y-m-d H:i:s',$f->model()->end_time);
- if ($f->model()->start_time == 0 || $f->model()->end_time)
- {
- $f->model()->end_time = date('Y-m-d H:i:s',time());
- $f->model()->start_time = date('Y-m-d H:i:s',time());
- }
- });
- $type = request('type');
- if (isset($type))
- {
- admin_toastr('最低消费金额不能小于满减的优惠钱数', 'error');
- }
- $form->text('name', __('优惠券名称'));
- $form->text('title', __('券标题'));
- $form->text('desc', __('使用说明'));
- $form->text('rules', __('使用规则'));
- $form->image('icon', __('券图标'))->help('图标大小48px * 48px');
- $form->select('type', '类型')
- ->options([
- 1 => '满减券',
- 2 => '折扣券',
- ])->when(1, function (Form $form) {
- $form->text('money', '满减券的优惠钱数');
- $form->text('min_consume_amount', '最低消费金额');
- })->when(2, function (Form $form) {
- $form->text('discount', '折扣');
- $form->text('max_reduce_amount', '折扣券的最大抵扣金额');
- });
- $form->select('position_type',__('发放平台'))->options([1=>'领券中心',2=>'后台发放']);
- $form->number('num', __('数量'));
- if ($form->isCreating())
- {
- $form->select('usable_type',__('可用类型'))
- ->options([
- 1=>'全部产品通用',
- 2=>'部分产品可用'
- ])->when(2,function (Form $form){
- $form->hasMany('CouponType','可用类型',function (Form\NestedForm $form){
- $form->select('product_type','可用类型')->options([
- 1=>'电话咨询',
- 2=>'图文咨询',
- 3=>'门诊咨询',
- 4=>'疫苗接种',
- 5=>'儿保预约',
- 6=>'服务包',
- 7=>'充值',
- ]);
- });
- });
- }else
- {
- $form->select('usable_type',__('可用类型'))
- ->options([
- 1=>'全部产品通用',
- 2=>'部分产品可用'
- ]);
- }
- $form->select('expire_type', '过期类型')
- ->options([
- 1 => '领取N天过期',
- 2 => '设置固定过期时间',
- ])->when(1, function (Form $form) {
- $form->text('effective_days', __('有效天数'));
- })->when(2, function (Form $form) {
- $form->datetime('start_time', __('有效期开始时间'));
- $form->datetime('end_time', __('有效期结束时间'));
- $form->saving(function ($form){
- $form->start_time = strtotime($form->start_time);
- $form->end_time = strtotime($form->end_time);
- $form->money = $form->money*100;
- $form->min_consume_amount = $form->min_consume_amount*100;
- $form->max_reduce_amount = $form->max_reduce_amount*100;
- if ($form->money >$form->min_consume_amount)
- {
- if ($form->isEditing)
- {
- $arr = request()->route()->parameters;
- return redirect('admin/coupons/'.$arr['coupon'].'/edit?type=1');
- }else
- {
- return redirect('admin/coupons/create?type=1');
- }
- }
- //如果过期类型是领取n天的话就把开始时间和结束时间置空
- if ($form->expire_type == 1)
- {
- $form->start_time = 0;
- $form->end_time = 0;
- }else{
- $form->effective_days = 0;
- }
- });
- });
- return $form;
- }
- }
|