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 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Models\Banking;
  3. use App\Models\Setting\Currency;
  4. use Database\Factories\AccountFactory;
  5. use Illuminate\Database\Eloquent\Factories\Factory;
  6. use Illuminate\Database\Eloquent\Factories\HasFactory;
  7. use Illuminate\Database\Eloquent\Model;
  8. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  9. use Illuminate\Support\Facades\Auth;
  10. use Illuminate\Support\Facades\Config;
  11. use Wallo\FilamentCompanies\FilamentCompanies;
  12. class Account extends Model
  13. {
  14. use HasFactory;
  15. protected $table = 'accounts';
  16. protected $fillable = [
  17. 'type',
  18. 'name',
  19. 'number',
  20. 'currency_code',
  21. 'opening_balance',
  22. 'enabled',
  23. 'bank_name',
  24. 'bank_phone',
  25. 'bank_address',
  26. 'company_id',
  27. ];
  28. protected $casts = [
  29. 'enabled' => 'boolean',
  30. ];
  31. public function company(): BelongsTo
  32. {
  33. return $this->belongsTo(FilamentCompanies::companyModel(), 'company_id');
  34. }
  35. public function owner(): BelongsTo
  36. {
  37. return $this->company->owner;
  38. }
  39. public function currency(): BelongsTo
  40. {
  41. return $this->belongsTo(Currency::class, 'currency_code', 'code');
  42. }
  43. public static function getAccountTypes(): array
  44. {
  45. return [
  46. 'bank' => 'Bank',
  47. 'card' => 'Credit Card',
  48. ];
  49. }
  50. public static function getCurrencyCodes(): array
  51. {
  52. $codes = array_keys(Config::get('money'));
  53. return array_combine($codes, $codes);
  54. }
  55. public static function getDefaultCurrencyCode(): ?string
  56. {
  57. $defaultCurrency = Currency::where('enabled', true)
  58. ->where('company_id', Auth::user()->currentCompany->id)
  59. ->first();
  60. return $defaultCurrency?->code;
  61. }
  62. protected static function newFactory(): Factory
  63. {
  64. return AccountFactory::new();
  65. }
  66. }