Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Currency.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\Models\Setting;
  3. use App\Models\Banking\Account;
  4. use App\Scopes\CurrentCompanyScope;
  5. use App\Traits\Blamable;
  6. use App\Traits\CompanyOwned;
  7. use Database\Factories\CurrencyFactory;
  8. use Illuminate\Database\Eloquent\Factories\Factory;
  9. use Illuminate\Database\Eloquent\Factories\HasFactory;
  10. use Illuminate\Database\Eloquent\Model;
  11. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  12. use Illuminate\Database\Eloquent\Relations\HasMany;
  13. use Illuminate\Database\Eloquent\Relations\HasOne;
  14. use Illuminate\Support\Facades\Config;
  15. use Wallo\FilamentCompanies\FilamentCompanies;
  16. class Currency extends Model
  17. {
  18. use Blamable, CompanyOwned, HasFactory;
  19. protected $table = 'currencies';
  20. protected $fillable = [
  21. 'company_id',
  22. 'name',
  23. 'code',
  24. 'rate',
  25. 'precision',
  26. 'symbol',
  27. 'symbol_first',
  28. 'decimal_mark',
  29. 'thousands_separator',
  30. 'enabled',
  31. 'created_by',
  32. 'updated_by',
  33. ];
  34. protected $casts = [
  35. 'enabled' => 'boolean',
  36. 'symbol_first' => 'boolean',
  37. ];
  38. public function company(): BelongsTo
  39. {
  40. return $this->belongsTo(FilamentCompanies::companyModel(), 'company_id');
  41. }
  42. public function defaultCurrency(): HasOne
  43. {
  44. return $this->hasOne(DefaultSetting::class, 'currency_code', 'code');
  45. }
  46. public function accounts(): HasMany
  47. {
  48. return $this->hasMany(Account::class, 'currency_code', 'code');
  49. }
  50. public function createdBy(): BelongsTo
  51. {
  52. return $this->belongsTo(FilamentCompanies::userModel(), 'created_by');
  53. }
  54. public function updatedBy(): BelongsTo
  55. {
  56. return $this->belongsTo(FilamentCompanies::userModel(), 'updated_by');
  57. }
  58. public static function getCurrencyCodes(): array
  59. {
  60. $allCodes = array_keys(Config::get('money'));
  61. $storedCodes = static::query()
  62. ->pluck('code')
  63. ->toArray();
  64. $codes = array_diff($allCodes, $storedCodes);
  65. return array_combine($codes, $codes);
  66. }
  67. public static function getDefaultCurrency(): ?string
  68. {
  69. $defaultCurrency = self::where('enabled', true)
  70. ->first();
  71. return $defaultCurrency->code ?? null;
  72. }
  73. public static function convertBalance($balance, $oldCurrency, $newCurrency): float|int
  74. {
  75. $currencies = self::whereIn('code', [$oldCurrency, $newCurrency])->get();
  76. $oldCurrency = $currencies->firstWhere('code', $oldCurrency);
  77. $newCurrency = $currencies->firstWhere('code', $newCurrency);
  78. $oldRate = $oldCurrency?->rate;
  79. $newRate = $newCurrency?->rate;
  80. $precision = $newCurrency?->precision;
  81. $baseBalance = $balance / $oldRate;
  82. return round($baseBalance * $newRate, $precision);
  83. }
  84. protected static function newFactory(): Factory
  85. {
  86. return CurrencyFactory::new();
  87. }
  88. }