| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Attachment; |
| 6 |
use FluentSupport\App\Models\Conversation; |
| 7 |
use FluentSupport\App\Models\Customer; |
| 8 |
use FluentSupport\App\Models\Ticket; |
| 9 |
use FluentSupport\App\Services\Helper; |
| 10 |
use FluentSupport\App\Services\ProfileInfoService; |
| 11 |
use FluentSupport\Framework\Request\Request; |
| 12 |
use FluentSupport\Framework\Support\Arr; |
| 13 |
|
| 14 |
class CustomerController extends Controller |
| 15 |
{ |
| 16 |
public function index(Request $request) |
| 17 |
{ |
| 18 |
$customersQuery = Customer::orderBy('id', 'DESC') |
| 19 |
->orderBy($request->get('order_by', 'id'), $request->get('order_type', 'ASC')); |
| 20 |
|
| 21 |
if ($request->get('search')) { |
| 22 |
$customersQuery->searchBy($request->get('search')); |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
$status = $request->get('status'); |
| 27 |
if ($status && $status != 'all') { |
| 28 |
$customersQuery->filterByStatues([$status]); |
| 29 |
} |
| 30 |
|
| 31 |
$customers = $customersQuery->paginate(); |
| 32 |
|
| 33 |
foreach ($customers as $customer) { |
| 34 |
$customer->total_tickets = $customer->getTicketCounts(); |
| 35 |
$customer->total_responses = $customer->getResponseCounts(); |
| 36 |
if ($customer->user_id) { |
| 37 |
$customer->user_profile = admin_url('user-edit.php?user_id=' . $customer->user_id); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
return [ |
| 42 |
'customers' => $customers, |
| 43 |
]; |
| 44 |
} |
| 45 |
|
| 46 |
public function getCustomer(Request $request, $customerId) |
| 47 |
{ |
| 48 |
$customer = Customer::findOrFail($customerId); |
| 49 |
|
| 50 |
$data = [ |
| 51 |
'customer' => $customer |
| 52 |
]; |
| 53 |
|
| 54 |
$with = $request->get('with', []); |
| 55 |
|
| 56 |
if (in_array('widgets', $with)) { |
| 57 |
$data['widgets'] = ProfileInfoService::getProfileExtraWidgets($customer); |
| 58 |
} |
| 59 |
|
| 60 |
if (in_array('tickets', $with)) { |
| 61 |
$data['tickets'] = Ticket::select(['id', 'title', 'status', 'customer_id', 'created_at']) |
| 62 |
->where('customer_id', $customer->id) |
| 63 |
->orderBy('id', 'DESC') |
| 64 |
->limit(20) |
| 65 |
->get(); |
| 66 |
} |
| 67 |
|
| 68 |
if(in_array('fluentcrm_profile', $with)) { |
| 69 |
$data['fluentcrm_profile'] = Helper::getFluentCrmContactData($customer); |
| 70 |
} |
| 71 |
|
| 72 |
return $data; |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
public function create(Request $request) |
| 77 |
{ |
| 78 |
$data = $request->all(); |
| 79 |
$this->validate($data, [ |
| 80 |
'email' => 'required|email|unique:fs_persons' |
| 81 |
]); |
| 82 |
|
| 83 |
$email = $data['email']; |
| 84 |
|
| 85 |
$data = Arr::only($data, (new Customer)->getFillable()); |
| 86 |
|
| 87 |
$user = get_user_by('email', $email); |
| 88 |
|
| 89 |
if ($user) { |
| 90 |
$data['user_id'] = $user->ID; |
| 91 |
if (empty($data['first_name'])) { |
| 92 |
$data['first_name'] = $user->first_name; |
| 93 |
} |
| 94 |
if (empty($data['last_name'])) { |
| 95 |
$data['last_name'] = $user->last_name; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
$customer = Customer::create($data); |
| 100 |
|
| 101 |
return [ |
| 102 |
'message' => __('Customer has been added', 'fluent-support'), |
| 103 |
'customer' => $customer |
| 104 |
]; |
| 105 |
} |
| 106 |
|
| 107 |
public function update(Request $request, $customerId) |
| 108 |
{ |
| 109 |
$customer = Customer::findOrFail($customerId); |
| 110 |
$data = $request->all(); |
| 111 |
$this->validate($data, [ |
| 112 |
'email' => 'required|email', |
| 113 |
'first_name' => 'required' |
| 114 |
]); |
| 115 |
|
| 116 |
if ($otherCustomer = Customer::where('id', '!=', $customerId)->where('email', $data['email'])->first()) { |
| 117 |
return $this->sendError([ |
| 118 |
'message' => __('Another Customer has same email address', 'fluent-support'), |
| 119 |
'errors' => [ |
| 120 |
'email' => [ |
| 121 |
'unique' => __('Email address has been assigned to other customer', 'fluent-support') |
| 122 |
] |
| 123 |
] |
| 124 |
], 423); |
| 125 |
} |
| 126 |
|
| 127 |
$validKeys = (new Customer)->getFillable(); |
| 128 |
unset($validKeys['hash']); |
| 129 |
unset($validKeys['user_id']); |
| 130 |
|
| 131 |
$updateData = Arr::only($data, $validKeys); |
| 132 |
|
| 133 |
$user = get_user_by('email', $data['email']); |
| 134 |
|
| 135 |
if ($user) { |
| 136 |
$updateData['user_id'] = $user->ID; |
| 137 |
} |
| 138 |
|
| 139 |
Customer::where('id', $customer->id) |
| 140 |
->update($updateData); |
| 141 |
|
| 142 |
return [ |
| 143 |
'message' => __('Customer has been updated', 'fluent-support'), |
| 144 |
'customer' => Customer::findOrFail($customerId) |
| 145 |
]; |
| 146 |
} |
| 147 |
|
| 148 |
public function delete(Request $request, $customerId) |
| 149 |
{ |
| 150 |
$customer = Customer::findOrFail($customerId); |
| 151 |
|
| 152 |
$tickets = Ticket::where('customer_id', $customer->id)->get(); |
| 153 |
|
| 154 |
foreach ($tickets as $ticket) { |
| 155 |
$ticket->deleteTicket(); |
| 156 |
} |
| 157 |
|
| 158 |
$customer->delete(); |
| 159 |
return [ |
| 160 |
'message' => __('Customer Deleted Successfully', 'fluent-support') |
| 161 |
]; |
| 162 |
} |
| 163 |
} |
| 164 |
|