feat(billing): renewals, coupons, VAT numbering, billing cycles and invoice payment tracking
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
ea9a53f742
commit
25f23d541e
@@ -5,10 +5,12 @@ namespace App\Http\Controllers\Api\Admin\Invoice\Concerns;
|
||||
use App\Jobs\Mail\SendInvoiceMailJob;
|
||||
use App\Mail\InvoiceNotificationMail;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\InvoiceAttachment;
|
||||
use App\Services\InvoiceReminderService;
|
||||
use App\Support\AutoEmailGate;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
|
||||
@@ -56,6 +58,26 @@ trait SendsInvoiceNotifications
|
||||
]);
|
||||
}
|
||||
|
||||
public function pdf(Request $request, int $id): Response|JsonResponse
|
||||
{
|
||||
$invoice = $this->invoiceRepository->find($id);
|
||||
if (!$invoice) {
|
||||
return response()->json(['error' => true, 'message' => "Can't find invoice #$id"], 404);
|
||||
}
|
||||
|
||||
$vat = $request->query('variant') === 'vat';
|
||||
if ($vat && !$invoice->vat_number) {
|
||||
return response()->json(['error' => true, 'message' => 'Hoá đơn chưa được cấp số VAT'], 422);
|
||||
}
|
||||
|
||||
$fileName = $vat ? $this->invoicePdfService->vatFileName($invoice) : $this->invoicePdfService->generateFileName($invoice);
|
||||
|
||||
return response($this->invoicePdfService->generate($invoice), 200, [
|
||||
'Content-Type' => 'application/pdf',
|
||||
'Content-Disposition' => 'inline; filename="' . $fileName . '"',
|
||||
]);
|
||||
}
|
||||
|
||||
public function previewMail(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$invoice = $this->invoiceForMail($request, $id);
|
||||
@@ -72,6 +94,8 @@ trait SendsInvoiceNotifications
|
||||
'subject' => $mail->envelope()->subject,
|
||||
'html' => $mail->render(),
|
||||
'attachment_filename' => $this->invoicePdfService->generateFileName($invoice),
|
||||
'vat_attachment_filename' => $invoice->vat_number ? $this->invoicePdfService->vatFileName($invoice) : null,
|
||||
'contracts' => $this->contractsOf($invoice)->map(fn (InvoiceAttachment $file) => ['id' => $file->id, 'filename' => $file->filename])->values(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
@@ -95,6 +119,8 @@ trait SendsInvoiceNotifications
|
||||
$request->input('subject'),
|
||||
$request->filled('html') ? $request->input('html') : null,
|
||||
Auth::guard('admin')->id(),
|
||||
$request->boolean('attach_vat'),
|
||||
$this->contractsOf($invoice)->whereIn('id', (array) $request->input('contract_ids', []))->pluck('id')->all(),
|
||||
));
|
||||
|
||||
return response()->json([
|
||||
@@ -119,6 +145,9 @@ trait SendsInvoiceNotifications
|
||||
'type' => 'nullable|string|in:invoice,renewal',
|
||||
'subject' => 'nullable|required_with:html|string|max:255',
|
||||
'html' => 'nullable|required_with:subject|string|max:500000',
|
||||
'attach_vat' => 'nullable|boolean',
|
||||
'contract_ids' => 'nullable|array|max:10',
|
||||
'contract_ids.*' => 'integer',
|
||||
]);
|
||||
|
||||
$invoice = $this->invoiceRepository->find($id);
|
||||
@@ -131,6 +160,18 @@ trait SendsInvoiceNotifications
|
||||
return response()->json(['error' => true, 'message' => 'Customer email not found'], 422);
|
||||
}
|
||||
|
||||
if ($request->boolean('attach_vat') && !$invoice->vat_number) {
|
||||
return response()->json(['error' => true, 'message' => 'Hoá đơn chưa được cấp số VAT, không thể đính kèm hoá đơn VAT'], 422);
|
||||
}
|
||||
|
||||
return $invoice;
|
||||
}
|
||||
|
||||
private function contractsOf(Invoice $invoice)
|
||||
{
|
||||
return InvoiceAttachment::query()
|
||||
->where('invoice_id', $invoice->id)
|
||||
->where('type', InvoiceAttachment::TYPE_CONTRACT)
|
||||
->get();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user