lihai-oa/app/api/controller/FinanceExpense.php

569 lines
18 KiB
PHP
Raw Normal View History

2023-11-03 14:44:54 +08:00
<?php
/**
* @copyright Copyright (c) 2021 勾股工作室
* @license https://opensource.org/licenses/GPL-3.0
* @link https://www.gougucms.com
*/
declare (strict_types = 1);
namespace app\api\controller;
use app\api\ApiController;
use app\api\middleware\Auth;
use app\finance\model\Expense as ExpenseList;
use app\finance\validate\ExpenseCheck;
use think\exception\ValidateException;
use think\facade\Db;
class FinanceExpense extends ApiController
{
protected $middleware = [
Auth::class => ['except' => []]
];
//我申请的报销
public function index()
{
$this->checkAuth();
$param = get_params();
$this->uid = JWT_UID;
$where = [];
$where[] = ['delete_time', '=', 0];
//按时间检索
if (!empty($param['diff_time'])) {
$diff_time =explode('~', $param['diff_time']);
$where[] = ['expense_time', 'between', [strtotime(urldecode($diff_time[0])),strtotime(urldecode($diff_time[1]))]];
}
$where[] = ['admin_id','=',$this->uid];
if (!empty($param['check_status']) && $param['check_status']!='') {
$where[] = ['check_status', '=', $param['check_status']];
}
$model = new ExpenseList;
$list = $model->get_list($param,$where);
$this->apiSuccess('获取成功', $list);
}
//待审批的报销
public function list()
{
$this->checkAuth();
$param = get_params();
$this->uid = JWT_UID;
$status = isset($param['status'])?$param['status']:0;
$user_id = $this->uid;
//查询条件
$map1 = [];
$map2 = [];
$map1[] = ['', 'exp', Db::raw("FIND_IN_SET('{$user_id}',check_admin_ids)")];
$map1[] = ['delete_time', '=', 0];
$map2[] = ['', 'exp', Db::raw("FIND_IN_SET('{$user_id}',flow_admin_ids)")];
$map2[] = ['delete_time', '=', 0];
if($status == 0){
$model = new ExpenseList;
$list = $model->get_list($param,[$map1,$map2],'or');
}
if($status == 1){
$model = new ExpenseList;
$list = $model->get_list($param,$map1);
}
if($status == 2){
$model = new ExpenseList;
$list = $model->get_list($param,$map2);
}
$this->apiSuccess('获取成功', $list);
}
//抄送给我的报销
public function copy()
{
$this->checkAuth();
$param = get_params();
$user_id = JWT_UID;
//查询条件
$map = [];
//按时间检索
if (!empty($param['diff_time'])) {
$diff_time =explode('~', $param['diff_time']);
$map[] = ['expense_time', 'between', [strtotime(urldecode($diff_time[0])),strtotime(urldecode($diff_time[1]))]];
}
$map[] = ['check_status', 'in', [2,3,5]];
$map[] = ['', 'exp', Db::raw("FIND_IN_SET('{$user_id}',copy_uids)")];
$model = new ExpenseList;
$list = $model->get_list($param,$map);
$this->apiSuccess('获取成功', $list);
}
//添加
public function add()
{
$this->checkAuth();
$this->uid = JWT_UID;
$param = get_params();
$admin_id = $this->uid;
$loginAdmin = Db::name('Admin')->where(['id' => $this->uid])->find();
$this->did = $loginAdmin['did'];
if (empty($param['flow_id'])) {
$this->apiError("审批流程id不能为空");
}
if (empty($param['code'])) {
$this->apiError("报销凭证编号不能为空");
}
if (empty($param['expense_time'])) {
$this->apiError("原始单据日期不能为空");
}
if (empty($param['income_month'])) {
$this->apiError("入账月份不能为空");
}
if (empty($param['amount'])) {
$this->apiError("报销金额不能为空");
}
if (empty($param['cate_id'])) {
$this->apiError("报销类型不能为空");
}
$param['income_month'] = isset($param['income_month']) ? strtotime(urldecode($param['income_month'])) : 0;
$param['expense_time'] = isset($param['expense_time']) ? strtotime(urldecode($param['expense_time'])) : 0;
$param['check_status'] = 1;
$param['check_step_sort'] = 0;
$amountData = isset($param['amount']) ? $param['amount'] : '0';
if ($amountData == 0) {
$this->apiError('报销金额不完善');
}
else{
foreach ($amountData as $key => $value) {
if ($value == 0) {
$this->apiError('第' . ($key + 1) . '条报销金额不能为零');
}
}
}
$flow_list = Db::name('Flow')->where('id',$param['flow_id'])->value('flow_list');
$flow = unserialize($flow_list);
if (!isset($param['check_admin_ids'])) {
if($flow[0]['flow_type'] == 1){
//部门负责人
$leader = get_department_leader($admin_id);
if($leader == 0){
$this->apiError('审批流程设置有问题当前部门负责人还未设置请联系HR或者管理员');
}
}
else if($flow[0]['flow_type'] == 2){
//上级部门负责人
$leader = get_department_leader($admin_id,1);
if($leader == 0){
$this->apiError('审批流程设置有问题上级部门负责人还未设置请联系HR或者管理员');
}
}
}
$dbRes = false;
if (!empty($param['id']) && $param['id'] > 0) {
try {
validate(ExpenseCheck::class)->scene('edit')->check($param);
} catch (ValidateException $e) {
$this->apiError($e->getError());
}
$param['update_time'] = time();
Db::startTrans();
try {
//删除原来的审核流程和审核记录
Db::name('FlowStep')->where(['action_id'=>$param['id'],'type'=>2,'delete_time'=>0])->update(['delete_time'=>time()]);
Db::name('FlowRecord')->where(['action_id'=>$param['id'],'type'=>2,'delete_time'=>0])->update(['delete_time'=>time()]);
if (!isset($param['check_admin_ids'])) {
if($flow[0]['flow_type'] == 1){
//部门负责人
$leader = get_department_leader($this->uid);
if($leader == 0){
$this->apiError('审批流程设置有问题当前部门负责人还未设置请联系HR或者管理员');
}
else{
$param['check_admin_ids'] = $leader;
}
}
else if($flow[0]['flow_type'] == 2){
//上级部门负责人
$leader = get_department_leader($this->uid,1);
if($leader == 0){
$this->apiError('审批流程设置有问题上级部门负责人还未设置请联系HR或者管理员');
}
else{
$param['check_admin_ids'] = $leader;
}
}
else{
$param['check_admin_ids'] = $flow[0]['flow_uids'];
}
foreach ($flow as $key => &$value){
$value['action_id'] = $param['id'];
$value['sort'] = $key;
$value['type'] = 2;
$value['create_time'] = time();
}
//增加审核流程
Db::name('FlowStep')->strict(false)->field(true)->insertAll($flow);
}
else{
$flow_step = array(
'action_id' => $param['id'],
'type' => 2,
'flow_uids' => $param['check_admin_ids'],
'create_time' => time()
);
//增加审核流程
Db::name('FlowStep')->strict(false)->field(true)->insertGetId($flow_step);
}
$res = ExpenseList::where('id', $param['id'])->strict(false)->field(true)->update($param);
$exid = $param['id'];
//相关内容多个数组;
$amountData = isset($param['amount']) ? $param['amount'] : '';
$remarksData = isset($param['remarks']) ? $param['remarks'] : '';
$cateData = isset($param['cate_id']) ? $param['cate_id'] : '';
$idData = isset($param['expense_id']) ? $param['expense_id'] : 0;
if ($amountData) {
foreach ($amountData as $key => $value) {
if (!$value) {
continue;
}
$data = [];
$data['id'] = $idData[$key];
$data['exid'] = $exid;
$data['admin_id'] = $admin_id;
$data['amount'] = $amountData[$key];
$data['cate_id'] = $cateData[$key];
$data['remarks'] = $remarksData[$key];
if ($data['id'] > 0) {
$data['update_time'] = time();
$resa = Db::name('ExpenseInterfix')->strict(false)->field(true)->update($data);
} else {
$data['create_time'] = time();
$eid = Db::name('ExpenseInterfix')->strict(false)->field(true)->insertGetId($data);
}
}
}
//添加提交申请记录
$checkData=array(
'action_id' => $exid,
'check_user_id' => $this->uid,
'content' => '重新提交申请',
'type' => 2,
'check_time' => time(),
'create_time' => time()
);
$record_id = Db::name('FlowRecord')->strict(false)->field(true)->insertGetId($checkData);
add_log('edit', $exid, $param);
//发送消息通知
$msg=[
'from_uid'=>$this->uid,
'title'=>'报销',
'action_id'=>$param['id']
];
$users = $param['check_admin_ids'];
sendMessage($users,31,$msg);
Db::commit();
$dbRes = true;
} catch (\Exception $e) { ##这里参数不能删除($e错误信息)
Db::rollback();
$this->apiError($e->getMessage());
}
} else {
try {
validate(ExpenseCheck::class)->scene('add')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
$param['create_time'] = time();
$param['admin_id'] = $admin_id;
$param['did'] = $this->did;
Db::startTrans();
try {
if (!isset($param['check_admin_ids'])) {
if($flow[0]['flow_type'] == 1){
//部门负责人
$leader = get_department_leader($this->uid);
if($leader == 0){
return to_assign(1,'审批流程设置有问题当前部门负责人还未设置请联系HR或者管理员');
}
else{
$param['check_admin_ids'] = $leader;
}
}
else if($flow[0]['flow_type'] == 2){
//上级部门负责人
$leader = get_department_leader($this->uid,1);
if($leader == 0){
return to_assign(1,'审批流程设置有问题上级部门负责人还未设置请联系HR或者管理员');
}
else{
$param['check_admin_ids'] = $leader;
}
}
else{
$param['check_admin_ids'] = $flow[0]['flow_uids'];
}
$exid = ExpenseList::strict(false)->field(true)->insertGetId($param);
foreach ($flow as $key => &$value){
$value['action_id'] = $exid;
$value['sort'] = $key;
$value['type'] = 2;
$value['create_time'] = time();
}
//增加审核流程
Db::name('FlowStep')->strict(false)->field(true)->insertAll($flow);
}
else{
$exid = ExpenseList::strict(false)->field(true)->insertGetId($param);
$flow_step = array(
'action_id' => $exid,
'type' => 2,
'flow_uids' => $param['check_admin_ids'],
'create_time' => time()
);
//增加审核流程
Db::name('FlowStep')->strict(false)->field(true)->insertGetId($flow_step);
}
//相关内容多个数组;
$amountData = isset($param['amount']) ? $param['amount'] : '';
$remarksData = isset($param['remarks']) ? $param['remarks'] : '';
$cateData = isset($param['cate_id']) ? $param['cate_id'] : '';
if ($amountData) {
foreach ($amountData as $key => $value) {
if (!$value) {
continue;
}
$data = [];
$data['exid'] = $exid;
$data['admin_id'] = $admin_id;
$data['amount'] = $amountData[$key];
$data['cate_id'] = $cateData[$key];
$data['remarks'] = $remarksData[$key];
$data['create_time'] = time();
$eid = Db::name('ExpenseInterfix')->strict(false)->field(true)->insertGetId($data);
}
}
//添加提交申请记录
$checkData=array(
'action_id' => $exid,
'check_user_id' => $this->uid,
'content' => '提交申请',
'type' => 2,
'check_time' => time(),
'create_time' => time()
);
$record_id = Db::name('FlowRecord')->strict(false)->field(true)->insertGetId($checkData);
add_log('add', $exid, $param);
//发送消息通知
$msg=[
'from_uid'=>$this->uid,
'title'=>'报销',
'action_id'=>$exid
];
$users = $param['check_admin_ids'];
sendMessage($users,31,$msg);
Db::commit();
$dbRes = true;
} catch (\Exception $e) { ##这里参数不能删除($e错误信息)
Db::rollback();
return to_assign(1, $e->getMessage());
}
}
if ($dbRes == true) {
$this->apiSuccess('操作成功');
} else {
$this->apiError('操作失败');
}
}
2023-11-03 17:43:28 +08:00
//获取报销流程
public function get_expense_flow()
{
2023-11-03 14:44:54 +08:00
$this->uid = JWT_UID;
$loginAdmin = Db::name('Admin')->where(['id' => $this->uid])->find();
$this->did = $loginAdmin['did'];
$department = $this->did;
$flows = get_type_department_flows(6, $department);
$this->apiSuccess('获取成功', $flows);
2023-11-03 17:43:28 +08:00
}
2023-11-03 14:44:54 +08:00
//查看
public function view()
{
2023-11-03 15:00:16 +08:00
$this->checkAuth();
$this->uid = JWT_UID;
$param = get_params();
2023-11-03 14:44:54 +08:00
$id = empty(get_params('id')) ? 0 : get_params('id');
$detail = (new ExpenseList())->detail($id);
2023-11-03 15:00:16 +08:00
if (empty($detail)) {
$this->apiError('报销流程不存在');
}
2023-11-03 14:44:54 +08:00
$flows = Db::name('FlowStep')->where(['action_id'=>$detail['id'],'type'=>2,'sort'=>$detail['check_step_sort'],'delete_time'=>0])->find();
$detail['check_user'] = '-';
$detail['copy_user'] = '-';
$check_user_ids = [];
if($detail['check_status']==1){
if($flows['flow_type']==1){
$detail['check_user'] = '部门负责人';
$check_user_ids[]=get_department_leader($detail['admin_id']);
}
else if($flows['flow_type']==2){
$detail['check_user'] = '上级部门负责人';
$check_user_ids[]=get_department_leader($detail['admin_id'],1);
}
else{
$check_user_ids = explode(',',$flows['flow_uids']);
$check_user = Db::name('Admin')->where('id','in',$flows['flow_uids'])->column('name');
$detail['check_user'] = implode(',',$check_user);
}
}
if($detail['copy_uids'] !=''){
$copy_user = Db::name('Admin')->where('id','in',$detail['copy_uids'])->column('name');
$detail['copy_user'] = implode(',',$copy_user);
}
if($detail['file_ids'] !=''){
$fileArray = Db::name('File')->where('id','in',$detail['file_ids'])->select();
$detail['fileArray'] = $fileArray;
}
$is_check_admin = 0;
$is_create_admin = 0;
if($detail['admin_id'] == $this->uid){
$is_create_admin = 1;
}
if(in_array($this->uid,$check_user_ids)){
$is_check_admin = 1;
//当前审核节点详情
$step = Db::name('FlowStep')->where(['action_id'=>$detail['id'],'type'=>2,'sort'=>$detail['check_step_sort'],'delete_time'=>0])->find();
if($step['flow_type'] == 4){
$check_count = Db::name('FlowRecord')->where(['action_id'=>$detail['id'],'type'=>2,'step_id'=>$step['id'],'check_user_id'=>$this->uid])->count();
if($check_count>0){
$is_check_admin = 0;
}
}
}
$check_record = Db::name('FlowRecord')->field('f.*,a.name,a.thumb')
->alias('f')
->join('Admin a', 'a.id = f.check_user_id', 'left')
->where(['f.action_id'=>$detail['id'],'f.type'=>2])
->order('check_time desc')
->select()->toArray();
foreach ($check_record as $kk => &$vv) {
$vv['check_time_str'] = date('Y-m-d H:i', $vv['check_time']);
2023-11-03 15:00:16 +08:00
$vv['check_time'] = date('Y-m-d H:i:s', $vv['check_time']);
2023-11-03 14:44:54 +08:00
$vv['status_str'] = '提交';
if($vv['status'] == 1){
$vv['status_str'] = '审核通过';
}
else if($vv['status'] == 2){
$vv['status_str'] = '审核拒绝';
}
if($vv['status'] == 3){
$vv['status_str'] = '撤销';
}
}
2023-11-03 15:00:16 +08:00
// is_create_admin 是否为创建人 is_check_admin 是否为审核人
$this->apiSuccess('获取成功', compact('is_create_admin', 'is_check_admin', 'check_record', 'detail', 'flows'));
2023-11-03 14:44:54 +08:00
}
//删除
public function delete()
{
2023-11-03 15:00:16 +08:00
$this->checkAuth();
$this->uid = JWT_UID;
2023-11-03 14:44:54 +08:00
$id = get_params("id");
$expense = (new ExpenseList())->detail($id);
2023-11-03 15:00:16 +08:00
if (empty($expense)) {
$this->apiError('报销流程不存在');
}
2023-11-03 14:44:54 +08:00
if ($expense['check_status'] == 2) {
2023-11-03 15:00:16 +08:00
$this->apiError('已审核的报销记录不能删除');
2023-11-03 14:44:54 +08:00
}
if ($expense['check_status'] == 5) {
2023-11-03 15:00:16 +08:00
$this->apiError('已打款的报销记录不能删除');
2023-11-03 14:44:54 +08:00
}
$data['delete_time'] = time();
$data['id'] = $id;
if (Db::name('expense')->update($data) !== false) {
add_log('delete', $id);
2023-11-03 15:00:16 +08:00
$this->apiSuccess('删除成功');
2023-11-03 14:44:54 +08:00
} else {
2023-11-03 15:00:16 +08:00
$this->apiError('删除失败');
2023-11-03 14:44:54 +08:00
}
}
2023-11-03 15:44:33 +08:00
//报销打款
public function checkedlist()
{
$this->uid = JWT_UID;
$auth = isAuthExpense($this->uid);
if($auth == 0){
2023-11-06 11:23:10 +08:00
$this->apiError('你没有权限请联系管理员或者HR', [], 2);
2023-11-03 15:44:33 +08:00
}
$param = get_params();
$where = [];
if (!empty($param['check_status'])) {
$where[] = ['check_status','=',$param['check_status']];
}
else{
$where[] = ['check_status','in',[2,5]];
}
//按时间检索
if (!empty($param['diff_time'])) {
$diff_time =explode('~', $param['diff_time']);
$where[] = ['expense_time', 'between', [strtotime(urldecode($diff_time[0])),strtotime(urldecode($diff_time[1]))]];
}
$model = new ExpenseList;
$list = $model->get_list($param,$where);
$this->apiSuccess($list);
}
2023-11-03 15:52:42 +08:00
//报销设置为已打款
public function topay()
{
$param = get_params();
$this->uid = JWT_UID;
$auth = isAuthExpense($this->uid);
if($auth == 0){
2023-11-06 11:23:10 +08:00
$this->apiError('你没有打款权限请联系管理员或者HR', [], 2);
2023-11-03 15:52:42 +08:00
}
2023-11-03 16:20:42 +08:00
$expense = (new ExpenseList())->detail($param['id'] ?? 0);
if (empty($expense)) {
$this->apiError('报销流程不存在');
}
if ($expense['check_status'] == 5) {
$this->apiError('该报销已打款');
}
if ($expense['check_status'] != 2) {
$this->apiError('该报销流程审核未通过');
}
2023-11-03 15:52:42 +08:00
$param['check_status'] = 5;
$param['pay_admin_id'] = $this->uid;
$param['pay_time'] = time();
2023-11-03 15:57:57 +08:00
$res = ExpenseList::where('id', $param['id'])->strict(false)->field(true)->update($param);
2023-11-03 16:20:42 +08:00
if ($res) {
2023-11-03 15:52:42 +08:00
add_log('topay', $param['id'],$param,'报销');
2023-11-03 15:57:57 +08:00
$detail = ExpenseList::where(['id' => $param['id']])->find();
2023-11-03 15:54:03 +08:00
$msg = [
2023-11-03 15:52:42 +08:00
'create_time'=>$detail['create_time'],
'title'=>'报销',
'action_id'=>$detail['id']
];
$users = $detail['admin_id'];
sendMessage($users, 34, $msg);
$this->apiSuccess('操作成功');
} else {
$this->apiError('操作失败');
}
}
2023-11-03 14:44:54 +08:00
}