Summary of completed work: - Complete buyer portal (browse, cart, checkout, orders, invoices) - Complete seller portal (orders, manifests, fleet, picking) - Business onboarding wizards (buyer 4-step, seller 5-step) - Email verification and registration flows - Notification system for buyers and sellers - Payment term surcharges and pickup/delivery workflows - Filament admin resources (Business, Brand, Product, Order, Invoice, User) - 51 migrations executed successfully Renamed Company -> Business throughout codebase for consistency. Unified authentication flows with password reset. Added audit trail and telescope for debugging. Next: Week 4 Data Migration (Days 22-28) - Product migration (883 products from cannabrands_crm) - Company migration (81 buyer companies) - User migration (preserve password hashes) - Order history migration
101 lines
3.4 KiB
PHP
101 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Invoice;
|
|
use App\Models\Order;
|
|
use App\Services\CartService;
|
|
use Illuminate\Http\Request;
|
|
|
|
class BuyerDashboardController extends Controller
|
|
{
|
|
/**
|
|
* Display the buyer marketplace dashboard
|
|
*/
|
|
public function index(CartService $cartService)
|
|
{
|
|
$user = auth()->user();
|
|
|
|
// Get user's primary business
|
|
$business = $user->primaryBusiness();
|
|
|
|
// Check if user needs to complete onboarding
|
|
$needsOnboarding = !$user->business_onboarding_completed
|
|
|| ($business && in_array($business->status, ['not_started', 'in_progress', 'rejected']));
|
|
|
|
// Check if application is pending approval
|
|
$isPending = $business && $business->status === 'submitted';
|
|
|
|
// Check if application was rejected
|
|
$isRejected = $business && $business->status === 'rejected';
|
|
|
|
// Get user's businesses
|
|
$userBusinessIds = $user->businesses->pluck('id')->toArray();
|
|
|
|
// Get orders
|
|
$orders = Order::with(['items', 'business', 'invoice'])
|
|
->where(function ($query) use ($user, $userBusinessIds) {
|
|
$query->where('user_id', $user->id)
|
|
->orWhereIn('business_id', $userBusinessIds);
|
|
})
|
|
->latest()
|
|
->get();
|
|
|
|
// Get invoices
|
|
$invoices = Invoice::with(['order', 'business'])
|
|
->whereHas('order', function ($query) use ($user, $userBusinessIds) {
|
|
$query->where('user_id', $user->id)
|
|
->orWhereIn('business_id', $userBusinessIds);
|
|
})
|
|
->latest()
|
|
->get();
|
|
|
|
// Calculate statistics
|
|
$stats = [
|
|
// Order stats
|
|
'active_orders' => $orders->whereIn('status', ['new', 'accepted', 'in_progress', 'ready_for_invoice'])->count(),
|
|
'in_delivery' => $orders->whereIn('status', ['ready_for_delivery', 'ready_for_manifest'])->count(),
|
|
'total_orders' => $orders->count(),
|
|
|
|
// Invoice stats
|
|
'pending_invoices' => $invoices->whereIn('payment_status', ['unpaid', 'partially_paid'])->count(),
|
|
'overdue_invoices' => $invoices->filter(fn($inv) => $inv->isOverdue())->count(),
|
|
'total_unpaid' => $invoices->where('payment_status', '!=', 'paid')->sum('amount_due'),
|
|
|
|
// Cart stats
|
|
'cart_items' => $cartService->getItemCount(),
|
|
];
|
|
|
|
// Recent orders (last 5)
|
|
$recentOrders = $orders->take(5);
|
|
|
|
// Upcoming deliveries
|
|
$upcomingDeliveries = $orders->whereIn('status', ['ready_for_delivery', 'ready_for_manifest'])
|
|
->sortBy('created_at')
|
|
->take(5);
|
|
|
|
// Pending invoices (unpaid or partially paid)
|
|
$pendingInvoices = $invoices->whereIn('payment_status', ['unpaid', 'partially_paid'])
|
|
->sortBy('due_date')
|
|
->take(5);
|
|
|
|
// Overdue invoices
|
|
$overdueInvoices = $invoices->filter(fn($inv) => $inv->isOverdue())
|
|
->sortBy('due_date')
|
|
->take(5);
|
|
|
|
return view('buyer.dashboard', compact(
|
|
'user',
|
|
'needsOnboarding',
|
|
'isPending',
|
|
'isRejected',
|
|
'business',
|
|
'stats',
|
|
'recentOrders',
|
|
'upcomingDeliveries',
|
|
'pendingInvoices',
|
|
'overdueInvoices'
|
|
));
|
|
}
|
|
}
|