Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\Customer;
use App\Models\Product;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

class HomeController extends Controller
{
Expand All @@ -32,29 +33,28 @@ public function index()
$low_stock_products = Product::where('quantity', '<', 10)->get();

$bestSellingProducts = DB::table('products')
->select('products.*', DB::raw('SUM(order_items.quantity) AS total_sold'))
->select('products.id', 'products.name', 'products.price', 'products.quantity', DB::raw('SUM(order_items.quantity) AS total_sold'))
->join('order_items', 'order_items.product_id', '=', 'products.id')
->join('orders', 'orders.id', '=', 'order_items.order_id')
->groupBy('products.id')
->groupBy('products.id', 'products.name', 'products.price', 'products.quantity')
->havingRaw('SUM(order_items.quantity) > 10')
->get();

$currentMonthBestSelling = DB::table('products')
->select('products.*', DB::raw('SUM(order_items.quantity) AS total_sold'))
->select('products.id', 'products.name', 'products.price', 'products.quantity', DB::raw('SUM(order_items.quantity) AS total_sold'))
->join('order_items', 'order_items.product_id', '=', 'products.id')
->join('orders', 'orders.id', '=', 'order_items.order_id')
->whereYear('orders.created_at', date('Y'))
->whereMonth('orders.created_at', date('m'))
->groupBy('products.id')
->groupBy('products.id', 'products.name', 'products.price', 'products.quantity')
->havingRaw('SUM(order_items.quantity) > 500') // Best-selling threshold for the current month
->get();

$pastSixMonthsHotProducts = DB::table('products')
->select('products.*', DB::raw('SUM(order_items.quantity) AS total_sold'))
->select('products.id', 'products.name', 'products.price', 'products.quantity', 'products.created_at', 'products.updated_at', DB::raw('SUM(order_items.quantity) AS total_sold'))
->join('order_items', 'order_items.product_id', '=', 'products.id')
->join('orders', 'orders.id', '=', 'order_items.order_id')
->where('orders.created_at', '>=', now()->subMonths(6)) // Filter for the past 6 months
->groupBy('products.id')
->groupBy('products.id', 'products.name', 'products.price', 'products.quantity', 'products.created_at', 'products.updated_at')
->havingRaw('SUM(order_items.quantity) > 1000') // Hot product threshold for past 6 months
->get();

Expand Down