|
|
<?php |
|
|
|
|
|
namespace App\Http\Controllers; |
|
|
|
|
|
use Illuminate\Http\Request; |
|
|
use Illuminate\Support\Facades\Storage; |
|
|
use App\Models\Product; |
|
|
use App\Models\Order; |
|
|
|
|
|
class CartController extends Controller |
|
|
{ |
|
|
public function index() |
|
|
{ |
|
|
$cart = session()->get('cart', []); |
|
|
return view('cart', compact('cart')); |
|
|
} |
|
|
|
|
|
public function add(Request $request, $id) |
|
|
{ |
|
|
$product = Product::findOrFail($id); |
|
|
$quantity = (int) $request->input('quantity', 1); |
|
|
|
|
|
|
|
|
if ($product->Amount < $quantity) { |
|
|
if ($request->ajax()) { |
|
|
return response()->json([ |
|
|
'success' => false, |
|
|
'message' => 'Insufficient stock. Available: ' . $product->Amount |
|
|
]); |
|
|
} |
|
|
return back()->with('error', 'Insufficient stock. Available: ' . $product->Amount); |
|
|
} |
|
|
|
|
|
$cart = session()->get('cart', []); |
|
|
|
|
|
if (isset($cart[$id])) { |
|
|
$newQuantity = $cart[$id]['quantity'] + $quantity; |
|
|
if ($newQuantity > $product->Amount) { |
|
|
if ($request->ajax()) { |
|
|
return response()->json([ |
|
|
'success' => false, |
|
|
'message' => 'Cannot add more items. Stock limit: ' . $product->Amount |
|
|
]); |
|
|
} |
|
|
return back()->with('error', 'Cannot add more items. Stock limit: ' . $product->Amount); |
|
|
} |
|
|
$cart[$id]['quantity'] = $newQuantity; |
|
|
} else { |
|
|
$cart[$id] = [ |
|
|
'id' => $product->id, |
|
|
'name' => $product->name, |
|
|
'image' => $product->image, |
|
|
'price' => $product->price, |
|
|
'quantity' => $quantity, |
|
|
'game' => $product->game, |
|
|
]; |
|
|
} |
|
|
|
|
|
session()->put('cart', $cart); |
|
|
|
|
|
|
|
|
if ($request->ajax()) { |
|
|
return response()->json([ |
|
|
'success' => true, |
|
|
'message' => 'Item added to cart successfully', |
|
|
'cart_count' => array_sum(array_column($cart, 'quantity')) |
|
|
]); |
|
|
} |
|
|
|
|
|
return redirect()->route('cart.index')->with('success', 'เพิ่มสินค้าลงตะกร้าเรียบร้อยแล้ว'); |
|
|
} |
|
|
|
|
|
public function remove($id) |
|
|
{ |
|
|
$cart = session()->get('cart', []); |
|
|
|
|
|
if (isset($cart[$id])) { |
|
|
unset($cart[$id]); |
|
|
session()->put('cart', $cart); |
|
|
} |
|
|
|
|
|
return redirect()->route('cart.index')->with('success', 'ลบสินค้าออกจากตะกร้าเรียบร้อยแล้ว'); |
|
|
} |
|
|
public function checkout(Request $request) |
|
|
{ |
|
|
$request->validate([ |
|
|
'customer_name' => 'required|string|max:255', |
|
|
'phone' => 'required|string|max:20', |
|
|
'payment_slip' => 'required|image|mimes:jpeg,png,jpg,gif|max:10240', // 10MB limit |
|
|
]); |
|
|
|
|
|
try { |
|
|
$path = $request->file('payment_slip')->store('payment_slips', 'public'); |
|
|
|
|
|
|
|
|
$cart = session()->get('cart', []); |
|
|
|
|
|
if (empty($cart)) { |
|
|
return back()->with('error', 'Your cart is empty'); |
|
|
} |
|
|
|
|
|
|
|
|
$totalAmount = 0; |
|
|
foreach ($cart as $item) { |
|
|
$product = Product::find($item['id']); |
|
|
if (!$product) { |
|
|
return back()->with('error', "Product {$item['name']} no longer exists"); |
|
|
} |
|
|
if ($product->Amount < $item['quantity']) { |
|
|
return back()->with('error', "Insufficient stock for {$product->name}. Available: {$product->Amount}, Requested: {$item['quantity']}"); |
|
|
} |
|
|
$totalAmount += $item['price'] * $item['quantity']; |
|
|
} |
|
|
|
|
|
|
|
|
$order = Order::create([ |
|
|
'customer_name' => $request->customer_name, |
|
|
'phone' => $request->phone, |
|
|
'payment_slip_path' => $path, |
|
|
'cart_data' => json_encode($cart), |
|
|
'total_amount' => $totalAmount, |
|
|
'status' => 'pending', |
|
|
]); |
|
|
|
|
|
|
|
|
foreach ($cart as $item) { |
|
|
$product = Product::find($item['id']); |
|
|
if ($product) { |
|
|
$product->Amount = max(0, $product->Amount - $item['quantity']); |
|
|
$product->save(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
session()->forget('cart'); |
|
|
|
|
|
return redirect()->route('cart.index')->with('success', 'Thank you for your order! We have received your order successfully.'); |
|
|
|
|
|
} catch (\Exception $e) { |
|
|
return back()->with('error', 'Failed to process order: ' . $e->getMessage()); |
|
|
} |
|
|
} |
|
|
public function order() |
|
|
{ |
|
|
$orders = Order::latest()->get(); |
|
|
return view('order', compact('orders')); |
|
|
} |
|
|
public function deleteOrder($id) |
|
|
{ |
|
|
try { |
|
|
$order = Order::findOrFail($id); |
|
|
|
|
|
|
|
|
if ($order->payment_slip_path && Storage::disk('public')->exists($order->payment_slip_path)) { |
|
|
Storage::disk('public')->delete($order->payment_slip_path); |
|
|
} |
|
|
|
|
|
$order->delete(); |
|
|
|
|
|
return back()->with('success', 'Order deleted successfully'); |
|
|
} catch (\Exception $e) { |
|
|
return back()->with('error', 'Failed to delete order: ' . $e->getMessage()); |
|
|
} |
|
|
} |
|
|
|
|
|
public function show($id) |
|
|
{ |
|
|
try { |
|
|
$order = Order::findOrFail($id); |
|
|
return view('order-detail', compact('order')); |
|
|
} catch (\Exception $e) { |
|
|
return redirect()->route('orders.index')->with('error', 'Order not found'); |
|
|
} |
|
|
} |
|
|
|
|
|
public function completeOrder($id) |
|
|
{ |
|
|
try { |
|
|
$order = Order::findOrFail($id); |
|
|
|
|
|
|
|
|
$order->status = 'completed'; |
|
|
$order->save(); |
|
|
|
|
|
return redirect()->route('orders.index')->with('success', 'Order #' . str_pad($order->id, 4, '0', STR_PAD_LEFT) . ' has been ACCEPTED successfully!'); |
|
|
} catch (\Exception $e) { |
|
|
return redirect()->route('orders.index')->with('error', 'Failed to accept order: ' . $e->getMessage()); |
|
|
} |
|
|
} |
|
|
|
|
|
public function rejectOrder($id) |
|
|
{ |
|
|
try { |
|
|
$order = Order::findOrFail($id); |
|
|
|
|
|
|
|
|
$order->status = 'cancelled'; |
|
|
$order->save(); |
|
|
|
|
|
return redirect()->route('orders.index')->with('success', 'Order #' . str_pad($order->id, 4, '0', STR_PAD_LEFT) . ' has been REJECTED. Customer will see it as cancelled.'); |
|
|
} catch (\Exception $e) { |
|
|
return redirect()->route('orders.index')->with('error', 'Failed to reject order: ' . $e->getMessage()); |
|
|
} |
|
|
} |
|
|
|
|
|
public function customerOrders(Request $request) |
|
|
{ |
|
|
|
|
|
|
|
|
$orders = Order::latest()->get(); |
|
|
|
|
|
|
|
|
if ($request->ajax()) { |
|
|
$page = $request->get('page', 1); |
|
|
$perPage = 3; |
|
|
$offset = ($page - 1) * $perPage; |
|
|
$status = $request->get('status'); |
|
|
$game = $request->get('game'); |
|
|
|
|
|
|
|
|
$filteredOrders = $orders; |
|
|
|
|
|
if ($status && $status !== 'recently') { |
|
|
$filteredOrders = $orders->where('status', $status); |
|
|
} |
|
|
|
|
|
|
|
|
if ($game && $game !== 'all') { |
|
|
$filteredOrders = $filteredOrders->filter(function($order) use ($game) { |
|
|
$items = json_decode($order->cart_data, true); |
|
|
if (is_array($items)) { |
|
|
foreach ($items as $item) { |
|
|
$product = \App\Models\Product::find($item['id'] ?? null); |
|
|
if ($product && $product->game === $game) { |
|
|
return true; |
|
|
} |
|
|
} |
|
|
} |
|
|
return false; |
|
|
}); |
|
|
} |
|
|
|
|
|
$paginatedOrders = $filteredOrders->slice($offset, $perPage); |
|
|
$hasMore = $filteredOrders->count() > ($offset + $perPage); |
|
|
|
|
|
return response()->json([ |
|
|
'orders' => $paginatedOrders->values(), |
|
|
'hasMore' => $hasMore, |
|
|
'currentPage' => $page |
|
|
]); |
|
|
} |
|
|
|
|
|
return view('customer-orders', compact('orders')); |
|
|
} |
|
|
} |
|
|
|