83 lines
2.3 KiB
PHP
83 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use App\Models\Menu;
|
|
use App\Models\Role;
|
|
use App\Models\Privilege;
|
|
|
|
class SetupStockAuditMenu extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'setup:stock-audit-menu';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Setup Stock Audit menu and privileges';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
$this->info('Setting up Stock Audit menu...');
|
|
|
|
// Check if menu already exists
|
|
$existingMenu = Menu::where('link', 'stock-audit.index')->first();
|
|
|
|
if ($existingMenu) {
|
|
$this->warn('Stock Audit menu already exists!');
|
|
return 0;
|
|
}
|
|
|
|
// Create Stock Audit menu
|
|
$menu = Menu::create([
|
|
'name' => 'Audit Histori Stock',
|
|
'link' => 'stock-audit.index',
|
|
'created_at' => now(),
|
|
'updated_at' => now()
|
|
]);
|
|
|
|
$this->info('Stock Audit menu created with ID: ' . $menu->id);
|
|
|
|
// Give all roles access to this menu
|
|
$roles = Role::all();
|
|
$privilegeCount = 0;
|
|
|
|
foreach($roles as $role) {
|
|
// Check if privilege already exists
|
|
$existingPrivilege = Privilege::where('role_id', $role->id)
|
|
->where('menu_id', $menu->id)
|
|
->first();
|
|
|
|
if (!$existingPrivilege) {
|
|
Privilege::create([
|
|
'role_id' => $role->id,
|
|
'menu_id' => $menu->id,
|
|
'create' => 0, // Stock audit is view-only
|
|
'update' => 0, // Stock audit is view-only
|
|
'delete' => 0, // Stock audit is view-only
|
|
'view' => 1, // Allow viewing
|
|
'created_at' => now(),
|
|
'updated_at' => now()
|
|
]);
|
|
$privilegeCount++;
|
|
}
|
|
}
|
|
|
|
$this->info("Created {$privilegeCount} privileges for Stock Audit menu.");
|
|
$this->info('Stock Audit menu setup completed successfully!');
|
|
|
|
return 0;
|
|
}
|
|
}
|