You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. protected static function booted(): void
  39. {
  40. static::addGlobalScope(new CurrentCompanyScope);
  41. }
  42. public function company(): BelongsTo
  43. {
  44. return $this->belongsTo(FilamentCompanies::companyModel(), 'company_id');
  45. }
  46. public function createdBy(): BelongsTo
  47. {
  48. return $this->belongsTo(FilamentCompanies::userModel(), 'created_by');
  49. }
  50. public function updatedBy(): BelongsTo
  51. {
  52. return $this->belongsTo(FilamentCompanies::userModel(), 'updated_by');
  53. }
  54. public function currency(): BelongsTo
  55. {
  56. return $this->belongsTo(Currency::class, 'currency_code', 'code');
  57. }
  58. public function documents(): HasMany
  59. {
  60. return $this->hasMany(Document::class);
  61. }
  62. public static function getCountryOptions(): array
  63. {
  64. $allCountries = Country::all();
  65. // Default countries to show at the top of the options list
  66. $defaultCountryNames = ['United States', 'Canada', 'United Kingdom', 'Australia']; // replace with actual country names
  67. $defaultCountryOptions = [];
  68. $countryOptions = [];
  69. foreach ($allCountries as $country) {
  70. if (in_array($country->name, $defaultCountryNames, true)) {
  71. $defaultCountryOptions[$country->name] = $country->name;
  72. } else {
  73. $countryOptions[$country->name] = $country->name;
  74. }
  75. }
  76. // Guarantee the order of default countries
  77. $orderedDefaultCountryOptions = [];
  78. foreach ($defaultCountryNames as $name) {
  79. if (isset($defaultCountryOptions[$name])) {
  80. $orderedDefaultCountryOptions[$name] = $defaultCountryOptions[$name];
  81. }
  82. }
  83. return $orderedDefaultCountryOptions + $countryOptions;
  84. }
  85. public static function getRegionOptions(string $countryName): array
  86. {
  87. $country = Country::where('name', $countryName)->first();
  88. if (!$country) {
  89. return [];
  90. }
  91. return Region::where('country_id', $country->id)
  92. ->pluck('name', 'name')
  93. ->toArray();
  94. }
  95. public function bills(): HasMany
  96. {
  97. return $this->documents()->where('type', 'bill');
  98. }
  99. public function invoices(): HasMany
  100. {
  101. return $this->documents()->where('type', 'invoice');
  102. }
  103. public function scopeVendor($query)
  104. {
  105. return $query->where('type', 'vendor');
  106. }
  107. public function scopeCustomer($query)
  108. {
  109. return $query->where('type', 'customer');
  110. }
  111. public function scopeCompany($query)
  112. {
  113. return $query->where('entity', 'company');
  114. }
  115. public function scopeIndividual($query)
  116. {
  117. return $query->where('entity', 'individual');
  118. }
  119. }