Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

DefaultSetting.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace App\Models\Setting;
  3. use App\Models\Banking\Account;
  4. use App\Scopes\CurrentCompanyScope;
  5. use Illuminate\Database\Eloquent\Factories\HasFactory;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  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. 'sales_discount_id',
  20. 'purchase_discount_id',
  21. 'income_category_id',
  22. 'expense_category_id',
  23. 'updated_by',
  24. ];
  25. protected static function booted(): void
  26. {
  27. static::addGlobalScope(new CurrentCompanyScope);
  28. }
  29. public function company(): BelongsTo
  30. {
  31. return $this->belongsTo(FilamentCompanies::companyModel(), 'company_id');
  32. }
  33. public function account(): BelongsTo
  34. {
  35. return $this->belongsTo(Account::class, 'account_id');
  36. }
  37. public function currency(): BelongsTo
  38. {
  39. return $this->belongsTo(Currency::class, 'currency_code', 'code');
  40. }
  41. public function salesTax(): BelongsTo
  42. {
  43. return $this->belongsTo(Tax::class,'sales_tax_id', 'id')
  44. ->where('type', 'sales');
  45. }
  46. public function purchaseTax(): BelongsTo
  47. {
  48. return $this->belongsTo(Tax::class,'purchase_tax_id', 'id')
  49. ->where('type', 'purchase');
  50. }
  51. public function salesDiscount(): BelongsTo
  52. {
  53. return $this->belongsTo(Discount::class,'sales_discount_id', 'id')
  54. ->where('type', 'sales');
  55. }
  56. public function purchaseDiscount(): BelongsTo
  57. {
  58. return $this->belongsTo(Discount::class,'purchase_discount_id', 'id')
  59. ->where('type', 'purchase');
  60. }
  61. public function incomeCategory(): BelongsTo
  62. {
  63. return $this->belongsTo(Category::class,'income_category_id', 'id')
  64. ->where('type', 'income');
  65. }
  66. public function expenseCategory(): BelongsTo
  67. {
  68. return $this->belongsTo(Category::class,'expense_category_id', 'id')
  69. ->where('type', 'expense');
  70. }
  71. public function updatedBy(): BelongsTo
  72. {
  73. return $this->belongsTo(FilamentCompanies::userModel(), 'updated_by');
  74. }
  75. public static function getAccounts(): array
  76. {
  77. return Account::pluck('name', 'id')->toArray();
  78. }
  79. public static function getCurrencies(): array
  80. {
  81. return Currency::pluck('name', 'code')->toArray();
  82. }
  83. public static function getSalesTaxes(): array
  84. {
  85. return Tax::where('type', 'sales')
  86. ->pluck('name', 'id')
  87. ->toArray();
  88. }
  89. public static function getPurchaseTaxes(): array
  90. {
  91. return Tax::where('type', 'purchase')
  92. ->pluck('name', 'id')
  93. ->toArray();
  94. }
  95. public static function getSalesDiscounts(): array
  96. {
  97. return Discount::where('type', 'sales')
  98. ->pluck('name', 'id')
  99. ->toArray();
  100. }
  101. public static function getPurchaseDiscounts(): array
  102. {
  103. return Discount::where('type', 'purchase')
  104. ->pluck('name', 'id')
  105. ->toArray();
  106. }
  107. public static function getIncomeCategories(): array
  108. {
  109. return Category::where('type', 'income')
  110. ->pluck('name', 'id')
  111. ->toArray();
  112. }
  113. public static function getExpenseCategories(): array
  114. {
  115. return Category::where('type', 'expense')
  116. ->pluck('name', 'id')
  117. ->toArray();
  118. }
  119. public static function getDefaultAccount()
  120. {
  121. $defaultAccount = Account::where('enabled', true)->first();
  122. return $defaultAccount->id ?? null;
  123. }
  124. public static function getDefaultCurrency()
  125. {
  126. $defaultCurrency = Currency::where('enabled', true)->first();
  127. return $defaultCurrency->code ?? null;
  128. }
  129. public static function getDefaultSalesTax()
  130. {
  131. $defaultSalesTax = Tax::where('enabled', true)
  132. ->where('type', 'sales')
  133. ->first();
  134. return $defaultSalesTax->id ?? null;
  135. }
  136. public static function getDefaultPurchaseTax()
  137. {
  138. $defaultPurchaseTax = Tax::where('enabled', true)
  139. ->where('type', 'purchase')
  140. ->first();
  141. return $defaultPurchaseTax->id ?? null;
  142. }
  143. public static function getDefaultSalesDiscount()
  144. {
  145. $defaultSalesDiscount = Discount::where('enabled', true)
  146. ->where('type', 'sales')
  147. ->first();
  148. return $defaultSalesDiscount->id ?? null;
  149. }
  150. public static function getDefaultPurchaseDiscount()
  151. {
  152. $defaultPurchaseDiscount = Discount::where('enabled', true)
  153. ->where('type', 'purchase')
  154. ->first();
  155. return $defaultPurchaseDiscount->id ?? null;
  156. }
  157. public static function getDefaultIncomeCategory()
  158. {
  159. $defaultIncomeCategory = Category::where('enabled', true)
  160. ->where('type', 'income')
  161. ->first();
  162. return $defaultIncomeCategory->id ?? null;
  163. }
  164. public static function getDefaultExpenseCategory()
  165. {
  166. $defaultExpenseCategory = Category::where('enabled', true)
  167. ->where('type', 'expense')
  168. ->first();
  169. return $defaultExpenseCategory->id ?? null;
  170. }
  171. }