@extends('admin.master') @section('content') @section('title','CORE BUSINESS ERP Dashboard - Full System') @php /* ========================================================= 100% ORIGINAL PHP LOGIC (DO NOT CHANGE ANYTHING HERE) ========================================================= */ $todayDate = date('Y-m-d'); $topCustomers = []; try { $topCustomers = \Illuminate\Support\Facades\DB::table('sales') ->join('customers', 'sales.customer_id', '=', 'customers.id') ->select('customers.name', \Illuminate\Support\Facades\DB::raw('SUM(CAST(sales.total_amount AS DECIMAL(10,2))) as total_spent')) ->groupBy('customers.id', 'customers.name')->orderBy('total_spent', 'desc')->limit(5)->get(); } catch(\Exception $e) {} $topProducts = []; try { $topProducts = \Illuminate\Support\Facades\DB::table('sale_details') ->join('products', 'sale_details.product_id', '=', 'products.id') ->select('products.name', \Illuminate\Support\Facades\DB::raw('SUM(sale_details.quantity) as total_sold')) ->groupBy('products.id', 'products.name')->orderBy('total_sold', 'desc')->limit(5)->get(); } catch(\Exception $e) {} $topCategories = []; try { $catCol = \Illuminate\Support\Facades\Schema::hasColumn('category', 'category_name') ? 'category.category_name' : 'category.name'; $topCategories = \Illuminate\Support\Facades\DB::table('sale_details') ->join('products', 'sale_details.product_id', '=', 'products.id') ->join('category', 'products.category_id', '=', 'category.id') ->select($catCol.' as cat_name', \Illuminate\Support\Facades\DB::raw('SUM(sale_details.quantity) as total_sold')) ->groupBy('category.id', $catCol)->orderBy('total_sold', 'desc')->limit(5)->get(); } catch(\Exception $e) {} $topSalesPersons = []; try { $topSalesPersons = \Illuminate\Support\Facades\DB::table('sales') ->join('user', 'sales.created_by', '=', 'user.user_id') ->select('user.full_name', \Illuminate\Support\Facades\DB::raw('SUM(CAST(sales.total_amount AS DECIMAL(10,2))) as total_generated')) ->groupBy('user.user_id', 'user.full_name')->orderBy('total_generated', 'desc')->limit(5)->get(); } catch(\Exception $e) {} $todayNetProfit = 0; try { $todayCogs = \Illuminate\Support\Facades\DB::table('sale_details') ->join('sales', 'sale_details.sale_id', '=', 'sales.id') ->whereDate('sales.created_at', $todayDate) ->sum(\Illuminate\Support\Facades\DB::raw('sale_details.quantity * CAST(sale_details.product_cost AS DECIMAL(10,2))')); $totalSalesToday = ($todayTotalPosSale ?? 0) + ($todayTotalEcomSale ?? 0); $todayNetProfit = $totalSalesToday - $todayCogs - ($todayTotalExpense ?? 0); } catch(\Exception $e) { $todayNetProfit = 0; } $supplierPayables = 0; try { if(\Illuminate\Support\Facades\Schema::hasColumn('suppliers', 'due')) { $supplierPayables = \Illuminate\Support\Facades\DB::table('suppliers')->sum('due'); } } catch(\Exception $e) {} $deadStockCount = 0; try { $threeMonthsAgo = date('Y-m-d', strtotime('-3 months')); $deadStockCount = \Illuminate\Support\Facades\DB::table('products')->whereDate('updated_at', '<', $threeMonthsAgo)->count(); } catch(\Exception $e) {} $recentTransactions = []; try { $recentTransactions = \Illuminate\Support\Facades\DB::table('sales') ->leftJoin('customers', 'sales.customer_id', '=', 'customers.id') ->select('sales.invoice_code', 'customers.name as customer_name', 'sales.total_amount', 'sales.payment_status', 'sales.created_at') ->orderBy('sales.id', 'desc')->limit(5)->get(); } catch(\Exception $e) {} $todayInvoices = 0; try { $todayInvoices = \Illuminate\Support\Facades\DB::table('sales')->whereDate('created_at', $todayDate)->count(); } catch(\Exception $e) {} $lowStockAlert = 0; try { $lowStockAlert = \Illuminate\Support\Facades\DB::table('products')->where('alert_qty', '>', \Illuminate\Support\Facades\DB::raw('0'))->count(); } catch(\Exception $e) {} $todayReturns = 0; try { $todayReturns = \Illuminate\Support\Facades\DB::table('sale_return')->whereDate('created_at', $todayDate)->count(); } catch(\Exception $e) {} $totalStock = 0; try { $totalStock = \Illuminate\Support\Facades\DB::table('product_stock')->sum('quantity'); } catch(\Exception $e) {} @endphp