Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Institution.php 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Models\Banking;
  3. use Illuminate\Database\Eloquent\Casts\Attribute;
  4. use Illuminate\Database\Eloquent\Collection;
  5. use Illuminate\Database\Eloquent\Factories\HasFactory;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Database\Eloquent\Relations\HasMany;
  8. use Illuminate\Database\Eloquent\Relations\HasOne;
  9. use Illuminate\Support\Facades\Storage;
  10. class Institution extends Model
  11. {
  12. use HasFactory;
  13. protected $table = 'institutions';
  14. protected $fillable = [
  15. 'external_institution_id',
  16. 'name',
  17. 'logo',
  18. 'website',
  19. 'phone',
  20. 'address',
  21. 'created_by',
  22. 'updated_by',
  23. ];
  24. protected $appends = [
  25. 'logo_url',
  26. ];
  27. public function bankAccounts(): HasMany
  28. {
  29. return $this->hasMany(BankAccount::class, 'institution_id');
  30. }
  31. public function getEnabledConnectedBankAccounts(): Collection
  32. {
  33. return $this->connectedBankAccounts()->where('import_transactions', true)->get();
  34. }
  35. public function connectedBankAccounts(): HasMany
  36. {
  37. return $this->hasMany(ConnectedBankAccount::class, 'institution_id');
  38. }
  39. public function latestImport(): HasOne
  40. {
  41. return $this->hasOne(ConnectedBankAccount::class, 'institution_id')->latestOfMany('last_imported_at');
  42. }
  43. protected function logoUrl(): Attribute
  44. {
  45. return Attribute::get(static function (mixed $value, array $attributes): ?string {
  46. if ($attributes['logo']) {
  47. return Storage::disk('public')->url($attributes['logo']);
  48. }
  49. return null;
  50. });
  51. }
  52. public function logo(): Attribute
  53. {
  54. return Attribute::set(static function (mixed $value): ?string {
  55. if ($value) {
  56. $decoded = base64_decode($value);
  57. $filename = 'institution_logo_' . uniqid('', true) . '.png';
  58. Storage::disk('public')->put($filename, $decoded);
  59. return $filename;
  60. }
  61. return null;
  62. });
  63. }
  64. }