feat: Implement payment proof upload for orders, add payment method selection API, and seed bank transfer payment options.
This commit is contained in:
@@ -22,11 +22,13 @@ use App\Repositories\Module\Vmware\VmwareRepositoryInterface;
|
||||
use App\Repositories\Order\OrderRepositoryInterface;
|
||||
use App\Repositories\Os\OsRepositoryInterface;
|
||||
use App\Repositories\VPS\VPSRepositoryInterface;
|
||||
use App\Models\Customer;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
|
||||
@@ -95,6 +97,14 @@ class OrderController extends Controller
|
||||
{
|
||||
$request->validated();
|
||||
|
||||
$customer = Customer::find($request->customer_id);
|
||||
if (!$customer || (int) $customer->status !== 1) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Tài khoản khách hàng không hoạt động. Không thể tạo đơn hàng.',
|
||||
], 422);
|
||||
}
|
||||
|
||||
$total = 0;
|
||||
$product_item = [];
|
||||
$relid_data = [];
|
||||
@@ -167,28 +177,32 @@ class OrderController extends Controller
|
||||
$total += $item['quantity'] * ($item['price_custom'] ?? ($product->price_sale ?? $product->price)) * $billingDetails['billing'];
|
||||
}
|
||||
|
||||
$paymentRecord = \App\Models\Payment::find((int) $request->payment_method);
|
||||
$isBankTransfer = $paymentRecord && (int) $paymentRecord->type === 1;
|
||||
|
||||
$order_item = [
|
||||
'order_num' => Helper::generateUniqueCode(),
|
||||
'customer_id' => $request->customer_id,
|
||||
'invoice_id' => 0,
|
||||
'discount_id' => $request->discount_id ?: null,
|
||||
'amount' => $total + ($total * (10 / 100)),
|
||||
'payment_method' => $request->payment_method,
|
||||
'user_id' => Auth::guard('admin')->id(),
|
||||
'status' => $request->status,
|
||||
'client_ip' => $request->getClientIp()
|
||||
'order_num' => Helper::generateUniqueCode(),
|
||||
'customer_id' => $request->customer_id,
|
||||
'invoice_id' => 0,
|
||||
'discount_id' => $request->discount_id ?: null,
|
||||
'amount' => $total + ($total * (10 / 100)),
|
||||
'payment_id' => $paymentRecord?->id,
|
||||
'payment_proof' => $request->payment_proof ?? null,
|
||||
'user_id' => Auth::guard('admin')->id(),
|
||||
'status' => $request->status,
|
||||
'client_ip' => $request->getClientIp(),
|
||||
];
|
||||
|
||||
$invoice_data = [
|
||||
'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_method' => $request->payment_method,
|
||||
'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' => $paymentRecord?->id,
|
||||
'customer_id' => $request->create_invoice ? $request->customer_id : 0,
|
||||
'status' => $request->status
|
||||
'status' => $request->status
|
||||
];
|
||||
|
||||
DB::beginTransaction();
|
||||
@@ -242,30 +256,39 @@ class OrderController extends Controller
|
||||
$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));
|
||||
\App\Models\OrderItem::insert(array_map(function ($item) use ($now) {
|
||||
$expiryDate = $now->copy()->addMonths($item['months']);
|
||||
return [
|
||||
'order_id' => $order->id,
|
||||
'product_id' => $item['product_id'],
|
||||
'billing_cycle' => $item['billing_cycle'],
|
||||
'months' => $item['months'],
|
||||
'expiry_date' => $expiryDate,
|
||||
'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));
|
||||
}
|
||||
}
|
||||
|
||||
// Deduct customer balance for non-bank-transfer payments
|
||||
if (!$isBankTransfer) {
|
||||
Customer::where('id', $request->customer_id)
|
||||
->decrement('total', (int) ($total + ($total * (10 / 100))));
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
return response()->json([
|
||||
@@ -284,10 +307,15 @@ class OrderController extends Controller
|
||||
|
||||
public function show(int $id): JsonResponse
|
||||
{
|
||||
$order = $this->orderRepository->findWithRelationships(
|
||||
$id,
|
||||
['orderItem', 'user', 'customer.invoiceItem.type', 'payment', 'discount', 'invoice.invoiceItem']
|
||||
);
|
||||
$order = Order::with([
|
||||
'orderItems.product',
|
||||
'orderItems.os',
|
||||
'user',
|
||||
'customer',
|
||||
'payment',
|
||||
'discount',
|
||||
'invoice'
|
||||
])->find($id);
|
||||
|
||||
if (!$order) {
|
||||
return response()->json([
|
||||
@@ -501,6 +529,19 @@ class OrderController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
public function uploadProof(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate(['file' => 'required|image|max:5120']);
|
||||
|
||||
$path = $request->file('file')->store('payment-proofs', 'public');
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'url' => Storage::disk('public')->url($path),
|
||||
'path' => $path,
|
||||
]);
|
||||
}
|
||||
|
||||
private function calculateBillingDetails($billing): array
|
||||
{
|
||||
$months = match ($billing) {
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Admin\OsManager;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Os;
|
||||
use App\Models\OsGroup;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class OsController extends Controller
|
||||
{
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'version' => 'required|string|max:100',
|
||||
'os_group_id' => 'required|integer|exists:os_group,id',
|
||||
'template_id' => 'nullable|string|max:255',
|
||||
'is_active' => 'boolean',
|
||||
]);
|
||||
|
||||
$os = Os::create([
|
||||
'version' => $validated['version'],
|
||||
'os_group_id' => $validated['os_group_id'],
|
||||
'template_id' => $validated['template_id'] ?? null,
|
||||
'status' => ($validated['is_active'] ?? true) ? 1 : 0,
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Thêm OS thành công.',
|
||||
'data' => $this->formatOs($os),
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function update(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$os = Os::find($id);
|
||||
|
||||
if (! $os) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy OS.'], 404);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'version' => 'sometimes|required|string|max:100',
|
||||
'os_group_id' => 'sometimes|integer|exists:os_group,id',
|
||||
'template_id' => 'nullable|string|max:255',
|
||||
'is_active' => 'boolean',
|
||||
]);
|
||||
|
||||
$os->update([
|
||||
'version' => $validated['version'] ?? $os->version,
|
||||
'os_group_id' => $validated['os_group_id'] ?? $os->os_group_id,
|
||||
'template_id' => array_key_exists('template_id', $validated) ? $validated['template_id'] : $os->template_id,
|
||||
'status' => isset($validated['is_active']) ? ($validated['is_active'] ? 1 : 0) : $os->status,
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Cập nhật OS thành công.',
|
||||
'data' => $this->formatOs($os->fresh()),
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy(int $id): JsonResponse
|
||||
{
|
||||
$os = Os::find($id);
|
||||
|
||||
if (! $os) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy OS.'], 404);
|
||||
}
|
||||
|
||||
$os->delete();
|
||||
|
||||
return response()->json(['error' => false, 'message' => 'Xoá OS thành công.']);
|
||||
}
|
||||
|
||||
// ── helpers ──────────────────────────────────────────────────────────────
|
||||
|
||||
private function formatOs(Os $os): array
|
||||
{
|
||||
return [
|
||||
'id' => $os->id,
|
||||
'version' => $os->version,
|
||||
'template_id' => $os->template_id,
|
||||
'is_active' => (bool) $os->status,
|
||||
'os_group_id' => $os->os_group_id,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Admin\OsManager;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\OsGroup;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class OsGroupController extends Controller
|
||||
{
|
||||
/**
|
||||
* List all OS groups with their OS entries.
|
||||
* Supports ?all=true for full list (no pagination).
|
||||
*/
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$query = OsGroup::withCount('os')
|
||||
->with(['os' => fn($q) => $q->orderBy('version')])
|
||||
->when(
|
||||
$request->get('filter'),
|
||||
fn($q, $f) => $q->where('name', 'like', "%{$f}%")
|
||||
)
|
||||
->orderBy('name');
|
||||
|
||||
if ($request->boolean('all')) {
|
||||
$items = $query->get();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => $items->map(fn($g) => $this->formatGroup($g)),
|
||||
]);
|
||||
}
|
||||
|
||||
$perPage = min((int) $request->get('per_page', 15), 100);
|
||||
$paginator = $query->paginate($perPage);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => [
|
||||
'items' => collect($paginator->items())->map(fn($g) => $this->formatGroup($g)),
|
||||
'total' => $paginator->total(),
|
||||
'per_page' => $paginator->perPage(),
|
||||
'current_page' => $paginator->currentPage(),
|
||||
'last_page' => $paginator->lastPage(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function show(int $id): JsonResponse
|
||||
{
|
||||
$group = OsGroup::withCount('os')
|
||||
->with(['os' => fn($q) => $q->orderBy('version')])
|
||||
->find($id);
|
||||
|
||||
if (! $group) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy nhóm OS.'], 404);
|
||||
}
|
||||
|
||||
return response()->json(['error' => false, 'data' => $this->formatGroup($group)]);
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'is_active' => 'boolean',
|
||||
]);
|
||||
|
||||
$group = OsGroup::create([
|
||||
'name' => $validated['name'],
|
||||
'status' => ($validated['is_active'] ?? true) ? 1 : 0,
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Tạo nhóm OS thành công.',
|
||||
'data' => $this->formatGroup($group->load('os')),
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function update(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$group = OsGroup::find($id);
|
||||
|
||||
if (! $group) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy nhóm OS.'], 404);
|
||||
}
|
||||
|
||||
$validated = $request->validate([
|
||||
'name' => 'sometimes|required|string|max:255',
|
||||
'is_active' => 'boolean',
|
||||
]);
|
||||
|
||||
$group->update([
|
||||
'name' => $validated['name'] ?? $group->name,
|
||||
'status' => isset($validated['is_active']) ? ($validated['is_active'] ? 1 : 0) : $group->status,
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Cập nhật nhóm OS thành công.',
|
||||
'data' => $this->formatGroup($group->fresh(['os'])),
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy(int $id): JsonResponse
|
||||
{
|
||||
$group = OsGroup::withCount('os')->find($id);
|
||||
|
||||
if (! $group) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy nhóm OS.'], 404);
|
||||
}
|
||||
|
||||
if ($group->os_count > 0) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => "Không thể xoá nhóm đang chứa {$group->os_count} OS. Hãy xoá các OS trước.",
|
||||
], 422);
|
||||
}
|
||||
|
||||
$group->delete();
|
||||
|
||||
return response()->json(['error' => false, 'message' => 'Xoá nhóm OS thành công.']);
|
||||
}
|
||||
|
||||
// ── helpers ──────────────────────────────────────────────────────────────
|
||||
|
||||
private function formatGroup(OsGroup $group): array
|
||||
{
|
||||
return [
|
||||
'id' => $group->id,
|
||||
'name' => $group->name,
|
||||
'is_active' => (bool) $group->status,
|
||||
'os_count' => $group->os_count ?? $group->os->count(),
|
||||
'os_list' => $group->relationLoaded('os')
|
||||
? $group->os->map(fn($o) => $this->formatOs($o))->values()
|
||||
: [],
|
||||
];
|
||||
}
|
||||
|
||||
private function formatOs(\App\Models\Os $os): array
|
||||
{
|
||||
return [
|
||||
'id' => $os->id,
|
||||
'version' => $os->version,
|
||||
'template_id' => $os->template_id,
|
||||
'is_active' => (bool) $os->status,
|
||||
'os_group_id' => $os->os_group_id,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,21 @@ class PaymentController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
public function forSelect(): JsonResponse
|
||||
{
|
||||
$payments = $this->paymentRepository->filter(['status' => 1]);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => $payments->map(fn($p) => [
|
||||
'id' => $p->id,
|
||||
'name' => $p->name,
|
||||
'type' => (int) $p->type,
|
||||
'status' => (int) $p->status,
|
||||
])->values(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(PaymentRequest $request): JsonResponse
|
||||
{
|
||||
$request->validated();
|
||||
|
||||
@@ -6,6 +6,7 @@ use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\Api\Admin\ProductResource;
|
||||
use App\Models\Product;
|
||||
use App\Repositories\Admin\Product\AdminProductRepositoryInterface;
|
||||
use App\Models\Os;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
@@ -44,6 +45,9 @@ class ProductController extends Controller
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy sản phẩm.'], 404);
|
||||
}
|
||||
|
||||
// Load operatingSystems relationship (product_os table)
|
||||
$product->load('operatingSystems');
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => new ProductResource($product),
|
||||
@@ -105,11 +109,13 @@ class ProductController extends Controller
|
||||
'product_categories_id' => 'nullable|integer|exists:product_categories,id',
|
||||
'product_type' => 'nullable|string',
|
||||
'is_active' => 'boolean',
|
||||
'setup_fee' => 'nullable|numeric|min:0',
|
||||
'cpu' => 'nullable|numeric|min:0',
|
||||
'ram' => 'nullable|numeric|min:0',
|
||||
'disk' => 'nullable|numeric|min:0',
|
||||
'bandwidth' => 'nullable|numeric|min:0',
|
||||
'setup_fee' => 'nullable|numeric|min:0',
|
||||
'cpu' => 'nullable|numeric|min:0',
|
||||
'ram' => 'nullable|numeric|min:0',
|
||||
'disk' => 'nullable|numeric|min:0',
|
||||
'bandwidth' => 'nullable|numeric|min:0',
|
||||
'os_ids' => 'nullable|array',
|
||||
'os_ids.*' => 'integer|exists:os,id',
|
||||
]);
|
||||
|
||||
if (isset($validated['name']) && $validated['name'] !== $product->name) {
|
||||
@@ -121,8 +127,17 @@ class ProductController extends Controller
|
||||
$validated['product_type'] = $category?->type;
|
||||
}
|
||||
|
||||
// Handle OS associations via product_os table
|
||||
$osIds = $validated['os_ids'] ?? null;
|
||||
unset($validated['os_ids']);
|
||||
|
||||
$product->update($validated);
|
||||
|
||||
// Sync OS associations in product_os table
|
||||
if ($osIds !== null) {
|
||||
$product->operatingSystems()->sync($osIds);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Cập nhật sản phẩm thành công.',
|
||||
@@ -151,7 +166,7 @@ class ProductController extends Controller
|
||||
$product = Product::find($id);
|
||||
|
||||
if (!$product) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy sản phẩm.'], 404);
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy sản phờng trình.'], 404);
|
||||
}
|
||||
|
||||
$osList = $product->osVersions()
|
||||
@@ -171,4 +186,49 @@ class ProductController extends Controller
|
||||
'data' => $osList,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getOsList(): JsonResponse
|
||||
{
|
||||
$osList = Os::with('osGroup')
|
||||
->where('status', 1)
|
||||
->get()
|
||||
->map(fn($os) => [
|
||||
'id' => $os->id,
|
||||
'name' => $os->name,
|
||||
'version' => $os->version,
|
||||
'os_group_name' => $os->osGroup?->name,
|
||||
'template_id' => $os->template_id,
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => $osList,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getOperatingSystems(int $id): JsonResponse
|
||||
{
|
||||
$product = Product::find($id);
|
||||
|
||||
if (!$product) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy sản phẩm.'], 404);
|
||||
}
|
||||
|
||||
$osList = $product->operatingSystems()
|
||||
->with('osGroup')
|
||||
->where('status', 1)
|
||||
->get()
|
||||
->map(fn($os) => [
|
||||
'id' => $os->id,
|
||||
'name' => $os->name,
|
||||
'version' => $os->version,
|
||||
'os_group_name' => $os->osGroup?->name,
|
||||
'template_id' => $os->template_id,
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => $osList,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user