Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Contact.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Models\Common;
  3. use App\Concerns\Blamable;
  4. use App\Concerns\CompanyOwned;
  5. use App\Enums\Common\ContactType;
  6. use Database\Factories\Common\ContactFactory;
  7. use Illuminate\Database\Eloquent\Casts\Attribute;
  8. use Illuminate\Database\Eloquent\Factories\Factory;
  9. use Illuminate\Database\Eloquent\Factories\HasFactory;
  10. use Illuminate\Database\Eloquent\Model;
  11. use Illuminate\Database\Eloquent\Relations\MorphTo;
  12. class Contact extends Model
  13. {
  14. use Blamable;
  15. use CompanyOwned;
  16. use HasFactory;
  17. protected $table = 'contacts';
  18. protected $fillable = [
  19. 'company_id',
  20. 'type',
  21. 'first_name',
  22. 'last_name',
  23. 'email',
  24. 'phones',
  25. 'is_primary',
  26. 'created_by',
  27. 'updated_by',
  28. ];
  29. protected $casts = [
  30. 'type' => ContactType::class,
  31. 'phones' => 'array',
  32. ];
  33. public function contactable(): MorphTo
  34. {
  35. return $this->morphTo();
  36. }
  37. protected function fullName(): Attribute
  38. {
  39. return Attribute::get(function () {
  40. return trim("{$this->first_name} {$this->last_name}");
  41. });
  42. }
  43. protected function primaryPhone(): Attribute
  44. {
  45. return Attribute::get(function () {
  46. return $this->getPhoneByType('primary');
  47. });
  48. }
  49. protected function mobilePhone(): Attribute
  50. {
  51. return Attribute::get(function () {
  52. return $this->getPhoneByType('mobile');
  53. });
  54. }
  55. protected function faxPhone(): Attribute
  56. {
  57. return Attribute::get(function () {
  58. return $this->getPhoneByType('fax');
  59. });
  60. }
  61. protected function tollFreePhone(): Attribute
  62. {
  63. return Attribute::get(function () {
  64. return $this->getPhoneByType('toll_free');
  65. });
  66. }
  67. protected function firstAvailablePhone(): Attribute
  68. {
  69. return Attribute::get(function () {
  70. $priority = ['primary', 'mobile', 'toll_free', 'fax'];
  71. foreach ($priority as $type) {
  72. $phone = $this->getPhoneByType($type);
  73. if ($phone) {
  74. return $phone;
  75. }
  76. }
  77. return null; // Return null if no phone numbers are available
  78. });
  79. }
  80. private function getPhoneByType(string $type): ?string
  81. {
  82. if (! is_array($this->phones)) {
  83. return null;
  84. }
  85. foreach ($this->phones as $phone) {
  86. if ($phone['type'] === $type) {
  87. return $phone['data']['number'] ?? null;
  88. }
  89. }
  90. return null;
  91. }
  92. protected static function newFactory(): Factory
  93. {
  94. return ContactFactory::new();
  95. }
  96. }