工单审批流程实现
xuexi 2025-08-27 11:12:03 发表在:PHP 查看数:36

工单部分

无论是什么工单,在提交的时候。插入流程字段。即区别该工单走的是哪个流程,同时获取提交人的id,因为类型对应了执行的顺序(流程),默认从1开始,所以提交工单的时候。默认加上1的节点,状态为“审批中”。例如:

  $form->hidden('jiedian_id')->default(1);//节点的顺序是从1开始的,默认给节点1审批
 $form->hidden('admin_id', __('申请人ID'))->default( \Auth::user()->id);
 $form->hidden('zt', __('申请人ID'))->default('审批中');
$form->select('leixing_id', __('提交流程类型'))->options(ShenpiLeixing::all()->pluck('name', 'id'))->required();

审批待办

流程提交以后,需要参与审批的领导们进行审核。首先他们需要知道那些工单需要进行审核

$grid->model()
    ->from('cb_shenpi_jiedian')
    ->join('cb_faqi as g', 'cb_shenpi_jiedian.leixing_id', '=', 'g.leixing_id')
    ->where('cb_shenpi_jiedian.shenpirenid', \Auth::user()->id)
    //->whereRaw('cb_shenpi_jiedian.shunxu = g.jiedian_id + 1') // 修正这里
    ->whereColumn('cb_shenpi_jiedian.shunxu', 'g.jiedian_id') // 关键修正
    ->where('g.zt', '审批中')
    ->orderBy('g.id', 'desc')
    ->select('cb_shenpi_jiedian.*', 'g.*', 'g.id as id', 'g.zt as zt', 'g.created_at as shenpi_time', 'g.title as title', 'g.sqms as sqms', 'g.leixing_id as leixing_id','cb_shenpi_jiedian.shunxu as jiedian_id');

点击按钮进行审核

$grid->column('sh', __('审核'))->display(function(){

       return '<a href="jilu/insert/create?jiedian_id='.$this->jiedian_id.'&leixing_id='.$this->leixing_id.'&gongdan_id='.$this->id.'"  class="btn btn-sm btn-primary">审批</a>';  

       });

写入审批记录

<?php

namespace App\Admin\Controllers\Shenpi;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Models\Shenpi\Shenpijilu;
use App\Models\Shebei\Shebeilist;
use App\Models\Shenpi\Shenpi;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Encore\Admin\Facades\Admin;


class JiluinsertController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '流程审批记录';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Shenpijilu);
        $grid->model()->where('shenpiren_id', Admin::user()->id)->orderBy('id', 'desc');




        $grid->column('id', __('ID'))->sortable();  
        $grid->column('gongdan_id', __('工单'))->display(function(){

              return $this->gongdan_id;
        });  

        $grid->column('shenpijieguo', __('审批结果'));


    //导出结束
    // 完全移除操作列
  //$grid->disableActions();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Shenpi::findOrFail($id));

        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {

         /* 2. 记录审批操作,同意或者拒绝 */
        $form = new Form(new Shenpijilu);

        $form->hidden('id', __('ID'));
        $form->radio('shenpijieguo', __('结论'))->options(['同意' => '同意', '拒绝'=> '拒绝']);
        $form->textarea('yijian', __('意见'))->rows(10);
     //   $form->hidden('shenpi_id', __('审批流程ID'))->default(request('shenpi_id'));
        $form->hidden('shenpiren_id', __('审批人'))->default( \Auth::user()->id );
        $form->hidden('jiedian_id')->default(  request('jiedian_id')  );
        $form->hidden('leixing_id')->default(  request('leixing_id') );
        $form->hidden('gongdan_id')->default(  request('gongdan_id') );
        $form->hidden('created_at', __('创建日期'));
        $form->hidden('updated_at', __('更新日期'));

        $form->saved(function (Form $form) {

        //return response(request('shenpi_id'));
        //查询该用户对应类型的节点id

       $zuida = DB::table('cb_shenpi_jiedian')
            ->where('leixing_id', $form->leixing_id)
            ->max('shunxu');  //查询该流程下的审批步骤最多是几步

        //判断是同意还是拒绝
        if($form->shenpijieguo=='同意'){
        /* 1. 更新当前审批节点状态 */

          if( $zuida == $form->jiedian_id){

              DB::table('cb_faqi')
            ->where('id', $form->gongdan_id ) 
            ->update(['zt' => '通过','jiedian_id'=>$form->jiedian_id, 'updated_at' => now()]);
             }else{
           /**更新状态**/  /***激活下一个流程***/
             DB::table('cb_faqi')
            ->where('id', $form->gongdan_id ) 
            ->update(['zt' => '审批中','jiedian_id'=>$form->jiedian_id + 1, 'updated_at' => now()]);

           }

    }

      if($form->shenpijieguo=='拒绝')  
         {
              /* 1. 更新当前审批节点状态 */ /* 3. 终止整个工单 */
        DB::table('cb_faqi')
        ->where('id', $form->gongdan_id ) 
        ->update(['zt' => $form->shenpijieguo,'jiedian_id'=>$form->jiedian_id, 'updated_at' => now()]);

        } 

  });


        return $form;
    }
}

已审批查看

<?php

namespace App\Admin\Controllers\Shenpi;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Models\Shenpi\Shenpijilu;
use App\Models\Shebei\Shebeilist;
use App\Models\Shenpi\Shenpijiedian;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class YishenpiController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '已审批列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Shenpijiedian);
         $grid->disableCreateButton();
        $curl = "daiban";
    $grid->tools(function (Grid\Tools $tools) use($curl){
    $tools->append('<div class="btn-group pull-right grid-create-btn" style="margin-right: 10px">
          <a href="'.$curl.'" class="btn btn-sm btn-success" title="待审批">
             <i class="fa fa-file-word-o"></i><span class="hidden-xs">&nbsp;&nbsp;待审批</span>
          </a>
      </div>' );
    });

       //查询待审批工单
      //  $grid->model()->where('shenpirenid', \Auth::user()->id ) ->where('zt', '待处理')->orderBy('id', 'desc');
      // 联表并选择需要的字段

//   $grid->model()
//     // 主表为 cb_shenpi
//     ->from('cb_shenpi')
//     // 关联工单表 cb_faqi
//     ->join('cb_faqi as g', 'cb_shenpi.gongdan_id', '=', 'g.id')
//     // 审批人ID条件(主表条件)
//     ->where('cb_shenpi.shenpirenid', \Auth::user()->id)
//     // 工单状态条件(关联表条件)
//     ->where('cb_shenpi.zt', '!=', '审批中') // 不等于 '审批中'
//     // 按主表ID排序
//     ->orderBy('cb_shenpi.id', 'desc')
//     // 选择需要的字段(主表和关联表)
//     ->select('cb_shenpi.*', 'g.*', 'cb_shenpi.id as id' ,'g.zt as zt','cb_shenpi.created_at as shenpi_time','g.title as title','g.sqms as sqms');


 $grid->model()
    // 主表为 cb_shenpi
    ->from('cb_shenpi_jiedian')
    // 关联工单表 cb_faqi
    ->join('cb_faqi as g', 'cb_shenpi_jiedian.leixing_id', '=', 'g.leixing_id')
    // 审批人ID条件(主表条件)
    ->where('cb_shenpi_jiedian.shenpirenid', \Auth::user()->id)
    // 工单状态条件(关联表条件)
    ->where('g.zt', '!=', '审批中') // 不等于 '审批中'
    // 按主表ID排序
    ->orderBy('g.id', 'desc')
    // 选择需要的字段(主表和关联表)
    ->select('cb_shenpi_jiedian.*', 'g.*', 'g.id as id' ,'g.zt as zt','g.created_at as shenpi_time','g.title as title','g.sqms as sqms');

// 在表格中展示关联表字段




// 在表格中展示关联表字段
          $grid->column('id', __('ID'))->sortable();
          $grid->column('shenpi_time', '工单发起时间');
          $grid->column('title', '工单名称');

          $grid->column('sqms', __('工单描述'));
          $grid->column('user_id', __('申请人'))->display(function(){

             return $this->adminuser->name;
          });
          $grid->column('zt', __('工单状态'));

        $grid->column('a', __('配置流程节点'))->display(function(){
         return sprintf(
        '<a href="liucheng?leixing_id=%d&leixing_name=%s" style="color:blue">审批节点</a>',
        $this->id,
         urlencode($this->name)
        );

        });  
       $grid->column('sh', __('审核'))->display(function(){

       return '<a href="jilu/insert/create?shenpi_id='.$this->id.'"  class="btn btn-sm btn-primary">审批</a>';  

       });

    //导出结束
    // 完全移除操作列
  //$grid->disableActions();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Shenpijiedian::findOrFail($id));

        $show->field('id', __('ID'));
        $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 Shenpijilu);

        $form->hidden('id', __('ID'));



        return $form;
    }
}
最近访问时间:2025-08-30 17:54:19
知识库:376条鸣谢:TAY  备案号:蜀ICP备2024090044号-1