VaccineController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Community\Controllers;
  3. use App\Models\Order;
  4. use App\Models\Organization;
  5. use App\Models\OrganizationVaccine;
  6. use App\Models\OrganizationVaccines;
  7. use App\Models\Vaccine;
  8. use Encore\Admin\Controllers\AdminController;
  9. use Encore\Admin\Facades\Admin;
  10. use Encore\Admin\Form;
  11. use Encore\Admin\Grid;
  12. use Encore\Admin\Show;
  13. use Encore\Admin\Widgets\Table;
  14. class VaccineController extends AdminController
  15. {
  16. /**
  17. * Title for current resource.
  18. *
  19. * @var string
  20. */
  21. protected $title = '疫苗';
  22. /**
  23. * Make a grid builder.
  24. *
  25. * @return Grid
  26. */
  27. protected function grid()
  28. {
  29. $grid = new Grid(new Vaccine());
  30. $grid->model()->orderByDesc('id');
  31. $org_id = Admin::user()->org_id;
  32. if(!empty($org_id)){
  33. $grid->model()->where(['org_id'=>$org_id]);
  34. }
  35. $grid->disableCreateButton(false);
  36. $grid->column('id', __('ID'));
  37. $grid->column('name', __('疫苗名称'));
  38. // $grid->column('organizations', '机构库存')->display(function (){
  39. // return '点击查看';
  40. // })->modal('机构库存', function ($model) {
  41. // $org = $model->organizationvaccines()->get()->map(function ($comment) {
  42. // return $comment->only(['org_id','vaccine_id','stock']);
  43. // });
  44. // $org = $org->toArray();
  45. // for($i=0;$i<count($org);$i++)
  46. // {
  47. // $id = $org[$i]['org_id'];
  48. // $org[$i]['vaccine_id'] = $this->name;
  49. // $org[$i]['org_name'] = Organization::where('id',$id)->value('name');
  50. // }
  51. // return new Table(['ID','疫苗','库存','机构名称'], $org);
  52. // });
  53. $grid->column('stocks', __('剩余库存'))->display(function (){
  54. return OrganizationVaccine::where(['vaccine_id'=>$this->id])->sum('stock');
  55. });
  56. $grid->column('org_id', __('已使用量'))->display(function () {
  57. return Order::where(['product_type'=>4])->wherehas('orderVaccine',function ($query){
  58. $query->where('vaccine_id',$this->id);
  59. })->count();
  60. });
  61. $grid->column('today_num', __('今日预约'))->display(function () {
  62. return Order::where(['product_type'=>4])->wherehas('orderVaccine',function ($query){
  63. $query->where('vaccine_id',$this->id);
  64. })->count();
  65. });;
  66. $grid->filter(function ($fliter){
  67. // $fliter->equal('type','类别')->select(OrganizationVaccine::getType());
  68. $fliter->like('name','疫苗');
  69. });
  70. $grid->actions(function ($actions) {
  71. // append一个操作
  72. $actions->append('<a href=""><i class="fa fa-header">跳转</i></a>');
  73. // prepend一个操作
  74. $actions->prepend('<a href="http://www.baidu.com" target="_blank"><i class="fa fa-cab"></i></a>');
  75. $actions->disableDelete();
  76. });
  77. return $grid;
  78. }
  79. /**
  80. * Make a show builder.
  81. *
  82. * @param mixed $id
  83. * @return Show
  84. */
  85. protected function detail($id)
  86. {
  87. $show = new Show(Vaccine::findOrFail($id));
  88. $show->field('id', __('Id'));
  89. $show->field('name', __('疫苗名称'));
  90. $show->field('created_at', __('创建时间'));
  91. $show->field('updated_at', __('更新时间'));
  92. return $show;
  93. }
  94. /**
  95. * Make a form builder.
  96. *
  97. * @return Form
  98. */
  99. protected function form()
  100. {
  101. $form = new Form(new Vaccine());
  102. $orglist = Organization::pluck('name','id');
  103. $org_id = Admin::user()->org_id;
  104. // if($org_id){
  105. // $orglist = Organization::where(['id'=>$org_id])->pluck('name','id');
  106. // }
  107. // $form->editing(function ($f){
  108. // $f->model()->price /= 100;
  109. // });
  110. $form->hidden('org_id', __('类型'))->value($org_id);
  111. // $form->select('type', __('类型'))->options([1=>'一类',2=>'二类'])->default(1);
  112. // $form->select('org_id','机构')->options($orglist)->rules('required',['requried'=>'请选择机构']);
  113. $form->text('name', __('疫苗名称'))->rules('required',['required'=>'请输入疫苗']);
  114. // $form->text('name', __('疫苗名称'))->creationRules(['required', "unique:vaccines"])
  115. // ->updateRules(['required', "unique:vaccines,name,{{id}}"]);
  116. $states = [
  117. 'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
  118. 'on' => ['value' => 1, 'text' => '启用', 'color' => 'success'],
  119. ];
  120. $form->switch('states','状态')->states($states)->default(1);
  121. // $form->text('stock', __('库存'))->rules('required',['requried'=>'请填写库存']);
  122. // $form->textarea('remark', __('备注'));
  123. // $form->saving(function ($form){
  124. // $form->price = $form->price*100;
  125. // });
  126. return $form;
  127. }
  128. }