746 lines
32 KiB
PHP
746 lines
32 KiB
PHP
<?php
|
||
|
||
namespace app\oa\controller\works\finance;
|
||
|
||
use app\admin\controller\BaseAdminController;
|
||
use app\oa\validate\works\finance\ExpenseValidate;
|
||
use app\common\model\auth\Admin;
|
||
use app\common\model\dept\Dept;
|
||
use app\common\model\works\bgsp\OaFlow;
|
||
use think\facade\Db;
|
||
|
||
class ExpenseController extends BaseAdminController
|
||
{
|
||
public function add(){
|
||
$params = (new ExpenseValidate())->post()->goCheck('add');
|
||
$params['income_month'] = strtotime($params['income_month']);
|
||
$params['expense_time'] = strtotime($params['expense_time']);
|
||
$params['admin_id'] = $this->adminId;
|
||
$params['annex'] = !empty($params['annex']) ? json_encode($params['annex']) : null;
|
||
$params['check_status'] = 0;
|
||
$params['check_step_sort'] = 0;
|
||
$params['create_time'] = time();
|
||
$params['detail'] = json_encode($params['detail']);
|
||
$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_expense')->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'] = 2;
|
||
}
|
||
Db::name('oa_flow_step')->strict(false)->field(true)->insertAll($flow_list);
|
||
}
|
||
else{
|
||
$aid = Db::name('oa_expense')->strict(false)->field(true)->insertGetId($params);
|
||
$flow_step = array(
|
||
'action_id' => $aid,
|
||
'flow_uids' => $params['check_admin_ids'],
|
||
'create_time' => time(),
|
||
'type' => 2
|
||
);
|
||
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',2)->order('id asc')->find();
|
||
//添加提交申请记录
|
||
$checkData=array(
|
||
'action_id' => $aid,
|
||
'check_user_id' => $this->adminId,
|
||
'content' => '提交申请',
|
||
'check_time' => time(),
|
||
'create_time' => time(),
|
||
'type' => 2,
|
||
'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 ExpenseValidate())->post()->goCheck('edit');
|
||
$params['income_month'] = strtotime($params['income_month']);
|
||
$params['expense_time'] = strtotime($params['expense_time']);
|
||
$params['annex'] = !empty($params['annex']) ? json_encode($params['annex']) : null;
|
||
$params['check_status'] = 0;
|
||
$params['check_step_sort'] = 0;
|
||
$params['detail'] = json_encode($params['detail']);
|
||
$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'=>2])->update(['delete_time'=>time()]);
|
||
Db::name('oa_flow_record')->where(['action_id'=>$params['id'],'type'=>2])->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_expense')->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'] = 2;
|
||
}
|
||
Db::name('oa_flow_step')->strict(false)->field(true)->insertAll($flow_list);
|
||
}
|
||
else{
|
||
Db::name('oa_expense')->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' => 2
|
||
);
|
||
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',2)->order('id asc')->find();
|
||
//添加提交申请记录
|
||
$checkData=array(
|
||
'action_id' => $params['id'],
|
||
'check_user_id' => $this->adminId,
|
||
'content' => '提交申请',
|
||
'check_time' => time(),
|
||
'create_time' => time(),
|
||
'type' => 2,
|
||
'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 detail(){
|
||
$params = (new ExpenseValidate())->get()->goCheck('detail');
|
||
$data = Db::name('oa_expense')->where('id',$params['id'])->find();
|
||
$data['check_status_text'] = match ($data['check_status']){
|
||
0=>'待审核',1=>'审核中',2=>'审核通过,待打款',3=>'审核不通过',4=>'撤销审核'
|
||
};
|
||
$data['income_month'] = date('Y-m',$data['income_month']);
|
||
$data['expense_time'] = date('Y-m-d',$data['expense_time']);
|
||
$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['check_admin_users'] = !empty($check_admin_users) ? implode(',',$check_admin_users) : '';
|
||
$data['copy_users'] = !empty($copy_users) ? implode(',',$copy_users) : '';
|
||
$data['detail'] = empty($data['detail']) ? [] : json_decode($data['detail'],true);
|
||
$data['annex'] = empty($data['annex']) ? [] : json_decode($data['annex'],true);
|
||
$data['total_amount'] = 0;
|
||
foreach ($data['detail'] as &$v){
|
||
$data['total_amount'] += $v['amount'];
|
||
//1-交通费 2-住宿费 3-餐补费 4-招待费 5-汽油费 6-其他费
|
||
$v['cate_name'] = match ($v['cate_id']){
|
||
'1'=>'交通费', '2'=>'住宿费', '3'=>'餐补费', '4'=>'招待费', '5'=>'汽油费', '6'=>'其他费', default=>''
|
||
};
|
||
}
|
||
$data['admin_name'] = Admin::where('id',$data['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',2)->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',2)->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'=>2])->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 lists(){
|
||
$params = $this->request->get();
|
||
$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],['pay_admin_id','=',0],['pay_time','=',0]], //审批通过,待打款
|
||
'4'=> ['check_status','=',3], //审核不通过
|
||
'5'=> ['check_status','=',4], //撤销审核
|
||
'6'=> [['check_status','=',2],['pay_admin_id','>',0],['pay_time','>',0]], //审批通过,已打款
|
||
};
|
||
}else{
|
||
$where[] = ['check_status','in','0,1,2,3,4'];
|
||
}
|
||
$lists = Db::name('oa_expense')->field('id,code,income_month,expense_time,admin_id,check_admin_ids,check_status,pay_admin_id,pay_time,create_time,detail')->where('admin_id',$this->adminId)->where($where)
|
||
->page($page_no, $page_size)->order('id desc')->select()->each(function($data){
|
||
$admin = Admin::field('id,name,dept_id')->where('id',$data['admin_id'])->findOrEmpty();
|
||
if (!empty($admin['dept_id'])) {
|
||
$dept = Dept::where('id',$admin['dept_id'])->value('name');
|
||
$data['dept_name'] = $dept;
|
||
} else {
|
||
$data['dept_name'] = '';
|
||
}
|
||
$check_admin_users = Admin::where('id','in',$data['check_admin_ids'])->column('name');
|
||
$data['user_name'] = $admin['name'];
|
||
$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['income_month'] = date('Y-m',$data['income_month']);
|
||
$data['expense_time'] = date('Y-m-d',$data['expense_time']);
|
||
$data['pay_time'] = !empty($data['pay_time']) ? date('Y-m-d H:i:s',$data['pay_time']) : '';
|
||
$data['pay_admin_name'] = Admin::where('id',$data['pay_admin_id'])?->value('name');
|
||
$data['check_status_text'] = match ($data['check_status']){
|
||
0=>'待审核',1=>'审核中',2=>'审核通过',3=>'审核不通过',4=>'撤销审核'
|
||
};
|
||
if(!empty($data['pay_admin_id']) && !empty($data['pay_time']) && $data['check_status'] == 2){
|
||
$data['check_status_text'] = '已打款';
|
||
}
|
||
$data['total_amount'] = 0;
|
||
foreach (json_decode($data['detail'],true) as $v){
|
||
$data['total_amount'] += $v['amount'];
|
||
}
|
||
return $data;
|
||
})->toArray();
|
||
$count = Db::name('oa_expense')->where($where)->where('admin_id',$this->adminId)->count();
|
||
return $this->success('成功', compact('count', 'lists', 'page_no', 'page_size'));
|
||
}
|
||
|
||
public function copy(){
|
||
$params = $this->request->get();
|
||
$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_expense')
|
||
->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){
|
||
$item['create_time'] = date('Y-m-d H:i', $item['create_time']);
|
||
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['income_month'] = date('Y-m',$item['income_month']);
|
||
$item['expense_time'] = date('Y-m-d',$item['expense_time']);
|
||
$item['pay_time'] = !empty($item['pay_time']) ? date('Y-m-d H:i:s',$item['pay_time']) : '';
|
||
$item['pay_admin_name'] = Admin::where('id',$item['pay_admin_id'])?->value('name');
|
||
$item['check_status_text'] = match ($item['check_status']){
|
||
0=>'待审核',1=>'审核中',2=>'审核通过',3=>'审核不通过',4=>'撤销审核'
|
||
};
|
||
if(!empty($item['pay_admin_id']) && !empty($item['pay_time']) && $item['check_status'] == 2){
|
||
$item['check_status_text'] = '已打款';
|
||
}
|
||
$item['total_amount'] = 0;
|
||
foreach (json_decode($item['detail'],true) as $v){
|
||
$item['total_amount'] += $v['amount'];
|
||
}
|
||
return $item;
|
||
})->toArray();
|
||
$count = Db::name('oa_expense')
|
||
->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_expense')
|
||
->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){
|
||
$item['create_time'] = date('Y-m-d H:i', $item['create_time']);
|
||
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['income_month'] = date('Y-m',$item['income_month']);
|
||
$item['expense_time'] = date('Y-m-d',$item['expense_time']);
|
||
$item['pay_time'] = !empty($item['pay_time']) ? date('Y-m-d H:i:s',$item['pay_time']) : '';
|
||
$item['pay_admin_name'] = Admin::where('id',$item['pay_admin_id'])?->value('name');
|
||
$item['check_status_text'] = match ($item['check_status']){
|
||
0=>'待审核',1=>'审核中',2=>'审核通过',3=>'审核不通过',4=>'撤销审核'
|
||
};
|
||
if(!empty($item['pay_admin_id']) && !empty($item['pay_time']) && $item['check_status'] == 2){
|
||
$item['check_status_text'] = '已打款';
|
||
}
|
||
$item['total_amount'] = 0;
|
||
foreach (json_decode($item['detail'],true) as $v){
|
||
$item['total_amount'] += $v['amount'];
|
||
}
|
||
return $item;
|
||
})->toArray();
|
||
$count = Db::name('oa_expense')
|
||
->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_expense')
|
||
->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){
|
||
$item['create_time'] = date('Y-m-d H:i', $item['create_time']);
|
||
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['income_month'] = date('Y-m',$item['income_month']);
|
||
$item['expense_time'] = date('Y-m-d',$item['expense_time']);
|
||
$item['pay_time'] = !empty($item['pay_time']) ? date('Y-m-d H:i:s',$item['pay_time']) : '';
|
||
$item['pay_admin_name'] = Admin::where('id',$item['pay_admin_id'])?->value('name');
|
||
$item['check_status_text'] = match ($item['check_status']){
|
||
0=>'待审核',1=>'审核中',2=>'审核通过,待打款',3=>'审核不通过',4=>'撤销审核'
|
||
};
|
||
if(!empty($item['pay_admin_id']) && !empty($item['pay_time']) && $item['check_status'] == 2){
|
||
$item['check_status_text'] = '已打款';
|
||
}
|
||
$item['total_amount'] = 0;
|
||
foreach (json_decode($item['detail'],true) as $v){
|
||
$item['total_amount'] += $v['amount'];
|
||
}
|
||
return $item;
|
||
})->toArray();
|
||
$count = Db::name('oa_expense')
|
||
->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_expense')
|
||
->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){
|
||
$item['create_time'] = date('Y-m-d H:i', $item['create_time']);
|
||
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['income_month'] = date('Y-m',$item['income_month']);
|
||
$item['expense_time'] = date('Y-m-d',$item['expense_time']);
|
||
$item['pay_time'] = !empty($item['pay_time']) ? date('Y-m-d H:i:s',$item['pay_time']) : '';
|
||
$item['pay_admin_name'] = Admin::where('id',$item['pay_admin_id'])?->value('name');
|
||
$item['check_status_text'] = match ($item['check_status']){
|
||
0=>'待审核',1=>'审核中',2=>'审核通过,待打款',3=>'审核不通过',4=>'撤销审核'
|
||
};
|
||
if(!empty($item['pay_admin_id']) && !empty($item['pay_time']) && $item['check_status'] == 2){
|
||
$item['check_status_text'] = '已打款';
|
||
}
|
||
$item['total_amount'] = 0;
|
||
foreach (json_decode($item['detail'],true) as $v){
|
||
$item['total_amount'] += $v['amount'];
|
||
}
|
||
return $item;
|
||
})->toArray();
|
||
$count = Db::name('oa_expense')
|
||
->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();
|
||
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_expense')->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_expense')->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' => 2,
|
||
'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'=>2])->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'=>2])->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_expense')->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' => 2,
|
||
'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'=>2])->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'=>2])->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'=>2])->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' => 2,
|
||
'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'=>2])->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_expense')->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' => 2,
|
||
'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();
|
||
$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],['pay_admin_id','=',0],['pay_time','=',0]], //审批通过,待打款
|
||
'2'=> [['check_status','=',2],['pay_admin_id','>',0],['pay_time','>',0]], //审批通过,已打款
|
||
};
|
||
}else{
|
||
$where[] = ['check_status','=',2];
|
||
}
|
||
$lists = Db::name('oa_expense')->field('id,code,income_month,expense_time,admin_id,check_admin_ids,check_status,pay_admin_id,pay_time,create_time,detail')->where($where)
|
||
->page($page_no, $page_size)->order('id desc')->select()->each(function($data){
|
||
$admin = Admin::field('id,name,dept_id')->where('id',$data['admin_id'])->findOrEmpty();
|
||
if (!empty($admin['dept_id'])) {
|
||
$dept = Dept::where('id',$admin['dept_id'])->value('name');
|
||
$data['dept_name'] = $dept;
|
||
} else {
|
||
$data['dept_name'] = '';
|
||
}
|
||
$check_admin_users = Admin::where('id','in',$data['check_admin_ids'])->column('name');
|
||
$data['user_name'] = $admin['name'];
|
||
$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['income_month'] = date('Y-m',$data['income_month']);
|
||
$data['expense_time'] = date('Y-m-d',$data['expense_time']);
|
||
$data['pay_time'] = !empty($data['pay_time']) ? date('Y-m-d H:i:s',$data['pay_time']) : '';
|
||
$data['pay_admin_name'] = Admin::where('id',$data['pay_admin_id'])?->value('name');
|
||
$data['check_status_text'] = match ($data['check_status']){
|
||
0=>'待审核',1=>'审核中',2=>'审核通过,待打款',3=>'审核不通过',4=>'撤销审核'
|
||
};
|
||
if(!empty($data['pay_admin_id']) && !empty($data['pay_time']) && $data['check_status'] == 2){
|
||
$data['check_status_text'] = '已打款';
|
||
}
|
||
$data['total_amount'] = 0;
|
||
foreach (json_decode($data['detail'],true) as $v){
|
||
$data['total_amount'] += $v['amount'];
|
||
}
|
||
return $data;
|
||
})->toArray();
|
||
$count = Db::name('oa_expense')->where($where)->where('admin_id',$this->adminId)->count();
|
||
return $this->success('成功', compact('count', 'lists', 'page_no', 'page_size'));
|
||
}
|
||
|
||
public function payment(){
|
||
$params = (new ExpenseValidate())->post()->goCheck('pay');
|
||
$data = Db::name('oa_expense')->where('id',$params['id'])->find();
|
||
if(empty($data)){
|
||
return $this->success('申请信息不存在');
|
||
}
|
||
if($data['check_status'] !== 2){
|
||
return $this->success('当前申请未审核通过');
|
||
}
|
||
$res = Db::name('oa_expense')->where('id',$params['id'])->update(['pay_admin_id'=>$this->adminId,'pay_time'=>time()]);
|
||
return $res ? $this->success('ok') : $this->fail('fail');
|
||
}
|
||
} |