diff --git a/app/Filament/Central/Resources/CompanyResource/Pages/ViewCompany.php b/app/Filament/Central/Resources/CompanyResource/Pages/ViewCompany.php index 3a7349a..0cc9754 100644 --- a/app/Filament/Central/Resources/CompanyResource/Pages/ViewCompany.php +++ b/app/Filament/Central/Resources/CompanyResource/Pages/ViewCompany.php @@ -27,7 +27,27 @@ class ViewCompany extends Page public function mount(int|string $record): void { - $this->record = Company::with(['plan', 'subscriptions' => fn ($q) => $q->latest('period_end')->limit(10)])->findOrFail($record); + // The {record} route param may arrive as a scalar id, an Eloquent model, + // or (via Livewire's typed-property hydration) a JSON-encoded model. + // Normalize all of these down to the integer primary key. + $key = $this->resolveRecordKey($record); + + $this->record = Company::with(['plan', 'subscriptions' => fn ($q) => $q->latest('period_end')->limit(10)]) + ->findOrFail($key); + } + + private function resolveRecordKey(mixed $record): int|string + { + if ($record instanceof Company) { + return $record->getKey(); + } + if (is_string($record) && str_starts_with(ltrim($record), '{')) { + $decoded = json_decode($record, true); + if (is_array($decoded) && isset($decoded['id'])) { + return $decoded['id']; + } + } + return $record; } public function getTitle(): string @@ -50,8 +70,8 @@ class ViewCompany extends Page 'work_orders_open' => WorkOrder::whereNotIn('status', ['done', 'cancelled'])->count(), 'parts' => Part::count(), 'parts_low_stock' => Part::where('is_active', true) - ->whereColumn('stock', '<=', 'low_stock_threshold') - ->where('stock', '>', 0) + ->whereColumn('qty', '<=', 'min_qty') + ->where('qty', '>', 0) ->count(), 'revenue_this_month' => (float) Payment::whereYear('paid_at', date('Y')) ->whereMonth('paid_at', date('m'))->sum('amount'), diff --git a/tests/Feature/CentralCompanyViewTest.php b/tests/Feature/CentralCompanyViewTest.php new file mode 100644 index 0000000..aa3fc07 --- /dev/null +++ b/tests/Feature/CentralCompanyViewTest.php @@ -0,0 +1,37 @@ + 'Free', 'slug' => 'free', 'price' => 0, 'features' => []]); + $company = Company::create([ + 'plan_id' => $plan->id, 'slug' => 'viewco', 'name' => 'View Co', + 'status' => 'active', + ]); + + $admin = SuperAdmin::create([ + 'name' => 'Op', 'email' => 'op@example.com', + 'password' => bcrypt('secret'), 'is_active' => true, 'role' => 'owner', + ]); + + app(TenantManager::class)->setCurrent(null); + + $response = $this->actingAs($admin, 'central') + ->get("http://service.mir.md/admin/companies/{$company->id}"); + + $response->assertStatus(200); + $response->assertSee('View Co'); + } +}