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.

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