add budgets

This commit is contained in:
Jagad R R
2025-01-07 17:23:42 +07:00
parent 8c62fb6a19
commit 1027b0dfb4
24 changed files with 868 additions and 36 deletions
@@ -0,0 +1,44 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('budget_control', function (Blueprint $table) {
$table->id();
$table->foreignId('cabang_id')->nullable()->constrained('cabang');
$table->foreignId('sender_id')->constrained('admins');
$table->foreignId('receiver_id')->constrained('admins');
$table->integer('amount');
$table->string('remarks')->nullable();
$table->string('periode_month');
$table->integer('periode_year');
$table->timestamp('transferred_at')->nullable();
$table->timestamp('closing_at')->nullable();
$table->string('status')->default('Unassigned');
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('budget_control');
}
};
@@ -0,0 +1,44 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('budget_control_log', function (Blueprint $table) {
$table->id();
$table->foreignId('cabang_id')->nullable()->constrained('cabang');
$table->foreignId('sender_id')->constrained('admins');
$table->foreignId('receiver_id')->constrained('admins');
$table->integer('amount');
$table->string('remarks')->nullable();
$table->string('periode_month');
$table->integer('periode_year');
$table->timestamp('transferred_at')->nullable();
$table->timestamp('closing_at')->nullable();
$table->string('status')->default('Unassigned');
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('budget_control_log');
}
};
+3 -1
View File
@@ -10,6 +10,7 @@ use Database\Seeds\RolePermissionSeeder2;
use Database\Seeds\RolePermissionSeeder3;
use Database\Seeds\RolePermissionSeeder4;
use Database\Seeds\RolePermissionSeeder5;
use Database\Seeds\RolePermissionSeeder6;
class DatabaseSeeder extends Seeder
{
@@ -28,6 +29,7 @@ class DatabaseSeeder extends Seeder
// $this->call(RolePermissionSeeder2::class);
// $this->call(RolePermissionSeeder3::class);
// $this->call(RolePermissionSeeder4::class);
$this->call(RolePermissionSeeder5::class);
// $this->call(RolePermissionSeeder5::class);
$this->call(RolePermissionSeeder6::class);
}
}
+79
View File
@@ -0,0 +1,79 @@
<?php
namespace Database\Seeds;
use App\Models\Admin;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
class RolePermissionSeeder6 extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
/**
* Enable these options if you need same role and other permission for User Model
* Else, please follow the below steps for admin guard
*/
$permissions = [
[
'group_name' => 'budget_control',
'permissions' => [
'budget_control.view',
'budget_control.create',
'budget_control.edit',
'budget_control.delete',
'budget_control.approve',
]
],
];
// Do same for the admin guard for tutorial purposes.
$admin = Admin::where('username', 'superadmin')->first();
$roleSuperAdmin = $this->maybeCreateSuperAdminRole($admin);
// Create and Assign Permissions
for ($i = 0; $i < count($permissions); $i++) {
$permissionGroup = $permissions[$i]['group_name'];
for ($j = 0; $j < count($permissions[$i]['permissions']); $j++) {
$permissionExist = Permission::where('name', $permissions[$i]['permissions'][$j])->first();
if (is_null($permissionExist)) {
$permission = Permission::create(
[
'name' => $permissions[$i]['permissions'][$j],
'group_name' => $permissionGroup,
'guard_name' => 'admin'
]
);
$roleSuperAdmin->givePermissionTo($permission);
$permission->assignRole($roleSuperAdmin);
}
}
}
// Assign super admin role permission to superadmin user
if ($admin) {
$admin->assignRole($roleSuperAdmin);
}
}
private function maybeCreateSuperAdminRole($admin): Role
{
if (is_null($admin)) {
$roleSuperAdmin = Role::create(['name' => 'superadmin', 'guard_name' => 'admin']);
} else {
$roleSuperAdmin = Role::where('name', 'superadmin')->where('guard_name', 'admin')->first();
}
if (is_null($roleSuperAdmin)) {
$roleSuperAdmin = Role::create(['name' => 'superadmin', 'guard_name' => 'admin']);
}
return $roleSuperAdmin;
}
}