feat: Reorganize database migrations for module snapshots, VMware, and OS groups, and implement customer order creation email notifications.

This commit is contained in:
pbao9
2026-03-12 12:31:52 +07:00
parent 1934be9a19
commit 508d5186db
19 changed files with 350 additions and 195 deletions
+28 -9
View File
@@ -5,6 +5,7 @@ namespace App\Services;
use App\Enums\Product\ProductBillingCycle;
use App\Http\Helpers\Helper;
use App\Jobs\Hosting\CreateHostingJobs;
use App\Jobs\Mail\Customer\OrderCreatedJob;
use App\Jobs\VirtualMachine\Create\CreateVirtualMachineJob;
use App\Models\Customer;
use App\Models\Order;
@@ -37,20 +38,25 @@ class OrderService
}
$paymentRecord = Payment::find((int) $data['payment_method']);
$isBankTransfer = $paymentRecord && (int) $paymentRecord->type === 1;
$isBalancePayment = $paymentRecord && (int) $paymentRecord->type === 0;
// Balance payment → auto-confirm order & invoice
$effectiveStatus = $isBalancePayment ? 1 : (int) $data['status'];
[$productItems, $relIdData, $total] = $this->buildProductItems($data);
$orderTotal = (int) ($total + ($total * (10 / 100)));
$invoiceData = [
'invoice_number' => Helper::generateUniqueCode(),
'due_date' => Carbon::today()->toDateString(),
'paid_at' => (int) $data['status'] === 1 ? Carbon::today()->toDateString() : null,
'paid_at' => $effectiveStatus === 1 ? Carbon::now() : null,
'sub_total' => $total,
'tax' => $total * (10 / 100),
'total' => $total + ($total * (10 / 100)),
'total' => $orderTotal,
'payment_method' => $paymentRecord?->id,
'customer_id' => $data['customer_id'],
'status' => $data['status'],
'status' => $effectiveStatus,
];
$orderData = [
@@ -58,15 +64,15 @@ class OrderService
'customer_id' => $data['customer_id'],
'discount_id' => $data['discount_id'] ?? null,
'sub_total' => $total,
'total' => $total + ($total * (10 / 100)),
'total' => $orderTotal,
'payment_method' => $paymentRecord?->code ?? null,
'payment_proof' => $data['payment_proof'] ?? null,
'user_id' => Auth::guard('admin')->id(),
'status' => $data['status'],
'status' => $effectiveStatus,
'client_ip' => request()->getClientIp(),
];
return DB::transaction(function () use ($invoiceData, $orderData, $productItems, $relIdData, $data, $total, $isBankTransfer) {
$order = DB::transaction(function () use ($invoiceData, $orderData, $productItems, $relIdData, $data, $orderTotal, $isBalancePayment) {
$invoice = $this->invoiceRepository->create($invoiceData);
$productItems = array_map(fn ($item) => array_merge($item, ['invoice_id' => $invoice->id]), $productItems);
@@ -77,6 +83,8 @@ class OrderService
$order = $this->orderRepository->create($orderData);
$invoice->update(['order_id' => $order->id]);
$relIdData = array_map(function ($item) use ($order) {
$item['data']['order_id'] = $order->id;
return $item;
@@ -116,8 +124,7 @@ class OrderService
}
}
if (!$isBankTransfer) {
$orderTotal = (int) ($total + ($total * (10 / 100)));
if ($isBalancePayment) {
$customer = Customer::lockForUpdate()->find($data['customer_id']);
if ($customer->total < $orderTotal) {
@@ -129,6 +136,18 @@ class OrderService
return $order;
});
OrderCreatedJob::dispatch([
'customer_email' => $customer->email,
'client_name' => $customer->fullname,
'order_num' => $orderData['order_num'],
'invoice_num' => $invoiceData['invoice_number'],
'order_total' => number_format($orderTotal, 0, ',', '.'),
'company_name' => setting('general.company_name') ?: config('app.name'),
'company_logo' => setting('general.company_logo') ?: '',
]);
return $order;
}
private function buildProductItems(array $data): array