PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.4.0
Fluent Support – Helpdesk & Customer Support Ticket System v1.4.0
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / app / Models / Customer.php

Customer.php in Fluent Support – Helpdesk & Customer Support Ticket System 1.4.0, at app/Models/Customer.php

121 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 maybeCreateCustomer($customerData)
38 {
39 $customer = self::getCustomerFromData($customerData);
40
41 $user = get_user_by('email', $customerData['email']);
42 if($user) {
43 if($user->first_name) {
44 $customerData['first_name'] = $user->first_name;
45 }
46 if($user->last_name) {
47 $customerData['last_name'] = $user->last_name;
48 }
49 if(empty($customerData['first_name']) && empty($customerData['last_name'])) {
50 $customerData['first_name'] = $user->display_name;
51 }
52
53 $customerData['user_id'] = $user->ID;
54 }
55
56 if(!$customer) {
57 if(!empty($customerData['last_ip_address'])) {
58 $customerData['ip_address'] = $customerData['last_ip_address'];
59 }
60 // we have to create customer
61 $customer = self::create($customerData);
62 } else {
63 if(!empty($customerData['user_id']) || !empty($customerData['remote_uid'])) {
64 $customerData = array_filter($customerData);
65 $customer->fill($customerData);
66 $customer->save();
67 }
68 }
69
70 return $customer;
71 }
72
73
74 /**
75 * One2Many: Customer has to many Click Tickets
76 * @return Model Collection
77 */
78 public function tickets()
79 {
80 $foreign_key = self::$type . '_id';
81
82 $class = __NAMESPACE__ . '\Ticket';
83
84 return $this->hasMany(
85 $class, $foreign_key, 'id'
86 );
87 }
88
89 public static function getCustomerFromData($customerData)
90 {
91 $remoteUid = Arr::get($customerData, 'remote_uid');
92 $email = $customerData['email'];
93
94 $customer = false;
95 if($remoteUid) {
96 $customer = self::where('remote_uid', $remoteUid)->first();
97 }
98
99 if(!$customer) {
100 if(!empty($customerData['user_id'])) {
101 $customer = self::where('user_id', $customerData['user_id'])->first();
102 }
103 if(!$customer) {
104 $customer = self::where('email', $email)->first();
105 }
106 }
107
108 return $customer;
109 }
110
111 public function getTicketCounts()
112 {
113 return Ticket::where('customer_id', $this->id)->count();
114 }
115
116 public function getResponseCounts()
117 {
118 return Conversation::where('person_id', $this->id)->count();
119 }
120 }
121