Files
duolamaojiazhen/application/admin/controller/Invoice.php
2025-12-29 17:28:14 +08:00

239 lines
7.4 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\admin\controller;
use app\admin\model\Invoice as InvoiceModel;
use app\admin\model\InvoiceLog;
use app\common\server\UrlServer;
use think\Db;
class Invoice extends AdminBase
{
/**
* 发票列表
*/
public function lists()
{
if ($this->request->isAjax()) {
$get = $this->request->get();
// 构建查询条件
$where = [];
if (!empty($get['keyword'])) {
$where[] = ['oid|invoice_id', 'like', '%' . $get['keyword'] . '%'];
}
if (isset($get['state']) && $get['state'] !== '') {
$where[] = ['state', '=', intval($get['state'])];
}
// 查询数据(关联订单表获取订单编号)
$list = Db::name('invoice_log')
->where($where)
->order('state asc')
->select();
// 格式化数据
$currentDomain = $_SERVER['HTTP_HOST'];
$result = [];
foreach ($list as $item) {
$result[] = [
'id' => $item['id'] ?? 0,
'invoice_id' => $item['invoice_id'] ?? 0,
'oid' => $item['oid'] ?? 0,
'state' => $item['state'] ?? 1,
'document' => "http://".$currentDomain."/public/".$item['document'] ?? '',
'create_time' => $item['create_time'] ?? '',
'update_time' => $item['update_time'] ?? '',
];
}
$data = [
'count' => count($result),
'lists' => $result
];
$this->_success('', $data);
}
return $this->fetch();
}
/**
* 查看详情
*/
public function info()
{
$id = $this->request->param('id', 0);
if (empty($id)) {
$this->_error('参数错误');
}
$invoice = InvoiceLog::with([
"order" => function ($query) {
$query->field('id, order_sn');
},
"invoice" => function ($query) {
$query->with('user');
},
])
->where('id', $id)
->find();
if (empty($invoice)) {
$this->_error('数据不存在');
}
// 处理文件路径转换为完整URL
$currentDomain = $_SERVER['HTTP_HOST'];
if (!empty($invoice['document'])) {
$invoice['document_url'] = "http://".$currentDomain."/public/".$invoice['document'];
}
$this->assign([
'invoice' => $invoice,
]);
return $this->fetch();
}
/**
* 上传
*/
public function edit()
{
$id = $this->request->param('id', 0);
if (empty($id)) {
$this->_error('参数错误');
}
if ($this->request->isAjax() && $this->request->isPost()) {
$post = $this->request->post();
// 表单验证
if (empty($post['invoice_id'])) {
$this->_error('请输入发票信息ID');
}
if (empty($post['document'])){
$this->_error('请上传发票文件');
}
$data = [
'state' => $post['state'],
'document' => $post['document'],
'update_time' => date('Y-m-d H:i:s', time()),
];
// 更新数据
$result = Db::name('invoice_log')->where('id', $id)->update($data);
if ($result) {
$this->_success('上传成功');
} else {
$this->_error('上传失败');
}
}
// 查询订单列表
$orders = Db::name('order')
->where('order_status', '>', 0)
->field('id, order_sn')
->order('id desc')
->limit(100)
->select();
$invoice = InvoiceLog::with(['invoice'])->where('id',$id)->find();
// 处理文件路径转换为完整URL用于显示
if (!empty($invoice['document'])) {
$currentDomain = $_SERVER['HTTP_HOST'];
$invoice['document'] = 'http://'.$currentDomain.'/public/'.$invoice['document'];
}
$this->assign([
'invoice' => $invoice,
'orders' => $orders,
]);
return $this->fetch();
}
/**
* 删除
*/
public function del()
{
if ($this->request->isAjax() && $this->request->isPost()) {
$id = $this->request->post('id', 0);
if (empty($id)) {
$this->_error('参数错误');
}
// 检查数据是否存在
$invoice = Db::name('invoice_log')->where('id', $id)->find();
if (empty($invoice)) {
$this->_error('数据不存在');
}
// 删除数据
$result = Db::name('invoice_log')->where('id', $id)->delete();
if ($result) {
$this->_success('删除成功');
} else {
$this->_error('删除失败');
}
} else {
$this->_error('请求方式错误');
}
}
/**
* 上传发票文件
*/
public function upload()
{
if ($this->request->isPost()) {
$file = $this->request->file('file');
if (empty($file)) {
$this->_error('请选择文件');
}
// 获取文件信息
$fileInfo = $file->getInfo();
$fileName = $fileInfo['name'] ?? '';
// 验证文件类型PDF- 从文件名获取扩展名(最可靠的方法)
$ext = '';
if (!empty($fileName)) {
// 使用pathinfo获取扩展名支持中文文件名
$ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
}
// 如果从文件名获取失败尝试从MIME类型判断
if (empty($ext) && isset($fileInfo['type'])) {
$mimeType = strtolower($fileInfo['type']);
if (strpos($mimeType, 'pdf') !== false || $mimeType === 'application/pdf') {
$ext = 'pdf';
}
}
// 验证扩展名
if (empty($ext) || $ext !== 'pdf') {
$this->_error('只支持PDF格式文件当前文件' . $fileName . ',扩展名:' . ($ext ?: '未知'));
}
// 设置验证规则
$file->validate(['ext' => 'pdf']);
// 上传文件
$uploadPath = rtrim(ROOT_PATH, '/\\') . '/public/uploads/invoice';
$info = $file->move($uploadPath);
if ($info) {
// 返回文件路径(相对路径)
$filePath = 'uploads/invoice/' . $info->getSaveName();
$this->_success('上传成功', ['path' => $filePath]);
} else {
$errorMsg = $file->getError();
$this->_error('上传失败:' . ($errorMsg ?: '未知错误'));
}
} else {
$this->_error('请求方式错误');
}
}
}