Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DefaultSetting.php 5.4KB

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