Setting.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Setting extends Model
  5. {
  6. public const KEY_DEFAULT_MAF_ORDER_USER_ID = 'default_maf_order_user_id';
  7. public const KEY_RECLAMATION_ACT_REPRESENTATIVE_USER_ID = 'reclamation_act_representative_user_id';
  8. public const KEY_TTN_NEXT_NUMBER = 'ttn_next_number';
  9. public const KEY_TECHNICAL_DESCRIPTION_PRICE_FIELD = 'technical_description_price_field';
  10. public const DEFAULT_TECHNICAL_DESCRIPTION_PRICE_FIELD = 'project_price';
  11. protected $fillable = [
  12. 'key',
  13. 'value',
  14. ];
  15. public static function get(string $key, mixed $default = null): mixed
  16. {
  17. $value = static::query()->where('key', $key)->value('value');
  18. return $value ?? $default;
  19. }
  20. public static function getInt(string $key, ?int $default = null): ?int
  21. {
  22. $value = static::get($key, $default);
  23. if ($value === null || $value === '') {
  24. return $default;
  25. }
  26. return (int) $value;
  27. }
  28. public static function set(string $key, mixed $value): void
  29. {
  30. static::query()->updateOrCreate(
  31. ['key' => $key],
  32. ['value' => $value === null ? null : (string) $value]
  33. );
  34. }
  35. public static function technicalDescriptionPriceField(): string
  36. {
  37. $field = (string) static::get(
  38. self::KEY_TECHNICAL_DESCRIPTION_PRICE_FIELD,
  39. self::DEFAULT_TECHNICAL_DESCRIPTION_PRICE_FIELD,
  40. );
  41. return array_key_exists($field, CommonCatalogItem::PRICE_FIELDS)
  42. ? $field
  43. : self::DEFAULT_TECHNICAL_DESCRIPTION_PRICE_FIELD;
  44. }
  45. }