You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Account.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Models\Banking;
  3. use App\Models\Setting\Currency;
  4. use App\Models\Setting\DefaultSetting;
  5. use Database\Factories\AccountFactory;
  6. use Illuminate\Database\Eloquent\Factories\Factory;
  7. use Illuminate\Database\Eloquent\Factories\HasFactory;
  8. use Illuminate\Database\Eloquent\Model;
  9. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  10. use Illuminate\Database\Eloquent\Relations\HasMany;
  11. use Illuminate\Support\Facades\Auth;
  12. use Illuminate\Support\Facades\Config;
  13. use Spatie\Tags\HasTags;
  14. use Wallo\FilamentCompanies\FilamentCompanies;
  15. class Account extends Model
  16. {
  17. use HasFactory;
  18. use HasTags;
  19. protected $table = 'accounts';
  20. protected $fillable = [
  21. 'company_id',
  22. 'type',
  23. 'name',
  24. 'number',
  25. 'currency_code',
  26. 'opening_balance',
  27. 'description',
  28. 'notes',
  29. 'status',
  30. 'bank_name',
  31. 'bank_phone',
  32. 'bank_address',
  33. 'bank_website',
  34. 'bic_swift_code',
  35. 'iban',
  36. 'aba_routing_number',
  37. 'ach_routing_number',
  38. 'enabled',
  39. 'created_by',
  40. 'updated_by',
  41. ];
  42. protected $casts = [
  43. 'enabled' => 'boolean',
  44. ];
  45. public function company(): BelongsTo
  46. {
  47. return $this->belongsTo(FilamentCompanies::companyModel(), 'company_id');
  48. }
  49. public function owner(): BelongsTo
  50. {
  51. return $this->company->owner;
  52. }
  53. public function currency(): BelongsTo
  54. {
  55. return $this->belongsTo(Currency::class, 'currency_code', 'code');
  56. }
  57. public function createdBy(): BelongsTo
  58. {
  59. return $this->belongsTo(FilamentCompanies::userModel(), 'created_by');
  60. }
  61. public function updatedBy(): BelongsTo
  62. {
  63. return $this->belongsTo(FilamentCompanies::userModel(), 'updated_by');
  64. }
  65. public function default_settings(): HasMany
  66. {
  67. return $this->hasMany(DefaultSetting::class, 'account_id', 'id');
  68. }
  69. public static function getAccountTypes(): array
  70. {
  71. return [
  72. 'checking' => 'Checking',
  73. 'savings' => 'Savings',
  74. 'money_market' => 'Money Market',
  75. 'certificate_of_deposit' => 'Certificate of Deposit',
  76. 'credit_card' => 'Credit Card',
  77. ];
  78. }
  79. public static function getAccountStatuses(): array
  80. {
  81. return [
  82. 'open' => 'Open',
  83. 'active' => 'Active',
  84. 'dormant' => 'Dormant',
  85. 'restricted' => 'Restricted',
  86. 'closed' => 'Closed',
  87. ];
  88. }
  89. public static function getCurrencyCodes(): array
  90. {
  91. $codes = array_keys(Config::get('money'));
  92. return array_combine($codes, $codes);
  93. }
  94. public static function getDefaultCurrencyCode(): ?string
  95. {
  96. $defaultCurrency = Currency::where('enabled', true)
  97. ->where('company_id', Auth::user()->currentCompany->id)
  98. ->first();
  99. return $defaultCurrency?->code;
  100. }
  101. protected static function newFactory(): Factory
  102. {
  103. return AccountFactory::new();
  104. }
  105. }