整理应付款项导航分组 #158

Merged
renshen merged 1 commits from feature/renshen-payables-navigation-group into main 2026-09-11 11:47:05 +08:00
2 changed files with 70 additions and 2 deletions

View File

@ -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;
}

View File

@ -0,0 +1,34 @@
<?php
namespace Tests\Unit\View;
use App\Listeners\Menu\ShowInAdmin;
use PHPUnit\Framework\TestCase;
require_once dirname(__DIR__, 3) . '/app/Listeners/Menu/ShowInAdmin.php';
class PayablesGroupTest extends TestCase
{
public function test_group_preserves_sections_and_does_not_include_inventory(): void
{
$original = require dirname(__DIR__, 3) . '/config/business_navigation.php';
$grouped = ShowInAdmin::groupPayables($original);
$slugs = array_column($grouped, 'slug');
$index = array_search('payable-items', $slugs, true);
$this->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([]));
}
}