Files
portal-api/app/Http/Controllers/Api/Admin/Invoice/Concerns/HandlesInvoiceAttachments.php
T
baodev0andClaude Opus 5.5 46aa138eec feat(admin): IP pool allocation, billing exports, service attachments
- IP pool: sheet import (xlsx/csv) matched by address, bulk allocate/revoke
  with notification email (ip_allocated/ip_revoked), ip_pool_logs history.
- Orders/invoices: Excel export on branded template (BrandedReportWriter),
  shared billing filters, order item service info in list responses.
- Web design quote Excel export; activity log export.
- Service attachments with copy-to-invoice; deployment invoice attachments.
- Customer email attachments stored on disk and attached by path.
- Admin password change endpoint; user.fullname backfill migration;
  orders.user_id backfill from logs.
- Disable deploy workflow until CI/CD is set up.
- Ignore stored customer email and service attachment uploads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 19:58:36 +07:00

80 lines
2.7 KiB
PHP

<?php
namespace App\Http\Controllers\Api\Admin\Invoice\Concerns;
use App\Models\InvoiceAttachment;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
trait HandlesInvoiceAttachments
{
public function listAttachments(int $id): JsonResponse
{
$invoice = $this->invoiceRepository->find($id);
if (!$invoice) {
return response()->json(['error' => true, 'message' => "Can't find invoice #$id"], 404);
}
$attachments = $this->attachmentRepository->getByInvoice($id);
return response()->json([
'error' => false,
'data' => $attachments,
]);
}
public function uploadAttachment(Request $request, int $id): JsonResponse
{
$invoice = $this->invoiceRepository->find($id);
if (!$invoice) {
return response()->json(['error' => true, 'message' => "Can't find invoice #$id"], 404);
}
$request->validate([
'file' => InvoiceAttachment::UPLOAD_RULE,
'type' => 'nullable|in:' . InvoiceAttachment::TYPE_PAYMENT_PROOF . ',' . InvoiceAttachment::TYPE_DEPLOYMENT,
]);
$file = $request->file('file');
$path = $file->store("invoice-attachments/{$id}", 'public');
$uploader = $request->user()?->username ?? $request->user()?->name ?? 'admin';
$attachment = $this->attachmentRepository->create([
'invoice_id' => $id,
'type' => $request->input('type', InvoiceAttachment::TYPE_PAYMENT_PROOF),
'filename' => $file->getClientOriginalName(),
'path' => $path,
'disk' => 'public',
'mime_type' => $file->getMimeType(),
'size' => $file->getSize(),
'uploaded_by' => $uploader,
]);
return response()->json([
'error' => false,
'message' => 'File uploaded successfully',
'data' => $attachment,
], 201);
}
public function deleteAttachment(int $id, int $attachmentId): JsonResponse
{
$invoice = $this->invoiceRepository->find($id);
if (!$invoice) {
return response()->json(['error' => true, 'message' => "Can't find invoice #$id"], 404);
}
$attachment = $this->attachmentRepository->find($attachmentId);
if (!$attachment || $attachment->invoice_id !== $id) {
return response()->json(['error' => true, 'message' => 'Attachment not found'], 404);
}
$this->attachmentRepository->deleteWithFile($attachmentId);
return response()->json([
'error' => false,
'message' => 'Attachment deleted',
]);
}
}