fix: exclude soft-deleted records and validate relations in analytics queries

This commit is contained in:
Yoga Pangestu 2026-05-02 18:07:24 +07:00
parent f8529b2459
commit 30fcfb4f20
2 changed files with 23 additions and 1 deletions

View File

@ -43,9 +43,10 @@ public function __invoke(Request $request)
'cogs' => $this->getStats(fn () => $applyFilter(Order::query())->sum('cogs')), 'cogs' => $this->getStats(fn () => $applyFilter(Order::query())->sum('cogs')),
'total_discount' => $this->getStats(fn () => $applyFilter(Order::query())->sum('discount')), 'total_discount' => $this->getStats(fn () => $applyFilter(Order::query())->sum('discount')),
'total_purchases' => $this->getStats(fn () => $applyFilter(Purchase::query())->sum('total')), 'total_purchases' => $this->getStats(fn () => $applyFilter(Purchase::query())->sum('total')),
'products_sold' => $this->getStats(fn () => $applyFilter(OrderItem::query())->sum('qty')), 'products_sold' => $this->getStats(fn () => $applyFilter(OrderItem::query())->whereHas('order')->sum('qty')),
'total_customers' => $this->getStats(fn () => $applyFilter(Order::query())->whereNotNull('customer_name')->distinct('customer_name')->count('customer_name')), 'total_customers' => $this->getStats(fn () => $applyFilter(Order::query())->whereNotNull('customer_name')->distinct('customer_name')->count('customer_name')),
'repeat_customers' => $this->getStats(fn () => $applyFilter(DB::table('orders')) 'repeat_customers' => $this->getStats(fn () => $applyFilter(DB::table('orders'))
->whereNull('deleted_at')
->whereNotNull('customer_name') ->whereNotNull('customer_name')
->select('customer_name') ->select('customer_name')
->groupBy('customer_name') ->groupBy('customer_name')
@ -76,6 +77,7 @@ public function __invoke(Request $request)
// Revenue vs Purchases per month // Revenue vs Purchases per month
$revenueByMonth = $applyFilter(DB::table('orders')) $revenueByMonth = $applyFilter(DB::table('orders'))
->whereNull('deleted_at')
->select( ->select(
DB::raw("$monthSelect as month"), DB::raw("$monthSelect as month"),
DB::raw('SUM(total) as total') DB::raw('SUM(total) as total')
@ -84,6 +86,7 @@ public function __invoke(Request $request)
->get(); ->get();
$purchasesByMonth = $applyFilter(DB::table('purchases')) $purchasesByMonth = $applyFilter(DB::table('purchases'))
->whereNull('deleted_at')
->select( ->select(
DB::raw("$monthSelect as month"), DB::raw("$monthSelect as month"),
DB::raw('SUM(total) as total') DB::raw('SUM(total) as total')
@ -105,6 +108,7 @@ public function __invoke(Request $request)
}); });
$paymentMethods = $applyFilter(DB::table('orders')) $paymentMethods = $applyFilter(DB::table('orders'))
->whereNull('deleted_at')
->select('payment_method as name', DB::raw('COUNT(*) as total')) ->select('payment_method as name', DB::raw('COUNT(*) as total'))
->groupBy('payment_method') ->groupBy('payment_method')
->get() ->get()
@ -117,6 +121,7 @@ public function __invoke(Request $request)
}); });
$orderStatuses = $applyFilter(DB::table('orders')) $orderStatuses = $applyFilter(DB::table('orders'))
->whereNull('deleted_at')
->select('order_status as name', DB::raw('COUNT(*) as total')) ->select('order_status as name', DB::raw('COUNT(*) as total'))
->groupBy('order_status') ->groupBy('order_status')
->get() ->get()
@ -129,6 +134,7 @@ public function __invoke(Request $request)
}); });
$orderChannels = $applyFilter(DB::table('orders')) $orderChannels = $applyFilter(DB::table('orders'))
->whereNull('deleted_at')
->select('order_channel as name', DB::raw('COUNT(*) as total')) ->select('order_channel as name', DB::raw('COUNT(*) as total'))
->groupBy('order_channel') ->groupBy('order_channel')
->get() ->get()
@ -142,13 +148,17 @@ public function __invoke(Request $request)
$topProducts = $applyFilter(DB::table('order_items'), 'order_items.created_at') $topProducts = $applyFilter(DB::table('order_items'), 'order_items.created_at')
->join('products', 'order_items.product_id', '=', 'products.id') ->join('products', 'order_items.product_id', '=', 'products.id')
->join('orders', 'order_items.order_id', '=', 'orders.id')
->select('products.name as name', DB::raw('SUM(order_items.qty) as total')) ->select('products.name as name', DB::raw('SUM(order_items.qty) as total'))
->whereNull('orders.deleted_at')
->whereNull('order_items.deleted_at')
->groupBy('products.name') ->groupBy('products.name')
->orderByDesc('total') ->orderByDesc('total')
->limit(5) ->limit(5)
->get(); ->get();
$topCustomers = $applyFilter(DB::table('orders'), 'orders.created_at') $topCustomers = $applyFilter(DB::table('orders'), 'orders.created_at')
->whereNull('orders.deleted_at')
->select('customer_name as name', DB::raw('SUM(total) as total')) ->select('customer_name as name', DB::raw('SUM(total) as total'))
->whereNotNull('customer_name') ->whereNotNull('customer_name')
->groupBy('customer_name') ->groupBy('customer_name')
@ -159,6 +169,7 @@ public function __invoke(Request $request)
$hourSelect = $isSqlite ? "CAST(strftime('%H', created_at) AS INTEGER)" : 'HOUR(created_at)'; $hourSelect = $isSqlite ? "CAST(strftime('%H', created_at) AS INTEGER)" : 'HOUR(created_at)';
$salesByHourRaw = $applyFilter(DB::table('orders')) $salesByHourRaw = $applyFilter(DB::table('orders'))
->whereNull('deleted_at')
->select( ->select(
DB::raw("$hourSelect as hour"), DB::raw("$hourSelect as hour"),
DB::raw('COUNT(*) as total') DB::raw('COUNT(*) as total')
@ -177,6 +188,7 @@ public function __invoke(Request $request)
// Revenue & Profit & Volume per month // Revenue & Profit & Volume per month
$ordersByMonth = $applyFilter(DB::table('orders')) $ordersByMonth = $applyFilter(DB::table('orders'))
->whereNull('deleted_at')
->select( ->select(
DB::raw("$monthSelect as month"), DB::raw("$monthSelect as month"),
DB::raw('SUM(total) as revenue'), DB::raw('SUM(total) as revenue'),
@ -239,7 +251,9 @@ public function __invoke(Request $request)
->join('products', 'order_items.product_id', '=', 'products.id') ->join('products', 'order_items.product_id', '=', 'products.id')
->join('category_product', 'products.id', '=', 'category_product.product_id') ->join('category_product', 'products.id', '=', 'category_product.product_id')
->join('categories', 'category_product.category_id', '=', 'categories.id') ->join('categories', 'category_product.category_id', '=', 'categories.id')
->join('orders', 'order_items.order_id', '=', 'orders.id')
->select('categories.name', DB::raw('SUM(order_items.qty) as total')) ->select('categories.name', DB::raw('SUM(order_items.qty) as total'))
->whereNull('orders.deleted_at')
->whereNull('order_items.deleted_at') ->whereNull('order_items.deleted_at')
->groupBy('categories.id', 'categories.name') ->groupBy('categories.id', 'categories.name')
->orderByDesc('total') ->orderByDesc('total')

View File

@ -42,6 +42,7 @@ public function __invoke()
// Get sales by hour // Get sales by hour
$todayOrders = DB::table('orders') $todayOrders = DB::table('orders')
->whereNull('deleted_at')
->whereDate('created_at', $today) ->whereDate('created_at', $today)
->select( ->select(
DB::raw("$hourSelect as hour"), DB::raw("$hourSelect as hour"),
@ -51,6 +52,7 @@ public function __invoke()
->get(); ->get();
$yesterdayOrders = DB::table('orders') $yesterdayOrders = DB::table('orders')
->whereNull('deleted_at')
->whereDate('created_at', $yesterday) ->whereDate('created_at', $yesterday)
->select( ->select(
DB::raw("$hourSelect as hour"), DB::raw("$hourSelect as hour"),
@ -71,6 +73,7 @@ public function __invoke()
}); });
$paymentMethods = DB::table('orders') $paymentMethods = DB::table('orders')
->whereNull('deleted_at')
->select('payment_method as name', DB::raw('COUNT(*) as total')) ->select('payment_method as name', DB::raw('COUNT(*) as total'))
->whereDate('created_at', $today) ->whereDate('created_at', $today)
->groupBy('payment_method') ->groupBy('payment_method')
@ -84,6 +87,7 @@ public function __invoke()
}); });
$orderStatuses = DB::table('orders') $orderStatuses = DB::table('orders')
->whereNull('deleted_at')
->select('order_status as name', DB::raw('COUNT(*) as total')) ->select('order_status as name', DB::raw('COUNT(*) as total'))
->whereDate('created_at', $today) ->whereDate('created_at', $today)
->groupBy('order_status') ->groupBy('order_status')
@ -97,6 +101,7 @@ public function __invoke()
}); });
$orderChannels = DB::table('orders') $orderChannels = DB::table('orders')
->whereNull('deleted_at')
->select('order_channel as name', DB::raw('COUNT(*) as total')) ->select('order_channel as name', DB::raw('COUNT(*) as total'))
->whereDate('created_at', $today) ->whereDate('created_at', $today)
->groupBy('order_channel') ->groupBy('order_channel')
@ -113,6 +118,8 @@ public function __invoke()
->join('products', 'order_items.product_id', '=', 'products.id') ->join('products', 'order_items.product_id', '=', 'products.id')
->join('orders', 'order_items.order_id', '=', 'orders.id') ->join('orders', 'order_items.order_id', '=', 'orders.id')
->select('products.name as name', DB::raw('SUM(order_items.qty) as total')) ->select('products.name as name', DB::raw('SUM(order_items.qty) as total'))
->whereNull('orders.deleted_at')
->whereNull('order_items.deleted_at')
->whereDate('orders.created_at', $today) ->whereDate('orders.created_at', $today)
->groupBy('products.name') ->groupBy('products.name')
->orderByDesc('total') ->orderByDesc('total')
@ -120,6 +127,7 @@ public function __invoke()
->get(); ->get();
$topCustomers = DB::table('orders') $topCustomers = DB::table('orders')
->whereNull('deleted_at')
->select('customer_name as name', DB::raw('SUM(total) as total')) ->select('customer_name as name', DB::raw('SUM(total) as total'))
->whereDate('created_at', $today) ->whereDate('created_at', $today)
->whereNotNull('customer_name') ->whereNotNull('customer_name')