選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DefaultSetting.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace App\Models\Setting;
  3. use App\Models\Banking\Account;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Support\Facades\Auth;
  8. use Wallo\FilamentCompanies\FilamentCompanies;
  9. class DefaultSetting extends Model
  10. {
  11. use HasFactory;
  12. protected $table = 'default_settings';
  13. protected $fillable = [
  14. 'company_id',
  15. 'account_id',
  16. 'currency_code',
  17. 'sales_tax_id',
  18. 'purchase_tax_id',
  19. 'income_category_id',
  20. 'expense_category_id',
  21. 'updated_by',
  22. ];
  23. public function company(): BelongsTo
  24. {
  25. return $this->belongsTo(FilamentCompanies::companyModel(), 'company_id');
  26. }
  27. public function account(): BelongsTo
  28. {
  29. return $this->belongsTo(Account::class, 'account_id');
  30. }
  31. public function currency(): BelongsTo
  32. {
  33. return $this->belongsTo(Currency::class, 'currency_code', 'code');
  34. }
  35. public function salesTax(): BelongsTo
  36. {
  37. return $this->belongsTo(Tax::class,'sales_tax_id', 'id');
  38. }
  39. public function purchaseTax(): BelongsTo
  40. {
  41. return $this->belongsTo(Tax::class,'purchase_tax_id', 'id');
  42. }
  43. public function incomeCategory(): BelongsTo
  44. {
  45. return $this->belongsTo(Category::class,'income_category_id', 'id');
  46. }
  47. public function expenseCategory(): BelongsTo
  48. {
  49. return $this->belongsTo(Category::class,'expense_category_id', 'id');
  50. }
  51. public function updatedBy(): BelongsTo
  52. {
  53. return $this->belongsTo(FilamentCompanies::userModel(), 'updated_by');
  54. }
  55. public static function getAccounts(): array
  56. {
  57. return Account::where('company_id', Auth::user()->currentCompany->id)
  58. ->pluck('name', 'id')
  59. ->toArray();
  60. }
  61. public static function getCurrencies(): array
  62. {
  63. return Currency::where('company_id', Auth::user()->currentCompany->id)
  64. ->pluck('name', 'code')
  65. ->toArray();
  66. }
  67. public static function getSalesTaxes(): array
  68. {
  69. return Tax::where('company_id', Auth::user()->currentCompany->id)
  70. ->where('type', 'sales')
  71. ->pluck('name', 'id')
  72. ->toArray();
  73. }
  74. public static function getPurchaseTaxes(): array
  75. {
  76. return Tax::where('company_id', Auth::user()->currentCompany->id)
  77. ->where('type', 'purchase')
  78. ->pluck('name', 'id')
  79. ->toArray();
  80. }
  81. public static function getIncomeCategories(): array
  82. {
  83. return Category::where('company_id', Auth::user()->currentCompany->id)
  84. ->where('type', 'income')
  85. ->pluck('name', 'id')
  86. ->toArray();
  87. }
  88. public static function getExpenseCategories(): array
  89. {
  90. return Category::where('company_id', Auth::user()->currentCompany->id)
  91. ->where('type', 'expense')
  92. ->pluck('name', 'id')
  93. ->toArray();
  94. }
  95. public static function getDefaultAccount()
  96. {
  97. $defaultAccount = Account::where('enabled', true)
  98. ->where('company_id', Auth::user()->currentCompany->id)
  99. ->first();
  100. return $defaultAccount->id ?? null;
  101. }
  102. public static function getDefaultCurrency()
  103. {
  104. $defaultCurrency = Currency::where('enabled', true)
  105. ->where('company_id', Auth::user()->currentCompany->id)
  106. ->first();
  107. return $defaultCurrency->code ?? null;
  108. }
  109. public static function getDefaultSalesTax()
  110. {
  111. $defaultSalesTax = Tax::where('enabled', true)
  112. ->where('company_id', Auth::user()->currentCompany->id)
  113. ->where('type', 'sales')
  114. ->first();
  115. return $defaultSalesTax->id ?? null;
  116. }
  117. public static function getDefaultPurchaseTax()
  118. {
  119. $defaultPurchaseTax = Tax::where('enabled', true)
  120. ->where('company_id', Auth::user()->currentCompany->id)
  121. ->where('type', 'purchase')
  122. ->first();
  123. return $defaultPurchaseTax->id ?? null;
  124. }
  125. public static function getDefaultIncomeCategory()
  126. {
  127. $defaultIncomeCategory = Category::where('enabled', true)
  128. ->where('company_id', Auth::user()->currentCompany->id)
  129. ->where('type', 'income')
  130. ->first();
  131. return $defaultIncomeCategory->id ?? null;
  132. }
  133. public static function getDefaultExpenseCategory()
  134. {
  135. $defaultExpenseCategory = Category::where('enabled', true)
  136. ->where('company_id', Auth::user()->currentCompany->id)
  137. ->where('type', 'expense')
  138. ->first();
  139. return $defaultExpenseCategory->id ?? null;
  140. }
  141. }