mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-09-01 21:00:58 +00:00
* refactor: stabilize model identities for domain migration * refactor: extract module platform context * refactor: assign models to domain contexts * refactor: extract ai platform context * refactor: extract storage platform context * refactor: extract mail platform context * refactor: extract pdf platform context * refactor: extract operations platform context * refactor: move installation into operations platform * refactor: extract money domain context * refactor: extract taxation domain context * refactor: extract catalog domain context * refactor: extract metadata domain context * refactor: extract reporting domain context * refactor: extract purchases domain context * refactor: extract receivables domain context * refactor: extract accounts domain context * refactor: complete reporting statement boundary * refactor: extract contacts domain context * refactor: extract sales domain context * refactor: remove legacy application layers * fix: migrate legacy bouncer role identities
83 lines
2.2 KiB
PHP
83 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Accounts\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class CompanySetting extends Model
|
|
{
|
|
protected $table = 'company_settings';
|
|
|
|
use HasFactory;
|
|
|
|
protected $fillable = ['company_id', 'option', 'value'];
|
|
|
|
public function company(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Company::class);
|
|
}
|
|
|
|
public function scopeWhereCompany($query, $company_id)
|
|
{
|
|
$query->where('company_id', $company_id);
|
|
}
|
|
|
|
/**
|
|
* Bulk create or update settings for a specific company.
|
|
*/
|
|
public static function setSettings(array $settings, mixed $company_id): void
|
|
{
|
|
foreach ($settings as $key => $value) {
|
|
self::updateOrCreate(
|
|
[
|
|
'option' => $key,
|
|
'company_id' => $company_id,
|
|
],
|
|
[
|
|
'option' => $key,
|
|
'company_id' => $company_id,
|
|
'value' => $value,
|
|
]
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Retrieve all settings for a company as a key-value collection.
|
|
*/
|
|
public static function getAllSettings(mixed $company_id): Collection
|
|
{
|
|
return static::whereCompany($company_id)->get()->mapWithKeys(function ($item) {
|
|
return [$item['option'] => $item['value']];
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Retrieve specific settings for a company as a key-value collection.
|
|
*/
|
|
public static function getSettings(array $settings, mixed $company_id): Collection
|
|
{
|
|
return static::whereIn('option', $settings)->whereCompany($company_id)
|
|
->get()->mapWithKeys(function ($item) {
|
|
return [$item['option'] => $item['value']];
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Retrieve a single company setting value by key, or null if not found.
|
|
*/
|
|
public static function getSetting(string $key, mixed $company_id): mixed
|
|
{
|
|
$setting = static::whereOption($key)->whereCompany($company_id)->first();
|
|
|
|
if ($setting) {
|
|
return $setting->value;
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
}
|