
PK 
<?php
use App\Models\Message;
use App\Models\Category;
use App\Models\PostTag;
use App\Models\PostCategory;
use App\Models\Order;
use App\Models\Wishlist;
use App\Models\Shipping;
use App\Models\Cart;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Auth;
class Helper
{
public static function messageList()
{
return Message::whereNull('read_at')->orderBy('created_at', 'desc')->get();
}
public static function getAllCategory()
{
$category = new Category();
$menu = $category->getAllParentWithChild();
return $menu;
}
public static function getHeaderCategory()
{
$category = new Category();
$menu = $category->getAllParentWithChild()
->sortByDesc('category_sortby');
if ($menu) {
$output = '';
$defaultThumbnail = asset('backend/img/thumbnail-default.jpg');
foreach ($menu as $cat_info) {
if ($cat_info->child_cat->count() > 0) {
// Sort child categories by category_sortby before processing
$child_cats = $cat_info->child_cat->sortByDesc('category_sortby');
$output .= '<li class="dropdown menu-large nav-item">';
$output .= '<a href="'.route('product-cat', $cat_info->slug).'" class="dropdown-toggle nav-link" data-toggle="dropdown">'.$cat_info->title.'</a>';
$output .= '<ul class="dropdown-menu megamenu">';
$output .= '<li class="dropdown-item">';
$output .= '<div class="row">';
foreach ($child_cats as $sub_menu) {
$imageUrl = !empty($sub_menu->photo) ? $sub_menu->photo : $defaultThumbnail;
$output .= '<div class="col-6 col-md-6 col-lg-2 p-1 text-center">';
$output .= '<div class="card menucard">';
$output .= '<a href="'.route('product-sub-cat', [$cat_info->slug, $sub_menu->slug]).'" class="thumbnail">';
$output .= '<img src="'.$imageUrl.'" alt="'.$sub_menu->title.'">';
$output .= '<p>'.$sub_menu->title.'</p>';
$output .= '</a>';
$output .= '</div>';
$output .= '</div>';
}
$output .= '</div>';
$output .= '</li>';
$output .= '</ul>';
$output .= '</li>';
} else {
$output .= '<li class="nav-item">';
$output .= '<a href="'.route('product-cat', $cat_info->slug).'" class="nav-link">'.$cat_info->title.'</a>';
$output .= '</li>';
}
}
return $output;
}
}
public static function productCategoryList($option = 'all')
{
if ($option == 'all') {
return Category::orderBy('id', 'DESC')->get();
}
return Category::has('products')->orderBy('id', 'DESC')->get();
}
public static function postTagList($option = 'all')
{
if ($option == 'all') {
return PostTag::orderBy('id', 'desc')->get();
}
return PostTag::has('posts')->orderBy('id', 'desc')->get();
}
public static function postCategoryList($option = "all")
{
if ($option == 'all') {
return PostCategory::orderBy('id', 'DESC')->get();
}
return PostCategory::has('posts')->orderBy('id', 'DESC')->get();
}
// Cart Count
public static function cartCount($user_id = '')
{
if (Auth::check()) {
if ($user_id == "") $user_id = auth()->user()->id;
return Cart::where('user_id', $user_id)->where('order_id', null)->sum('quantity');
} else {
return 0;
}
}
// relationship cart with product
public function product()
{
return $this->hasOne('App\Models\Product', 'id', 'product_id');
}
public static function getAllProductFromCart($user_id = '')
{
if (Auth::check()) {
if ($user_id == "") $user_id = auth()->user()->id;
return Cart::with('product')->where('user_id', $user_id)->where('order_id', null)->get();
} else {
return 0;
}
}
// Total amount cart
public static function totalCartPrice($user_id = '')
{
if (Auth::check()) {
if ($user_id == "") $user_id = auth()->user()->id;
return Cart::where('user_id', $user_id)->where('order_id', null)->sum('amount');
} else {
return 0;
}
}
// Wishlist Count
public static function wishlistCount($user_id = '')
{
if (Auth::check()) {
if ($user_id == "") $user_id = auth()->user()->id;
return Wishlist::where('user_id', $user_id)->where('cart_id', null)->sum('quantity');
} else {
return 0;
}
}
public static function getAllProductFromWishlist($user_id = '')
{
if (Auth::check()) {
if ($user_id == "") $user_id = auth()->user()->id;
return Wishlist::with('product')->where('user_id', $user_id)->where('cart_id', null)->get();
} else {
return 0;
}
}
public static function totalWishlistPrice($user_id = '')
{
if (Auth::check()) {
if ($user_id == "") $user_id = auth()->user()->id;
return Wishlist::where('user_id', $user_id)->where('cart_id', null)->sum('amount');
} else {
return 0;
}
}
// Total price with shipping and coupon
public static function grandPrice($id, $user_id)
{
$order = Order::find($id);
if ($order) {
$shipping_price = (float)$order->shipping->price;
$order_price = self::orderPrice($id, $user_id);
return number_format((float)($order_price + $shipping_price), 2, '.', '');
} else {
return 0;
}
}
// Admin home
public static function earningPerMonth()
{
$month_data = Order::where('status', 'delivered')->get();
$price = 0;
foreach ($month_data as $data) {
$price = $data->cart_info->sum('price');
}
return number_format((float)($price), 2, '.', '');
}
public static function shipping()
{
return Shipping::orderBy('id', 'DESC')->get();
}
}
if (!function_exists('generateUniqueSlug')) {
function generateUniqueSlug($title, $modelClass)
{
$slug = Str::slug($title);
$count = $modelClass::where('slug', $slug)->count();
if ($count > 0) {
$slug = $slug . '-' . date('ymdis') . '-' . rand(0, 999);
}
return $slug;
}
}
?>


PK 99