mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-10-31 16:37:31 -04:00 
			
		
		
		
	
		
			
				
	
	
		
			185 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			185 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace App\Models;
 | |
| 
 | |
| use Utils;
 | |
| use Illuminate\Auth\Authenticatable;
 | |
| use Illuminate\Auth\Passwords\CanResetPassword;
 | |
| use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
 | |
| use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
 | |
| use Illuminate\Database\Eloquent\SoftDeletes;
 | |
| use App\Models\LookupContact;
 | |
| 
 | |
| /**
 | |
|  * Class Contact.
 | |
|  */
 | |
| class Contact extends EntityModel implements AuthenticatableContract, CanResetPasswordContract
 | |
| {
 | |
|     use SoftDeletes, Authenticatable, CanResetPassword;
 | |
|     /**
 | |
|      * @var array
 | |
|      */
 | |
|     protected $dates = ['deleted_at'];
 | |
| 
 | |
|     /**
 | |
|      * @return mixed
 | |
|      */
 | |
|     public function getEntityType()
 | |
|     {
 | |
|         return ENTITY_CONTACT;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @var array
 | |
|      */
 | |
|     protected $fillable = [
 | |
|         'first_name',
 | |
|         'last_name',
 | |
|         'email',
 | |
|         'phone',
 | |
|         'send_invoice',
 | |
|         'custom_value1',
 | |
|         'custom_value2',
 | |
|     ];
 | |
| 
 | |
|     /**
 | |
|      * @var string
 | |
|      */
 | |
|     public static $fieldFirstName = 'first_name';
 | |
| 
 | |
|     /**
 | |
|      * @var string
 | |
|      */
 | |
|     public static $fieldLastName = 'last_name';
 | |
| 
 | |
|     /**
 | |
|      * @var string
 | |
|      */
 | |
|     public static $fieldEmail = 'email';
 | |
| 
 | |
|     /**
 | |
|      * @var string
 | |
|      */
 | |
|     public static $fieldPhone = 'phone';
 | |
| 
 | |
|     /**
 | |
|      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 | |
|      */
 | |
|     public function account()
 | |
|     {
 | |
|         return $this->belongsTo('App\Models\Account');
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @return mixed
 | |
|      */
 | |
|     public function user()
 | |
|     {
 | |
|         return $this->belongsTo('App\Models\User')->withTrashed();
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @return mixed
 | |
|      */
 | |
|     public function client()
 | |
|     {
 | |
|         return $this->belongsTo('App\Models\Client')->withTrashed();
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @return mixed
 | |
|      */
 | |
|     public function getPersonType()
 | |
|     {
 | |
|         return PERSON_CONTACT;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @return mixed|string
 | |
|      */
 | |
|     public function getName()
 | |
|     {
 | |
|         return $this->getDisplayName();
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @return mixed|string
 | |
|      */
 | |
|     public function getDisplayName()
 | |
|     {
 | |
|         if ($this->getFullName()) {
 | |
|             return $this->getFullName();
 | |
|         } else {
 | |
|             return $this->email;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param $contact_key
 | |
|      *
 | |
|      * @return mixed
 | |
|      */
 | |
|     public function getContactKeyAttribute($contact_key)
 | |
|     {
 | |
|         if (empty($contact_key) && $this->id) {
 | |
|             $this->contact_key = $contact_key = strtolower(str_random(RANDOM_KEY_LENGTH));
 | |
|             static::where('id', $this->id)->update(['contact_key' => $contact_key]);
 | |
|         }
 | |
| 
 | |
|         return $contact_key;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @return string
 | |
|      */
 | |
|     public function getFullName()
 | |
|     {
 | |
|         if ($this->first_name || $this->last_name) {
 | |
|             return trim($this->first_name.' '.$this->last_name);
 | |
|         } else {
 | |
|             return '';
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @return string
 | |
|      */
 | |
|     public function getLinkAttribute()
 | |
|     {
 | |
|         if (! $this->account) {
 | |
|             $this->load('account');
 | |
|         }
 | |
| 
 | |
|         $account = $this->account;
 | |
|         $url = trim(SITE_URL, '/');
 | |
| 
 | |
|         if ($account->hasFeature(FEATURE_CUSTOM_URL)) {
 | |
|             if (Utils::isNinjaProd()) {
 | |
|                 $url = $account->present()->clientPortalLink();
 | |
|             }
 | |
| 
 | |
|             if ($this->account->subdomain) {
 | |
|                 $url = Utils::replaceSubdomain($url, $account->subdomain);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         return "{$url}/client/dashboard/{$this->contact_key}";
 | |
|     }
 | |
| }
 | |
| 
 | |
| Contact::creating(function ($contact)
 | |
| {
 | |
|     LookupContact::createNew($contact->account->account_key, [
 | |
|         'contact_key' => $contact->contact_key,
 | |
|     ]);
 | |
| });
 | |
| 
 | |
| Contact::deleted(function ($contact)
 | |
| {
 | |
|     if ($contact->forceDeleting) {
 | |
|         LookupContact::deleteWhere([
 | |
|             'contact_key' => $contact->contact_key,
 | |
|         ]);
 | |
|     }
 | |
| });
 |