Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

DefaultSetting.php 6.1KB

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