Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

DefaultSetting.php 5.6KB

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