528 lines
19 KiB
PHP
528 lines
19 KiB
PHP
<?php
|
||
/**
|
||
* @copyright Copyright (c) 2023-2024 美天智能科技
|
||
* @link http://www.meteteme.com
|
||
* @author 李志强
|
||
*/
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace app\reimbursement\controller;
|
||
|
||
use app\base\BaseController;
|
||
use app\model\Reimbursement as ReimbursementList;
|
||
use app\model\ReimbursementCategory;
|
||
use app\model\Admin;
|
||
use app\model\ReimbursementOrder as ReimbursementOrderList;
|
||
use app\model\Project as ProjectList;
|
||
use app\model\Department as DepartmentList;
|
||
use app\reimbursement\validate\ReimbursementCheck;
|
||
use think\facade\Db;
|
||
use think\facade\View;
|
||
use think\facade\Response;
|
||
|
||
class Index extends BaseController
|
||
{
|
||
public function index()
|
||
{
|
||
if (request()->isAjax()) {
|
||
$param = get_params();
|
||
$admin = $this->uid;
|
||
$rows = empty($param['limit']) ? get_md_contentconfig('app.page_size') : $param['limit'];
|
||
|
||
$list = ReimbursementOrderList::withoutField('')
|
||
->where('delete_time', null)
|
||
->where('creater', $admin) // 只显示creater等于$admin的数据
|
||
->order('order')
|
||
->order('id', 'desc')
|
||
->paginate($rows, false, ['query' => $param])
|
||
->each(function ($item, $key) {
|
||
$ro = ReimbursementOrderList::where(['id' => $item->id])->find();
|
||
$item->order = $ro['order'];
|
||
$item->total = $ro['total'];
|
||
$item->department = DepartmentList::where('id', $ro['department'])->value('title');
|
||
$item->project = ProjectList::where('id', $ro['project'])->value('name_short');
|
||
$item->creater = Admin::where('id', $ro['creater'])->value('name');
|
||
$item->status = $ro['status'];
|
||
$item->remark = $ro['remark'];
|
||
})
|
||
->filter(function ($item) {
|
||
return $item->delete_time === null;
|
||
});
|
||
return table_assign(0, '', $list);
|
||
} else {
|
||
return view();
|
||
}
|
||
}
|
||
|
||
//获取报销列表
|
||
public function getReimbursementlist()
|
||
{
|
||
$list = ReimbursementOrderList::order('id', 'desc')
|
||
->whereNull('delete_time')
|
||
->select()
|
||
->map(function ($item) {
|
||
// 查询 department 的名称
|
||
$department = DepartmentList::where('id', $item->department)->value('title');
|
||
|
||
// 查询 creater 的名称
|
||
$creater = Admin::where('id', $item->creater)->value('name');
|
||
|
||
// 将 project 和 cate 名称赋值到当前项
|
||
$item->department = $department;
|
||
$item->creater = $creater;
|
||
|
||
return $item;
|
||
});
|
||
|
||
return json(['data' => $list, 'code' => 0, 'msg' => '获取账单成功']);
|
||
}
|
||
|
||
// 获取账单详情
|
||
public function getorderviewdetail($pid)
|
||
{
|
||
// 获取符合条件的记录列表
|
||
$list = ReimbursementList::where('pid', $pid)
|
||
->whereNull('delete_time')
|
||
->order('id', 'desc')
|
||
->select()
|
||
->map(function ($item) {
|
||
// 查询 project 的名称
|
||
$project = ProjectList::where('id', $item->project)->value('name_short');
|
||
|
||
// 查询 cate 的名称
|
||
$cate = ReimbursementCategory::where('id', $item->cate)->value('name');
|
||
|
||
// 将 project 和 cate 名称赋值到当前项
|
||
$item->project = $project;
|
||
$item->cate = $cate;
|
||
|
||
return $item;
|
||
});
|
||
|
||
return json(['data' => $list, 'code' => 0, 'msg' => '获取账单详情成功']);
|
||
}
|
||
|
||
// 获取报销内容
|
||
public function getreimbursementdetail($id)
|
||
{
|
||
// 获取符合条件的记录列表
|
||
$detail = ReimbursementList::where('id', $id)
|
||
->whereNull('delete_time')
|
||
->order('id', 'desc')
|
||
->select()
|
||
->map(function ($item) {
|
||
// 查询 project 的名称
|
||
$project = ProjectList::where('id', $item->project)->value('name_short');
|
||
|
||
// 查询 cate 的名称
|
||
$cate = ReimbursementCategory::where('id', $item->cate)->value('name');
|
||
|
||
// 将 project 和 cate 名称赋值到当前项
|
||
$item->project = $project;
|
||
$item->cate = $cate;
|
||
|
||
return $item;
|
||
});
|
||
|
||
return json(['data' => $detail, 'code' => 0, 'msg' => '获取报销成功']);
|
||
}
|
||
|
||
|
||
//添加条目
|
||
public function additem()
|
||
{
|
||
$param = get_params();
|
||
if (request()->isPost()) {
|
||
|
||
// 新增逻辑
|
||
$param['create_time'] = date('Y-m-d H:i:s', time());
|
||
$param['admin_id'] = $this->uid;
|
||
|
||
$sid = ReimbursementList::strict(false)->field(true)->insertGetId($param);
|
||
if ($sid) {
|
||
add_log('add', $sid, $param);
|
||
// 从报销列表中重新计算总值
|
||
$total = ReimbursementList::where('pid', $param['pid'])->sum('price');
|
||
$param['total'] = $total;
|
||
// 更新 ReimbursementOrderList,将重新计算的总值更新到对应的 pid=id 的 total 字段中
|
||
ReimbursementOrderList::where('id', $param['pid'])->strict(false)->update(['total' => $total]);
|
||
return json(["code" => 2, "msg" => "添加成功"]); // 返回JSON格式数据
|
||
} else {
|
||
return json(["code" => -2, "msg" => "添加失败"]); // 返回JSON格式数据
|
||
}
|
||
|
||
} else {
|
||
$id = isset($param['id']) ? $param['id'] : 0;
|
||
if ($id > 0) {
|
||
$reimbursementLists = ReimbursementList::where('id', $id)
|
||
->field('id, pid, times, department, project, cate, price, creater, haveticket, tickets, remark, create_time')
|
||
->find();
|
||
if (empty($reimbursementLists)) {
|
||
return json(["code" => 1, "msg" => "信息不存在"]); // 返回JSON格式数据
|
||
}
|
||
View::assign('sid', $reimbursementLists);
|
||
}
|
||
View::assign('id', $id);
|
||
return view();
|
||
}
|
||
}
|
||
|
||
//编辑条目(查看)
|
||
public function edititem($id)
|
||
{
|
||
$reimbursementLists = ReimbursementList::where('id', $id)
|
||
->field('id, pid, times, department, project, cate, price, creater, haveticket, tickets, remark, create_time')
|
||
->find();
|
||
// print_r($reimbursementLists);
|
||
|
||
if ($reimbursementLists) {
|
||
// 获取发生时间
|
||
$times = $reimbursementLists['times'];
|
||
// 获取部门名称
|
||
$department = DepartmentList::where('id', $reimbursementLists['department'])->value('title');
|
||
// 获取创建者名称
|
||
$creater = Admin::where('id', $reimbursementLists['creater'])->value('name');
|
||
// print_r($creater);
|
||
|
||
if ($times) {
|
||
$timesDate = date('Y年m月d日', strtotime($times));
|
||
$reimbursementLists['times'] = $timesDate;
|
||
}
|
||
|
||
if ($department) {
|
||
$reimbursementLists['department'] = $department;
|
||
}
|
||
if ($creater) {
|
||
$reimbursementLists['creater'] = $creater;
|
||
}
|
||
|
||
if ($reimbursementLists) {
|
||
View::assign('detail', $reimbursementLists);
|
||
} else {
|
||
View::assign('message', '没有详细数据');
|
||
}
|
||
}
|
||
|
||
return view();
|
||
// return $reimbursementLists;
|
||
}
|
||
|
||
// 编辑条目详情
|
||
public function editdetailitem()
|
||
{
|
||
$param = get_params();
|
||
if (request()->isPost()) {
|
||
$id = $param['id'];
|
||
$reimbursementLists = ReimbursementList::where('id', $id)
|
||
->field('id, pid, times, department, project, cate, price, creater, haveticket, tickets, remark, create_time')
|
||
->find();
|
||
|
||
if ($reimbursementLists) {
|
||
$dateString = $param['times'];
|
||
$dateString = str_replace(['年', '月', '日'], '-', $dateString);
|
||
$dateString = rtrim($dateString, '-');
|
||
|
||
$reimbursementLists->department = $param['department'];
|
||
$reimbursementLists->project = $param['project'];
|
||
$reimbursementLists->cate = $param['cate'];
|
||
$reimbursementLists->price = $param['price'];
|
||
$reimbursementLists->haveticket = $param['haveticket'];
|
||
$reimbursementLists->tickets = $param['tickets'];
|
||
$reimbursementLists->remark = $param['remark'];
|
||
$reimbursementLists->save();
|
||
return json(["code" => 0, "msg" => "编辑成功"]);
|
||
} else {
|
||
return json(["code" => 1, "msg" => "信息不存在"]);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
//添加
|
||
public function add()
|
||
{
|
||
$param = get_params();
|
||
if (request()->isPost()) {
|
||
if (!empty($param['id']) && $param['id'] > 0) {
|
||
$ro = (new ReimbursementOrderList())->detail($param['id']);
|
||
$param['update_time'] = time();
|
||
$res = ReimbursementOrderList::where('id', $param['id'])->strict(false)->field(true)->update($param);
|
||
if ($res) {
|
||
add_log('edit', $param['id'], $param, $ro);
|
||
}
|
||
return to_assign();
|
||
} else {
|
||
$param['create_time'] = date('Y-m-d H:i:s', time());
|
||
$param['admin_id'] = $this->uid;
|
||
$param['creater'] = $this->uid;
|
||
$param['status'] = '未报销'; // 添加状态字段赋值
|
||
$sid = ReimbursementOrderList::strict(false)->field(true)->insertGetId($param);
|
||
if ($sid) {
|
||
add_log('add', $sid, $param);
|
||
$log_data = array(
|
||
'module' => 'plans',
|
||
// 'id' => $sid,
|
||
'new_content' => $param['order'],
|
||
'field' => 'new',
|
||
'action' => 'add',
|
||
'admin_id' => $this->uid,
|
||
'create_time' => time(),
|
||
);
|
||
Db::name('Log')->strict(false)->field(true)->insert($log_data);
|
||
}
|
||
return to_assign(2, '添加成功!');
|
||
}
|
||
} else {
|
||
$id = isset($param['id']) ? $param['id'] : 0;
|
||
if ($id > 0) {
|
||
$detail = (new ReimbursementOrderList())->detail($id);
|
||
if (empty($detail)) {
|
||
return to_assign(1, '1.信息不存在');
|
||
}
|
||
View::assign('detail', $detail);
|
||
}
|
||
View::assign('id', $id);
|
||
return view();
|
||
}
|
||
}
|
||
|
||
//查看
|
||
public function view($id)
|
||
{
|
||
$reimbursementOrder = ReimbursementOrderList::where('id', $id)
|
||
->field('id, order, total, department, project, creater, remark, status, lock, create_time')
|
||
->find();
|
||
// print_r($reimbursementOrder);
|
||
|
||
if ($reimbursementOrder) {
|
||
// 获取项目名称
|
||
$project = ProjectList::where('id', $reimbursementOrder['project'])->value('name_short');
|
||
// 获取部门名称
|
||
$department = DepartmentList::where('id', $reimbursementOrder['department'])->value('title');
|
||
// 获取创建者名称
|
||
$creater = Admin::where('id', $reimbursementOrder['creater'])->value('name');
|
||
// print_r($creater);
|
||
|
||
if ($project) {
|
||
$reimbursementOrder['project'] = $project;
|
||
}
|
||
if ($department) {
|
||
$reimbursementOrder['department'] = $department;
|
||
}
|
||
if ($creater) {
|
||
$reimbursementOrder['creater'] = $creater;
|
||
}
|
||
|
||
if ($reimbursementOrder) {
|
||
View::assign('detail', $reimbursementOrder);
|
||
} else {
|
||
View::assign('message', '没有详细数据');
|
||
}
|
||
}
|
||
|
||
return view();
|
||
}
|
||
|
||
// 查看报销详情
|
||
public function viewitemlist($id)
|
||
{
|
||
$reimbursementLists = ReimbursementList::where('pid', $id)
|
||
->whereNull('delete_time')
|
||
->order('times', 'desc')
|
||
->select();
|
||
|
||
foreach ($reimbursementLists as $item) {
|
||
$cateName = ReimbursementCategory::where('id', $item->cate)->value('name');
|
||
$projectName = ProjectList::where('id', $item->project)->value('name_short');
|
||
$item->cate = $cateName;
|
||
$item->project = $projectName;
|
||
}
|
||
|
||
return json(["code" => 0, "data" => $reimbursementLists]);
|
||
}
|
||
|
||
|
||
//删除
|
||
public function delete()
|
||
{
|
||
$request = request();
|
||
if ($request->isDelete() || $request->isAjax()) {
|
||
$id = get_params("id");
|
||
$detail = ReimbursementList::where('id', $id)->find();
|
||
|
||
if (ReimbursementList::where('id', $id)->update(['delete_time' => date('Y-m-d H:i:s')]) !== false) {
|
||
$log_data = array(
|
||
'module' => 'reimbursement',
|
||
'field' => 'delete',
|
||
'action' => 'delete',
|
||
'admin_id' => $this->uid,
|
||
'old_content' => '',
|
||
'new_content' => $detail['id'],
|
||
'create_time' => time(),
|
||
);
|
||
Db::name('Log')->strict(false)->field(true)->insert($log_data);
|
||
return to_assign(0, "删除成功");
|
||
} else {
|
||
return to_assign(0, "删除失败");
|
||
}
|
||
|
||
} else {
|
||
return to_assign(1, "错误的请求");
|
||
}
|
||
}
|
||
|
||
//批量删除
|
||
public function batch_delete()
|
||
{
|
||
$request = request();
|
||
if ($request->isDelete() || $request->isAjax()) {
|
||
$ids = get_params("ids");
|
||
foreach ($ids as $id) {
|
||
$detail = ReimbursementOrderList::where('id', $id)->find();
|
||
if (ReimbursementOrderList::where('id', $id)->update(['delete_time' => date('Y-m-d H:i:s')]) !== false) {
|
||
$log_data = array(
|
||
'module' => 'reimbursement',
|
||
'field' => 'delete',
|
||
'action' => 'delete',
|
||
'admin_id' => $this->uid,
|
||
'old_content' => '',
|
||
'new_content' => $detail['id'],
|
||
'create_time' => time(),
|
||
);
|
||
Db::name('Log')->strict(false)->field(true)->insert($log_data);
|
||
}
|
||
}
|
||
return to_assign(0, "批量删除成功");
|
||
} else {
|
||
return to_assign(1, "错误的请求");
|
||
}
|
||
}
|
||
|
||
//获取项目列表
|
||
public function getproject()
|
||
{
|
||
$pageNumber = $_GET['page'] ?? 1; // 获取当前页码,默认为第1页
|
||
$pageSize = 50; // 每页显示的数据条数
|
||
|
||
$totalRecords = ProjectList::where('delete_time', null)->count(); // 获取总记录数
|
||
$totalPages = ceil($totalRecords / $pageSize); // 计算总页数
|
||
|
||
$offset = ($pageNumber - 1) * $pageSize; // 计算偏移量
|
||
|
||
$projects = ProjectList::limit($offset, $pageSize)->order('id', 'desc')->column('name_short', 'id');
|
||
$formattedProjects = [];
|
||
foreach ($projects as $id => $project) {
|
||
$formattedProjects[] = ["id" => $id, "project" => $project];
|
||
}
|
||
|
||
return json(["code" => 0, "count" => $totalRecords, "data" => $formattedProjects, "pages" => $totalPages]);
|
||
}
|
||
|
||
//获取项目列表
|
||
public function getdepartment()
|
||
{
|
||
$pageNumber = $_GET['page'] ?? 1; // 获取当前页码,默认为第1页
|
||
$pageSize = 10; // 每页显示的数据条数
|
||
|
||
$totalRecords = DepartmentList::count(); // 获取总记录数
|
||
$totalPages = ceil($totalRecords / $pageSize); // 计算总页数
|
||
|
||
$offset = ($pageNumber - 1) * $pageSize; // 计算偏移量
|
||
|
||
$departments = DepartmentList::limit($offset, $pageSize)->order('id', 'desc')->column('title', 'id');
|
||
$formattedDepartments = [];
|
||
foreach ($departments as $id => $department) {
|
||
$formattedDepartments[] = ["id" => $id, "department" => $department];
|
||
}
|
||
|
||
return json(["code" => 0, "count" => $totalRecords, "data" => $formattedDepartments, "pages" => $totalPages]);
|
||
}
|
||
|
||
//获取报销类别
|
||
public function reimcate()
|
||
{
|
||
$pageNumber = $_GET['page'] ?? 1; // 获取当前页码,默认为第1页
|
||
$pageSize = 50; // 每页显示的数据条数
|
||
|
||
$totalRecords = ReimbursementCategory::count(); // 获取总记录数
|
||
$totalPages = ceil($totalRecords / $pageSize); // 计算总页数
|
||
|
||
$offset = ($pageNumber - 1) * $pageSize; // 计算偏移量
|
||
|
||
$reimcates = ReimbursementCategory::limit($offset, $pageSize)->order('id', 'desc')->column('name', 'id');
|
||
$formattedReimcates = [];
|
||
foreach ($reimcates as $id => $reimcate) {
|
||
$formattedReimcates[] = ["id" => $id, "cate" => $reimcate];
|
||
}
|
||
|
||
return json(["code" => 0, "count" => $totalRecords, "data" => $formattedReimcates, "pages" => $totalPages]);
|
||
}
|
||
|
||
//获取创建者
|
||
public function getcreater()
|
||
{
|
||
$createrId = $this->uid;
|
||
$createrName = Admin::where('id', $createrId)->value('name');
|
||
header('Content-Type: application/json');
|
||
echo json_encode([
|
||
'createrId' => $createrId,
|
||
'createrName' => $createrName,
|
||
]);
|
||
exit;
|
||
}
|
||
|
||
//编辑报销状态
|
||
public function editstatus($id, $status)
|
||
{
|
||
$validStatus = ['未报销', '已报销'];
|
||
if (in_array($status, $validStatus)) {
|
||
ReimbursementOrderList::where('id', $id)->update(['status' => $status]);
|
||
return json(["code" => 0, "msg" => "状态修改成功"]);
|
||
} else {
|
||
return json(["code" => 1, "msg" => "无效的状态值"]);
|
||
}
|
||
}
|
||
|
||
//编辑锁定状态
|
||
public function editlock($id, $lock)
|
||
{
|
||
$validStatus = ['未锁定', '锁定'];
|
||
if (in_array($lock, $validStatus)) {
|
||
ReimbursementOrderList::where('id', $id)->update(['lock' => $lock]);
|
||
return json(["code" => 0, "msg" => "状态修改成功"]);
|
||
} else {
|
||
return json(["code" => 1, "msg" => "无效的状态值"]);
|
||
}
|
||
}
|
||
|
||
//增加费用类型(app端)
|
||
public function addexpensetype()
|
||
{
|
||
$param = get_params();
|
||
if (request()->isPost()) {
|
||
|
||
$sid = ReimbursementCategory::strict(false)->field(true)->insertGetId($param);
|
||
if ($sid) {
|
||
add_log('add', $sid, $param);
|
||
return json(["code" => 0, "msg" => "添加成功"]); // 返回JSON格式数据
|
||
} else {
|
||
return json(["code" => 1, "msg" => "添加失败"]); // 返回JSON格式数据
|
||
}
|
||
|
||
} else {
|
||
$id = isset($param['id']) ? $param['id'] : 0;
|
||
if ($id > 0) {
|
||
$businessinfoLists = ReimbursementCategory::where('id', $id)
|
||
->field('id, name')
|
||
->find();
|
||
if (empty($businessinfoLists)) {
|
||
return json(["code" => 1, "msg" => "信息不存在"]); // 返回JSON格式数据
|
||
}
|
||
}
|
||
json(["code" => 0, "msg" => "编辑成功"]); // 返回JSON格式数据
|
||
}
|
||
}
|
||
|
||
} |