?php namespace App\Http\Controllers; use App\Brands; use App\Business; use App\Category; use App\Product; use App\Variation; use App\Contact; use App\Transaction; use App\BusinessLocation; use App\HomepageSetting; use App\Utils\ProductUtil; use App\Utils\TransactionUtil; use Illuminate\Http\Request; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; class BusinessPublicController extends Controller { protected $productUtil; protected $transactionUtil; protected $business; public function __construct(ProductUtil $productUtil, TransactionUtil $transactionUtil) { $this->productUtil = $productUtil; $this->transactionUtil = $transactionUtil; $this->middleware(function ($request, $next) { $business = \App\Business::first(); if ($business) { app()->instance('current_business', $business); $this->business = $business; } return $next($request); }); } // ───────────────────────────────────────────────────────── // HELPER — Business খোঁজো // ───────────────────────────────────────────────────────── private function getBusinessFromRequest(Request $request): ?Business { if ($this->business) return $this->business; return Cache::remember('single_business', 3600, function () { return Business::first(); }); } // ───────────────────────────────────────────────────────── // CURRENCY HELPER — DB থেকে সরাসরি currency data নাও // ───────────────────────────────────────────────────────── private function getCurrencyData(Business $business): array { $currency = Cache::remember( "business_currency_{$business->id}", 3600, fn() => \App\Currency::find($business->currency_id) ); return [ 'symbol' => $currency->symbol ?? 'OMR', 'dec_sep' => $currency->decimal_separator ?? '.', 'thou_sep' => $currency->thousand_separator ?? ',', 'precision' => (int)($business->currency_precision ?? 3), 'placement' => $business->currency_symbol_placement ?? 'before', ]; } // ───────────────────────────────────────────────────────── // HOMEPAGE // ───────────────────────────────────────────────────────── public function index(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); $currencyData = $this->getCurrencyData($business); $posSettings = Cache::remember( "homepage_settings_{$business->id}", 300, fn() => HomepageSetting::getAll($business->id) ); $slides = []; for ($i = 1; $i <= 10; $i++) { if (!empty($posSettings["carousel_image_$i"])) { $slides[] = asset('storage/' . $posSettings["carousel_image_$i"]); } } $location = BusinessLocation::where('business_id', $business->id)->first(); $location_id = $location->id ?? null; $featuredProducts = Product::where('business_id', $business->id) ->where('is_inactive', 0) ->where('not_for_selling', 0) ->where('show_in_online_store', 1) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'unit', 'media', 'brand', 'category' ]) ->latest() ->limit(12) ->get(); $categories = Category::where('business_id', $business->id) ->where('parent_id', 0) ->withCount(['products' => function ($q) { $q->where('show_in_online_store', 1) ->where('is_inactive', 0); }]) ->get(); $brands = Brands::where('business_id', $business->id)->get(); $gamingProducts = Product::where('business_id', $business->id) ->where('is_inactive', 0) ->where('show_in_online_store', 1) ->whereHas('category', function ($q) { $q->where('slug', 'gaming'); }) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'media' ]) ->limit(10) ->get(); $accessProducts = Product::where('business_id', $business->id) ->where('is_inactive', 0) ->where('show_in_online_store', 1) ->whereHas('category', function ($q) { $q->where('slug', 'accessories'); }) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'media' ]) ->limit(10) ->get(); return view('business_public.index', compact( 'business', 'currencyData', 'posSettings', 'slides', 'featuredProducts', 'categories', 'brands', 'gamingProducts', 'accessProducts' )); } // ───────────────────────────────────────────────────────── // PRODUCT LISTING // ───────────────────────────────────────────────────────── public function products(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404); $currencyData = $this->getCurrencyData($business); $location = BusinessLocation::where('business_id', $business->id)->first(); $location_id = $location->id ?? null; $query = Product::where('business_id', $business->id) ->where('is_inactive', 0) ->where('not_for_selling', 0) ->where('show_in_online_store', 1) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'media', 'brand', 'category' ]); if ($request->filled('category')) { $catValue = $request->category; $query->whereHas('category', function ($q) use ($catValue) { $q->where('name', $catValue)->orWhere('slug', $catValue); }); } if ($request->filled('brand')) { $query->where('brand_id', $request->brand); } if ($request->filled('search')) { $search = $request->search; $query->where(function ($q) use ($search) { $q->where('name', 'like', "%$search%")->orWhere('sku', 'like', "%$search%"); }); } // ── Website price দিয়ে min/max filter (আগে ছিল sell_price_inc_tax) ── if ($request->filled('min_price')) { $query->whereHas('variations', function ($q) use ($request) { $q->whereRaw('COALESCE(website_price_inc_tax, sell_price_inc_tax) >= ?', [(float) $request->min_price]); }); } if ($request->filled('max_price')) { $query->whereHas('variations', function ($q) use ($request) { $q->whereRaw('COALESCE(website_price_inc_tax, sell_price_inc_tax) <= ?', [(float) $request->max_price]); }); } // ── শেষ ── $sort = $request->get('sort', 'latest'); switch ($sort) { case 'price_asc': $query->join('variations', 'products.id', '=', 'variations.product_id') ->orderByRaw('COALESCE(variations.website_price_inc_tax, variations.sell_price_inc_tax) asc') ->select('products.*'); break; case 'price_desc': $query->join('variations', 'products.id', '=', 'variations.product_id') ->orderByRaw('COALESCE(variations.website_price_inc_tax, variations.sell_price_inc_tax) desc') ->select('products.*'); break; case 'name_asc': $query->orderBy('name', 'asc'); break; case 'name_desc': $query->orderBy('name', 'desc'); break; default: $query->latest(); break; } $perPage = $request->get('show', 20); $products = $query->paginate($perPage)->appends($request->query()); $categories = Category::where('business_id', $business->id) ->where('parent_id', 0)->withCount('products')->get(); $brands = Brands::where('business_id', $business->id)->get(); return view('business_public.products', compact( 'business', 'currencyData', 'products', 'categories', 'brands' )); } // ───────────────────────────────────────────────────────── // PRODUCT DETAIL // ───────────────────────────────────────────────────────── public function showProduct($id, Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); $currencyData = $this->getCurrencyData($business); $location = BusinessLocation::where('business_id', $business->id)->first(); $location_id = $location->id ?? null; $product = Product::where('business_id', $business->id) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'unit', 'brand', 'category', 'sub_category', 'media', 'variations.media' ]) ->findOrFail($id); $relatedProducts = Product::where('business_id', $business->id) ->where('id', '!=', $id) ->where('category_id', $product->category_id) ->where('is_inactive', 0) ->where('show_in_online_store', 1) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'media' ]) ->limit(8) ->get(); return view('business_public.product_detail', compact( 'business', 'currencyData', 'product', 'relatedProducts' )); } // ───────────────────────────────────────────────────────── // CART PAGE // ───────────────────────────────────────────────────────── public function cart(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); $currencyData = $this->getCurrencyData($business); [$items, $total] = $this->buildCartItems(); return view('business_public.cart', compact('business', 'currencyData', 'items', 'total')); } // ───────────────────────────────────────────────────────── // CART ITEMS (AJAX GET) // ───────────────────────────────────────────────────────── public function cartItems() { [$items, $total] = $this->buildCartItems(); return response()->json(['items' => $items, 'total' => $total]); } // ───────────────────────────────────────────────────────── // CART COUNT (AJAX) // ───────────────────────────────────────────────────────── public function getCartCount() { $cart = session()->get('cart', []); $count = array_sum(array_column($cart, 'quantity')); return response()->json(['count' => $count]); } // ───────────────────────────────────────────────────────── // ADD TO CART (AJAX) // ───────────────────────────────────────────────────────── public function addToCart(Request $request) { $productId = $request->product_id; $variationId = $request->variation_id; $quantity = max(1, $request->quantity ?? 1); $product = Product::with('media')->find($productId); $variation = Variation::find($variationId); if (!$product || !$variation) { return response()->json(['success' => false, 'message' => 'Product not found']); } // ── Website price ব্যবহার (আগে ছিল শুধু sell_price_inc_tax) ── $price = (!empty($variation->website_price_inc_tax) && $variation->website_price_inc_tax > 0) ? $variation->website_price_inc_tax : ($variation->sell_price_inc_tax ?? 0); // ── শেষ ── $cart = session()->get('cart', []); $key = $productId . '_' . $variationId; if (!empty($product->image)) { $imagePath = 'uploads/img/' . $product->image; } elseif ($product->media->first()) { $imagePath = 'uploads/media/' . $product->media->first()->file_name; } else { $imagePath = null; } if (isset($cart[$key])) { $cart[$key]['quantity'] += $quantity; } else { $cart[$key] = [ 'product_id' => $productId, 'variation_id' => $variationId, 'name' => $product->name, 'price' => $price, 'quantity' => $quantity, 'image' => $imagePath, ]; } session()->put('cart', $cart); $totalItems = array_sum(array_column($cart, 'quantity')); return response()->json(['success' => true, 'cart_count' => $totalItems, 'message' => 'Added to cart']); } // ───────────────────────────────────────────────────────── // UPDATE CART (AJAX) // ───────────────────────────────────────────────────────── public function updateCart(Request $request) { $key = $request->key; $quantity = max(1, $request->quantity); $cart = session()->get('cart', []); if (isset($cart[$key])) { $cart[$key]['quantity'] = $quantity; session()->put('cart', $cart); } return response()->json(['success' => true]); } // ───────────────────────────────────────────────────────── // REMOVE FROM CART (AJAX) // ───────────────────────────────────────────────────────── public function removeFromCart(Request $request) { $key = $request->key; $cart = session()->get('cart', []); if (isset($cart[$key])) { unset($cart[$key]); session()->put('cart', $cart); } return response()->json(['success' => true]); } // ───────────────────────────────────────────────────────── // CHECKOUT GET // ───────────────────────────────────────────────────────── public function checkout(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); if (!session('customer')) { return redirect('/')->with('open_auth', true)->with('after_login_redirect', '/checkout'); } $cart = session()->get('cart', []); if (empty($cart)) { return redirect('/')->with('error', 'Your cart is empty.'); } $currencyData = $this->getCurrencyData($business); [$items, $total] = $this->buildCartItems(); $customerSession = session('customer'); $customer = \App\Customer::find($customerSession['id']); if (!$customer) $customer = $customerSession; return view('business_public.checkout', compact( 'business', 'currencyData', 'items', 'total', 'customer' )); } // ───────────────────────────────────────────────────────── // PLACE ORDER // ───────────────────────────────────────────────────────── public function placeOrder(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); $cart = session()->get('cart', []); if (empty($cart)) { return redirect('/')->with('error', 'Cart is empty.'); } $request->validate([ 'name' => 'required|string|max:255', 'email' => 'nullable|email', 'phone' => 'required|string|max:20', 'address' => 'required|string', 'city' => 'required|string', 'payment_method' => 'required|in:cash,bkash,nagad,card', ]); DB::beginTransaction(); try { $customerSession = session('customer'); $customer = null; if ($customerSession) { $customer = \App\Customer::find($customerSession['id']); } $contact = null; if ($customer && !empty($customer->contact_id)) { $contact = Contact::find($customer->contact_id); } if (!$contact && $customer) { $contact = Contact::where('business_id', $business->id) ->where(function ($q) use ($customer) { $q->where('mobile', $customer->phone)->orWhere('email', $customer->email); })->first(); } if (!$contact) { $contact = Contact::where('business_id', $business->id) ->where('mobile', $request->phone)->first(); } if (!$contact) { $contact = Contact::create([ 'business_id' => $business->id, 'type' => 'customer', 'name' => $request->name, 'email' => $request->email, 'mobile' => $request->phone, 'address' => $request->address, 'city' => $request->city, 'created_by' => 1, 'contact_status' => 'active', ]); if ($customer && empty($customer->contact_id)) { $customer->contact_id = $contact->id; $customer->save(); session()->put('customer.contact_id', $contact->id); } } else { if ($customer && empty($customer->contact_id)) { $customer->contact_id = $contact->id; $customer->save(); session()->put('customer.contact_id', $contact->id); } } $location = BusinessLocation::where('business_id', $business->id)->first(); if (!$location) throw new \Exception('No business location found.'); $total = 0; foreach ($cart as $item) { $total += $item['price'] * $item['quantity']; } $shippingCharge = ($total >= 5000) ? 0 : 120; $finalTotal = $total + $shippingCharge; $transaction = new Transaction(); $transaction->business_id = $business->id; $transaction->location_id = $location->id; $transaction->contact_id = $contact->id; $transaction->type = 'sell'; $transaction->status = 'draft'; $transaction->sub_status = 'online_order'; $transaction->payment_status = 'due'; $transaction->shipping_status = 'ordered'; $transaction->transaction_date = now(); $transaction->total_before_tax = $total; $transaction->final_total = $finalTotal; $transaction->shipping_charges = $shippingCharge; $transaction->created_by = 1; $transaction->source = 'online'; $transaction->save(); foreach ($cart as $item) { $variation = Variation::find($item['variation_id']); if (!$variation) continue; $transaction->sell_lines()->create([ 'product_id' => $item['product_id'], 'variation_id' => $item['variation_id'], 'quantity' => $item['quantity'], 'unit_price_before_discount' => $item['price'], 'unit_price' => $item['price'], 'unit_price_inc_tax' => $item['price'], 'item_tax' => 0, 'tax_id' => null, ]); } DB::commit(); session()->forget('cart'); return redirect('/')->with('success', 'অর্ডার সফল হয়েছে! আপনার অর্ডারটি প্রসেস করা হবে।'); } catch (\Exception $e) { DB::rollBack(); \Log::error('PlaceOrder Error: ' . $e->getMessage()); return back()->with('error', 'দয়া করে আবার চেষ্টা করুন।'); } } // ───────────────────────────────────────────────────────── // PRIVATE HELPER — cart array তৈরি করো // ───────────────────────────────────────────────────────── private function buildCartItems(): array { $cart = session()->get('cart', []); $items = []; $total = 0; foreach ($cart as $key => $item) { $items[] = [ 'key' => $key, 'product_id' => $item['product_id'], 'variation_id' => $item['variation_id'], 'name' => $item['name'], 'quantity' => $item['quantity'], 'price' => $item['price'], 'subtotal' => $item['price'] * $item['quantity'], 'image' => $item['image'] ?? null, ]; $total += $item['price'] * $item['quantity']; } return [$items, $total]; } }?php namespace App\Http\Controllers; use App\Brands; use App\Business; use App\Category; use App\Product; use App\Variation; use App\Contact; use App\Transaction; use App\BusinessLocation; use App\HomepageSetting; use App\Utils\ProductUtil; use App\Utils\TransactionUtil; use Illuminate\Http\Request; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; class BusinessPublicController extends Controller { protected $productUtil; protected $transactionUtil; protected $business; public function __construct(ProductUtil $productUtil, TransactionUtil $transactionUtil) { $this->productUtil = $productUtil; $this->transactionUtil = $transactionUtil; $this->middleware(function ($request, $next) { $business = \App\Business::first(); if ($business) { app()->instance('current_business', $business); $this->business = $business; } return $next($request); }); } // ───────────────────────────────────────────────────────── // HELPER — Business খোঁজো // ───────────────────────────────────────────────────────── private function getBusinessFromRequest(Request $request): ?Business { if ($this->business) return $this->business; return Cache::remember('single_business', 3600, function () { return Business::first(); }); } // ───────────────────────────────────────────────────────── // CURRENCY HELPER — DB থেকে সরাসরি currency data নাও // ───────────────────────────────────────────────────────── private function getCurrencyData(Business $business): array { $currency = Cache::remember( "business_currency_{$business->id}", 3600, fn() => \App\Currency::find($business->currency_id) ); return [ 'symbol' => $currency->symbol ?? 'OMR', 'dec_sep' => $currency->decimal_separator ?? '.', 'thou_sep' => $currency->thousand_separator ?? ',', 'precision' => (int)($business->currency_precision ?? 3), 'placement' => $business->currency_symbol_placement ?? 'before', ]; } // ───────────────────────────────────────────────────────── // HOMEPAGE // ───────────────────────────────────────────────────────── public function index(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); $currencyData = $this->getCurrencyData($business); $posSettings = Cache::remember( "homepage_settings_{$business->id}", 300, fn() => HomepageSetting::getAll($business->id) ); $slides = []; for ($i = 1; $i <= 10; $i++) { if (!empty($posSettings["carousel_image_$i"])) { $slides[] = asset('storage/' . $posSettings["carousel_image_$i"]); } } $location = BusinessLocation::where('business_id', $business->id)->first(); $location_id = $location->id ?? null; $featuredProducts = Product::where('business_id', $business->id) ->where('is_inactive', 0) ->where('not_for_selling', 0) ->where('show_in_online_store', 1) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'unit', 'media', 'brand', 'category' ]) ->latest() ->limit(12) ->get(); $categories = Category::where('business_id', $business->id) ->where('parent_id', 0) ->withCount(['products' => function ($q) { $q->where('show_in_online_store', 1) ->where('is_inactive', 0); }]) ->get(); $brands = Brands::where('business_id', $business->id)->get(); $gamingProducts = Product::where('business_id', $business->id) ->where('is_inactive', 0) ->where('show_in_online_store', 1) ->whereHas('category', function ($q) { $q->where('slug', 'gaming'); }) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'media' ]) ->limit(10) ->get(); $accessProducts = Product::where('business_id', $business->id) ->where('is_inactive', 0) ->where('show_in_online_store', 1) ->whereHas('category', function ($q) { $q->where('slug', 'accessories'); }) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'media' ]) ->limit(10) ->get(); return view('business_public.index', compact( 'business', 'currencyData', 'posSettings', 'slides', 'featuredProducts', 'categories', 'brands', 'gamingProducts', 'accessProducts' )); } // ───────────────────────────────────────────────────────── // PRODUCT LISTING // ───────────────────────────────────────────────────────── public function products(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404); $currencyData = $this->getCurrencyData($business); $location = BusinessLocation::where('business_id', $business->id)->first(); $location_id = $location->id ?? null; $query = Product::where('business_id', $business->id) ->where('is_inactive', 0) ->where('not_for_selling', 0) ->where('show_in_online_store', 1) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'media', 'brand', 'category' ]); if ($request->filled('category')) { $catValue = $request->category; $query->whereHas('category', function ($q) use ($catValue) { $q->where('name', $catValue)->orWhere('slug', $catValue); }); } if ($request->filled('brand')) { $query->where('brand_id', $request->brand); } if ($request->filled('search')) { $search = $request->search; $query->where(function ($q) use ($search) { $q->where('name', 'like', "%$search%")->orWhere('sku', 'like', "%$search%"); }); } // ── Website price দিয়ে min/max filter (আগে ছিল sell_price_inc_tax) ── if ($request->filled('min_price')) { $query->whereHas('variations', function ($q) use ($request) { $q->whereRaw('COALESCE(website_price_inc_tax, sell_price_inc_tax) >= ?', [(float) $request->min_price]); }); } if ($request->filled('max_price')) { $query->whereHas('variations', function ($q) use ($request) { $q->whereRaw('COALESCE(website_price_inc_tax, sell_price_inc_tax) <= ?', [(float) $request->max_price]); }); } // ── শেষ ── $sort = $request->get('sort', 'latest'); switch ($sort) { case 'price_asc': $query->join('variations', 'products.id', '=', 'variations.product_id') ->orderByRaw('COALESCE(variations.website_price_inc_tax, variations.sell_price_inc_tax) asc') ->select('products.*'); break; case 'price_desc': $query->join('variations', 'products.id', '=', 'variations.product_id') ->orderByRaw('COALESCE(variations.website_price_inc_tax, variations.sell_price_inc_tax) desc') ->select('products.*'); break; case 'name_asc': $query->orderBy('name', 'asc'); break; case 'name_desc': $query->orderBy('name', 'desc'); break; default: $query->latest(); break; } $perPage = $request->get('show', 20); $products = $query->paginate($perPage)->appends($request->query()); $categories = Category::where('business_id', $business->id) ->where('parent_id', 0)->withCount('products')->get(); $brands = Brands::where('business_id', $business->id)->get(); return view('business_public.products', compact( 'business', 'currencyData', 'products', 'categories', 'brands' )); } // ───────────────────────────────────────────────────────── // PRODUCT DETAIL // ───────────────────────────────────────────────────────── public function showProduct($id, Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); $currencyData = $this->getCurrencyData($business); $location = BusinessLocation::where('business_id', $business->id)->first(); $location_id = $location->id ?? null; $product = Product::where('business_id', $business->id) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'unit', 'brand', 'category', 'sub_category', 'media', 'variations.media' ]) ->findOrFail($id); $relatedProducts = Product::where('business_id', $business->id) ->where('id', '!=', $id) ->where('category_id', $product->category_id) ->where('is_inactive', 0) ->where('show_in_online_store', 1) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'media' ]) ->limit(8) ->get(); return view('business_public.product_detail', compact( 'business', 'currencyData', 'product', 'relatedProducts' )); } // ───────────────────────────────────────────────────────── // CART PAGE // ───────────────────────────────────────────────────────── public function cart(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); $currencyData = $this->getCurrencyData($business); [$items, $total] = $this->buildCartItems(); return view('business_public.cart', compact('business', 'currencyData', 'items', 'total')); } // ───────────────────────────────────────────────────────── // CART ITEMS (AJAX GET) // ───────────────────────────────────────────────────────── public function cartItems() { [$items, $total] = $this->buildCartItems(); return response()->json(['items' => $items, 'total' => $total]); } // ───────────────────────────────────────────────────────── // CART COUNT (AJAX) // ───────────────────────────────────────────────────────── public function getCartCount() { $cart = session()->get('cart', []); $count = array_sum(array_column($cart, 'quantity')); return response()->json(['count' => $count]); } // ───────────────────────────────────────────────────────── // ADD TO CART (AJAX) // ───────────────────────────────────────────────────────── public function addToCart(Request $request) { $productId = $request->product_id; $variationId = $request->variation_id; $quantity = max(1, $request->quantity ?? 1); $product = Product::with('media')->find($productId); $variation = Variation::find($variationId); if (!$product || !$variation) { return response()->json(['success' => false, 'message' => 'Product not found']); } // ── Website price ব্যবহার (আগে ছিল শুধু sell_price_inc_tax) ── $price = (!empty($variation->website_price_inc_tax) && $variation->website_price_inc_tax > 0) ? $variation->website_price_inc_tax : ($variation->sell_price_inc_tax ?? 0); // ── শেষ ── $cart = session()->get('cart', []); $key = $productId . '_' . $variationId; if (!empty($product->image)) { $imagePath = 'uploads/img/' . $product->image; } elseif ($product->media->first()) { $imagePath = 'uploads/media/' . $product->media->first()->file_name; } else { $imagePath = null; } if (isset($cart[$key])) { $cart[$key]['quantity'] += $quantity; } else { $cart[$key] = [ 'product_id' => $productId, 'variation_id' => $variationId, 'name' => $product->name, 'price' => $price, 'quantity' => $quantity, 'image' => $imagePath, ]; } session()->put('cart', $cart); $totalItems = array_sum(array_column($cart, 'quantity')); return response()->json(['success' => true, 'cart_count' => $totalItems, 'message' => 'Added to cart']); } // ───────────────────────────────────────────────────────── // UPDATE CART (AJAX) // ───────────────────────────────────────────────────────── public function updateCart(Request $request) { $key = $request->key; $quantity = max(1, $request->quantity); $cart = session()->get('cart', []); if (isset($cart[$key])) { $cart[$key]['quantity'] = $quantity; session()->put('cart', $cart); } return response()->json(['success' => true]); } // ───────────────────────────────────────────────────────── // REMOVE FROM CART (AJAX) // ───────────────────────────────────────────────────────── public function removeFromCart(Request $request) { $key = $request->key; $cart = session()->get('cart', []); if (isset($cart[$key])) { unset($cart[$key]); session()->put('cart', $cart); } return response()->json(['success' => true]); } // ───────────────────────────────────────────────────────── // CHECKOUT GET // ───────────────────────────────────────────────────────── public function checkout(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); if (!session('customer')) { return redirect('/')->with('open_auth', true)->with('after_login_redirect', '/checkout'); } $cart = session()->get('cart', []); if (empty($cart)) { return redirect('/')->with('error', 'Your cart is empty.'); } $currencyData = $this->getCurrencyData($business); [$items, $total] = $this->buildCartItems(); $customerSession = session('customer'); $customer = \App\Customer::find($customerSession['id']); if (!$customer) $customer = $customerSession; return view('business_public.checkout', compact( 'business', 'currencyData', 'items', 'total', 'customer' )); } // ───────────────────────────────────────────────────────── // PLACE ORDER // ───────────────────────────────────────────────────────── public function placeOrder(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); $cart = session()->get('cart', []); if (empty($cart)) { return redirect('/')->with('error', 'Cart is empty.'); } $request->validate([ 'name' => 'required|string|max:255', 'email' => 'nullable|email', 'phone' => 'required|string|max:20', 'address' => 'required|string', 'city' => 'required|string', 'payment_method' => 'required|in:cash,bkash,nagad,card', ]); DB::beginTransaction(); try { $customerSession = session('customer'); $customer = null; if ($customerSession) { $customer = \App\Customer::find($customerSession['id']); } $contact = null; if ($customer && !empty($customer->contact_id)) { $contact = Contact::find($customer->contact_id); } if (!$contact && $customer) { $contact = Contact::where('business_id', $business->id) ->where(function ($q) use ($customer) { $q->where('mobile', $customer->phone)->orWhere('email', $customer->email); })->first(); } if (!$contact) { $contact = Contact::where('business_id', $business->id) ->where('mobile', $request->phone)->first(); } if (!$contact) { $contact = Contact::create([ 'business_id' => $business->id, 'type' => 'customer', 'name' => $request->name, 'email' => $request->email, 'mobile' => $request->phone, 'address' => $request->address, 'city' => $request->city, 'created_by' => 1, 'contact_status' => 'active', ]); if ($customer && empty($customer->contact_id)) { $customer->contact_id = $contact->id; $customer->save(); session()->put('customer.contact_id', $contact->id); } } else { if ($customer && empty($customer->contact_id)) { $customer->contact_id = $contact->id; $customer->save(); session()->put('customer.contact_id', $contact->id); } } $location = BusinessLocation::where('business_id', $business->id)->first(); if (!$location) throw new \Exception('No business location found.'); $total = 0; foreach ($cart as $item) { $total += $item['price'] * $item['quantity']; } $shippingCharge = ($total >= 5000) ? 0 : 120; $finalTotal = $total + $shippingCharge; $transaction = new Transaction(); $transaction->business_id = $business->id; $transaction->location_id = $location->id; $transaction->contact_id = $contact->id; $transaction->type = 'sell'; $transaction->status = 'draft'; $transaction->sub_status = 'online_order'; $transaction->payment_status = 'due'; $transaction->shipping_status = 'ordered'; $transaction->transaction_date = now(); $transaction->total_before_tax = $total; $transaction->final_total = $finalTotal; $transaction->shipping_charges = $shippingCharge; $transaction->created_by = 1; $transaction->source = 'online'; $transaction->save(); foreach ($cart as $item) { $variation = Variation::find($item['variation_id']); if (!$variation) continue; $transaction->sell_lines()->create([ 'product_id' => $item['product_id'], 'variation_id' => $item['variation_id'], 'quantity' => $item['quantity'], 'unit_price_before_discount' => $item['price'], 'unit_price' => $item['price'], 'unit_price_inc_tax' => $item['price'], 'item_tax' => 0, 'tax_id' => null, ]); } DB::commit(); session()->forget('cart'); return redirect('/')->with('success', 'অর্ডার সফল হয়েছে! আপনার অর্ডারটি প্রসেস করা হবে।'); } catch (\Exception $e) { DB::rollBack(); \Log::error('PlaceOrder Error: ' . $e->getMessage()); return back()->with('error', 'দয়া করে আবার চেষ্টা করুন।'); } } // ───────────────────────────────────────────────────────── // PRIVATE HELPER — cart array তৈরি করো // ───────────────────────────────────────────────────────── private function buildCartItems(): array { $cart = session()->get('cart', []); $items = []; $total = 0; foreach ($cart as $key => $item) { $items[] = [ 'key' => $key, 'product_id' => $item['product_id'], 'variation_id' => $item['variation_id'], 'name' => $item['name'], 'quantity' => $item['quantity'], 'price' => $item['price'], 'subtotal' => $item['price'] * $item['quantity'], 'image' => $item['image'] ?? null, ]; $total += $item['price'] * $item['quantity']; } return [$items, $total]; } }?php namespace App\Http\Controllers; use App\Brands; use App\Business; use App\Category; use App\Product; use App\Variation; use App\Contact; use App\Transaction; use App\BusinessLocation; use App\HomepageSetting; use App\Utils\ProductUtil; use App\Utils\TransactionUtil; use Illuminate\Http\Request; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; class BusinessPublicController extends Controller { protected $productUtil; protected $transactionUtil; protected $business; public function __construct(ProductUtil $productUtil, TransactionUtil $transactionUtil) { $this->productUtil = $productUtil; $this->transactionUtil = $transactionUtil; $this->middleware(function ($request, $next) { $business = \App\Business::first(); if ($business) { app()->instance('current_business', $business); $this->business = $business; } return $next($request); }); } // ───────────────────────────────────────────────────────── // HELPER — Business খোঁজো // ───────────────────────────────────────────────────────── private function getBusinessFromRequest(Request $request): ?Business { if ($this->business) return $this->business; return Cache::remember('single_business', 3600, function () { return Business::first(); }); } // ───────────────────────────────────────────────────────── // CURRENCY HELPER — DB থেকে সরাসরি currency data নাও // ───────────────────────────────────────────────────────── private function getCurrencyData(Business $business): array { $currency = Cache::remember( "business_currency_{$business->id}", 3600, fn() => \App\Currency::find($business->currency_id) ); return [ 'symbol' => $currency->symbol ?? 'OMR', 'dec_sep' => $currency->decimal_separator ?? '.', 'thou_sep' => $currency->thousand_separator ?? ',', 'precision' => (int)($business->currency_precision ?? 3), 'placement' => $business->currency_symbol_placement ?? 'before', ]; } // ───────────────────────────────────────────────────────── // HOMEPAGE // ───────────────────────────────────────────────────────── public function index(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); $currencyData = $this->getCurrencyData($business); $posSettings = Cache::remember( "homepage_settings_{$business->id}", 300, fn() => HomepageSetting::getAll($business->id) ); $slides = []; for ($i = 1; $i <= 10; $i++) { if (!empty($posSettings["carousel_image_$i"])) { $slides[] = asset('storage/' . $posSettings["carousel_image_$i"]); } } $location = BusinessLocation::where('business_id', $business->id)->first(); $location_id = $location->id ?? null; $featuredProducts = Product::where('business_id', $business->id) ->where('is_inactive', 0) ->where('not_for_selling', 0) ->where('show_in_online_store', 1) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'unit', 'media', 'brand', 'category' ]) ->latest() ->limit(12) ->get(); $categories = Category::where('business_id', $business->id) ->where('parent_id', 0) ->withCount(['products' => function ($q) { $q->where('show_in_online_store', 1) ->where('is_inactive', 0); }]) ->get(); $brands = Brands::where('business_id', $business->id)->get(); $gamingProducts = Product::where('business_id', $business->id) ->where('is_inactive', 0) ->where('show_in_online_store', 1) ->whereHas('category', function ($q) { $q->where('slug', 'gaming'); }) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'media' ]) ->limit(10) ->get(); $accessProducts = Product::where('business_id', $business->id) ->where('is_inactive', 0) ->where('show_in_online_store', 1) ->whereHas('category', function ($q) { $q->where('slug', 'accessories'); }) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'media' ]) ->limit(10) ->get(); return view('business_public.index', compact( 'business', 'currencyData', 'posSettings', 'slides', 'featuredProducts', 'categories', 'brands', 'gamingProducts', 'accessProducts' )); } // ───────────────────────────────────────────────────────── // PRODUCT LISTING // ───────────────────────────────────────────────────────── public function products(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404); $currencyData = $this->getCurrencyData($business); $location = BusinessLocation::where('business_id', $business->id)->first(); $location_id = $location->id ?? null; $query = Product::where('business_id', $business->id) ->where('is_inactive', 0) ->where('not_for_selling', 0) ->where('show_in_online_store', 1) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'media', 'brand', 'category' ]); if ($request->filled('category')) { $catValue = $request->category; $query->whereHas('category', function ($q) use ($catValue) { $q->where('name', $catValue)->orWhere('slug', $catValue); }); } if ($request->filled('brand')) { $query->where('brand_id', $request->brand); } if ($request->filled('search')) { $search = $request->search; $query->where(function ($q) use ($search) { $q->where('name', 'like', "%$search%")->orWhere('sku', 'like', "%$search%"); }); } // ── Website price দিয়ে min/max filter (আগে ছিল sell_price_inc_tax) ── if ($request->filled('min_price')) { $query->whereHas('variations', function ($q) use ($request) { $q->whereRaw('COALESCE(website_price_inc_tax, sell_price_inc_tax) >= ?', [(float) $request->min_price]); }); } if ($request->filled('max_price')) { $query->whereHas('variations', function ($q) use ($request) { $q->whereRaw('COALESCE(website_price_inc_tax, sell_price_inc_tax) <= ?', [(float) $request->max_price]); }); } // ── শেষ ── $sort = $request->get('sort', 'latest'); switch ($sort) { case 'price_asc': $query->join('variations', 'products.id', '=', 'variations.product_id') ->orderByRaw('COALESCE(variations.website_price_inc_tax, variations.sell_price_inc_tax) asc') ->select('products.*'); break; case 'price_desc': $query->join('variations', 'products.id', '=', 'variations.product_id') ->orderByRaw('COALESCE(variations.website_price_inc_tax, variations.sell_price_inc_tax) desc') ->select('products.*'); break; case 'name_asc': $query->orderBy('name', 'asc'); break; case 'name_desc': $query->orderBy('name', 'desc'); break; default: $query->latest(); break; } $perPage = $request->get('show', 20); $products = $query->paginate($perPage)->appends($request->query()); $categories = Category::where('business_id', $business->id) ->where('parent_id', 0)->withCount('products')->get(); $brands = Brands::where('business_id', $business->id)->get(); return view('business_public.products', compact( 'business', 'currencyData', 'products', 'categories', 'brands' )); } // ───────────────────────────────────────────────────────── // PRODUCT DETAIL // ───────────────────────────────────────────────────────── public function showProduct($id, Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); $currencyData = $this->getCurrencyData($business); $location = BusinessLocation::where('business_id', $business->id)->first(); $location_id = $location->id ?? null; $product = Product::where('business_id', $business->id) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'unit', 'brand', 'category', 'sub_category', 'media', 'variations.media' ]) ->findOrFail($id); $relatedProducts = Product::where('business_id', $business->id) ->where('id', '!=', $id) ->where('category_id', $product->category_id) ->where('is_inactive', 0) ->where('show_in_online_store', 1) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'media' ]) ->limit(8) ->get(); return view('business_public.product_detail', compact( 'business', 'currencyData', 'product', 'relatedProducts' )); } // ───────────────────────────────────────────────────────── // CART PAGE // ───────────────────────────────────────────────────────── public function cart(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); $currencyData = $this->getCurrencyData($business); [$items, $total] = $this->buildCartItems(); return view('business_public.cart', compact('business', 'currencyData', 'items', 'total')); } // ───────────────────────────────────────────────────────── // CART ITEMS (AJAX GET) // ───────────────────────────────────────────────────────── public function cartItems() { [$items, $total] = $this->buildCartItems(); return response()->json(['items' => $items, 'total' => $total]); } // ───────────────────────────────────────────────────────── // CART COUNT (AJAX) // ───────────────────────────────────────────────────────── public function getCartCount() { $cart = session()->get('cart', []); $count = array_sum(array_column($cart, 'quantity')); return response()->json(['count' => $count]); } // ───────────────────────────────────────────────────────── // ADD TO CART (AJAX) // ───────────────────────────────────────────────────────── public function addToCart(Request $request) { $productId = $request->product_id; $variationId = $request->variation_id; $quantity = max(1, $request->quantity ?? 1); $product = Product::with('media')->find($productId); $variation = Variation::find($variationId); if (!$product || !$variation) { return response()->json(['success' => false, 'message' => 'Product not found']); } // ── Website price ব্যবহার (আগে ছিল শুধু sell_price_inc_tax) ── $price = (!empty($variation->website_price_inc_tax) && $variation->website_price_inc_tax > 0) ? $variation->website_price_inc_tax : ($variation->sell_price_inc_tax ?? 0); // ── শেষ ── $cart = session()->get('cart', []); $key = $productId . '_' . $variationId; if (!empty($product->image)) { $imagePath = 'uploads/img/' . $product->image; } elseif ($product->media->first()) { $imagePath = 'uploads/media/' . $product->media->first()->file_name; } else { $imagePath = null; } if (isset($cart[$key])) { $cart[$key]['quantity'] += $quantity; } else { $cart[$key] = [ 'product_id' => $productId, 'variation_id' => $variationId, 'name' => $product->name, 'price' => $price, 'quantity' => $quantity, 'image' => $imagePath, ]; } session()->put('cart', $cart); $totalItems = array_sum(array_column($cart, 'quantity')); return response()->json(['success' => true, 'cart_count' => $totalItems, 'message' => 'Added to cart']); } // ───────────────────────────────────────────────────────── // UPDATE CART (AJAX) // ───────────────────────────────────────────────────────── public function updateCart(Request $request) { $key = $request->key; $quantity = max(1, $request->quantity); $cart = session()->get('cart', []); if (isset($cart[$key])) { $cart[$key]['quantity'] = $quantity; session()->put('cart', $cart); } return response()->json(['success' => true]); } // ───────────────────────────────────────────────────────── // REMOVE FROM CART (AJAX) // ───────────────────────────────────────────────────────── public function removeFromCart(Request $request) { $key = $request->key; $cart = session()->get('cart', []); if (isset($cart[$key])) { unset($cart[$key]); session()->put('cart', $cart); } return response()->json(['success' => true]); } // ───────────────────────────────────────────────────────── // CHECKOUT GET // ───────────────────────────────────────────────────────── public function checkout(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); if (!session('customer')) { return redirect('/')->with('open_auth', true)->with('after_login_redirect', '/checkout'); } $cart = session()->get('cart', []); if (empty($cart)) { return redirect('/')->with('error', 'Your cart is empty.'); } $currencyData = $this->getCurrencyData($business); [$items, $total] = $this->buildCartItems(); $customerSession = session('customer'); $customer = \App\Customer::find($customerSession['id']); if (!$customer) $customer = $customerSession; return view('business_public.checkout', compact( 'business', 'currencyData', 'items', 'total', 'customer' )); } // ───────────────────────────────────────────────────────── // PLACE ORDER // ───────────────────────────────────────────────────────── public function placeOrder(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); $cart = session()->get('cart', []); if (empty($cart)) { return redirect('/')->with('error', 'Cart is empty.'); } $request->validate([ 'name' => 'required|string|max:255', 'email' => 'nullable|email', 'phone' => 'required|string|max:20', 'address' => 'required|string', 'city' => 'required|string', 'payment_method' => 'required|in:cash,bkash,nagad,card', ]); DB::beginTransaction(); try { $customerSession = session('customer'); $customer = null; if ($customerSession) { $customer = \App\Customer::find($customerSession['id']); } $contact = null; if ($customer && !empty($customer->contact_id)) { $contact = Contact::find($customer->contact_id); } if (!$contact && $customer) { $contact = Contact::where('business_id', $business->id) ->where(function ($q) use ($customer) { $q->where('mobile', $customer->phone)->orWhere('email', $customer->email); })->first(); } if (!$contact) { $contact = Contact::where('business_id', $business->id) ->where('mobile', $request->phone)->first(); } if (!$contact) { $contact = Contact::create([ 'business_id' => $business->id, 'type' => 'customer', 'name' => $request->name, 'email' => $request->email, 'mobile' => $request->phone, 'address' => $request->address, 'city' => $request->city, 'created_by' => 1, 'contact_status' => 'active', ]); if ($customer && empty($customer->contact_id)) { $customer->contact_id = $contact->id; $customer->save(); session()->put('customer.contact_id', $contact->id); } } else { if ($customer && empty($customer->contact_id)) { $customer->contact_id = $contact->id; $customer->save(); session()->put('customer.contact_id', $contact->id); } } $location = BusinessLocation::where('business_id', $business->id)->first(); if (!$location) throw new \Exception('No business location found.'); $total = 0; foreach ($cart as $item) { $total += $item['price'] * $item['quantity']; } $shippingCharge = ($total >= 5000) ? 0 : 120; $finalTotal = $total + $shippingCharge; $transaction = new Transaction(); $transaction->business_id = $business->id; $transaction->location_id = $location->id; $transaction->contact_id = $contact->id; $transaction->type = 'sell'; $transaction->status = 'draft'; $transaction->sub_status = 'online_order'; $transaction->payment_status = 'due'; $transaction->shipping_status = 'ordered'; $transaction->transaction_date = now(); $transaction->total_before_tax = $total; $transaction->final_total = $finalTotal; $transaction->shipping_charges = $shippingCharge; $transaction->created_by = 1; $transaction->source = 'online'; $transaction->save(); foreach ($cart as $item) { $variation = Variation::find($item['variation_id']); if (!$variation) continue; $transaction->sell_lines()->create([ 'product_id' => $item['product_id'], 'variation_id' => $item['variation_id'], 'quantity' => $item['quantity'], 'unit_price_before_discount' => $item['price'], 'unit_price' => $item['price'], 'unit_price_inc_tax' => $item['price'], 'item_tax' => 0, 'tax_id' => null, ]); } DB::commit(); session()->forget('cart'); return redirect('/')->with('success', 'অর্ডার সফল হয়েছে! আপনার অর্ডারটি প্রসেস করা হবে।'); } catch (\Exception $e) { DB::rollBack(); \Log::error('PlaceOrder Error: ' . $e->getMessage()); return back()->with('error', 'দয়া করে আবার চেষ্টা করুন।'); } } // ───────────────────────────────────────────────────────── // PRIVATE HELPER — cart array তৈরি করো // ───────────────────────────────────────────────────────── private function buildCartItems(): array { $cart = session()->get('cart', []); $items = []; $total = 0; foreach ($cart as $key => $item) { $items[] = [ 'key' => $key, 'product_id' => $item['product_id'], 'variation_id' => $item['variation_id'], 'name' => $item['name'], 'quantity' => $item['quantity'], 'price' => $item['price'], 'subtotal' => $item['price'] * $item['quantity'], 'image' => $item['image'] ?? null, ]; $total += $item['price'] * $item['quantity']; } return [$items, $total]; } }?php namespace App\Http\Controllers; use App\Brands; use App\Business; use App\Category; use App\Product; use App\Variation; use App\Contact; use App\Transaction; use App\BusinessLocation; use App\HomepageSetting; use App\Utils\ProductUtil; use App\Utils\TransactionUtil; use Illuminate\Http\Request; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; class BusinessPublicController extends Controller { protected $productUtil; protected $transactionUtil; protected $business; public function __construct(ProductUtil $productUtil, TransactionUtil $transactionUtil) { $this->productUtil = $productUtil; $this->transactionUtil = $transactionUtil; $this->middleware(function ($request, $next) { $business = \App\Business::first(); if ($business) { app()->instance('current_business', $business); $this->business = $business; } return $next($request); }); } // ───────────────────────────────────────────────────────── // HELPER — Business খোঁজো // ───────────────────────────────────────────────────────── private function getBusinessFromRequest(Request $request): ?Business { if ($this->business) return $this->business; return Cache::remember('single_business', 3600, function () { return Business::first(); }); } // ───────────────────────────────────────────────────────── // CURRENCY HELPER — DB থেকে সরাসরি currency data নাও // ───────────────────────────────────────────────────────── private function getCurrencyData(Business $business): array { $currency = Cache::remember( "business_currency_{$business->id}", 3600, fn() => \App\Currency::find($business->currency_id) ); return [ 'symbol' => $currency->symbol ?? 'OMR', 'dec_sep' => $currency->decimal_separator ?? '.', 'thou_sep' => $currency->thousand_separator ?? ',', 'precision' => (int)($business->currency_precision ?? 3), 'placement' => $business->currency_symbol_placement ?? 'before', ]; } // ───────────────────────────────────────────────────────── // HOMEPAGE // ───────────────────────────────────────────────────────── public function index(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); $currencyData = $this->getCurrencyData($business); $posSettings = Cache::remember( "homepage_settings_{$business->id}", 300, fn() => HomepageSetting::getAll($business->id) ); $slides = []; for ($i = 1; $i <= 10; $i++) { if (!empty($posSettings["carousel_image_$i"])) { $slides[] = asset('storage/' . $posSettings["carousel_image_$i"]); } } $location = BusinessLocation::where('business_id', $business->id)->first(); $location_id = $location->id ?? null; $featuredProducts = Product::where('business_id', $business->id) ->where('is_inactive', 0) ->where('not_for_selling', 0) ->where('show_in_online_store', 1) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'unit', 'media', 'brand', 'category' ]) ->latest() ->limit(12) ->get(); $categories = Category::where('business_id', $business->id) ->where('parent_id', 0) ->withCount(['products' => function ($q) { $q->where('show_in_online_store', 1) ->where('is_inactive', 0); }]) ->get(); $brands = Brands::where('business_id', $business->id)->get(); $gamingProducts = Product::where('business_id', $business->id) ->where('is_inactive', 0) ->where('show_in_online_store', 1) ->whereHas('category', function ($q) { $q->where('slug', 'gaming'); }) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'media' ]) ->limit(10) ->get(); $accessProducts = Product::where('business_id', $business->id) ->where('is_inactive', 0) ->where('show_in_online_store', 1) ->whereHas('category', function ($q) { $q->where('slug', 'accessories'); }) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'media' ]) ->limit(10) ->get(); return view('business_public.index', compact( 'business', 'currencyData', 'posSettings', 'slides', 'featuredProducts', 'categories', 'brands', 'gamingProducts', 'accessProducts' )); } // ───────────────────────────────────────────────────────── // PRODUCT LISTING // ───────────────────────────────────────────────────────── public function products(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404); $currencyData = $this->getCurrencyData($business); $location = BusinessLocation::where('business_id', $business->id)->first(); $location_id = $location->id ?? null; $query = Product::where('business_id', $business->id) ->where('is_inactive', 0) ->where('not_for_selling', 0) ->where('show_in_online_store', 1) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'media', 'brand', 'category' ]); if ($request->filled('category')) { $catValue = $request->category; $query->whereHas('category', function ($q) use ($catValue) { $q->where('name', $catValue)->orWhere('slug', $catValue); }); } if ($request->filled('brand')) { $query->where('brand_id', $request->brand); } if ($request->filled('search')) { $search = $request->search; $query->where(function ($q) use ($search) { $q->where('name', 'like', "%$search%")->orWhere('sku', 'like', "%$search%"); }); } // ── Website price দিয়ে min/max filter (আগে ছিল sell_price_inc_tax) ── if ($request->filled('min_price')) { $query->whereHas('variations', function ($q) use ($request) { $q->whereRaw('COALESCE(website_price_inc_tax, sell_price_inc_tax) >= ?', [(float) $request->min_price]); }); } if ($request->filled('max_price')) { $query->whereHas('variations', function ($q) use ($request) { $q->whereRaw('COALESCE(website_price_inc_tax, sell_price_inc_tax) <= ?', [(float) $request->max_price]); }); } // ── শেষ ── $sort = $request->get('sort', 'latest'); switch ($sort) { case 'price_asc': $query->join('variations', 'products.id', '=', 'variations.product_id') ->orderByRaw('COALESCE(variations.website_price_inc_tax, variations.sell_price_inc_tax) asc') ->select('products.*'); break; case 'price_desc': $query->join('variations', 'products.id', '=', 'variations.product_id') ->orderByRaw('COALESCE(variations.website_price_inc_tax, variations.sell_price_inc_tax) desc') ->select('products.*'); break; case 'name_asc': $query->orderBy('name', 'asc'); break; case 'name_desc': $query->orderBy('name', 'desc'); break; default: $query->latest(); break; } $perPage = $request->get('show', 20); $products = $query->paginate($perPage)->appends($request->query()); $categories = Category::where('business_id', $business->id) ->where('parent_id', 0)->withCount('products')->get(); $brands = Brands::where('business_id', $business->id)->get(); return view('business_public.products', compact( 'business', 'currencyData', 'products', 'categories', 'brands' )); } // ───────────────────────────────────────────────────────── // PRODUCT DETAIL // ───────────────────────────────────────────────────────── public function showProduct($id, Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); $currencyData = $this->getCurrencyData($business); $location = BusinessLocation::where('business_id', $business->id)->first(); $location_id = $location->id ?? null; $product = Product::where('business_id', $business->id) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'unit', 'brand', 'category', 'sub_category', 'media', 'variations.media' ]) ->findOrFail($id); $relatedProducts = Product::where('business_id', $business->id) ->where('id', '!=', $id) ->where('category_id', $product->category_id) ->where('is_inactive', 0) ->where('show_in_online_store', 1) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'media' ]) ->limit(8) ->get(); return view('business_public.product_detail', compact( 'business', 'currencyData', 'product', 'relatedProducts' )); } // ───────────────────────────────────────────────────────── // CART PAGE // ───────────────────────────────────────────────────────── public function cart(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); $currencyData = $this->getCurrencyData($business); [$items, $total] = $this->buildCartItems(); return view('business_public.cart', compact('business', 'currencyData', 'items', 'total')); } // ───────────────────────────────────────────────────────── // CART ITEMS (AJAX GET) // ───────────────────────────────────────────────────────── public function cartItems() { [$items, $total] = $this->buildCartItems(); return response()->json(['items' => $items, 'total' => $total]); } // ───────────────────────────────────────────────────────── // CART COUNT (AJAX) // ───────────────────────────────────────────────────────── public function getCartCount() { $cart = session()->get('cart', []); $count = array_sum(array_column($cart, 'quantity')); return response()->json(['count' => $count]); } // ───────────────────────────────────────────────────────── // ADD TO CART (AJAX) // ───────────────────────────────────────────────────────── public function addToCart(Request $request) { $productId = $request->product_id; $variationId = $request->variation_id; $quantity = max(1, $request->quantity ?? 1); $product = Product::with('media')->find($productId); $variation = Variation::find($variationId); if (!$product || !$variation) { return response()->json(['success' => false, 'message' => 'Product not found']); } // ── Website price ব্যবহার (আগে ছিল শুধু sell_price_inc_tax) ── $price = (!empty($variation->website_price_inc_tax) && $variation->website_price_inc_tax > 0) ? $variation->website_price_inc_tax : ($variation->sell_price_inc_tax ?? 0); // ── শেষ ── $cart = session()->get('cart', []); $key = $productId . '_' . $variationId; if (!empty($product->image)) { $imagePath = 'uploads/img/' . $product->image; } elseif ($product->media->first()) { $imagePath = 'uploads/media/' . $product->media->first()->file_name; } else { $imagePath = null; } if (isset($cart[$key])) { $cart[$key]['quantity'] += $quantity; } else { $cart[$key] = [ 'product_id' => $productId, 'variation_id' => $variationId, 'name' => $product->name, 'price' => $price, 'quantity' => $quantity, 'image' => $imagePath, ]; } session()->put('cart', $cart); $totalItems = array_sum(array_column($cart, 'quantity')); return response()->json(['success' => true, 'cart_count' => $totalItems, 'message' => 'Added to cart']); } // ───────────────────────────────────────────────────────── // UPDATE CART (AJAX) // ───────────────────────────────────────────────────────── public function updateCart(Request $request) { $key = $request->key; $quantity = max(1, $request->quantity); $cart = session()->get('cart', []); if (isset($cart[$key])) { $cart[$key]['quantity'] = $quantity; session()->put('cart', $cart); } return response()->json(['success' => true]); } // ───────────────────────────────────────────────────────── // REMOVE FROM CART (AJAX) // ───────────────────────────────────────────────────────── public function removeFromCart(Request $request) { $key = $request->key; $cart = session()->get('cart', []); if (isset($cart[$key])) { unset($cart[$key]); session()->put('cart', $cart); } return response()->json(['success' => true]); } // ───────────────────────────────────────────────────────── // CHECKOUT GET // ───────────────────────────────────────────────────────── public function checkout(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); if (!session('customer')) { return redirect('/')->with('open_auth', true)->with('after_login_redirect', '/checkout'); } $cart = session()->get('cart', []); if (empty($cart)) { return redirect('/')->with('error', 'Your cart is empty.'); } $currencyData = $this->getCurrencyData($business); [$items, $total] = $this->buildCartItems(); $customerSession = session('customer'); $customer = \App\Customer::find($customerSession['id']); if (!$customer) $customer = $customerSession; return view('business_public.checkout', compact( 'business', 'currencyData', 'items', 'total', 'customer' )); } // ───────────────────────────────────────────────────────── // PLACE ORDER // ───────────────────────────────────────────────────────── public function placeOrder(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); $cart = session()->get('cart', []); if (empty($cart)) { return redirect('/')->with('error', 'Cart is empty.'); } $request->validate([ 'name' => 'required|string|max:255', 'email' => 'nullable|email', 'phone' => 'required|string|max:20', 'address' => 'required|string', 'city' => 'required|string', 'payment_method' => 'required|in:cash,bkash,nagad,card', ]); DB::beginTransaction(); try { $customerSession = session('customer'); $customer = null; if ($customerSession) { $customer = \App\Customer::find($customerSession['id']); } $contact = null; if ($customer && !empty($customer->contact_id)) { $contact = Contact::find($customer->contact_id); } if (!$contact && $customer) { $contact = Contact::where('business_id', $business->id) ->where(function ($q) use ($customer) { $q->where('mobile', $customer->phone)->orWhere('email', $customer->email); })->first(); } if (!$contact) { $contact = Contact::where('business_id', $business->id) ->where('mobile', $request->phone)->first(); } if (!$contact) { $contact = Contact::create([ 'business_id' => $business->id, 'type' => 'customer', 'name' => $request->name, 'email' => $request->email, 'mobile' => $request->phone, 'address' => $request->address, 'city' => $request->city, 'created_by' => 1, 'contact_status' => 'active', ]); if ($customer && empty($customer->contact_id)) { $customer->contact_id = $contact->id; $customer->save(); session()->put('customer.contact_id', $contact->id); } } else { if ($customer && empty($customer->contact_id)) { $customer->contact_id = $contact->id; $customer->save(); session()->put('customer.contact_id', $contact->id); } } $location = BusinessLocation::where('business_id', $business->id)->first(); if (!$location) throw new \Exception('No business location found.'); $total = 0; foreach ($cart as $item) { $total += $item['price'] * $item['quantity']; } $shippingCharge = ($total >= 5000) ? 0 : 120; $finalTotal = $total + $shippingCharge; $transaction = new Transaction(); $transaction->business_id = $business->id; $transaction->location_id = $location->id; $transaction->contact_id = $contact->id; $transaction->type = 'sell'; $transaction->status = 'draft'; $transaction->sub_status = 'online_order'; $transaction->payment_status = 'due'; $transaction->shipping_status = 'ordered'; $transaction->transaction_date = now(); $transaction->total_before_tax = $total; $transaction->final_total = $finalTotal; $transaction->shipping_charges = $shippingCharge; $transaction->created_by = 1; $transaction->source = 'online'; $transaction->save(); foreach ($cart as $item) { $variation = Variation::find($item['variation_id']); if (!$variation) continue; $transaction->sell_lines()->create([ 'product_id' => $item['product_id'], 'variation_id' => $item['variation_id'], 'quantity' => $item['quantity'], 'unit_price_before_discount' => $item['price'], 'unit_price' => $item['price'], 'unit_price_inc_tax' => $item['price'], 'item_tax' => 0, 'tax_id' => null, ]); } DB::commit(); session()->forget('cart'); return redirect('/')->with('success', 'অর্ডার সফল হয়েছে! আপনার অর্ডারটি প্রসেস করা হবে।'); } catch (\Exception $e) { DB::rollBack(); \Log::error('PlaceOrder Error: ' . $e->getMessage()); return back()->with('error', 'দয়া করে আবার চেষ্টা করুন।'); } } // ───────────────────────────────────────────────────────── // PRIVATE HELPER — cart array তৈরি করো // ───────────────────────────────────────────────────────── private function buildCartItems(): array { $cart = session()->get('cart', []); $items = []; $total = 0; foreach ($cart as $key => $item) { $items[] = [ 'key' => $key, 'product_id' => $item['product_id'], 'variation_id' => $item['variation_id'], 'name' => $item['name'], 'quantity' => $item['quantity'], 'price' => $item['price'], 'subtotal' => $item['price'] * $item['quantity'], 'image' => $item['image'] ?? null, ]; $total += $item['price'] * $item['quantity']; } return [$items, $total]; } }?php namespace App\Http\Controllers; use App\Brands; use App\Business; use App\Category; use App\Product; use App\Variation; use App\Contact; use App\Transaction; use App\BusinessLocation; use App\HomepageSetting; use App\Utils\ProductUtil; use App\Utils\TransactionUtil; use Illuminate\Http\Request; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; class BusinessPublicController extends Controller { protected $productUtil; protected $transactionUtil; protected $business; public function __construct(ProductUtil $productUtil, TransactionUtil $transactionUtil) { $this->productUtil = $productUtil; $this->transactionUtil = $transactionUtil; $this->middleware(function ($request, $next) { $business = \App\Business::first(); if ($business) { app()->instance('current_business', $business); $this->business = $business; } return $next($request); }); } // ───────────────────────────────────────────────────────── // HELPER — Business খোঁজো // ───────────────────────────────────────────────────────── private function getBusinessFromRequest(Request $request): ?Business { if ($this->business) return $this->business; return Cache::remember('single_business', 3600, function () { return Business::first(); }); } // ───────────────────────────────────────────────────────── // CURRENCY HELPER — DB থেকে সরাসরি currency data নাও // ───────────────────────────────────────────────────────── private function getCurrencyData(Business $business): array { $currency = Cache::remember( "business_currency_{$business->id}", 3600, fn() => \App\Currency::find($business->currency_id) ); return [ 'symbol' => $currency->symbol ?? 'OMR', 'dec_sep' => $currency->decimal_separator ?? '.', 'thou_sep' => $currency->thousand_separator ?? ',', 'precision' => (int)($business->currency_precision ?? 3), 'placement' => $business->currency_symbol_placement ?? 'before', ]; } // ───────────────────────────────────────────────────────── // HOMEPAGE // ───────────────────────────────────────────────────────── public function index(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); $currencyData = $this->getCurrencyData($business); $posSettings = Cache::remember( "homepage_settings_{$business->id}", 300, fn() => HomepageSetting::getAll($business->id) ); $slides = []; for ($i = 1; $i <= 10; $i++) { if (!empty($posSettings["carousel_image_$i"])) { $slides[] = asset('storage/' . $posSettings["carousel_image_$i"]); } } $location = BusinessLocation::where('business_id', $business->id)->first(); $location_id = $location->id ?? null; $featuredProducts = Product::where('business_id', $business->id) ->where('is_inactive', 0) ->where('not_for_selling', 0) ->where('show_in_online_store', 1) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'unit', 'media', 'brand', 'category' ]) ->latest() ->limit(12) ->get(); $categories = Category::where('business_id', $business->id) ->where('parent_id', 0) ->withCount(['products' => function ($q) { $q->where('show_in_online_store', 1) ->where('is_inactive', 0); }]) ->get(); $brands = Brands::where('business_id', $business->id)->get(); $gamingProducts = Product::where('business_id', $business->id) ->where('is_inactive', 0) ->where('show_in_online_store', 1) ->whereHas('category', function ($q) { $q->where('slug', 'gaming'); }) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'media' ]) ->limit(10) ->get(); $accessProducts = Product::where('business_id', $business->id) ->where('is_inactive', 0) ->where('show_in_online_store', 1) ->whereHas('category', function ($q) { $q->where('slug', 'accessories'); }) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'media' ]) ->limit(10) ->get(); return view('business_public.index', compact( 'business', 'currencyData', 'posSettings', 'slides', 'featuredProducts', 'categories', 'brands', 'gamingProducts', 'accessProducts' )); } // ───────────────────────────────────────────────────────── // PRODUCT LISTING // ───────────────────────────────────────────────────────── public function products(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404); $currencyData = $this->getCurrencyData($business); $location = BusinessLocation::where('business_id', $business->id)->first(); $location_id = $location->id ?? null; $query = Product::where('business_id', $business->id) ->where('is_inactive', 0) ->where('not_for_selling', 0) ->where('show_in_online_store', 1) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'media', 'brand', 'category' ]); if ($request->filled('category')) { $catValue = $request->category; $query->whereHas('category', function ($q) use ($catValue) { $q->where('name', $catValue)->orWhere('slug', $catValue); }); } if ($request->filled('brand')) { $query->where('brand_id', $request->brand); } if ($request->filled('search')) { $search = $request->search; $query->where(function ($q) use ($search) { $q->where('name', 'like', "%$search%")->orWhere('sku', 'like', "%$search%"); }); } // ── Website price দিয়ে min/max filter (আগে ছিল sell_price_inc_tax) ── if ($request->filled('min_price')) { $query->whereHas('variations', function ($q) use ($request) { $q->whereRaw('COALESCE(website_price_inc_tax, sell_price_inc_tax) >= ?', [(float) $request->min_price]); }); } if ($request->filled('max_price')) { $query->whereHas('variations', function ($q) use ($request) { $q->whereRaw('COALESCE(website_price_inc_tax, sell_price_inc_tax) <= ?', [(float) $request->max_price]); }); } // ── শেষ ── $sort = $request->get('sort', 'latest'); switch ($sort) { case 'price_asc': $query->join('variations', 'products.id', '=', 'variations.product_id') ->orderByRaw('COALESCE(variations.website_price_inc_tax, variations.sell_price_inc_tax) asc') ->select('products.*'); break; case 'price_desc': $query->join('variations', 'products.id', '=', 'variations.product_id') ->orderByRaw('COALESCE(variations.website_price_inc_tax, variations.sell_price_inc_tax) desc') ->select('products.*'); break; case 'name_asc': $query->orderBy('name', 'asc'); break; case 'name_desc': $query->orderBy('name', 'desc'); break; default: $query->latest(); break; } $perPage = $request->get('show', 20); $products = $query->paginate($perPage)->appends($request->query()); $categories = Category::where('business_id', $business->id) ->where('parent_id', 0)->withCount('products')->get(); $brands = Brands::where('business_id', $business->id)->get(); return view('business_public.products', compact( 'business', 'currencyData', 'products', 'categories', 'brands' )); } // ───────────────────────────────────────────────────────── // PRODUCT DETAIL // ───────────────────────────────────────────────────────── public function showProduct($id, Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); $currencyData = $this->getCurrencyData($business); $location = BusinessLocation::where('business_id', $business->id)->first(); $location_id = $location->id ?? null; $product = Product::where('business_id', $business->id) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'unit', 'brand', 'category', 'sub_category', 'media', 'variations.media' ]) ->findOrFail($id); $relatedProducts = Product::where('business_id', $business->id) ->where('id', '!=', $id) ->where('category_id', $product->category_id) ->where('is_inactive', 0) ->where('show_in_online_store', 1) ->with([ 'variations', 'variations.variation_location_details' => function ($q) use ($location_id) { if ($location_id) $q->where('location_id', $location_id); }, 'media' ]) ->limit(8) ->get(); return view('business_public.product_detail', compact( 'business', 'currencyData', 'product', 'relatedProducts' )); } // ───────────────────────────────────────────────────────── // CART PAGE // ───────────────────────────────────────────────────────── public function cart(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); $currencyData = $this->getCurrencyData($business); [$items, $total] = $this->buildCartItems(); return view('business_public.cart', compact('business', 'currencyData', 'items', 'total')); } // ───────────────────────────────────────────────────────── // CART ITEMS (AJAX GET) // ───────────────────────────────────────────────────────── public function cartItems() { [$items, $total] = $this->buildCartItems(); return response()->json(['items' => $items, 'total' => $total]); } // ───────────────────────────────────────────────────────── // CART COUNT (AJAX) // ───────────────────────────────────────────────────────── public function getCartCount() { $cart = session()->get('cart', []); $count = array_sum(array_column($cart, 'quantity')); return response()->json(['count' => $count]); } // ───────────────────────────────────────────────────────── // ADD TO CART (AJAX) // ───────────────────────────────────────────────────────── public function addToCart(Request $request) { $productId = $request->product_id; $variationId = $request->variation_id; $quantity = max(1, $request->quantity ?? 1); $product = Product::with('media')->find($productId); $variation = Variation::find($variationId); if (!$product || !$variation) { return response()->json(['success' => false, 'message' => 'Product not found']); } // ── Website price ব্যবহার (আগে ছিল শুধু sell_price_inc_tax) ── $price = (!empty($variation->website_price_inc_tax) && $variation->website_price_inc_tax > 0) ? $variation->website_price_inc_tax : ($variation->sell_price_inc_tax ?? 0); // ── শেষ ── $cart = session()->get('cart', []); $key = $productId . '_' . $variationId; if (!empty($product->image)) { $imagePath = 'uploads/img/' . $product->image; } elseif ($product->media->first()) { $imagePath = 'uploads/media/' . $product->media->first()->file_name; } else { $imagePath = null; } if (isset($cart[$key])) { $cart[$key]['quantity'] += $quantity; } else { $cart[$key] = [ 'product_id' => $productId, 'variation_id' => $variationId, 'name' => $product->name, 'price' => $price, 'quantity' => $quantity, 'image' => $imagePath, ]; } session()->put('cart', $cart); $totalItems = array_sum(array_column($cart, 'quantity')); return response()->json(['success' => true, 'cart_count' => $totalItems, 'message' => 'Added to cart']); } // ───────────────────────────────────────────────────────── // UPDATE CART (AJAX) // ───────────────────────────────────────────────────────── public function updateCart(Request $request) { $key = $request->key; $quantity = max(1, $request->quantity); $cart = session()->get('cart', []); if (isset($cart[$key])) { $cart[$key]['quantity'] = $quantity; session()->put('cart', $cart); } return response()->json(['success' => true]); } // ───────────────────────────────────────────────────────── // REMOVE FROM CART (AJAX) // ───────────────────────────────────────────────────────── public function removeFromCart(Request $request) { $key = $request->key; $cart = session()->get('cart', []); if (isset($cart[$key])) { unset($cart[$key]); session()->put('cart', $cart); } return response()->json(['success' => true]); } // ───────────────────────────────────────────────────────── // CHECKOUT GET // ───────────────────────────────────────────────────────── public function checkout(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); if (!session('customer')) { return redirect('/')->with('open_auth', true)->with('after_login_redirect', '/checkout'); } $cart = session()->get('cart', []); if (empty($cart)) { return redirect('/')->with('error', 'Your cart is empty.'); } $currencyData = $this->getCurrencyData($business); [$items, $total] = $this->buildCartItems(); $customerSession = session('customer'); $customer = \App\Customer::find($customerSession['id']); if (!$customer) $customer = $customerSession; return view('business_public.checkout', compact( 'business', 'currencyData', 'items', 'total', 'customer' )); } // ───────────────────────────────────────────────────────── // PLACE ORDER // ───────────────────────────────────────────────────────── public function placeOrder(Request $request) { $business = $this->getBusinessFromRequest($request); if (!$business) abort(404, 'Business not found.'); $cart = session()->get('cart', []); if (empty($cart)) { return redirect('/')->with('error', 'Cart is empty.'); } $request->validate([ 'name' => 'required|string|max:255', 'email' => 'nullable|email', 'phone' => 'required|string|max:20', 'address' => 'required|string', 'city' => 'required|string', 'payment_method' => 'required|in:cash,bkash,nagad,card', ]); DB::beginTransaction(); try { $customerSession = session('customer'); $customer = null; if ($customerSession) { $customer = \App\Customer::find($customerSession['id']); } $contact = null; if ($customer && !empty($customer->contact_id)) { $contact = Contact::find($customer->contact_id); } if (!$contact && $customer) { $contact = Contact::where('business_id', $business->id) ->where(function ($q) use ($customer) { $q->where('mobile', $customer->phone)->orWhere('email', $customer->email); })->first(); } if (!$contact) { $contact = Contact::where('business_id', $business->id) ->where('mobile', $request->phone)->first(); } if (!$contact) { $contact = Contact::create([ 'business_id' => $business->id, 'type' => 'customer', 'name' => $request->name, 'email' => $request->email, 'mobile' => $request->phone, 'address' => $request->address, 'city' => $request->city, 'created_by' => 1, 'contact_status' => 'active', ]); if ($customer && empty($customer->contact_id)) { $customer->contact_id = $contact->id; $customer->save(); session()->put('customer.contact_id', $contact->id); } } else { if ($customer && empty($customer->contact_id)) { $customer->contact_id = $contact->id; $customer->save(); session()->put('customer.contact_id', $contact->id); } } $location = BusinessLocation::where('business_id', $business->id)->first(); if (!$location) throw new \Exception('No business location found.'); $total = 0; foreach ($cart as $item) { $total += $item['price'] * $item['quantity']; } $shippingCharge = ($total >= 5000) ? 0 : 120; $finalTotal = $total + $shippingCharge; $transaction = new Transaction(); $transaction->business_id = $business->id; $transaction->location_id = $location->id; $transaction->contact_id = $contact->id; $transaction->type = 'sell'; $transaction->status = 'draft'; $transaction->sub_status = 'online_order'; $transaction->payment_status = 'due'; $transaction->shipping_status = 'ordered'; $transaction->transaction_date = now(); $transaction->total_before_tax = $total; $transaction->final_total = $finalTotal; $transaction->shipping_charges = $shippingCharge; $transaction->created_by = 1; $transaction->source = 'online'; $transaction->save(); foreach ($cart as $item) { $variation = Variation::find($item['variation_id']); if (!$variation) continue; $transaction->sell_lines()->create([ 'product_id' => $item['product_id'], 'variation_id' => $item['variation_id'], 'quantity' => $item['quantity'], 'unit_price_before_discount' => $item['price'], 'unit_price' => $item['price'], 'unit_price_inc_tax' => $item['price'], 'item_tax' => 0, 'tax_id' => null, ]); } DB::commit(); session()->forget('cart'); return redirect('/')->with('success', 'অর্ডার সফল হয়েছে! আপনার অর্ডারটি প্রসেস করা হবে।'); } catch (\Exception $e) { DB::rollBack(); \Log::error('PlaceOrder Error: ' . $e->getMessage()); return back()->with('error', 'দয়া করে আবার চেষ্টা করুন।'); } } // ───────────────────────────────────────────────────────── // PRIVATE HELPER — cart array তৈরি করো // ───────────────────────────────────────────────────────── private function buildCartItems(): array { $cart = session()->get('cart', []); $items = []; $total = 0; foreach ($cart as $key => $item) { $items[] = [ 'key' => $key, 'product_id' => $item['product_id'], 'variation_id' => $item['variation_id'], 'name' => $item['name'], 'quantity' => $item['quantity'], 'price' => $item['price'], 'subtotal' => $item['price'] * $item['quantity'], 'image' => $item['image'] ?? null, ]; $total += $item['price'] * $item['quantity']; } return [$items, $total]; } } Target class [App\Http\Controllers\BusinessPublicController] does not exist.