220 lines
7.5 KiB
PHP
220 lines
7.5 KiB
PHP
<?php
|
||
|
||
namespace App\Admin\Controllers;
|
||
|
||
use App\Admin\Renderable\CouponRangeTable;
|
||
use App\Admin\Repositories\Coupon;
|
||
use App\Exceptions\BizException;
|
||
use App\Models\Coupon as CouponModel;
|
||
use Dcat\Admin\Admin;
|
||
use Dcat\Admin\Form;
|
||
use Dcat\Admin\Grid;
|
||
use Dcat\Admin\Http\Controllers\AdminController;
|
||
use Dcat\Admin\Layout\Content;
|
||
use Dcat\Admin\Show;
|
||
use Illuminate\Http\Request;
|
||
|
||
class CouponController extends AdminController
|
||
{
|
||
/**
|
||
* Make a grid builder.
|
||
*
|
||
* @return Grid
|
||
*/
|
||
protected function grid()
|
||
{
|
||
return Grid::make(new Coupon(), function (Grid $grid) {
|
||
$grid->column('id')->sortable();
|
||
$grid->column('name');
|
||
$grid->column('type')->using([
|
||
1 =>'抵扣券',
|
||
2 =>'折扣券',
|
||
])->label();
|
||
$grid->column('amount')->display(function ($value) {
|
||
if ($this->type == 1) {
|
||
return $value.'元';
|
||
} elseif ($this->type == 2) {
|
||
return $value.'折';
|
||
}
|
||
})->label();
|
||
$grid->column('threshold')->append('元');
|
||
$grid->column('limit')->display(function ($value) {
|
||
if ($value == 0) {
|
||
return '不限量';
|
||
}
|
||
return $value;
|
||
});
|
||
$grid->column('sent');
|
||
$grid->column('use_day')->append('天');
|
||
$grid->column('use_start_at');
|
||
$grid->column('use_end_at');
|
||
$grid->column('created_at')->sortable();
|
||
$grid->model()->orderBy('created_at', 'desc');
|
||
/** 操作 **/
|
||
//新增
|
||
if (Admin::user()->can('dcat.admin.coupons.create')) {
|
||
$grid->disableCreateButton(false);
|
||
$grid->enableDialogCreate();
|
||
$grid->setDialogFormDimensions('40%', '70%');
|
||
}
|
||
//修改
|
||
$grid->showQuickEditButton(Admin::user()->can('dcat.admin.coupons.edit'));
|
||
//删除以及自定义操作
|
||
$grid->actions(function (Grid\Displayers\Actions $actions) {
|
||
$actions->disableDelete(Admin::user()->cannot('dcat.admin.coupons.destroy'));
|
||
if (Admin::user()->can('dcat.admin.coupons.range_list')) {
|
||
$actions->append('<a href="'.admin_route('coupons.range_list', ['coupon'=>$actions->row]).'"><i class="fa fa-eye"></i> 使用范围</a>');
|
||
}
|
||
});
|
||
|
||
/** 查询 **/
|
||
$grid->filter(function (Grid\Filter $filter) {
|
||
$filter->panel();
|
||
$filter->liek('name')->width(3);
|
||
});
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Make a show builder.
|
||
*
|
||
* @param mixed $id
|
||
*
|
||
* @return Show
|
||
*/
|
||
protected function detail($id)
|
||
{
|
||
return Show::make($id, new Coupon(), function (Show $show) {
|
||
$show->field('id');
|
||
$show->field('name');
|
||
$show->field('type');
|
||
$show->field('amount');
|
||
$show->field('threshold');
|
||
$show->field('limit');
|
||
$show->field('sent');
|
||
$show->field('use_day');
|
||
$show->field('use_start_at');
|
||
$show->field('use_end_at');
|
||
$show->field('created_at');
|
||
$show->field('updated_at');
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Make a form builder.
|
||
*
|
||
* @return Form
|
||
*/
|
||
protected function form()
|
||
{
|
||
return Form::make(new Coupon(), function (Form $form) {
|
||
$form->display('id');
|
||
|
||
$form->text('name')->required();
|
||
$form->number('limit')->min(0)->default(0)->help('0为不限量');
|
||
|
||
if ($form->isCreating() || ($form->isEditing() && !$form->model()->hasReceived())) {
|
||
$form->radio('type')
|
||
->when(1, function (Form $form) {
|
||
$form->currency('amount1')->symbol('¥')->help('例:100.00表示优惠100元。')->value($form->model()->amount);
|
||
})
|
||
->when(2, function (Form $form) {
|
||
$form->currency('amount2')->symbol('%')->help('例:0.95表示95折。')->value($form->model()->amount);
|
||
})
|
||
->options([
|
||
1 =>'抵扣券',
|
||
2 =>'折扣券',
|
||
])->default(1);
|
||
$form->hidden('amount');
|
||
|
||
$form->currency('threshold')->symbol('¥')->default(0);
|
||
$form->number('use_day')->min(0)->default(1)->help('单位:天;指领取后几天内有效。');
|
||
$form->datetimeRange('use_start_at', 'use_end_at', '使用时间')->help('设置时间后,期限字段失效。');
|
||
}
|
||
|
||
// $form->editing(function (Form $form) {
|
||
// if (!$form->model()->hasReceived()) {
|
||
// $form->radio('type')
|
||
// ->when(1, function (Form $form) {
|
||
// $form->currency('amount1')->symbol('¥')->help('例:100.00表示优惠100元。')->value($form->model()->amount);
|
||
// })
|
||
// ->when(2, function (Form $form) {
|
||
// $form->currency('amount2')->symbol('%')->help('例:0.95表示95折。')->value($form->model()->amount);
|
||
// })
|
||
// ->options([
|
||
// 1 =>'抵扣券',
|
||
// 2 =>'折扣券',
|
||
// ])->default(1);
|
||
// $form->hidden('amount');
|
||
|
||
// $form->currency('threshold')->symbol('¥')->default(0);
|
||
// $form->number('use_day')->default(0)->help('单位:天;指领取后几天内有效。');
|
||
// $form->datetimeRange('use_start_at', 'use_end_at', '使用时间')->help('设置时间后,期限字段失效。');
|
||
// }
|
||
// });
|
||
|
||
$form->saving(function (Form $form) {
|
||
if ($form->type) {
|
||
$amount = 'amount'.$form->type;
|
||
$form->amount = $form->$amount;
|
||
|
||
$form->deleteInput('amount1');
|
||
$form->deleteInput('amount2');
|
||
}
|
||
});
|
||
|
||
$form->display('created_at');
|
||
$form->display('updated_at');
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 修改删除判断
|
||
*
|
||
* @param [type] $id
|
||
* @return void
|
||
*/
|
||
public function destroy($id)
|
||
{
|
||
$coupon = CouponModel::findOrFail($id);
|
||
if ($coupon->hasSendTask() || $coupon->hasReceived()) {
|
||
throw new BizException(__('coupon.options.deny_message'));
|
||
}
|
||
return parent::destroy($id);
|
||
}
|
||
|
||
/**
|
||
* 获取优惠券接口
|
||
*
|
||
* @param Request $request
|
||
* @return void
|
||
*/
|
||
public function coupons(Request $request)
|
||
{
|
||
$name = $request->input('q');
|
||
|
||
$query = CouponModel::select('id', 'name as text');
|
||
|
||
if ($name) {
|
||
$query->where('name', 'like', "%$name%");
|
||
return $query->paginate(null);
|
||
}
|
||
|
||
return response()->json($query->get());
|
||
}
|
||
|
||
/**
|
||
* 使用范围列表
|
||
*
|
||
* @param Content $content
|
||
* @param CouponModel $coupon
|
||
* @return void
|
||
*/
|
||
public function rangeList(Content $content, CouponModel $coupon)
|
||
{
|
||
return $content->header(__('coupon.labels.coupons'))
|
||
->description($coupon->name)
|
||
->body(CouponRangeTable::grid($coupon->id));
|
||
}
|
||
}
|