2025-02-27 09:55:33 +08:00

750 lines
31 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\adminapi\controller\works\finance;
use app\adminapi\controller\BaseAdminController;
use app\adminapi\validate\works\finance\InvoiceValidate;
use app\common\model\auth\Admin;
use app\common\model\dept\Dept;
use app\common\model\works\bgsp\OaFlow;
use think\facade\Db;
class InvoiceController extends BaseAdminController
{
public function add(){
$params = (new InvoiceValidate())->post()->goCheck('add');
$params['admin_id'] = $this->adminId;
$params['annex'] = !empty($params['annex']) ? json_encode($params['annex']) : null;
$params['create_time'] = time();
$params['check_status'] = 0;
$params['check_step_sort'] = 0;
$flow = Db::name('oa_flow')->where('id',$params['flow_id'])->find();
if(empty($flow)){
return $this->fail("审批流程信息不存在");
}
if($flow['check_type'] == 2){
if(empty($params['check_admin_ids'])){
return $this->fail("请选择审核人");
}
}else{
if(empty($flow['flow_list'])){
return $this->fail("当前审批流程未设置完全");
}
}
$params['copy_uids'] = !empty($params['copy_uids']) ? $params['copy_uids'] : $flow['copy_uids'];
if (empty($params['check_admin_ids'])) {
$flow_list = unserialize($flow['flow_list']);
if($flow_list[0]['flow_type'] == 1){
//获取部门负责人
$dept = Admin::where('id',$this->adminId)->value('dept_id');
if(empty($dept)){
return $this->fail('当前用户未设置部门,请联系管理员');
}
$leader = Dept::where('id',$dept)->value('leader_id');
if(empty($leader)){
return $this->fail('当前部门负责人还未设置,请联系管理员');
}else{
$params['check_admin_ids'] = $leader;
}
}else{
$params['check_admin_ids'] = $flow_list[0]['flow_uids'];
}
$aid = Db::name('oa_invoice')->strict(false)->field(true)->insertGetId($params);
foreach ($flow_list as $key => &$value){
$value['action_id'] = $aid;
$value['sort'] = $key;
$value['create_time'] = time();
$value['type'] = 3;
}
Db::name('oa_flow_step')->strict(false)->field(true)->insertAll($flow_list);
}
else{
$aid = Db::name('oa_invoice')->strict(false)->field(true)->insertGetId($params);
$flow_step = array(
'action_id' => $aid,
'flow_uids' => $params['check_admin_ids'],
'create_time' => time(),
'type' => 3
);
Db::name('oa_flow_step')->strict(false)->field(true)->insertGetId($flow_step);
}
$step_id = Db::name('oa_flow_step')->where('action_id',$aid)->where('type',3)->order('id asc')->find();
//添加提交申请记录
$checkData=array(
'action_id' => $aid,
'check_user_id' => $this->adminId,
'content' => '提交申请',
'check_time' => time(),
'create_time' => time(),
'type' => 3,
'step_id' => $step_id['id']
);
$record_id = Db::name('oa_flow_record')->strict(false)->field(true)->insertGetId($checkData);
return $record_id ? $this->success('ok') : $this->fail('fail');
}
public function edit(){
$params = (new InvoiceValidate())->post()->goCheck('edit');
$params['admin_id'] = $this->adminId;
$params['annex'] = !empty($params['annex']) ? json_encode($params['annex']) : null;
$params['create_time'] = time();
$params['check_status'] = 0;
$params['check_step_sort'] = 0;
$flow = Db::name('oa_flow')->where('id',$params['flow_id'])->find();
if(empty($flow)){
return $this->fail("审批流程信息不存在");
}
if($flow['check_type'] == 2){
if(empty($param['check_admin_ids'])){
return $this->fail("请选择审核人");
}
}else{
if(empty($flow['flow_list'])){
return $this->fail("当前审批流程未设置完全");
}
}
//删除原来的审核流程和审核记录
Db::name('oa_flow_step')->where(['action_id'=>$params['id'],'type'=>3])->update(['delete_time'=>time()]);
Db::name('oa_flow_record')->where(['action_id'=>$params['id'],'type'=>3])->update(['delete_time'=>time(),'is_invalid'=>1]);
if (empty($params['check_admin_ids'])) {
$flow_list = unserialize($flow['flow_list']);
if($flow_list[0]['flow_type'] == 1){
//获取部门负责人
$dept = Admin::where('id',$this->adminId)->value('dept_id');
if(empty($dept)){
return $this->fail('当前用户未设置部门,请联系管理员');
}
$leader = Dept::where('id',$dept)->value('leader_id');
if(empty($leader)){
return $this->fail('当前部门负责人还未设置,请联系管理员');
}else{
$params['check_admin_ids'] = $leader;
}
}else{
$params['check_admin_ids'] = $flow_list[0]['flow_uids'];
}
Db::name('oa_invoice')->where('id',$params['id'])->strict(false)->field(true)->update($params);
foreach ($flow_list as $key => &$value){
$value['action_id'] = $params['id'];
$value['sort'] = $key;
$value['create_time'] = time();
$value['type'] = 3;
}
Db::name('oa_flow_step')->strict(false)->field(true)->insertAll($flow_list);
}
else{
Db::name('oa_invoice')->where('id',$params['id'])->strict(false)->field(true)->insertGetId($params);
$flow_step = array(
'action_id' => $params['id'],
'flow_uids' => $params['check_admin_ids'],
'create_time' => time(),
'type' => 3
);
Db::name('oa_flow_step')->strict(false)->field(true)->insertGetId($flow_step);
}
$step_id = Db::name('oa_flow_step')->where('action_id',$params['id'])->where('type',3)->order('id asc')->find();
//添加提交申请记录
$checkData=array(
'action_id' => $params['id'],
'check_user_id' => $this->adminId,
'content' => '提交申请',
'check_time' => time(),
'create_time' => time(),
'type' => 3,
'step_id' => $step_id['id']
);
$record_id = Db::name('oa_flow_record')->strict(false)->field(true)->insertGetId($checkData);
return $record_id ? $this->success('ok') : $this->fail('fail');
}
public function lists(){
$params = $this->request->get(['start_time','end_time','status','page_no','page_size']);
$page_no = !empty($params['page_no']) ? $params['page_no'] : 1;
$page_size = !empty($params['page_size']) ? $params['page_size'] : 15;
$where = [];
if(!empty($params['start_time']) && !empty($params['end_time'])){
if(strtotime($params['end_time']) <= strtotime($params['start_time'])){
return $this->fail('结束时间不等小于开始时间');
}
$where[] = ['create_time','between',[strtotime($params['start_time']),strtotime($params['end_time'])]];
}
if(!empty($params['status'])){
$where[] = match ($params['status']){
'1'=> ['check_status','=',0], //待审核
'2'=> ['check_status','=',1], //审核中
'3'=> [['check_status','=',2]], //审批通过,待开具
'4'=> ['check_status','=',3], //审核不通过
'5'=> ['check_status','=',4], //撤销审核
'6'=> [['check_status','=',5]], //审批通过,已开具
'7'=> [['check_status','=',10]], //已作废
};
}else{
$where[] = ['check_status','in','0,1,2,3,4,5,10'];
}
$lists = Db::name('oa_invoice')->field('id,type,invoice_title,invoice_subject,amount,invoice_type,check_status,admin_id,create_time,check_admin_ids,open_admin_id,open_time,code')->where('admin_id',$this->adminId)->where($where)
->page($page_no, $page_size)->order('id desc')->select()->each(function($data){
$admin = Admin::field('name,dept_id')->where('id',$data['admin_id'])->findOrEmpty();
$dept = Dept::where('id',$admin['dept_id'])->value('name');
$check_admin_users = Admin::where('id','in',$data['check_admin_ids'])->column('name');
$data['user_name'] = $admin['name'];
$data['dept_name'] = $dept ?? '';
$data['check_admin_users'] = !empty($check_admin_users) ? implode(',',$check_admin_users) : '';
$data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);
$data['check_status_text'] = match ($data['check_status']){
0=>'待审核',1=>'审核中',2=>'审核通过,待开具',3=>'审核不通过',4=>'撤销审核',5=>'已开具',10=>'已作废'
};
$data['type_text'] = match ($data['type']){
1=>'企业',2=>'个人'
};
$data['invoice_type_text'] = match ($data['invoice_type']){
1=>'增值税专用发票',2=>'普通发票',3=>'专用发票'
};
$data['open_time'] = !empty($data['open_time']) ? date('Y-m-d',$data['open_time']) : '';
$data['open_admin_name'] = Admin::where('id',$data['open_admin_id'])?->value('name');
return $data;
})->toArray();
$count = Db::name('oa_invoice')->where($where)->where('admin_id',$this->adminId)->count();
return $this->success('成功', compact('count', 'lists', 'page_no', 'page_size'));
}
public function detail(){
$params = (new InvoiceValidate())->post()->goCheck('detail');
$data = Db::name('oa_invoice')->where('id',$params['id'])->find();
$admin = Admin::field('name,dept_id')->where('id',$data['admin_id'])->findOrEmpty();
$dept = Dept::where('id',$admin['dept_id'])->value('name');
$check_admin_users = Admin::where('id','in',$data['check_admin_ids'])->column('name');
$copy_users = Admin::where('id','in',$data['copy_uids'])->column('name');
$data['copy_users'] = !empty($copy_users) ? implode(',',$copy_users) : '';
$data['user_name'] = $admin['name'];
$data['dept_name'] = $dept ?? '';
$data['check_admin_users'] = !empty($check_admin_users) ? implode(',',$check_admin_users) : '';
$data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);
$data['annex'] = json_decode($data['annex'],true);
$data['check_status_text'] = match ($data['check_status']){
0=>'待审核',1=>'审核中',2=>'审核通过,待开具',3=>'审核不通过',4=>'撤销审核',5=>'已开具',10=>'已作废'
};
$data['type_text'] = match ($data['type']){
1=>'企业',2=>'个人'
};
$data['invoice_type_text'] = match ($data['invoice_type']){
1=>'增值税专用发票',2=>'普通发票',3=>'专用发票'
};
$data['is_cash_text'] = match ($data['is_cash']){
0=>'未到账',1=>'部分到账',2=>'全部到账'
};
$data['open_time'] = !empty($data['open_time']) ? date('Y-m-d',$data['open_time']) : '';
$data['open_admin_name'] = Admin::where('id',$data['open_admin_id'])?->value('name');
$data['flow_info'] = OaFlow::where('id',$data['flow_id'])->findOrEmpty();
$data['record'] = Db::name('oa_flow_record')->field('check_user_id,check_time,status,content')->where('action_id',$params['id'])->where('type',3)->order('id desc')->select()->each(function($data){
$data['check_time'] = date('Y-m-d H:i:s',$data['check_time']);
$user = Admin::where('id',$data['check_user_id'])->value('name');
$data['check_user_name'] = $user ?? '';
$data['status_text'] = match ($data['status']){
0=>'提交',1=>'通过',2=>'拒绝',3=>'撤销'
};
return $data;
})->toArray();
$step = Db::name('oa_flow_step')->field('id,action_id,flow_uids,flow_type,sort')->where('action_id',$params['id'])->where('type',3)->order('sort asc')->select()->toArray();
foreach ($step as $key => &$val) {
$user_id_info = Admin::field('id,name')->where('id','in',$val['flow_uids'])->select()->toArray();
foreach ($user_id_info as $k => &$v) {
$v['check_time'] = 0;
$v['content'] = '';
$v['status'] = 0;
$check_array = Db::name('oa_flow_record')->where(['check_user_id' => $v['id'],'step_id' => $val['id'],'type'=>3])->order('check_time desc')->select()->toArray();
if(!empty($check_array)){
$checked = $check_array[0];
$v['check_time'] = date('Y-m-d H:i', $checked['check_time']);
$v['content'] = $checked['content'];
$v['status'] = $checked['status'];
}
}
$check_list = Db::name('oa_flow_record')
->field('f.*,a.name')
->alias('f')
->join('admin a', 'a.id = f.check_user_id', 'left')
->where(['f.step_id' => $val['id']])->where('f.status','>',0)->select()->toArray();
foreach ($check_list as $kk => &$vv) {
$vv['check_time_str'] = date('Y-m-d H:i', $vv['check_time']);
}
$val['user_id_info'] = $user_id_info;
$val['check_list'] = $check_list;
}
$data['steps'] = $step;
return $this->data($data);
}
public function copy(){
$params = $this->request->get(['start_time','end_time','page_no','page_size']);
$page_no = !empty($params['page_no']) ? $params['page_no'] : 1;
$page_size = !empty($params['page_size']) ? $params['page_size'] : 15;
$map = [];
if(!empty($params['start_time']) && !empty($params['end_time'])){
if(strtotime($params['end_time']) <= strtotime($params['start_time'])){
return $this->fail('结束时间不等小于开始时间');
}
$map[] = ['create_time','between',[strtotime($params['start_time']),strtotime($params['end_time'])]];
}
//查询条件
$map = [];
$map[] = ['f.check_status', '=', 2];
$map[] = ['', 'exp', Db::raw("FIND_IN_SET('{$this->adminId}',f.copy_uids)")];
$lists = Db::name('oa_invoice')
->field('f.*,a.name as user_name,d.name as dept_name')
->alias('f')
->join('admin a', 'a.id = f.admin_id', 'left')
->join('dept d', 'd.id = a.dept_id', 'left')
->where($map)
->order('f.id desc')
->group('f.id')
->page($page_no, $page_size)->select()
->each(function($item){
if($item['check_status']<2 && !empty($item['check_admin_ids'])){
$check_user = Db::name('admin')->where('id','in',$item['check_admin_ids'])->column('name');
$item['check_admin_users'] = implode(',',$check_user);
}
$item['create_time'] = date('Y-m-d H:i:s',$item['create_time']);
$item['check_status_text'] = match ($item['check_status']){
0=>'待审核',1=>'审核中',2=>'审核通过,待开具',3=>'审核不通过',4=>'撤销审核',5=>'已开具',10=>'已作废'
};
$item['type_text'] = match ($item['type']){
1=>'企业',2=>'个人'
};
$item['invoice_type_text'] = match ($item['invoice_type']){
1=>'增值税专用发票',2=>'普通发票',3=>'专用发票'
};
$item['open_time'] = !empty($item['open_time']) ? date('Y-m-d',$item['open_time']) : '';
$item['open_admin_name'] = Admin::where('id',$item['open_admin_id'])?->value('name');
return $item;
})->toArray();
$count = Db::name('oa_invoice')
->field('f.*,a.name as user_name,d.name as dept_name')
->alias('f')
->join('admin a', 'a.id = f.admin_id', 'left')
->join('dept d', 'd.id = a.dept_id', 'left')
->where($map)->count();
return $this->success('成功', compact('count', 'lists', 'page_no', 'page_size'));
}
public function deal_with(){
$params = $this->request->get();
$status = !empty($params['status']) ? $params['status'] : 0;
//查询条件
$map1 = [];
$map2 = [];
$map1[] = ['', 'exp', Db::raw("FIND_IN_SET('{$this->adminId}',f.check_admin_ids)")];
$map2[] = ['', 'exp', Db::raw("FIND_IN_SET('{$this->adminId}',f.flow_admin_ids)")];
$page_no = !empty($params['page_no']) ? $params['page_no'] : 1;
$page_size = !empty($params['page_size']) ? $params['page_size'] : 15;
$lists = [];
$count = 0;
if($status == 0){
$lists = Db::name('oa_invoice')
->field('f.*,a.name as user_name,d.name as dept_name')
->alias('f')
->join('admin a', 'a.id = f.admin_id', 'left')
->join('dept d', 'd.id = a.dept_id', 'left')
->whereOr([$map1,$map2])
->order('f.id desc')
->group('f.id')
->page($page_no, $page_size)->select()
->each(function($item){
if($item['check_status']<2 && !empty($item['check_admin_ids'])){
$check_user = Db::name('admin')->where('id','in',$item['check_admin_ids'])->column('name');
$item['check_admin_users'] = implode(',',$check_user);
}
$item['create_time'] = date('Y-m-d H:i:s',$item['create_time']);
$item['check_status_text'] = match ($item['check_status']){
0=>'待审核',1=>'审核中',2=>'审核通过',3=>'审核不通过',4=>'撤销审核',5=>'已开具',10=>'已作废'
};
$item['type_text'] = match ($item['type']){
1=>'企业',2=>'个人'
};
$item['invoice_type_text'] = match ($item['invoice_type']){
1=>'增值税专用发票',2=>'普通发票',3=>'专用发票'
};
$item['open_time'] = !empty($item['open_time']) ? date('Y-m-d',$item['open_time']) : '';
$item['open_admin_name'] = Admin::where('id',$item['open_admin_id'])?->value('name');
return $item;
})->toArray();
$count = Db::name('oa_invoice')
->field('f.*,a.name as user_name,d.name as dept_name')
->alias('f')
->join('admin a', 'a.id = f.admin_id', 'left')
->join('dept d', 'd.id = a.dept_id', 'left')
->whereOr([$map1,$map2])->count();
}
if($status == 1){
$lists = Db::name('oa_invoice')
->field('f.*,a.name as user_name,d.name as dept_name')
->alias('f')
->join('admin a', 'a.id = f.admin_id', 'left')
->join('dept d', 'd.id = a.dept_id', 'left')
->where($map1)
->order('f.id desc')
->group('f.id')
->page($page_no, $page_size)->select()
->each(function($item){
if($item['check_status']<2 && !empty($item['check_admin_ids'])){
$check_user = Db::name('admin')->where('id','in',$item['check_admin_ids'])->column('name');
$item['check_admin_users'] = implode(',',$check_user);
}
$item['create_time'] = date('Y-m-d H:i:s',$item['create_time']);
$item['check_status_text'] = match ($item['check_status']){
0=>'待审核',1=>'审核中,待开具',2=>'审核通过',3=>'审核不通过',4=>'撤销审核',5=>'已开具',10=>'已作废'
};
$item['type_text'] = match ($item['type']){
1=>'企业',2=>'个人'
};
$item['invoice_type_text'] = match ($item['invoice_type']){
1=>'增值税专用发票',2=>'普通发票',3=>'专用发票'
};
$item['open_time'] = !empty($item['open_time']) ? date('Y-m-d',$item['open_time']) : '';
$item['open_admin_name'] = Admin::where('id',$item['open_admin_id'])?->value('name');
return $item;
})->toArray();
$count = Db::name('oa_invoice')
->field('f.*,a.name as user_name,d.name as dept_name')
->alias('f')
->join('admin a', 'a.id = f.admin_id', 'left')
->join('dept d', 'd.id = a.dept_id', 'left')
->where($map1)->count();
}
if($status == 2){
$lists = Db::name('oa_invoice')
->field('f.*,a.name as user_name,d.name as dept_name')
->alias('f')
->join('admin a', 'a.id = f.admin_id', 'left')
->join('dept d', 'd.id = a.dept_id', 'left')
->where($map2)
->order('f.id desc')
->group('f.id')
->page($page_no, $page_size)->select()
->each(function($item, $key){
if($item['check_status']<2 && !empty($item['check_admin_ids'])){
$check_user = Db::name('admin')->where('id','in',$item['check_admin_ids'])->column('name');
$item['check_admin_users'] = implode(',',$check_user);
}
$item['create_time'] = date('Y-m-d H:i:s',$item['create_time']);
$item['check_status_text'] = match ($item['check_status']){
0=>'待审核',1=>'审核中,待开具',2=>'审核通过',3=>'审核不通过',4=>'撤销审核',5=>'已开具',10=>'已作废'
};
$item['type_text'] = match ($item['type']){
1=>'企业',2=>'个人'
};
$item['invoice_type_text'] = match ($item['invoice_type']){
1=>'增值税专用发票',2=>'普通发票',3=>'专用发票'
};
$item['open_time'] = !empty($item['open_time']) ? date('Y-m-d',$item['open_time']) : '';
$item['open_admin_name'] = Admin::where('id',$item['open_admin_id'])?->value('name');
return $item;
})->toArray();
$count = Db::name('oa_invoice')
->field('f.*,a.name as user_name,d.name as dept_name')
->alias('f')
->join('admin a', 'a.id = f.admin_id', 'left')
->join('dept d', 'd.id = a.dept_id', 'left')
->where($map2)->count();
}
return $this->success('成功', compact('count', 'lists', 'page_no', 'page_size'));
}
public function check(){
$params = $this->request->post(['id','check','content','check_node','check_admin_ids']);
if(empty($params['id'])){
return $this->fail('缺少数据主键');
}
if(empty($params['check']) || !in_array($params['check'],[1,2,3])){
return $this->fail('审核参数错误');
}
$data = Db::name('oa_invoice')->where('id',$params['id'])->find();
if(empty($data)){
return $this->fail('数据信息不存在');
}
//撤销
if($params['check'] == 3){
if($data['admin_id'] != $this->adminId){
return $this->fail('你不是该申请的发起者,无权撤销');
}
//撤销审核,数据操作
$params['check_status'] = 4;
$params['check_admin_ids'] ='';
$params['check_step_sort'] = 0;
$res = Db::name('oa_invoice')->strict(false)->field('check_step_sort,check_status,check_admin_ids')->update($params);
if($res){
$checkData=array(
'action_id' => $params['id'],
'step_id' => 0,
'check_user_id' => $this->adminId,
'type' => 3,
'check_time' => time(),
'status' => $params['check'],
'content' => $params['content'] ?? '',
'create_time' => time()
);
Db::name('oa_flow_record')->strict(false)->field(true)->insertGetId($checkData);
return $this->success('ok');
}else{
return $this->fail('fail');
}
}
//拒绝
if($params['check'] == 2){
//当前审核节点详情
$step = Db::name('oa_flow_step')->where(['action_id'=>$params['id'],'sort'=>$data['check_step_sort'],'type'=>3])->find();
$check_admin_ids = explode(",", strval($data['check_admin_ids']));
if (!in_array($this->adminId, $check_admin_ids)){
return $this->fail('您没权限审核该审批');
}
//拒绝审核,数据操作
$params['check_status'] = 3;
$params['last_admin_id'] = $this->adminId;
$params['flow_admin_ids'] = $data['flow_admin_ids'].$this->adminId.',';
$params['check_admin_ids'] = '';
if($step['flow_type'] == 4){
//获取上一步的审核信息
$prev_step = Db::name('oa_flow_step')->where(['action_id'=>$params['id'],'sort'=>($data['check_step_sort']-1),'type'=>3])->find();
if($prev_step){
//存在上一步审核
$params['check_step_sort'] = $prev_step['sort'];
$params['check_admin_ids'] = $prev_step['flow_uids'];
$params['check_status'] = 1;
}
else{
//不存在上一步审核,审核初始化步骤
$params['check_step_sort'] = 0;
$params['check_admin_ids'] = '';
$params['check_status'] = 0;
}
}
$res = Db::name('oa_invoice')->strict(false)->field('check_step_sort,check_status,last_admin_id,flow_admin_ids,check_admin_ids')->update($params);
if($res){
$checkData=array(
'action_id' => $params['id'],
'step_id' => $step['id'],
'check_user_id' => $this->adminId,
'type' => 3,
'check_time' => time(),
'status' => $params['check'],
'content' => $params['content'] ?? '',
'create_time' => time()
);
Db::name('oa_flow_record')->strict(false)->field(true)->insertGetId($checkData);
return $this->success('ok');
}
else{
return $this->fail('fail');
}
}
//通过
if($params['check'] == 1){
//当前审核节点详情
$step = Db::name('oa_flow_step')->where(['action_id'=>$params['id'],'sort'=>$data['check_step_sort'],'type'=>3])->find();
$check_admin_ids = explode(",", strval($data['check_admin_ids']));
if (!in_array($this->adminId, $check_admin_ids)){
return $this->fail('您没权限审核该审批');
}
//多人会签审批
if($step['flow_type'] == 3){
//查询当前会签记录数
$check_count = Db::name('oa_flow_record')->where(['action_id'=>$params['id'],'step_id'=>$step['id'],'type'=>3])->count();
//当前会签记应有记录数
$flow_count = explode(',', $step['flow_uids']);
if(($check_count+1) >=count($flow_count)){
$next_step = Db::name('oa_flow_step')->where(['action_id'=>$params['id'],'sort'=>($data['check_step_sort']+1),'type'=>3])->find();
if($next_step){
//存在下一步审核
if($next_step['flow_type'] == 1){
//获取部门负责人
$dept = Admin::where('id',$data['admin_id'])->value('dept_id');
if(empty($dept)){
return $this->fail('当前用户未设置部门,请联系管理员');
}
$leader = Dept::where('id',$dept)->value('leader_id');
if(empty($leader)){
return $this->fail('当前部门负责人还未设置,请联系管理员');
}else{
$params['check_admin_ids'] = $leader;
}
}else{
$params['check_admin_ids'] = $next_step['flow_uids'];
}
$params['check_step_sort'] = $data['check_step_sort']+1;
$params['check_status'] = 1;
}else{
//不存在下一步审核,审核结束
$params['check_status'] = 2;
$params['check_admin_ids'] ='';
}
}else{
$params['check_status'] = 1;
$params['check_admin_ids'] = $step['flow_uids'];
}
}else if($step['flow_type'] == 0){
//自由人审批
if($params['check_node'] == 2){
$next_step = $data['check_step_sort']+1;
$flow_step = array(
'action_id' => $params['id'],
'sort' => $next_step,
'type' => 3,
'flow_uids' => $params['check_admin_ids'],
'create_time' => time()
);
$fid = Db::name('oa_flow_step')->strict(false)->field(true)->insertGetId($flow_step);
//下一步审核步骤
$params['check_step_sort'] = $next_step;
$params['check_status'] = 1;
}
else{
//不存在下一步审核,审核结束
$params['check_status'] = 2;
$params['check_admin_ids'] ='';
}
}else{
$next_step = Db::name('oa_flow_step')->where(['action_id'=>$params['id'],'sort'=>($data['check_step_sort']+1),'type'=>3])->find();
if($next_step){
//存在下一步审核
if($next_step['flow_type'] == 1){
//获取部门负责人
$dept = Admin::where('id',$data['admin_id'])->value('dept_id');
if(empty($dept)){
return $this->fail('当前用户未设置部门,请联系管理员');
}
$leader = Dept::where('id',$dept)->value('leader_id');
if(empty($leader)){
return $this->fail('当前部门负责人还未设置,请联系管理员');
}else{
$params['check_admin_ids'] = $leader;
}
}
else{
$params['check_admin_ids'] = $next_step['flow_uids'];
}
$params['check_step_sort'] = $data['check_step_sort']+1;
$params['check_status'] = 1;
}
else{
//不存在下一步审核,审核结束
$params['check_status'] = 2;
$params['check_admin_ids'] ='';
}
}
if($params['check_status'] == 1 && empty($params['check_admin_ids'])){
return $this->fail('找不到下一步的审批人该审批流程设置有问题请联系HR或者管理员');
}
//审核通过数据操作
$params['last_admin_id'] = $this->adminId;
$params['flow_admin_ids'] = $data['flow_admin_ids'].$this->adminId.',';
$res = Db::name('oa_invoice')->strict(false)->field('check_step_sort,check_status,last_admin_id,flow_admin_ids,check_admin_ids')->update($params);
if($res){
$checkData=array(
'action_id' => $params['id'],
'step_id' => $step['id'],
'check_user_id' => $this->adminId,
'type' => 3,
'check_time' => time(),
'status' => $params['check'],
'content' => $params['content'] ?? '',
'create_time' => time()
);
Db::name('oa_flow_record')->strict(false)->field(true)->insertGetId($checkData);
return $this->success('ok');
}
else{
return $this->fail('fail');
}
}
return $this->fail();
}
public function lists2(){
$params = $this->request->get(['start_time','end_time','status','page_no','page_size']);
$page_no = !empty($params['page_no']) ? $params['page_no'] : 1;
$page_size = !empty($params['page_size']) ? $params['page_size'] : 15;
$where = [];
if(!empty($params['start_time']) && !empty($params['end_time'])){
if(strtotime($params['end_time']) <= strtotime($params['start_time'])){
return $this->fail('结束时间不等小于开始时间');
}
$where[] = ['create_time','between',[strtotime($params['start_time']),strtotime($params['end_time'])]];
}
if(!empty($params['status'])){
$where[] = match ($params['status']){
'1'=> ['check_status','=',2], //审批通过,待开具
'2'=> ['check_status','=',5], //审批通过,已开具
'3'=> [['check_status','=',10]], //已作废
};
}else{
$where[] = ['check_status','in','2,5,10'];
}
$lists = Db::name('oa_invoice')->field('id,type,invoice_title,invoice_subject,amount,invoice_type,check_status,admin_id,create_time,check_admin_ids,open_admin_id,open_time,code,delivery')->where($where)
->page($page_no, $page_size)->order('id desc')->select()->each(function($data){
$admin = Admin::field('name,dept_id')->where('id',$data['admin_id'])->findOrEmpty();
$dept = Dept::where('id',$admin['dept_id'])->value('name');
$check_admin_users = Admin::where('id','in',$data['check_admin_ids'])->column('name');
$data['user_name'] = $admin['name'];
$data['dept_name'] = $dept ?? '';
$data['check_admin_users'] = !empty($check_admin_users) ? implode(',',$check_admin_users) : '';
$data['create_time'] = date('Y-m-d H:i:s',$data['create_time']);
$data['check_status_text'] = match ($data['check_status']){
0=>'待审核',1=>'审核中',2=>'审核通过,待开具',3=>'审核不通过',4=>'撤销审核',5=>'已开具',10=>'已作废'
};
$data['type_text'] = match ($data['type']){
1=>'企业',2=>'个人'
};
$data['invoice_type_text'] = match ($data['invoice_type']){
1=>'增值税专用发票',2=>'普通发票',3=>'专用发票'
};
$data['open_time'] = !empty($data['open_time']) ? date('Y-m-d',$data['open_time']) : '';
$data['open_admin_name'] = Admin::where('id',$data['open_admin_id'])?->value('name');
return $data;
})->toArray();
$count = Db::name('oa_invoice')->where($where)->count();
return $this->success('成功', compact('count', 'lists', 'page_no', 'page_size'));
}
public function invoice(){
$params = (new InvoiceValidate())->post()->goCheck('invoice');
$data = Db::name('oa_invoice')->where('id',$params['id'])->find();
if(empty($data)){
return $this->success('申请信息不存在');
}
if($data['check_status'] != 2 && $data['check_status'] != 5 ){
return $this->success('当前申请未审核通过');
}
$update_data = [
'code' => $params['code'],
'open_time' => strtotime($params['open_time']),
'open_admin_id' => $this->adminId,
'delivery' => $params['delivery'] ?? '',
'check_status' => 5,
];
$res = Db::name('oa_invoice')->where('id',$params['id'])->update($update_data);
return $res ? $this->success('ok') : $this->fail('fail');
}
public function voids(){
$params = (new InvoiceValidate())->post()->goCheck('detail');
$data = Db::name('oa_invoice')->where('id',$params['id'])->find();
if(empty($data)){
return $this->fail('申请信息不存在');
}
if($data['check_status'] < 5){
return $this->fail('当前申请未开票');
}
if($data['is_cash'] > 0){
return $this->fail('发票已经新增有到账记录,请先反到账后再作废发票');
}
if($data['check_status']==5){
$res = Db::name('oa_invoice')->where('id',$params['id'])->update(['check_status'=>10]);
}else{
$res = Db::name('oa_invoice')->where('id',$params['id'])->update(['check_status'=>5]);
}
return $res ? $this->success('ok') : $this->fail('fail');
}
}