| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Models; |
| 4 |
|
| 5 |
use FluentSupport\Framework\Database\Orm\Builder; |
| 6 |
use FluentSupport\Framework\Support\Arr; |
| 7 |
|
| 8 |
class Customer extends Person |
| 9 |
{ |
| 10 |
protected static $type = 'customer'; |
| 11 |
|
| 12 |
protected $searchable = [ |
| 13 |
'id', |
| 14 |
'first_name', |
| 15 |
'last_name', |
| 16 |
'email', |
| 17 |
'address_line_1', |
| 18 |
'address_line_2', |
| 19 |
'country' |
| 20 |
]; |
| 21 |
|
| 22 |
public static function boot() |
| 23 |
{ |
| 24 |
parent::boot(); |
| 25 |
|
| 26 |
static::creating(function ($model) { |
| 27 |
$model->person_type = static::$type; |
| 28 |
$model->hash = md5(time().wp_generate_uuid4()); |
| 29 |
}); |
| 30 |
|
| 31 |
static::addGlobalScope(function (Builder $builder) { |
| 32 |
$builder->where('person_type', 'customer'); |
| 33 |
}); |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
public static function mappables() |
| 38 |
{ |
| 39 |
return [ |
| 40 |
'title' => __('Customer Title', 'fluent-support'), |
| 41 |
'address_line_1' => __('Address Line 1', 'fluent-support'), |
| 42 |
'address_line_2' => __('Address Line 2', 'fluent-support'), |
| 43 |
'city' => __('City', 'fluent-support'), |
| 44 |
'state' => __('State', 'fluent-support'), |
| 45 |
'zip' => __('Zip Code', 'fluent-support'), |
| 46 |
'country' => __('Country', 'fluent-support'), |
| 47 |
]; |
| 48 |
} |
| 49 |
|
| 50 |
public static function maybeCreateCustomer($customerData) |
| 51 |
{ |
| 52 |
$customer = self::getCustomerFromData($customerData); |
| 53 |
$user = get_user_by('email', $customerData['email']); |
| 54 |
if($user) { |
| 55 |
if($user->first_name) { |
| 56 |
$customerData['first_name'] = $user->first_name; |
| 57 |
} |
| 58 |
if($user->last_name) { |
| 59 |
$customerData['last_name'] = $user->last_name; |
| 60 |
} |
| 61 |
if(empty($customerData['first_name']) && empty($customerData['last_name'])) { |
| 62 |
$customerData['first_name'] = $user->display_name; |
| 63 |
} |
| 64 |
$customerData['user_id'] = $user->ID; |
| 65 |
} |
| 66 |
|
| 67 |
if(!$customer) { |
| 68 |
if(!empty($customerData['last_ip_address'])) { |
| 69 |
$customerData['ip_address'] = $customerData['last_ip_address']; |
| 70 |
} |
| 71 |
// we have to create customer |
| 72 |
$customer = self::create($customerData); |
| 73 |
|
| 74 |
do_action('fluent_support/customer_created', $customer); |
| 75 |
|
| 76 |
} else { |
| 77 |
if(!empty($customerData['user_id']) || !empty($customerData['remote_uid'])) { |
| 78 |
$customerData = array_filter($customerData); |
| 79 |
$customer->fill($customerData); |
| 80 |
$customer->save(); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
return $customer; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
/** |
| 89 |
* One2Many: Customer has to many Click Tickets |
| 90 |
* @return Model Collection |
| 91 |
*/ |
| 92 |
public function tickets() |
| 93 |
{ |
| 94 |
$foreign_key = self::$type . '_id'; |
| 95 |
|
| 96 |
$class = __NAMESPACE__ . '\Ticket'; |
| 97 |
|
| 98 |
return $this->hasMany( |
| 99 |
$class, $foreign_key, 'id' |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
public static function getCustomerFromData($customerData) |
| 104 |
{ |
| 105 |
$remoteUid = Arr::get($customerData, 'remote_uid'); |
| 106 |
$email = $customerData['email']; |
| 107 |
|
| 108 |
$customer = false; |
| 109 |
if($remoteUid) { |
| 110 |
$customer = self::where('remote_uid', $remoteUid)->first(); |
| 111 |
} |
| 112 |
|
| 113 |
if(!$customer) { |
| 114 |
if(!empty($customerData['user_id'])) { |
| 115 |
$customer = self::where('user_id', $customerData['user_id'])->first(); |
| 116 |
} |
| 117 |
if(!$customer) { |
| 118 |
$customer = self::where('email', $email)->first(); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return $customer; |
| 123 |
} |
| 124 |
|
| 125 |
public function getTicketCounts() |
| 126 |
{ |
| 127 |
return Ticket::where('customer_id', $this->id)->count(); |
| 128 |
} |
| 129 |
|
| 130 |
public function getResponseCounts() |
| 131 |
{ |
| 132 |
return Conversation::where('person_id', $this->id)->count(); |
| 133 |
} |
| 134 |
} |
| 135 |
|