feat: add GeneratesUniqueSlug class for unique slug generation

feat: create PostCategoryFactory for generating post category data

feat: create PostFactory for generating post data

feat: add retry_requested_at column to services table

feat: add vcenter provisioning fields to products table

feat: add module_host_id to os table

feat: create email_notification_templates table for email templates

feat: add scenario and format fields to template table

feat: add admin fields to transactions table

fix: update ticket table columns and types

feat: add uuid to ticket table and backfill existing records

fix: make ticket_reply user_id nullable

feat: create notifications table for user notifications

fix: make ticket uuid nullable

feat: create post_categories table for blog categories

feat: create posts table for blog posts

feat: add invoice email template for PDF generation
This commit is contained in:
baodev0
2026-05-25 15:39:42 +07:00
parent a31d238135
commit 520f21cb11
73 changed files with 4682 additions and 440 deletions
@@ -3,6 +3,7 @@
namespace App\Http\Controllers\Api\Admin\EmailTemplate;
use App\Http\Controllers\Controller;
use App\Models\Setting;
use App\Models\Template;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
@@ -10,36 +11,53 @@ use Illuminate\Support\Facades\DB;
class EmailTemplateController extends Controller
{
public function index(): JsonResponse
public function index(Request $request): JsonResponse
{
$templates = Template::orderBy('type_id')->orderBy('id')->get();
$query = Template::with('invoiceTemplate:id,name,scenario')
->orderBy('scenario')
->orderBy('id');
if ($request->filled('scenario')) {
$query->where('scenario', $request->scenario);
}
$templates = $query->get()->map(fn($t) => $this->format($t));
return response()->json(['error' => false, 'data' => $templates]);
}
public function show(int $id): JsonResponse
{
$template = Template::findOrFail($id);
return response()->json(['error' => false, 'data' => $template]);
$template = Template::with('invoiceTemplate:id,name,scenario')->findOrFail($id);
return response()->json(['error' => false, 'data' => $this->format($template)]);
}
public function store(Request $request): JsonResponse
{
$data = $request->validate([
'type_id' => 'required|integer|min:1',
'name' => 'nullable|string|max:100',
'subject' => 'nullable|string|max:100',
'message' => 'nullable|string',
'status' => 'boolean',
'name' => 'nullable|string|max:255',
'scenario' => 'nullable|string|in:vm_credentials,renewal_reminder,news_notification,invoice_pdf',
'format' => 'nullable|in:html,json',
'invoice_template_id' => 'nullable|integer|exists:template,id',
'subject' => 'nullable|string|max:255',
'message' => 'nullable|string',
'status' => 'boolean',
]);
$template = DB::transaction(fn () => Template::create($data));
$template = DB::transaction(fn() => Template::create([
'type_id' => 0,
'name' => $data['name'] ?? null,
'scenario' => $data['scenario'] ?? null,
'format' => $data['format'] ?? 'html',
'invoice_template_id' => $data['invoice_template_id'] ?? null,
'subject' => $data['subject'] ?? null,
'message' => $data['message'] ?? null,
'status' => $data['status'] ?? 1,
]));
return response()->json([
'error' => false,
'message' => 'Thêm email template thành công',
'data' => $template,
'error' => false, 'message' => 'Tạo template thành công.',
'data' => $this->format($template),
], 201);
}
@@ -48,31 +66,90 @@ class EmailTemplateController extends Controller
$template = Template::findOrFail($id);
$data = $request->validate([
'type_id' => 'sometimes|integer|min:1',
'name' => 'sometimes|nullable|string|max:100',
'subject' => 'sometimes|nullable|string|max:100',
'message' => 'sometimes|nullable|string',
'status' => 'sometimes|boolean',
'name' => 'sometimes|nullable|string|max:255',
'scenario' => 'sometimes|nullable|string|in:vm_credentials,renewal_reminder,news_notification,invoice_pdf',
'format' => 'sometimes|nullable|in:html,json',
'invoice_template_id' => 'sometimes|nullable|integer|exists:template,id',
'subject' => 'sometimes|nullable|string|max:255',
'message' => 'sometimes|nullable|string',
'status' => 'sometimes|boolean',
]);
DB::transaction(fn () => $template->update($data));
DB::transaction(fn() => $template->update($data));
return response()->json([
'error' => false,
'message' => 'Cập nhật email template thành công',
'data' => $template->fresh(),
'error' => false, 'message' => 'Cập nhật template thành công.',
'data' => $this->format($template->fresh(['invoiceTemplate'])),
]);
}
public function destroy(int $id): JsonResponse
{
$template = Template::findOrFail($id);
DB::transaction(fn() => Template::findOrFail($id)->delete());
return response()->json(['error' => false, 'message' => 'Đã xoá template.']);
}
DB::transaction(fn () => $template->delete());
// ── Email settings (scenario assignments) ─────────────────────────────────
return response()->json([
'error' => false,
'message' => 'Đã xoá email template',
public function getEmailSettings(): JsonResponse
{
$setting = Setting::where('key', 'email_settings')->first();
$value = $setting ? json_decode($setting->value, true) : [];
return response()->json(['error' => false, 'data' => $value]);
}
public function updateEmailSettings(Request $request): JsonResponse
{
$data = $request->validate([
'vm_template_id' => 'nullable|integer|exists:template,id',
'renewal_template_id' => 'nullable|integer|exists:template,id',
'news_template_id' => 'nullable|integer|exists:template,id',
'invoice_template_id' => 'nullable|integer|exists:template,id',
]);
$setting = Setting::firstOrNew(['key' => 'email_settings']);
$current = $setting->value ? json_decode($setting->value, true) : [];
$setting->value = json_encode(array_merge($current, $data));
$setting->save();
return response()->json(['error' => false, 'message' => 'Đã lưu cài đặt email.', 'data' => json_decode($setting->value, true)]);
}
// ── Invoice PDF preview ───────────────────────────────────────────────────
public function previewInvoiceJson(int $id): JsonResponse
{
$template = Template::findOrFail($id);
if ($template->scenario !== 'invoice_pdf' || $template->format !== 'json') {
return response()->json(['error' => true, 'message' => 'Template này không phải invoice JSON.'], 422);
}
$config = json_decode($template->message ?? '{}', true);
// Return the config + blade HTML preview
$html = view('pdf.invoice-preview', compact('config'))->render();
return response()->json(['error' => false, 'html' => $html]);
}
// ── Helpers ───────────────────────────────────────────────────────────────
private function format(Template $t): array
{
return [
'id' => $t->id,
'type_id' => $t->type_id,
'name' => $t->name,
'scenario' => $t->scenario,
'scenario_label' => Template::SCENARIOS[$t->scenario] ?? null,
'format' => $t->format ?? 'html',
'invoice_template_id' => $t->invoice_template_id,
'invoice_template_name' => $t->invoiceTemplate?->name,
'subject' => $t->subject,
'message' => $t->message,
'status' => $t->status,
'created_at' => $t->created_at,
'updated_at' => $t->updated_at,
];
}
}