feat: Implement payment method fields, refactor order creation logic to support detailed order items and VM provisioning, and add admin notifications.

This commit is contained in:
pbao9
2026-03-11 10:14:32 +07:00
parent 7fdcc01027
commit 5649674eb4
9 changed files with 208 additions and 61 deletions
@@ -100,27 +100,28 @@ class OrderController extends Controller
$relid_data = [];
foreach ($request->product as $key => $item) {
$product = null;
$relid_data[$key]['type'] = $item['type'];
$product = match ((int) $item['type']) {
3 => $this->hostingRepository->find($item['product_id']),
4 => $this->vPSRepository->find($item['product_id']),
default => null
};
$product = \App\Models\Product::with('category')->find($item['product_id']);
if (!$product) {
return response()->json([
'error' => true,
'message' => "Invalid product."
'message' => "Sản phẩm không tồn tại (ID: {$item['product_id']})."
], 422);
}
// Derive legacy numeric type from product_type for module creation
$moduleType = match ($product->product_type) {
'hosting' => 3,
'vps' => 4,
default => 0,
};
$relid_data[$key]['type'] = $moduleType;
$message = $product->name;
$relid_data[$key]['data'] = [
'customer_id' => $request->customer_id,
'status' => 0,
'name' => $item['domain']
'name' => $item['domain'] ?? $product->name,
];
$billingDetails = $this->calculateBillingDetails($item['billing']);
@@ -132,16 +133,25 @@ class OrderController extends Controller
$generalCode = Helper::generateUniqueCode();
$billingCycle = match ((int) $item['billing']) {
2 => 'monthly',
3 => 'quarterly',
4 => 'semi_annually',
5 => 'annually',
default => 'monthly',
};
$product_item[$key] = [
'customer_id' => $request->customer_id,
'type_id' => $item['type'],
'product_id' => $item['product_id'],
'description' => $message,
'amount' => $price_select,
'billing' => $item['billing'],
'tax' => 1,
'due_date' => Carbon::today()->hour(23)->minute(59)->second(59)->timestamp,
'code' => $generalCode
'product_id' => $product->id,
'product_type' => $product->product_type,
'months' => $billingDetails['billing'],
'billing_cycle' => $billingCycle,
'os_id' => $item['os_id'] ?? null,
'quantity' => (int) ($item['quantity'] ?? 1),
'server_name' => $item['domain'] ?? null,
'price' => (int) $price_select,
'invoice_type' => 1,
'code' => $generalCode,
];
$relid_data[$key]['code'] = $generalCode;
@@ -160,24 +170,23 @@ class OrderController extends Controller
$order_item = [
'order_num' => Helper::generateUniqueCode(),
'customer_id' => $request->customer_id,
'date' => Carbon::today()->timestamp,
'invoice_id' => 0,
'discount_id' => $request->discount_id ?? 0,
'discount_id' => $request->discount_id ?: null,
'amount' => $total + ($total * (10 / 100)),
'payment_id' => $request->payment_id,
'payment_method' => $request->payment_method,
'user_id' => Auth::guard('admin')->id(),
'status' => $request->status,
'client_ip' => $request->getClientIp()
];
$invoice_data = [
'date' => Carbon::today()->timestamp,
'due_date' => Carbon::today()->hour(23)->minute(59)->second(59)->timestamp,
'date_paid' => (int)$request->status === 1 ? Carbon::today()->timestamp : null,
'date' => Carbon::today()->toDateString(),
'due_date' => Carbon::today()->toDateString(),
'date_paid' => (int)$request->status === 1 ? Carbon::today()->toDateString() : null,
'sub_total' => $total,
'tax' => $total * (10 / 100),
'total' => $total + ($total * (10 / 100)),
'payment_id' => $request->payment_id,
'payment_method' => $request->payment_method,
'customer_id' => $request->create_invoice ? $request->customer_id : 0,
'status' => $request->status
];
@@ -221,20 +230,41 @@ class OrderController extends Controller
}
}
$product_item = array_filter(array_map(function ($array) use ($relid) {
if (isset($relid[$array['code']])) {
$array['relid'] = $relid[$array['code']];
unset($array['code']);
return $array;
$product_item = array_map(function ($array) {
unset($array['code']);
return $array;
}, $product_item);
$invoiceItemData = array_map(function ($item) {
return \Illuminate\Support\Arr::except($item, ['billing_cycle']);
}, $product_item);
$this->invoiceItemRepository->insert($invoiceItemData);
$now = now();
\App\Models\OrderItem::insert(array_map(fn($item) => [
'order_id' => $order->id,
'product_id' => $item['product_id'],
'billing_cycle' => $item['billing_cycle'],
'months' => $item['months'],
'os_id' => $item['os_id'],
'quantity' => $item['quantity'],
'server_name' => $item['server_name'],
'price' => $item['price'],
'created_at' => $now,
'updated_at' => $now,
], $product_item));
// Dispatch VM creation job for VPS/cloud products
$vpsTypes = ['vps', 'cloud'];
$createdOrderItems = \App\Models\OrderItem::where('order_id', $order->id)->get();
foreach ($createdOrderItems as $orderItem) {
$pt = $orderItem->product?->product_type ?? $orderItem->billing_cycle;
if (in_array($orderItem->product?->product_type, $vpsTypes)) {
\App\Jobs\VirtualMachine\Create\CreateVirtualMachineJob::dispatch($orderItem)
->delay(now()->addSeconds(10));
}
return null;
}, $product_item));
$this->invoiceItemRepository->insert($product_item);
$last_id = $this->invoiceItemRepository->filter(['limit' => count($product_item)], [], [], ['id'])->pluck('id')->toArray();
$order->orderItem()->sync($last_id);
}
DB::commit();