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

Contact.php 3.6KB

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