Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Currency.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Models\Setting;
  3. use App\Casts\CurrencyRateCast;
  4. use App\Concerns\Blamable;
  5. use App\Concerns\CompanyOwned;
  6. use App\Concerns\HasDefault;
  7. use App\Concerns\SyncsWithCompanyDefaults;
  8. use App\Facades\Forex;
  9. use App\Models\Accounting\Account;
  10. use App\Models\Accounting\Bill;
  11. use App\Models\Accounting\Estimate;
  12. use App\Models\Accounting\Invoice;
  13. use App\Models\Accounting\RecurringInvoice;
  14. use App\Models\Common\Client;
  15. use App\Models\Common\Vendor;
  16. use App\Observers\CurrencyObserver;
  17. use App\Utilities\Currency\CurrencyAccessor;
  18. use Database\Factories\Setting\CurrencyFactory;
  19. use Illuminate\Database\Eloquent\Attributes\ObservedBy;
  20. use Illuminate\Database\Eloquent\Casts\Attribute;
  21. use Illuminate\Database\Eloquent\Factories\Factory;
  22. use Illuminate\Database\Eloquent\Factories\HasFactory;
  23. use Illuminate\Database\Eloquent\Model;
  24. use Illuminate\Database\Eloquent\Relations\HasMany;
  25. use Illuminate\Database\Eloquent\Relations\HasOne;
  26. #[ObservedBy(CurrencyObserver::class)]
  27. class Currency extends Model
  28. {
  29. use Blamable;
  30. use CompanyOwned;
  31. use HasDefault;
  32. use HasFactory;
  33. use SyncsWithCompanyDefaults;
  34. protected $table = 'currencies';
  35. protected $fillable = [
  36. 'company_id',
  37. 'name',
  38. 'code',
  39. 'rate',
  40. 'precision',
  41. 'symbol',
  42. 'symbol_first',
  43. 'decimal_mark',
  44. 'thousands_separator',
  45. 'enabled',
  46. 'created_by',
  47. 'updated_by',
  48. ];
  49. protected $casts = [
  50. 'enabled' => 'boolean',
  51. 'symbol_first' => 'boolean',
  52. 'rate' => CurrencyRateCast::class,
  53. ];
  54. protected $appends = ['live_rate'];
  55. protected function liveRate(): Attribute
  56. {
  57. return Attribute::get(static function (mixed $value, array $attributes): ?float {
  58. $baseCurrency = CurrencyAccessor::getDefaultCurrency();
  59. $targetCurrency = $attributes['code'];
  60. if ($baseCurrency === $targetCurrency) {
  61. return 1;
  62. }
  63. $exchangeRate = Forex::getCachedExchangeRate($baseCurrency, $targetCurrency);
  64. return $exchangeRate ?? null;
  65. });
  66. }
  67. public function defaultCurrency(): HasOne
  68. {
  69. return $this->hasOne(CompanyDefault::class, 'currency_code', 'code');
  70. }
  71. public function accounts(): HasMany
  72. {
  73. return $this->hasMany(Account::class, 'currency_code', 'code');
  74. }
  75. public function bills(): HasMany
  76. {
  77. return $this->hasMany(Bill::class, 'currency_code', 'code');
  78. }
  79. public function clients(): HasMany
  80. {
  81. return $this->hasMany(Client::class, 'currency_code', 'code');
  82. }
  83. public function estimates(): HasMany
  84. {
  85. return $this->hasMany(Estimate::class, 'currency_code', 'code');
  86. }
  87. public function invoices(): HasMany
  88. {
  89. return $this->hasMany(Invoice::class, 'currency_code', 'code');
  90. }
  91. public function recurringInvoices(): HasMany
  92. {
  93. return $this->hasMany(RecurringInvoice::class, 'currency_code', 'code');
  94. }
  95. public function vendors(): HasMany
  96. {
  97. return $this->hasMany(Vendor::class, 'currency_code', 'code');
  98. }
  99. protected static function newFactory(): Factory
  100. {
  101. return CurrencyFactory::new();
  102. }
  103. }