update email template
This commit is contained in:
@@ -3,16 +3,19 @@
|
||||
namespace App\Http\Controllers\Api\Admin\Invoice;
|
||||
|
||||
use App\Enums\Invoice\InvoiceStatus;
|
||||
use App\Helpers\OrderHelper;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\InvoiceRequest;
|
||||
use App\Http\Resources\Api\Admin\InvoiceResource;
|
||||
use App\Mail\InvoiceNotificationMail;
|
||||
use App\Models\Customer;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\InvoiceItem;
|
||||
use App\Models\Order;
|
||||
use App\Models\Payment;
|
||||
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;
|
||||
@@ -28,7 +31,6 @@ class InvoiceController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly InvoiceRepositoryInterface $invoiceRepository,
|
||||
private readonly OrderRepositoryInterface $orderRepository,
|
||||
private readonly InvoiceItemRepositoryInterface $invoiceItemRepository,
|
||||
private readonly InvoiceAttachmentRepositoryInterface $attachmentRepository,
|
||||
private readonly InvoicePdfService $invoicePdfService,
|
||||
@@ -76,38 +78,63 @@ class InvoiceController extends Controller
|
||||
->make(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Admin tạo thêm 1 invoice thủ công cho 1 order đã tồn tại — chỉ dùng cho phụ
|
||||
* phí/điều chỉnh thuộc CHÍNH dịch vụ trong order đó (vd. phạt trễ hạn, phí xử lý).
|
||||
* Order cố định: không dùng endpoint này để bán thêm dịch vụ mới — dịch vụ mới phải
|
||||
* là 1 Order riêng (qua OrderController::store). Đi qua cùng convention với renewal
|
||||
* invoice ở nơi khác trong codebase: gắn order_id vào order gốc, tự sinh invoice_number.
|
||||
*/
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'customer_id' => 'required|numeric',
|
||||
'payment_method' => 'required|numeric',
|
||||
'sub_total' => 'required|numeric',
|
||||
'total' => 'required|numeric',
|
||||
'order_id' => 'required|integer|exists:orders,id',
|
||||
'invoice_type' => 'nullable|integer|in:1,2,3',
|
||||
'description' => 'nullable|string|max:500',
|
||||
'due_date' => 'nullable|date',
|
||||
'sub_total' => 'required|numeric|min:0',
|
||||
'tax' => 'nullable|numeric|min:0',
|
||||
]);
|
||||
|
||||
$invoiceData = [
|
||||
'date' => time(),
|
||||
'due_date' => time() + (7 * 24 * 60 * 60),
|
||||
'sub_total' => $validated['sub_total'],
|
||||
'tax' => $validated['sub_total'] * 0.1,
|
||||
'total' => $validated['total'],
|
||||
'payment_method' => $validated['payment_method'],
|
||||
'customer_id' => $validated['customer_id'],
|
||||
'status' => 0,
|
||||
];
|
||||
$order = Order::find($validated['order_id']);
|
||||
$subTotal = (int) $validated['sub_total'];
|
||||
$tax = (int) ($validated['tax'] ?? 0);
|
||||
$total = $subTotal + $tax;
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$invoice = $this->invoiceRepository->create($invoiceData);
|
||||
DB::commit();
|
||||
$invoice = DB::transaction(function () use ($order, $validated, $subTotal, $tax, $total) {
|
||||
$invoice = Invoice::create([
|
||||
'invoice_number' => OrderHelper::generateOrderNumber('INV'),
|
||||
'customer_id' => $order->customer_id,
|
||||
'order_id' => $order->id,
|
||||
'invoice_type' => $validated['invoice_type'] ?? 3,
|
||||
'sub_total' => $subTotal,
|
||||
'discount_amount' => 0,
|
||||
'tax' => $tax,
|
||||
'total' => $total,
|
||||
'status' => InvoiceStatus::UNPAID->value,
|
||||
'due_date' => $validated['due_date'] ?? now()->addDays(7)->toDateString(),
|
||||
]);
|
||||
|
||||
InvoiceItem::create([
|
||||
'invoice_id' => $invoice->id,
|
||||
'type' => 'service',
|
||||
'description' => $validated['description'] ?: 'Hóa đơn bổ sung',
|
||||
'months' => 0,
|
||||
'quantity' => 1,
|
||||
'unit_price' => $subTotal,
|
||||
'total_price' => $subTotal,
|
||||
]);
|
||||
|
||||
return $invoice;
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Invoice created successfully',
|
||||
'data' => new InvoiceResource($invoice)
|
||||
'message' => 'Đã tạo hóa đơn',
|
||||
'data' => new InvoiceResource($invoice->load('invoiceItems')),
|
||||
], 201);
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => $e->getMessage()
|
||||
@@ -199,17 +226,17 @@ class InvoiceController extends Controller
|
||||
}
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$this->orderRepository->updateBy('invoice_id', $id, ['invoice_id' => 0]);
|
||||
// Note: `orders` has no `invoice_id` column (the FK runs the other way —
|
||||
// invoices.order_id -> orders.id), so there's nothing to clean up there. Any
|
||||
// `services.invoice_id` pointing at this invoice is already handled by the
|
||||
// FK's onDelete('set null').
|
||||
$this->invoiceRepository->delete($id);
|
||||
DB::commit();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Invoice deleted successfully'
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => $e->getMessage()
|
||||
|
||||
Reference in New Issue
Block a user