diff --git a/application/admin/controller/Invoice.php b/application/admin/controller/Invoice.php new file mode 100644 index 00000000..60aeb7e9 --- /dev/null +++ b/application/admin/controller/Invoice.php @@ -0,0 +1,236 @@ +request->isAjax()) { + $get = $this->request->get(); + + // 构建查询条件 + $where = []; + if (!empty($get['keyword'])) { + $where[] = ['i.oid|i.invoice_id', 'like', '%' . $get['keyword'] . '%']; + } + if (isset($get['state']) && $get['state'] !== '') { + $where[] = ['i.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('上传失败'); + } + } + + // 处理文件路径(转换为完整URL用于显示) + if (!empty($invoice['document'])) { + $invoice['document'] = UrlServer::getFileUrl($invoice['document']); + } + + // 查询订单列表 + $orders = Db::name('order') + ->where('order_status', '>', 0) + ->field('id, order_sn') + ->order('id desc') + ->limit(100) + ->select(); + + $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('请求方式错误'); + } + } +} \ No newline at end of file diff --git a/application/admin/logic/ControlLogic.php b/application/admin/logic/ControlLogic.php index 44c38e90..78dd0178 100644 --- a/application/admin/logic/ControlLogic.php +++ b/application/admin/logic/ControlLogic.php @@ -57,14 +57,11 @@ class ControlLogic{ $item['add']=$order_info['add']; $order=Db::name('order')->where('order_sn',$order_info['order_sn'])->find(); - $item['adderss']=$order['address']; - - $goods=Db::name('goods')->where('id',$order['goods_id'])->find(); - - $item['goods_name']=$goods['name']; - - - + if ($order != null){ + $item['adderss']=$order['address'] ?? ""; + $goods=Db::name('goods')->where('id',$order['goods_id'])->find(); + $item['goods_name']=$goods['name'] ?? ""; + } } return ['count'=>$count , 'lists'=>$lists]; diff --git a/application/admin/model/Invoice.php b/application/admin/model/Invoice.php new file mode 100644 index 00000000..77e75522 --- /dev/null +++ b/application/admin/model/Invoice.php @@ -0,0 +1,16 @@ +belongsTo(User::class, 'uid', 'id'); + } +} + diff --git a/application/admin/model/InvoiceLog.php b/application/admin/model/InvoiceLog.php new file mode 100644 index 00000000..86240a77 --- /dev/null +++ b/application/admin/model/InvoiceLog.php @@ -0,0 +1,20 @@ +belongsTo(Invoice::class, 'invoice_id', 'id'); + } + + public function order() + { + return $this->belongsTo(Order::class, 'oid', 'id'); + } +} \ No newline at end of file diff --git a/application/admin/view/invoice/details.html b/application/admin/view/invoice/details.html new file mode 100644 index 00000000..e69de29b diff --git a/application/admin/view/invoice/edit.html b/application/admin/view/invoice/edit.html new file mode 100644 index 00000000..c94d4d2c --- /dev/null +++ b/application/admin/view/invoice/edit.html @@ -0,0 +1,111 @@ +{layout name="layout2" /} +
+
+
+
+
+
+ +
+ +
+
+
+ +
+ + + +
+
+
+ +
+
+ +
+ +
+ {if $invoice.document} +
+ {$invoice.document} + 已上传 +
+ {/if} +
+
+
+
+
+
+
+
+
+ +
+
+
+ + + diff --git a/application/admin/view/invoice/info.html b/application/admin/view/invoice/info.html new file mode 100644 index 00000000..81f227ed --- /dev/null +++ b/application/admin/view/invoice/info.html @@ -0,0 +1,194 @@ +{layout name="layout2" /} + +
+
+ +
+
+ 基本信息 +
+
+
+ 记录ID: + {$invoice.id} +
+
+ 发票信息ID: + {$invoice.invoice_id} +
+
+ 状态: + + {if $invoice.state == 1} + 待开票 + {elseif $invoice.state == 2} + 驳回 + {elseif $invoice.state == 3} + 完成 + {else} + 未知 + {/if} + +
+
+ 发票文件: + + {if $invoice.document} +
+ + 查看文件 + + {$invoice.document_url} +
+ {else} + 无文件 + {/if} +
+
+
+ 创建时间: + {$invoice.create_time|default='--'} +
+
+ 更新时间: + {$invoice.update_time|default='--'} +
+
+
+ + +
+
+ 订单信息 +
+
+
+ 订单编号: + {$invoice.order.order_sn ?? "--"} +
+
+ 订单金额: + + ¥{$invoice.order.settle_amount ?? "0.00"} + +
+
+
+ + +
+
+ 用户信息 +
+
+
+ 系统用户名: + {$invoice.invoice.user.nickname ?? "--"} +
+
+
+ + +
+
+ 发票抬头信息 +
+
+
+ 类型: + + {if $invoice.invoice.type == 1} + 个人 + {elseif $invoice.invoice.type == 2} + 企业 + {else} + 未知 + {/if} + +
+
+ 发票类型: + {$invoice.invoice.invoice_type ?? "--"} +
+
+ 发票抬头/企业名称: + {$invoice.invoice.invoice_title ?? "--"} +
+
+ 纳税人识别号/统一社会信用代码: + + {$invoice.invoice.identification ?? "--"} + +
+
+ 注册地址: + {$invoice.invoice.address ?? "--"} +
+
+ 注册电话: + {$invoice.invoice.phone ?? "--"} +
+
+ 开户行: + {$invoice.invoice.bank ?? "--"} +
+
+ 开户账号: + + {$invoice.invoice.account ?? "--"} + +
+
+
+
+
diff --git a/application/admin/view/invoice/lists.html b/application/admin/view/invoice/lists.html new file mode 100644 index 00000000..40ab7b5b --- /dev/null +++ b/application/admin/view/invoice/lists.html @@ -0,0 +1,189 @@ +{layout name="layout1" /} +
+
+
+ +
+
+
+
+
+ +
+
+ +
+
+
+
+
+ + + +
+
+
+ +
+
+ + + +
+
+
+ + + diff --git a/application/admin/view/invoice/upload.html b/application/admin/view/invoice/upload.html new file mode 100644 index 00000000..e69de29b diff --git a/application/api/controller/Invoice.php b/application/api/controller/Invoice.php new file mode 100644 index 00000000..ea466083 --- /dev/null +++ b/application/api/controller/Invoice.php @@ -0,0 +1,120 @@ +request->post(); + if (empty($dataPost['type'])){ + return $this->_error('请选择发票类型'); + } + if (empty($dataPost['invoice_type'])){ + return $this->_error('请选择类型'); + } + if (empty($dataPost['invoice_title'])){ + return $this->_error('请填写发票抬头'); + } + if (empty($dataPost['identification'])){ + return $this->_error('请填写纳税人识别号'); + } + $uid = $this->user_id; + $data = [ + 'uid' => $uid, + 'type' => $dataPost['type'], + 'invoice_type' => $dataPost['invoice_type'], + 'invoice_title' => $dataPost['invoice_title'], + 'identification' => $dataPost['identification'], + 'address' => $dataPost['address'], + 'phone' => $dataPost['phone'], + 'bank' => $dataPost['bank'], + 'account' => $dataPost['account'], + 'create_time' => date('Y-m-d H:i:s', time()) + ]; + $res = Db::name('invoice')->insert($data); + if ($res){ + return $this->_success('添加成功'); + }else{ + return $this->_error('添加失败'); + } + } + + //发票列表 + public function list() + { + $uid = $this->user_id; + $list = Db::name('invoice')->where('uid', $uid)->select(); + return $this->_success('成功', $list); + } + + //申请发票 + public function apply() + { + $dataPost = $this->request->post(); + if (empty($dataPost['invoice_id'])) { + return $this->_error('请选择发票信息'); + } + if (empty($dataPost['order_id'])) { + return $this->_error('请选择要开票订单'); + } + $uid = $this->user_id; + //查询是否存在该开票信息 + $invoice = Db::name('invoice')->where('id', $dataPost['invoice_id'])->find(); + if (!$invoice) { + return $this->_error('发票信息不存在'); + } + $order = Db::name('order')->where('id', $dataPost['order_id'])->find(); + if (!$order) { + return $this->_error('订单信息不存在'); + } + $data = [ + 'uid' => $uid, + 'invoice_id' => $dataPost['invoice_id'], + 'oid' => $dataPost['order_id'], + 'state' => 1, + 'create_time' => date('Y-m-d H:i:s', time()) + ]; + $res = Db::name('invoice_log')->insert($data); + if ($res) { + return $this->_success('申请成功'); + } else { + return $this->_error('申请失败'); + } + } + + //发票申请列表 + public function applyList() + { + $uid = $this->user_id; + $list = InvoiceLog::with([ + 'invoice' => function ($query) { + $query->field('id,type,invoice_type,invoice_title'); + }, + 'order' => function ($query) { + $query->field('id,order_sn,settle_amount'); + } + ])->where('uid', $uid)->paginate(10); + $data = []; + if ($list != null){ + foreach ($list as $value){ + $data[] = [ + 'id' => $value['id'], + 'state' => $value['state'], + 'type' => $value['invoice']['type'], + 'invoice_type' => $value['invoice']['invoice_type'], + 'invoice_title' => $value['invoice']['invoice_title'], + 'order_sn' => $value['order']['order_sn'] ?? '', + 'order_money' => $value['order']['settle_amount'] ?? 0, + ]; + } + } + $datas = ['data_list' => $data, 'total' => $list->total()]; + return $this->_success('成功', $datas); + } +} \ No newline at end of file