feat: Implement order and invoice management, enhance addon features, and remove the 'Type' module.
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Service;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\Order\PayOrderRequest;
|
||||
use App\Http\Requests\Api\Order\StoreOrderRequest;
|
||||
use App\Repositories\Api\Order\OrderRepository;
|
||||
use App\Services\Api\Order\OrderService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class OrderController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
protected OrderService $orderService,
|
||||
protected OrderRepository $orderRepo
|
||||
) {}
|
||||
|
||||
public function storeOrder(StoreOrderRequest $request): JsonResponse
|
||||
{
|
||||
try {
|
||||
$customer = $request->user();
|
||||
|
||||
$result = $this->orderService->create($request->validated(), $customer);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => $result,
|
||||
'message' => 'Tạo đơn hàng thành công'
|
||||
], 201);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => $e->getMessage()
|
||||
], 400);
|
||||
}
|
||||
}
|
||||
|
||||
public function payOrder(int $id, PayOrderRequest $request): JsonResponse
|
||||
{
|
||||
try {
|
||||
$order = $this->orderRepo->find($id);
|
||||
|
||||
if (!$order) {
|
||||
return response()->json(['message' => 'Đơn hàng không tồn tại'], 404);
|
||||
}
|
||||
|
||||
if ($order->customer_id !== $request->user()->id) {
|
||||
return response()->json(['message' => 'Không có quyền truy cập'], 403);
|
||||
}
|
||||
|
||||
$this->orderService->pay($order, $request->payment_method);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Thanh toán thành công'
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => $e->getMessage()
|
||||
], 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Http\Controllers\Api\Service;
|
||||
|
||||
class PaymentController
|
||||
{
|
||||
/**
|
||||
* Thanh toán order bằng credit
|
||||
* POST /v1/services/orders/{order_id}/pay
|
||||
*/
|
||||
public function payOrder(Request $request, $orderId)
|
||||
{
|
||||
try {
|
||||
$customer = $request->user();
|
||||
$order = Order::with(['invoice', 'orderItems', 'customer'])
|
||||
->where('id', $orderId)
|
||||
->where('customer_id', $customer->id)
|
||||
->first();
|
||||
|
||||
if (!$order) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Không tìm thấy đơn hàng'
|
||||
], 404);
|
||||
}
|
||||
|
||||
if ($order->status == 2) { // Already paid
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Đơn hàng đã được thanh toán'
|
||||
], 400);
|
||||
}
|
||||
|
||||
// Kiểm tra credit
|
||||
if ($customer->credit < $order->amount) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Số dư không đủ để thanh toán',
|
||||
'data' => [
|
||||
'required' => $order->amount,
|
||||
'current_credit' => $customer->credit,
|
||||
'shortage' => $order->amount - $customer->credit
|
||||
]
|
||||
], 400);
|
||||
}
|
||||
|
||||
// Dispatch job để xử lý thanh toán và tạo VM
|
||||
ProcessOrderPaymentJob::dispatch($order);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Đơn hàng đang được xử lý. Bạn sẽ nhận được thông báo khi hoàn tất.',
|
||||
'data' => [
|
||||
'order_id' => $order->id,
|
||||
'order_num' => $order->order_num,
|
||||
'status' => 'processing'
|
||||
]
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Pay order error: ' . $e->getMessage());
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Có lỗi xảy ra khi thanh toán đơn hàng'
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,22 +5,38 @@ namespace App\Http\Controllers\Api\Service;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\Api\Service\ServiceListResource;
|
||||
use App\Http\Resources\Api\Service\ServiceResource;
|
||||
use App\Http\Services\Invoice\InvoiceService;
|
||||
use App\Jobs\VirtualMachine\ProcessOrderPaymentJob;
|
||||
use App\Models\Addon;
|
||||
use App\Models\Cart;
|
||||
use App\Models\CartAddon;
|
||||
use App\Models\Discount;
|
||||
use App\Models\Order;
|
||||
use App\Models\OrderAddon;
|
||||
use App\Models\OrderItem;
|
||||
use App\Models\Product;
|
||||
use App\Models\ProductPricings;
|
||||
use App\Repositories\Api\Service\ServiceRepository;
|
||||
use App\Services\Api\Service\ServiceService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class ServiceController extends Controller
|
||||
{
|
||||
protected $serviceService;
|
||||
protected $serviceRepository;
|
||||
|
||||
public function __construct(ServiceService $serviceService)
|
||||
public function __construct(
|
||||
ServiceService $serviceService,
|
||||
ServiceRepository $serviceRepository
|
||||
)
|
||||
{
|
||||
$this->serviceService = $serviceService;
|
||||
$this->serviceRepository = $serviceRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lấy danh sách dịch vụ (sản phẩm) đang hoạt động
|
||||
*/
|
||||
public function getService(Request $request)
|
||||
{
|
||||
$limit = $request->get('limit', 10);
|
||||
@@ -35,7 +51,12 @@ class ServiceController extends Controller
|
||||
|
||||
public function getDetailService(Product $product)
|
||||
{
|
||||
$product->load(['osVersions.osGroup', 'category', 'pricings' => fn ($q) => $q->where('is_active', 1)]);
|
||||
$product->load([
|
||||
'osVersions.osGroup',
|
||||
'category',
|
||||
'pricings' => fn($q) => $q->where('is_active', 1),
|
||||
'addons'
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
|
||||
@@ -8,13 +8,15 @@ use App\Models\Addon;
|
||||
use App\Repositories\Addon\AddonRepositoryInterface;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
|
||||
class AddonController extends Controller
|
||||
{
|
||||
//
|
||||
public function __construct(
|
||||
public AddonRepositoryInterface $addonRepository
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
public function index(Request $request)
|
||||
{
|
||||
$addOns = $this->addonRepository->filter(
|
||||
@@ -46,13 +48,13 @@ class AddonController extends Controller
|
||||
return back()->withErrors($error->getMessage());
|
||||
}
|
||||
|
||||
return redirect()->route('addon.index')->with('success', 'Added successfully');
|
||||
return redirect()->route('addon.index')->with('success', 'Added successfully');
|
||||
}
|
||||
public function edit($id = 0)
|
||||
{
|
||||
|
||||
$addon = $this->addonRepository->find($id);
|
||||
if (! $addon) {
|
||||
if (!$addon) {
|
||||
return back()->withErrors('Invalid request');
|
||||
}
|
||||
|
||||
@@ -63,7 +65,7 @@ class AddonController extends Controller
|
||||
public function update(int $id = 0, AddOnRequest $request)
|
||||
{
|
||||
$addon = $this->addonRepository->find($id);
|
||||
if (! $addon) {
|
||||
if (!$addon) {
|
||||
return back()->withErrors('Invalid request');
|
||||
}
|
||||
$request->validated();
|
||||
@@ -77,12 +79,12 @@ class AddonController extends Controller
|
||||
return back()->withErrors($error->getMessage());
|
||||
}
|
||||
|
||||
return redirect()->route('addon.index')->with('success', 'Updated successfully');
|
||||
return redirect()->route('addon.index')->with('success', 'Updated successfully');
|
||||
}
|
||||
public function destroy($id)
|
||||
{
|
||||
$addon = $this->addonRepository->find($id);
|
||||
if (! $addon) {
|
||||
if (!$addon) {
|
||||
return back()->withErrors('Invalid request');
|
||||
}
|
||||
DB::beginTransaction();
|
||||
@@ -96,4 +98,26 @@ class AddonController extends Controller
|
||||
|
||||
return redirect()->route('addon.index')->with('success', 'Deleted successfully');
|
||||
}
|
||||
|
||||
|
||||
public function datatable(Request $request)
|
||||
{
|
||||
$query = $this->addonRepository->filter(
|
||||
$request->all(),
|
||||
[],
|
||||
[],
|
||||
['*'],
|
||||
true
|
||||
);
|
||||
|
||||
return DataTables::eloquent($query)
|
||||
->addColumn(
|
||||
'action',
|
||||
function ($d) {
|
||||
return view('administrator.pages.addon.datatable.action', ['row' => $d])->render();
|
||||
}
|
||||
)
|
||||
->rawColumns(['status', 'action'])
|
||||
->make(true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ use App\Http\Requests\Product\ProductRequest;
|
||||
use App\Http\Services\ProductService;
|
||||
use App\Repositories\Group\GroupRepositoryInterface;
|
||||
use App\Repositories\Admin\Product\ProductRepositoryInterface;
|
||||
use App\Repositories\Type\TypeRepositoryInterface;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
use Exception;
|
||||
@@ -16,7 +16,6 @@ use Exception;
|
||||
class ProductController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
public TypeRepositoryInterface $typeRepository,
|
||||
public GroupRepositoryInterface $groupRepository,
|
||||
public ProductRepositoryInterface $productRepository,
|
||||
public ProductService $productService
|
||||
@@ -31,7 +30,7 @@ class ProductController extends Controller
|
||||
'page' => $request->get('page') ?? 1
|
||||
]
|
||||
);
|
||||
|
||||
|
||||
$title = "List All Product";
|
||||
|
||||
return view('administrator.pages.product.index', compact('title', 'products'));
|
||||
@@ -43,7 +42,7 @@ class ProductController extends Controller
|
||||
$title = 'Add new product';
|
||||
// Fetch Product Categories instead of generic groups
|
||||
$group = \App\Models\ProductCategories::where('is_active', \App\Enums\Status::ACTIVE->value)->get();
|
||||
$type = $this->typeRepository->getAll();
|
||||
$type = null;
|
||||
|
||||
// Fetch OS versions with their group for the OS selector component
|
||||
$osVersions = \App\Models\Os::with('osGroup')->where('status', 1)->get();
|
||||
@@ -93,7 +92,7 @@ class ProductController extends Controller
|
||||
|
||||
$title = 'Edit product: ' . $product->name;
|
||||
$group = \App\Models\ProductCategories::where('is_active', \App\Enums\Status::ACTIVE->value)->get();
|
||||
$type = $this->typeRepository->getAll();
|
||||
$type = null;
|
||||
|
||||
// Fetch OS versions for select
|
||||
$osVersions = \App\Models\Os::with('osGroup')->where('status', 1)->get();
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Api\Order;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class PayOrderRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'payment_method' => 'required|in:credit,vnpay'
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Api\Order;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
use Illuminate\Validation\Rule;
|
||||
use App\Enums\Product\ProductBillingCycle;
|
||||
|
||||
class StoreOrderRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'product_id' => 'required|exists:products,id',
|
||||
'billing_cycle'=> ['required', Rule::enum(ProductBillingCycle::class), 'exists:product_pricings,billing_cycle'],
|
||||
'server_name' => 'nullable|string|max:255',
|
||||
'os_id' => 'nullable|exists:os,id',
|
||||
'addons' => 'array',
|
||||
'addons.*.addon_id' => 'required|exists:product_addon,id',
|
||||
'addons.*.qty' => 'required|integer|min:1',
|
||||
];
|
||||
}
|
||||
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
'product_id.required' => 'Vui lòng chọn sản phẩm',
|
||||
'product_id.exists' => 'Sản phẩm không tồn tại',
|
||||
'billing_cycle.required' => 'Vui lòng chọn chu kỳ thanh toán',
|
||||
'billing_cycle.enum' => 'Chu kỳ thanh toán không hợp lệ',
|
||||
'billing_cycle.exists' => 'Chu kỳ thanh toán không áp dụng cho sản phẩm này',
|
||||
'os_id.exists' => 'Hệ điều hành không hợp lệ',
|
||||
'addons.*.addon_id.exists' => 'Addon không tồn tại',
|
||||
'addons.*.qty.min' => 'Số lượng addon phải lớn hơn 0',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -20,13 +20,13 @@ class ClientResource extends JsonResource
|
||||
|
||||
return [
|
||||
'customer' => [
|
||||
'name' => $this->fullname,
|
||||
'fullname' => $this->fullname,
|
||||
'email' => $this->email,
|
||||
'phone' => $this->phone,
|
||||
'address' => $this->address,
|
||||
'city' => $this->city,
|
||||
'ward' => $this->ward->name,
|
||||
'province' => $this->province->name,
|
||||
'ward' => $this->ward->name ?? '',
|
||||
'province' => $this->province->name ?? '',
|
||||
'gender' => $this->gender_label,
|
||||
'date' => $this->created_at ? $this->created_at->format('Y-m-d H:i:s') : null,
|
||||
'company' => $this->company ?? '',
|
||||
|
||||
@@ -39,17 +39,11 @@ class ServiceListResource extends JsonResource
|
||||
return [$pricing->billing_cycle => $pricing->price_sale];
|
||||
}),
|
||||
'group' => $this->category->name,
|
||||
'os' => $this->osVersions->groupBy('os_group_id')->map(function ($versions) {
|
||||
$group = $versions->first()->osGroup;
|
||||
'os' => $this->osVersions->map(function ($os) {
|
||||
return [
|
||||
'name' => $group?->name,
|
||||
'thumbnail' => $group?->thumbnail,
|
||||
'versions' => $versions->map(fn($os) => [
|
||||
'version' => $os->version,
|
||||
'name' => $os->name,
|
||||
])->values(),
|
||||
'group' => $os->osGroup->name ?? null,
|
||||
];
|
||||
})->values(),
|
||||
})->unique('group')->values(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user