Files
yuezi-saas/backend/app/Http/Controllers/Admin/Finance/InvoiceController.php
T
li c778639f05 fix: 修复列表筛选空参与树数据展示
将后端 status/audit_status 筛选从 has() 改为 filled(),避免空字符串导致列表被误筛空;修复预定 status=0 筛选失效;前端兼容财务分类树形返回与菜单/字典数组返回,解决新增成功但列表空。
2026-03-15 00:06:11 +08:00

95 lines
3.0 KiB
PHP

<?php
namespace App\Http\Controllers\Admin\Finance;
use App\Http\Controllers\Controller;
use App\Models\Finance\Invoice;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class InvoiceController extends Controller
{
public function index(Request $request): JsonResponse
{
$query = Invoice::query()->with(['customer']);
$query->when($request->invoice_no, fn($q, $v) => $q->where('invoice_no', 'like', "%{$v}%"));
$query->when($request->customer_id, fn($q, $v) => $q->where('customer_id', $v));
$query->when($request->filled('status'), fn($q) => $q->where('status', $request->status));
$query->when($request->type, fn($q, $v) => $q->where('type', $v));
return $this->paginate($query->latest()->paginate($request->input('per_page', 15)));
}
public function store(Request $request): JsonResponse
{
$validated = $request->validate([
'invoice_no' => 'required|string|max:50|unique:invoices',
'customer_id' => 'nullable|exists:customers,id',
'type' => 'sometimes|integer|in:1,2',
'amount' => 'required|numeric|min:0',
'tax_amount' => 'sometimes|numeric|min:0',
'title' => 'nullable|string|max:255',
'tax_no' => 'nullable|string|max:50',
'related_type' => 'nullable|string|max:50',
'related_id' => 'nullable|integer',
]);
$validated['operator_id'] = auth()->id();
$validated['status'] = 0;
return $this->success(Invoice::create($validated));
}
public function show(Invoice $invoice): JsonResponse
{
return $this->success($invoice->load(['customer', 'operator']));
}
public function update(Request $request, Invoice $invoice): JsonResponse
{
if ($invoice->status !== 0) {
return $this->error('只能修改待开票的发票', 40001);
}
$validated = $request->validate([
'customer_id' => 'nullable|exists:customers,id',
'type' => 'sometimes|integer|in:1,2',
'amount' => 'sometimes|numeric|min:0',
'tax_amount' => 'sometimes|numeric|min:0',
'title' => 'nullable|string|max:255',
'tax_no' => 'nullable|string|max:50',
'related_type' => 'nullable|string|max:50',
'related_id' => 'nullable|integer',
]);
$invoice->update($validated);
return $this->success($invoice);
}
public function destroy(Invoice $invoice): JsonResponse
{
if ($invoice->status !== 0) {
return $this->error('只能删除待开票的发票', 40001);
}
$invoice->delete();
return $this->success(null);
}
public function issue(Invoice $invoice): JsonResponse
{
if ($invoice->status !== 0) {
return $this->error('该发票已开具或已红冲', 40001);
}
$invoice->update([
'status' => 1,
'issued_at' => now(),
]);
return $this->success($invoice);
}
}