1️⃣ Migration: 3 Alan Ekle
Mevcut muzibu_corporate_accounts tablosuna sadece 3 alan eklenecek:
| Alan |
Tip |
Açıklama |
Örnek |
| branch_name |
string nullable |
Şube adı (sadece manager için) |
"Kadıköy Şubesi" |
| role |
enum |
owner, manager, employee |
"manager" |
| settings |
json nullable |
Esnek ayarlar (şehir, adres vb.) |
{"city": "İstanbul", "address": "..."} |
Migration Kodu:
public function up()
{
Schema::table('muzibu_corporate_accounts', function (Blueprint $table) {
$table->string('branch_name')->nullable()->after('company_name');
$table->enum('role', ['owner', 'manager', 'employee'])
->default('employee')->after('is_active');
$table->json('settings')->nullable()->after('role');
$table->index('role');
});
}
2️⃣ Model Güncelleme
MuzibuCorporateAccount.php modelinde küçük eklemeler:
protected $fillable = [
...,
'branch_name',
'role',
'settings',
];
protected $casts = [
'is_active' => 'boolean',
'settings' => 'array',
];
public function isOwner(): bool
{
return $this->role === 'owner';
}
public function isManager(): bool
{
return $this->role === 'manager';
}
public function isEmployee(): bool
{
return $this->role === 'employee';
}
public function getBranches()
{
return $this->children()->where('role', 'manager')->get();
}
public function getEmployees()
{
return $this->children()->where('role', 'employee')->get();
}
public function getCompanyName(): ?string
{
if ($this->isOwner()) {
return $this->company_name;
}
$root = $this;
while ($root->parent) {
$root = $root->parent;
}
return $root->company_name ?? null;
}
3️⃣ Kullanım Örnekleri
Firma Oluştur (Owner):
$firmaUser = User::create([
'name' => 'Starbucks Türkiye',
'email' => 'starbucks@firma.com',
'password' => Hash::make('password'),
]);
$corporate = MuzibuCorporateAccount::create([
'user_id' => $firmaUser->id,
'parent_id' => null,
'company_name' => 'Starbucks Türkiye',
'corporate_code' => MuzibuCorporateAccount::generateCode(),
'role' => 'owner',
'settings' => [
'max_branches' => 200,
'subscription_plan' => 'enterprise',
],
]);
Şube Ekle (Manager):
$subeUser = User::create([
'name' => 'Kadıköy Şubesi',
'email' => 'kadikoy@starbucks.com',
'password' => Hash::make('password'),
]);
$sube = MuzibuCorporateAccount::create([
'user_id' => $subeUser->id,
'parent_id' => $corporate->id,
'branch_name' => 'Kadıköy Şubesi',
'role' => 'manager',
'settings' => [
'city' => 'İstanbul',
'address' => 'Kadıköy Moda Caddesi No: 42',
'phone' => '0216 555 12 34',
],
]);
Çalışan Ekle (Employee):
$calisanUser = User::create([
'name' => 'Ahmet Yılmaz',
'email' => 'ahmet@starbucks.com',
'password' => Hash::make('password'),
]);
MuzibuCorporateAccount::create([
'user_id' => $calisanUser->id,
'parent_id' => $sube->id,
'role' => 'employee',
]);