This commit is contained in:
baodev0
2026-07-09 11:19:50 +07:00
parent 64232d9168
commit 61c0e026c8
28 changed files with 939 additions and 230 deletions
@@ -87,6 +87,12 @@ class CustomerController extends Controller
$data['email_marketing'] = $request->email_marketing === "on" ? 1 : 0;
$customer = $this->customerRepository->create($data);
if ($request->exists('note')) {
Customer::query()
->whereKey($customer->id)
->update(['note' => $request->input('note')]);
$customer = Customer::query()->find($customer->id);
}
DB::commit();
return response()->json([
@@ -142,6 +148,8 @@ class CustomerController extends Controller
], 404);
}
$customer->refresh();
return response()->json([
'error' => false,
'data' => new CustomerResource($customer)
@@ -170,12 +178,17 @@ class CustomerController extends Controller
}
$this->customerRepository->update($customer->id, $data);
if ($request->exists('note')) {
Customer::query()
->whereKey($customer->id)
->update(['note' => $request->input('note')]);
}
DB::commit();
return response()->json([
'error' => false,
'message' => 'Customer updated successfully',
'data' => new CustomerResource($this->customerRepository->find($customer->id))
'data' => new CustomerResource(Customer::query()->find($customer->id))
]);
} catch (\Exception $e) {
DB::rollBack();
@@ -96,6 +96,11 @@ class EmailTemplateController extends Controller
$setting = Setting::where('key', 'email_settings')->first();
$value = $setting ? json_decode($setting->value, true) : [];
// Ensure mail_enabled defaults to true when not set
if (!isset($value['mail_enabled'])) {
$value['mail_enabled'] = true;
}
return response()->json(['error' => false, 'data' => $value]);
}
@@ -106,6 +111,7 @@ class EmailTemplateController extends Controller
'renewal_template_id' => 'nullable|integer|exists:template,id',
'news_template_id' => 'nullable|integer|exists:template,id',
'invoice_template_id' => 'nullable|integer|exists:template,id',
'mail_enabled' => 'sometimes|boolean',
]);
$setting = Setting::firstOrNew(['key' => 'email_settings']);
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\Api\Admin\Invoice;
use App\Enums\Invoice\InvoiceStatus;
use App\Http\Controllers\Controller;
use App\Http\Requests\InvoiceRequest;
use App\Http\Resources\Api\Admin\InvoiceResource;
@@ -12,11 +13,15 @@ use App\Repositories\Invoice\InvoiceRepositoryInterface;
use App\Repositories\InvoiceAttachment\InvoiceAttachmentRepositoryInterface;
use App\Repositories\InvoiceItem\InvoiceItemRepositoryInterface;
use App\Repositories\Order\OrderRepositoryInterface;
use App\Services\InvoicePaymentService;
use App\Services\InvoicePdfService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use Throwable;
use Yajra\DataTables\Facades\DataTables;
class InvoiceController extends Controller
@@ -26,13 +31,15 @@ class InvoiceController extends Controller
private readonly OrderRepositoryInterface $orderRepository,
private readonly InvoiceItemRepositoryInterface $invoiceItemRepository,
private readonly InvoiceAttachmentRepositoryInterface $attachmentRepository,
private readonly InvoicePdfService $invoicePdfService,
private readonly InvoicePaymentService $invoicePaymentService,
) {}
public function index(Request $request): JsonResponse
{
$query = $this->invoiceRepository->filter(
$request->all(),
['payment', 'customer'],
['payment', 'customer', 'order'],
[],
['*'],
true
@@ -51,7 +58,7 @@ class InvoiceController extends Controller
{
$query = $this->invoiceRepository->filter(
$request->all(),
['payment', 'customer'],
['payment', 'customer', 'order'],
[],
['*'],
true
@@ -138,18 +145,33 @@ class InvoiceController extends Controller
$request->validated();
$status = match (strtolower($request->get('action'))) {
"paid" => 1,
"cancel" => 2,
default => 0
$action = strtolower((string) $request->get('action'));
$status = match ($action) {
'paid' => InvoiceStatus::PAID->value,
'cancel' => InvoiceStatus::CANCELLED->value,
default => null,
};
if ($status === null) {
return response()->json([
'error' => true,
'message' => 'Invalid invoice action.',
], 422);
}
try {
DB::beginTransaction();
$this->invoiceRepository->update($id, [
'status' => $status,
'date_paid' => $status === 1 ? time() : null
]);
$invoice->load(['order', 'customer']);
if ($status === InvoiceStatus::PAID->value) {
$this->invoicePaymentService->markPaidByAdmin($invoice, auth()->id());
} else {
$invoice->update([
'status' => $status,
'paid_at' => null,
]);
}
DB::commit();
return response()->json([
@@ -208,10 +230,10 @@ class InvoiceController extends Controller
try {
DB::beginTransaction();
$this->invoiceRepository->update($id, [
'status' => 1,
'date_paid' => time()
]);
$invoice->load(['order', 'customer']);
$this->invoicePaymentService->markPaidByAdmin($invoice, auth()->id());
DB::commit();
return response()->json([
@@ -240,9 +262,9 @@ class InvoiceController extends Controller
try {
DB::beginTransaction();
$this->invoiceRepository->update($id, [
'status' => 2,
'date_paid' => null
$invoice->update([
'status' => InvoiceStatus::CANCELLED->value,
'paid_at' => null
]);
DB::commit();
@@ -334,6 +356,7 @@ class InvoiceController extends Controller
{
$request->validate([
'message' => 'nullable|string|max:1000',
'type' => 'nullable|string|in:invoice,renewal',
]);
$invoice = $this->invoiceRepository->find($id);
@@ -349,11 +372,45 @@ class InvoiceController extends Controller
$attachments = $this->attachmentRepository->getByInvoice($id);
$companyName = setting('general.company_name') ?: config('app.name');
$message = $request->get('message', "Vui lòng xem thông tin hóa đơn #{$invoice->order_num} của bạn.");
Mail::to($customer->email)->queue(
new InvoiceNotificationMail($invoice, $attachments, $customer->fullname, $message, $companyName)
$type = $request->get('type', 'invoice');
$message = $request->get(
'message',
$type === 'renewal'
? "Dịch vụ của quý khách sắp đến hạn gia hạn. Vui lòng kiểm tra hóa đơn #{$invoice->order_num} được đính kèm và hoàn tất thanh toán để dịch vụ không bị gián đoạn."
: "Vui lòng xem thông tin hóa đơn #{$invoice->order_num} của bạn."
);
$paymentUrl = url('/client/invoices/' . $invoice->invoice_number . '/payment');
try {
$invoicePdf = $this->invoicePdfService->generate($invoice);
$invoicePdfFileName = $this->invoicePdfService->generateFileName($invoice);
Mail::to($customer->email)->send(
new InvoiceNotificationMail(
$invoice,
$attachments,
$customer->fullname,
$message,
$companyName,
$type,
$invoicePdf,
$invoicePdfFileName,
$paymentUrl
)
);
} catch (Throwable $e) {
Log::error('InvoiceController@sendMail failed', [
'invoice_id' => $id,
'invoice_number' => $invoice->invoice_number ?? null,
'customer_email' => $customer->email,
'type' => $type,
'error' => $e->getMessage(),
]);
return response()->json([
'error' => true,
'message' => 'Không thể gửi email: ' . $e->getMessage(),
], 500);
}
return response()->json([
'error' => false,
@@ -19,6 +19,7 @@ use App\Repositories\Api\Order\OrderRepository;
use App\Services\ActivityLogger;
use App\Services\Api\Order\OrderQueryService;
use App\Services\Api\Order\OrderService;
use App\Services\InvoicePaymentService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
@@ -29,6 +30,7 @@ class OrderControllerRefactor extends Controller
private readonly OrderRepository $orderRepository,
private readonly OrderQueryService $orderQueryService,
private readonly ActivityLogger $activityLogger,
private readonly InvoicePaymentService $invoicePaymentService,
) {
}
@@ -179,6 +181,50 @@ class OrderControllerRefactor extends Controller
return response()->json(['success' => true, 'data' => new InvoiceDetailResource($order->invoice->setRelation('order', $order))]);
}
public function getClientInvoiceDetailByNumber(Request $request, string $invoiceNumber): JsonResponse
{
$invoice = $this->orderQueryService->findCustomerInvoiceByNumber((int) $request->user()->id, $invoiceNumber);
if (! $invoice) {
return response()->json(['success' => false, 'message' => 'Hóa đơn không tồn tại.'], 404);
}
return response()->json([
'success' => true,
'data' => new InvoiceDetailResource($invoice),
'payment_url' => url('/client/invoices/' . $invoice->invoice_number . '/payment'),
]);
}
public function payClientInvoiceByNumber(Request $request, string $invoiceNumber): JsonResponse
{
$invoice = $this->orderQueryService->findCustomerInvoiceByNumber((int) $request->user()->id, $invoiceNumber);
if (! $invoice) {
return response()->json(['success' => false, 'message' => 'Hóa đơn không tồn tại.'], 404);
}
try {
$paidInvoice = $this->invoicePaymentService->payWithBalance($invoice, $request->user(), 'balance');
$this->activityLogger->log(
event: 'payment',
logName: 'Invoice',
description: "Customer {$request->user()->email} thanh toán hóa đơn #{$paidInvoice->invoice_number}",
subject: $paidInvoice,
newValues: ['invoice_number' => $paidInvoice->invoice_number, 'total' => $paidInvoice->total],
);
return response()->json([
'success' => true,
'message' => 'Thanh toán hóa đơn thành công.',
'data' => new InvoiceDetailResource($paidInvoice),
]);
} catch (\Throwable $e) {
return response()->json(['success' => false, 'message' => $e->getMessage()], 422);
}
}
public function getClientServices(Request $request): JsonResponse
{
$services = $this->orderQueryService->getCustomerServices((int) $request->user()->id);
@@ -40,4 +40,17 @@ class SettingController extends Controller
'error' => false
]);
}
public function getEmailSetting(): \Illuminate\Http\JsonResponse
{
$setting = Setting::where('key', 'email_settings')->first();
$value = $setting ? json_decode($setting->value, true) : [];
return response()->json([
'error' => false,
'data' => [
'mail_enabled' => $value['mail_enabled'] ?? true,
],
]);
}
}
@@ -33,6 +33,7 @@ class CreateCustomerRequest extends FormRequest
'postcode' => 'nullable',
'country' => 'nullable',
'email_marketing' => 'nullable',
'note' => 'nullable|string',
];
}
}
@@ -16,6 +16,7 @@ class CustomerResource extends JsonResource
'email' => $this->email,
'phone' => $this->phone,
'company' => $this->company,
'note' => $this->note,
'status' => (int) $this->status,
'total' => (int) $this->total,
'created_at' => $this->created_at,
@@ -10,21 +10,23 @@ class InvoiceResource extends JsonResource
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'order_num' => $this->order_num,
'customer' => $this->whenLoaded('customer', fn() => [
'id' => $this->id,
'invoice_number' => $this->invoice_number,
'order_num' => $this->whenLoaded('order', fn() => $this->order?->order_num),
'customer' => $this->whenLoaded('customer', fn() => [
'id' => $this->customer->id,
'fullname' => $this->customer->fullname,
'email' => $this->customer->email,
]),
'sub_total' => $this->sub_total,
'tax' => $this->tax,
'total' => $this->total,
'status' => $this->status,
'date' => $this->date,
'due_date' => $this->due_date,
'date_paid' => $this->date_paid,
'created_at' => $this->created_at,
'sub_total' => $this->sub_total,
'discount_amount' => $this->discount_amount,
'tax' => $this->tax,
'total' => $this->total,
'status' => $this->status,
'due_date' => $this->due_date,
'paid_at' => $this->paid_at,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}
+20 -18
View File
@@ -35,25 +35,27 @@ class OrderResource extends JsonResource
'value' => $this->discount->value,
]),
'invoice' => $this->whenLoaded('invoice', fn() => [
'id' => $this->invoice->id,
'order_num' => $this->invoice->order_num,
'sub_total' => $this->invoice->sub_total,
'tax' => $this->invoice->tax,
'total' => $this->invoice->total,
'status' => $this->invoice->status,
'date' => $this->invoice->date,
'due_date' => $this->invoice->due_date,
'paid_at' => $this->invoice->paid_at,
'id' => $this->invoice->id,
'invoice_number' => $this->invoice->invoice_number,
'sub_total' => $this->invoice->sub_total,
'discount_amount' => $this->invoice->discount_amount,
'tax' => $this->invoice->tax,
'total' => $this->invoice->total,
'status' => $this->invoice->status,
'date' => $this->invoice->created_at,
'due_date' => $this->invoice->due_date,
'date_paid' => $this->invoice->paid_at,
]),
'total' => $this->total,
'payment_method' => $this->whenLoaded('payment', fn() => $this->payment->name),
'payment_proof' => $this->payment_proof,
'note' => $this->note,
'client_ip' => $this->client_ip,
'status' => $this->status,
'paid_at' => $this->paid_at,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'sub_total' => $this->sub_total,
'discount_amount' => $this->discount_amount,
'total' => $this->total,
'payment_method' => $this->whenLoaded('payment', fn() => $this->payment->name, $this->payment_method),
'payment_proof' => $this->payment_proof,
'note' => $this->note,
'client_ip' => $this->client_ip,
'status' => $this->status,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'vmwares' => $this->whenLoaded('moduleVmwares', fn() => $this->moduleVmwares->map(fn($vm) => [
'id' => $vm->id,
'vmware_id' => $vm->vmware_id,