Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Institution.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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\Support\Carbon;
  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 getLastTransactionDate(): ?string
  40. {
  41. $latestDate = $this->connectedBankAccounts->map(function ($connectedBankAccount) {
  42. if ($connectedBankAccount->bankAccount) {
  43. return $connectedBankAccount->bankAccount->transactions()->max('posted_at');
  44. }
  45. return null;
  46. })->filter()->max();
  47. if ($latestDate) {
  48. return Carbon::parse($latestDate)->diffForHumans();
  49. }
  50. return null;
  51. }
  52. public function getLastImportDate(): ?string
  53. {
  54. $latestDate = $this->connectedBankAccounts->map(function ($connectedBankAccount) {
  55. return $connectedBankAccount->last_imported_at;
  56. })->filter()->max();
  57. if ($latestDate) {
  58. return Carbon::parse($latestDate)->diffForHumans();
  59. }
  60. return null;
  61. }
  62. protected function logoUrl(): Attribute
  63. {
  64. return Attribute::get(static function (mixed $value, array $attributes): ?string {
  65. if ($attributes['logo']) {
  66. return Storage::disk('public')->url($attributes['logo']);
  67. }
  68. return null;
  69. });
  70. }
  71. public function logo(): Attribute
  72. {
  73. return Attribute::set(static function (mixed $value): ?string {
  74. if ($value) {
  75. $decoded = base64_decode($value);
  76. $filename = 'institution_logo_' . uniqid('', true) . '.png';
  77. Storage::disk('public')->put($filename, $decoded);
  78. return $filename;
  79. }
  80. return null;
  81. });
  82. }
  83. }