您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Contact.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace App\Models;
  3. use App\Models\Document\Document;
  4. use App\Models\Setting\Currency;
  5. use App\Scopes\CurrentCompanyScope;
  6. use App\Traits\Blamable;
  7. use App\Traits\CompanyOwned;
  8. use Illuminate\Database\Eloquent\Factories\HasFactory;
  9. use Illuminate\Database\Eloquent\Model;
  10. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  11. use Illuminate\Database\Eloquent\Relations\HasMany;
  12. use Squire\Models\Country;
  13. use Squire\Models\Region;
  14. use Wallo\FilamentCompanies\FilamentCompanies;
  15. class Contact extends Model
  16. {
  17. use Blamable, CompanyOwned, HasFactory;
  18. protected $table = 'contacts';
  19. protected $fillable = [
  20. 'company_id',
  21. 'entity',
  22. 'type',
  23. 'name',
  24. 'email',
  25. 'tax_number',
  26. 'phone',
  27. 'address',
  28. 'city',
  29. 'zip_code',
  30. 'state',
  31. 'country',
  32. 'website',
  33. 'currency_code',
  34. 'reference',
  35. 'created_by',
  36. 'updated_by',
  37. ];
  38. public function company(): BelongsTo
  39. {
  40. return $this->belongsTo(FilamentCompanies::companyModel(), 'company_id');
  41. }
  42. public function createdBy(): BelongsTo
  43. {
  44. return $this->belongsTo(FilamentCompanies::userModel(), 'created_by');
  45. }
  46. public function updatedBy(): BelongsTo
  47. {
  48. return $this->belongsTo(FilamentCompanies::userModel(), 'updated_by');
  49. }
  50. public function currency(): BelongsTo
  51. {
  52. return $this->belongsTo(Currency::class, 'currency_code', 'code');
  53. }
  54. public function documents(): HasMany
  55. {
  56. return $this->hasMany(Document::class);
  57. }
  58. public static function getCountryOptions(): array
  59. {
  60. $allCountries = Country::all();
  61. // Default countries to show at the top of the options list
  62. $defaultCountryNames = ['United States', 'Canada', 'United Kingdom', 'Australia']; // replace with actual country names
  63. $defaultCountryOptions = [];
  64. $countryOptions = [];
  65. foreach ($allCountries as $country) {
  66. if (in_array($country->name, $defaultCountryNames, true)) {
  67. $defaultCountryOptions[$country->name] = $country->name;
  68. } else {
  69. $countryOptions[$country->name] = $country->name;
  70. }
  71. }
  72. // Guarantee the order of default countries
  73. $orderedDefaultCountryOptions = [];
  74. foreach ($defaultCountryNames as $name) {
  75. if (isset($defaultCountryOptions[$name])) {
  76. $orderedDefaultCountryOptions[$name] = $defaultCountryOptions[$name];
  77. }
  78. }
  79. return $orderedDefaultCountryOptions + $countryOptions;
  80. }
  81. public static function getRegionOptions(string $countryName): array
  82. {
  83. $country = Country::where('name', $countryName)->first();
  84. if (!$country) {
  85. return [];
  86. }
  87. return Region::where('country_id', $country->id)
  88. ->pluck('name', 'name')
  89. ->toArray();
  90. }
  91. public function bills(): HasMany
  92. {
  93. return $this->documents()->where('type', 'bill');
  94. }
  95. public function invoices(): HasMany
  96. {
  97. return $this->documents()->where('type', 'invoice');
  98. }
  99. public function scopeVendor($query)
  100. {
  101. return $query->where('type', 'vendor');
  102. }
  103. public function scopeCustomer($query)
  104. {
  105. return $query->where('type', 'customer');
  106. }
  107. public function scopeCompany($query)
  108. {
  109. return $query->where('entity', 'company');
  110. }
  111. public function scopeIndividual($query)
  112. {
  113. return $query->where('entity', 'individual');
  114. }
  115. }