diff --git a/app/Listeners/Menu/ShowInAdmin.php b/app/Listeners/Menu/ShowInAdmin.php index 38f9c8d9..d2c94b1d 100644 --- a/app/Listeners/Menu/ShowInAdmin.php +++ b/app/Listeners/Menu/ShowInAdmin.php @@ -20,9 +20,43 @@ class ShowInAdmin */ public function handle(Event $event): void { - $menu = $event->menu; + $this->renderSections($event->menu, self::groupPayables(SidebarPermissions::navigationFor(user()))); + } - foreach (SidebarPermissions::navigationFor(user()) as $sectionIndex => $section) { + public static function groupPayables(array $navigation): array + { + $keys = ['supplier-payables', 'courier-payables', 'raw-board-payables', + 'lamination-factory-payables', 'paper-materials', 'edge-banding-materials']; + $children = array_values(array_filter($navigation, static fn (array $section): bool => in_array($section['slug'], $keys, true))); + if ($children === []) { + return $navigation; + } + + $result = []; + $inserted = false; + foreach ($navigation as $section) { + if ($section['slug'] === 'payable-items' || in_array($section['slug'], $keys, true)) { + if (! $inserted) { + $result[] = ['slug' => 'payable-items', 'title' => '应付款项', 'icon' => 'payments', 'sections' => $children]; + $inserted = true; + } + continue; + } + $result[] = $section; + } + return $result; + } + + private function renderSections($menu, array $navigation): void + { + + foreach ($navigation as $sectionIndex => $section) { + if (! empty($section['sections'])) { + $menu->dropdown($section['title'], function ($sub) use ($section): void { + $this->renderSections($sub, $section['sections']); + }, ($sectionIndex + 1) * 10, ['title' => $section['title'], 'icon' => $section['icon']]); + continue; + } if (in_array($section['slug'], self::PROTOTYPE_NAV_SECTIONS, true)) { continue; } diff --git a/tests/Unit/View/PayablesGroupTest.php b/tests/Unit/View/PayablesGroupTest.php new file mode 100644 index 00000000..75d0688b --- /dev/null +++ b/tests/Unit/View/PayablesGroupTest.php @@ -0,0 +1,34 @@ +assertSame('store-receivables', $slugs[$index - 1]); + $this->assertCount(6, $grouped[$index]['sections']); + $this->assertContains('inventory-management', $slugs); + foreach ($grouped[$index]['sections'] as $section) { + $this->assertNotContains($section['slug'], $slugs); + $this->assertSame($original[array_search($section['slug'], array_column($original, 'slug'), true)], $section); + } + } + + public function test_only_authorized_sections_are_grouped(): void + { + $allowed = [['slug' => 'courier-payables', 'title' => '快递应付', 'pages' => [['slug' => 'freight-reconciliation']]]]; + $result = ShowInAdmin::groupPayables($allowed); + $this->assertSame($allowed, $result[0]['sections']); + $this->assertSame([], ShowInAdmin::groupPayables([])); + } +}