@extends('layouts.main')
@section('title', 'Select Branch')
@section('content')
{{-- Page Header --}}
{{-- Month Filter Bar --}}
{{-- Month Filter Bar --}}
{{-- Branch Cards --}}
@forelse($branches as $branch)
@php
// Calculate metrics for this branch based on selected month range
$filterYearFrom = $fromMonth ? substr($fromMonth, 0, 4) : null;
$filterMonthFrom = $fromMonth ? substr($fromMonth, 5, 2) : null;
$filterYearTo = $toMonth ? substr($toMonth, 0, 4) : null;
$filterMonthTo = $toMonth ? substr($toMonth, 5, 2) : null;
// Total Sales for the filtered month range
$totalSales = 0;
if ($fromMonth && $toMonth) {
$totalSales = \App\Models\DailySale::where('branch_id', $branch->id)
->whereBetween('sale_date', [
\Carbon\Carbon::parse($fromMonth . '-01')->startOfMonth(),
\Carbon\Carbon::parse($toMonth . '-01')->endOfMonth()
])
->sum('sales_amount');
} elseif ($fromMonth) {
$totalSales = \App\Models\DailySale::where('branch_id', $branch->id)
->whereYear('sale_date', $filterYearFrom)
->whereMonth('sale_date', $filterMonthFrom)
->sum('sales_amount');
} elseif ($toMonth) {
$totalSales = \App\Models\DailySale::where('branch_id', $branch->id)
->whereYear('sale_date', $filterYearTo)
->whereMonth('sale_date', $filterMonthTo)
->sum('sales_amount');
} else {
// If no filters, show current month sales
$totalSales = \App\Models\DailySale::where('branch_id', $branch->id)
->whereYear('sale_date', now()->year)
->whereMonth('sale_date', now()->month)
->sum('sales_amount');
}
// Profit Share for the filtered month range
$profitShare = 0;
if ($fromMonth && $toMonth) {
$profitShare = \App\Models\MonthlyProfit::where('branch_id', $branch->id)
->whereBetween('financial_month', [
$fromMonth . '-01',
$toMonth . '-01'
])
->sum('net_profit_amount') ?? 0;
} elseif ($fromMonth) {
$profitShare = \App\Models\MonthlyProfit::where('branch_id', $branch->id)
->where('financial_month', $fromMonth . '-01')
->value('net_profit_amount') ?? 0;
} elseif ($toMonth) {
$profitShare = \App\Models\MonthlyProfit::where('branch_id', $branch->id)
->where('financial_month', $toMonth . '-01')
->value('net_profit_amount') ?? 0;
} else {
$profitShare = \App\Models\MonthlyProfit::where('branch_id', $branch->id)
->where('financial_month', now()->format('Y-m-01'))
->value('net_profit_amount') ?? 0;
}
// Food Cost Percentage for the filtered month range
$foodCostPercentage = 0;
if ($fromMonth && $toMonth) {
$foodCosts = \App\Models\FoodCost::where('branch_id', $branch->id)
->whereBetween('month_year', [$fromMonth, $toMonth])
->pluck('food_cost_percentage');
if ($foodCosts->isNotEmpty()) {
$foodCostPercentage = $foodCosts->avg();
}
} elseif ($fromMonth) {
$foodCostPercentage = \App\Models\FoodCost::where('branch_id', $branch->id)
->where('month_year', $fromMonth)
->value('food_cost_percentage') ?? 0;
} elseif ($toMonth) {
$foodCostPercentage = \App\Models\FoodCost::where('branch_id', $branch->id)
->where('month_year', $toMonth)
->value('food_cost_percentage') ?? 0;
} else {
$foodCostPercentage = \App\Models\FoodCost::where('branch_id', $branch->id)
->where('month_year', now()->format('Y-m'))
->value('food_cost_percentage') ?? 0;
}
// Display range text
if ($fromMonth && $toMonth) {
$displayMonth = \Carbon\Carbon::parse($fromMonth . '-01')->format('M Y') . ' - ' . \Carbon\Carbon::parse($toMonth . '-01')->format('M Y');
} elseif ($fromMonth) {
$displayMonth = \Carbon\Carbon::parse($fromMonth . '-01')->format('M Y');
} elseif ($toMonth) {
$displayMonth = \Carbon\Carbon::parse($toMonth . '-01')->format('M Y');
} else {
$displayMonth = now()->format('M Y');
}
@endphp
@if(isset($branch->equity_percentage) && $branch->equity_percentage)
{{ $branch->equity_percentage }}% Equity
@endif
{{ $branch->branch_name }}
{{ $branch->branch_code }}
{{-- New Metrics Section: Total Sales, Profit Share, Food Cost --}}
{{-- New Metrics Section: Total Sales, Profit Share, Food Cost --}}
Total Sales ({{ $displayMonth }})
${{ number_format($totalSales, 2) }}
Profit Share ({{ $displayMonth }})
@if($profitShare > 0)
${{ number_format($profitShare, 2) }}
@else
--
@endif
Food Cost ({{ $displayMonth }})
@if($foodCostPercentage > 0)
{{ number_format($foodCostPercentage, 1) }}%
@else
--
@endif
@empty
{{-- No Results Message --}}
@endforelse
{{-- Superadmin Stats Section --}}
@if(Auth::user()->hasRole('superadmin') && $branches->count() > 0)
Total Branches
{{ $branches->count() }}
Across {{ $branches->pluck('city')->unique()->count() }} cities
Active Branches
{{ $branches->where('status', 'active')->count() }}
{{ $branches->count() > 0 ? round(($branches->where('status','active')->count() / $branches->count()) * 100) : 0 }}% of total
Total Staff
{{ $totalStaff }}
Across all branches
@endif
@endsection