mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-15 09:14:08 +00:00
Modernize all 16 models with missing type declarations: - Return types on ~87 methods (string, bool, void, array, mixed, etc.) - Typed parameters where missing - PHPDoc blocks on non-obvious methods explaining their purpose Models updated: Invoice, Estimate, Payment, User, Company, Customer, RecurringInvoice, Setting, CompanySetting, FileDisk, Transaction, EmailLog, ExchangeRateLog, PaymentMethod, CustomField, CustomFieldValue.
81 lines
2.2 KiB
PHP
81 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\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
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
}
|