| Total Complexity | 53 |
| Total Lines | 620 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ClientController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ClientController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class ClientController extends AdvanceSearchController |
||
| 21 | { |
||
| 22 | use PaymentsAndInvoices; |
||
|
|
|||
| 23 | |||
| 24 | public $user; |
||
| 25 | public $activate; |
||
| 26 | public $product; |
||
| 27 | |||
| 28 | public function __construct() |
||
| 29 | { |
||
| 30 | $this->middleware('auth'); |
||
| 31 | // $this->middleware('admin'); |
||
| 32 | $user = new User(); |
||
| 33 | $this->user = $user; |
||
| 34 | $activate = new AccountActivate(); |
||
| 35 | $this->activate = $activate; |
||
| 36 | $product = new \App\Model\Product\Product(); |
||
| 37 | $this->product = $product; |
||
| 38 | $license = new LicenseController(); |
||
| 39 | $this->licensing = $license; |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Display a listing of the resource. |
||
| 44 | * |
||
| 45 | * @return \Response |
||
| 46 | */ |
||
| 47 | public function index(Request $request) |
||
| 48 | { |
||
| 49 | $name = $request->input('name'); |
||
| 50 | $username = $request->input('username'); |
||
| 51 | $company = $request->input('company'); |
||
| 52 | $mobile = $request->input('mobile'); |
||
| 53 | $email = $request->input('email'); |
||
| 54 | $country = $request->input('country'); |
||
| 55 | $industry = $request->input('industry'); |
||
| 56 | $company_type = $request->input('company_type'); |
||
| 57 | $company_size = $request->input('company_size'); |
||
| 58 | $role = $request->input('role'); |
||
| 59 | $position = $request->input('position'); |
||
| 60 | $reg_from = $request->input('reg_from'); |
||
| 61 | $reg_till = $request->input('reg_till'); |
||
| 62 | |||
| 63 | return view( |
||
| 64 | 'themes.default1.user.client.index', |
||
| 65 | compact( |
||
| 66 | 'name', |
||
| 67 | 'username', |
||
| 68 | 'company', |
||
| 69 | 'mobile', |
||
| 70 | 'email', |
||
| 71 | 'country', |
||
| 72 | 'industry', |
||
| 73 | 'company_type', |
||
| 74 | 'company_size', |
||
| 75 | 'role', |
||
| 76 | 'position', |
||
| 77 | 'reg_from', |
||
| 78 | 'reg_till' |
||
| 79 | ) |
||
| 80 | ); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Get Clients for yajra datatable. |
||
| 85 | */ |
||
| 86 | public function getClients(Request $request) |
||
| 87 | { |
||
| 88 | $name = $request->input('name'); |
||
| 89 | $username = $request->input('username'); |
||
| 90 | $company = $request->input('company'); |
||
| 91 | $mobile = $request->input('mobile'); |
||
| 92 | $email = $request->input('email'); |
||
| 93 | $country = $request->input('country'); |
||
| 94 | $industry = $request->input('industry'); |
||
| 95 | $company_type = $request->input('company_type'); |
||
| 96 | $company_size = $request->input('company_size'); |
||
| 97 | $role = $request->input('role'); |
||
| 98 | $position = $request->input('position'); |
||
| 99 | $reg_from = $request->input('reg_from'); |
||
| 100 | $reg_till = $request->input('reg_till'); |
||
| 101 | $user = $this->advanceSearch( |
||
| 102 | $name, |
||
| 103 | $username, |
||
| 104 | $company, |
||
| 105 | $mobile, |
||
| 106 | $email, |
||
| 107 | $country, |
||
| 108 | $industry, |
||
| 109 | $company_type, |
||
| 110 | $company_size, |
||
| 111 | $role, |
||
| 112 | $position, |
||
| 113 | $reg_from, |
||
| 114 | $reg_till |
||
| 115 | ); |
||
| 116 | |||
| 117 | return\ DataTables::of($user->get()) |
||
| 118 | ->addColumn('checkbox', function ($model) { |
||
| 119 | return "<input type='checkbox' class='user_checkbox' |
||
| 120 | value=".$model->id.' name=select[] id=check>'; |
||
| 121 | }) |
||
| 122 | ->addColumn('first_name', function ($model) { |
||
| 123 | return '<a href='.url('clients/'.$model->id).'>' |
||
| 124 | .ucfirst($model->first_name).' '.ucfirst($model->last_name).'</a>'; |
||
| 125 | }) |
||
| 126 | ->addColumn('email', function ($model) { |
||
| 127 | return $model->email; |
||
| 128 | }) |
||
| 129 | ->addColumn('created_at', function ($model) { |
||
| 130 | $ends = $model->created_at; |
||
| 131 | if ($ends) { |
||
| 132 | $date1 = new DateTime($ends); |
||
| 133 | $tz = \Auth::user()->timezone()->first()->name; |
||
| 134 | $date1->setTimezone(new DateTimeZone($tz)); |
||
| 135 | $end = $date1->format('M j, Y, g:i a '); |
||
| 136 | } |
||
| 137 | |||
| 138 | return $end; |
||
| 139 | }) |
||
| 140 | // ->showColumns('email', 'created_at') |
||
| 141 | ->addColumn('active', function ($model) { |
||
| 142 | if ($model->active == 1) { |
||
| 143 | $email = "<span class='glyphicon glyphicon-envelope' |
||
| 144 | style='color:green' title='verified email'></span>"; |
||
| 145 | } else { |
||
| 146 | $email = "<span class='glyphicon glyphicon-envelope' |
||
| 147 | style='color:red' title='unverified email'></span>"; |
||
| 148 | } |
||
| 149 | if ($model->mobile_verified == 1) { |
||
| 150 | $mobile = "<span class='glyphicon glyphicon-phone' |
||
| 151 | style='color:green' title='verified mobile'></span>"; |
||
| 152 | } else { |
||
| 153 | $mobile = "<span class='glyphicon glyphicon-phone' |
||
| 154 | style='color:red' title='unverified mobile'></span>"; |
||
| 155 | } |
||
| 156 | |||
| 157 | return $email.' '.$mobile; |
||
| 158 | }) |
||
| 159 | ->addColumn('action', function ($model) { |
||
| 160 | return '<a href='.url('clients/'.$model->id.'/edit') |
||
| 161 | ." class='btn btn-sm btn-primary btn-xs'> |
||
| 162 | <i class='fa fa-edit' style='color:white;'> </i> Edit</a>" |
||
| 163 | .' <a href='.url('clients/'.$model->id) |
||
| 164 | ." class='btn btn-sm btn-primary btn-xs'> |
||
| 165 | <i class='fa fa-eye' style='color:white;'> </i> View</a>"; |
||
| 166 | // return 'hhhh'; |
||
| 167 | }) |
||
| 168 | ->rawColumns(['checkbox', 'first_name', 'email', 'created_at', 'active', 'action']) |
||
| 169 | ->make(true); |
||
| 170 | |||
| 171 | // ->searchColumns('email', 'first_name') |
||
| 172 | // ->orderColumns('email', 'first_name', 'created_at') |
||
| 173 | // ->make(); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Show the form for creating a new resource. |
||
| 178 | * |
||
| 179 | * @return \Response |
||
| 180 | */ |
||
| 181 | public function create() |
||
| 182 | { |
||
| 183 | $timezones = new \App\Model\Common\Timezone(); |
||
| 184 | $timezones = $timezones->pluck('name', 'id')->toArray(); |
||
| 185 | $bussinesses = \App\Model\Common\Bussiness::pluck('name', 'short')->toArray(); |
||
| 186 | $managers = User::where('role', 'admin')->where('position', 'manager') |
||
| 187 | ->pluck('first_name', 'id')->toArray(); |
||
| 188 | $timezonesList = \App\Model\Common\Timezone::get(); |
||
| 189 | foreach ($timezonesList as $timezone) { |
||
| 190 | $location = $timezone->location; |
||
| 191 | if ($location) { |
||
| 192 | $start = strpos($location, '('); |
||
| 193 | $end = strpos($location, ')', $start + 1); |
||
| 194 | $length = $end - $start; |
||
| 195 | $result = substr($location, $start + 1, $length - 1); |
||
| 196 | $display[] = (['id'=>$timezone->id, 'name'=> '('.$result.')'.' '.$timezone->name]); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | $timezones = array_column($display, 'name', 'id'); |
||
| 200 | |||
| 201 | return view('themes.default1.user.client.create', compact('timezones', 'bussinesses', 'managers')); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Store a newly created resource in storage. |
||
| 206 | * |
||
| 207 | * @return \Response |
||
| 208 | */ |
||
| 209 | public function store(ClientRequest $request) |
||
| 210 | { |
||
| 211 | try { |
||
| 212 | $user = $this->user; |
||
| 213 | $str = 'demopass'; |
||
| 214 | $password = \Hash::make($str); |
||
| 215 | $user->password = $password; |
||
| 216 | $cont = new \App\Http\Controllers\Front\PageController(); |
||
| 217 | $location = $cont->getLocation(); |
||
| 218 | $user->ip = $location['ip']; |
||
| 219 | $user->fill($request->input())->save(); |
||
| 220 | $this->sendWelcomeMail($user); |
||
| 221 | $mailchimp = new \App\Http\Controllers\Common\MailChimpController(); |
||
| 222 | $r = $mailchimp->addSubscriber($user->email); |
||
| 223 | |||
| 224 | return redirect()->back()->with('success', \Lang::get('message.saved-successfully')); |
||
| 225 | } catch (\Swift_TransportException $e) { |
||
| 226 | return redirect()->back()->with('warning', 'User has been created successfully |
||
| 227 | But email configuration has some problem!'); |
||
| 228 | } catch (\Exception $e) { |
||
| 229 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Display the specified resource. |
||
| 235 | * |
||
| 236 | * @param int $id |
||
| 237 | * |
||
| 238 | * @return \Response |
||
| 239 | */ |
||
| 240 | public function show($id) |
||
| 241 | { |
||
| 242 | try { |
||
| 243 | $invoice = new Invoice(); |
||
| 244 | $order = new Order(); |
||
| 245 | $invoices = $invoice->where('user_id', $id)->orderBy('created_at', 'desc')->get(); |
||
| 246 | $invoiceSum = $this->getTotalInvoice($invoices); |
||
| 247 | $amountReceived = $this->getAmountPaid($id); |
||
| 248 | $pendingAmount = $invoiceSum - $amountReceived; |
||
| 249 | // $pendingAmount = $invoiceSum - $amountReceived; |
||
| 250 | // if ($pendingAmount < 0) { |
||
| 251 | // $pendingAmount = 0; |
||
| 252 | // } |
||
| 253 | $extraAmt = $this->getExtraAmt($id); |
||
| 254 | $client = $this->user->where('id', $id)->first(); |
||
| 255 | |||
| 256 | // $client = ""; |
||
| 257 | $currency = $client->currency; |
||
| 258 | $orders = $order->where('client', $id)->get(); |
||
| 259 | $comments = $client->comments()->where('user_id', $client->id)->get(); |
||
| 260 | |||
| 261 | return view( |
||
| 262 | 'themes.default1.user.client.show', |
||
| 263 | compact( |
||
| 264 | 'id', |
||
| 265 | 'client', |
||
| 266 | 'invoices', |
||
| 267 | 'model_popup', |
||
| 268 | 'orders', |
||
| 269 | 'payments', |
||
| 270 | 'invoiceSum', |
||
| 271 | 'amountReceived', |
||
| 272 | 'pendingAmount', |
||
| 273 | 'currency', |
||
| 274 | 'extraAmt', |
||
| 275 | 'comments' |
||
| 276 | ) |
||
| 277 | ); |
||
| 278 | } catch (\Exception $ex) { |
||
| 279 | app('log')->info($ex->getMessage()); |
||
| 280 | Bugsnag::notifyException($ex); |
||
| 281 | |||
| 282 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 283 | } |
||
| 284 | } |
||
| 285 | |||
| 286 | public function getOrderDetail($id) |
||
| 287 | { |
||
| 288 | $client = $this->user->where('id', $id)->first(); |
||
| 289 | $responseData = []; |
||
| 290 | foreach ($client->order()->orderBy('created_at', 'desc')->get() as $order) { |
||
| 291 | $date = $order->created_at; |
||
| 292 | $productName = $order->product()->first() && $order->product()->first()->name ? |
||
| 293 | $order->product()->first()->name : 'Unknown'; |
||
| 294 | $number = $order->number; |
||
| 295 | $price = $order->price_override; |
||
| 296 | $status = $order->order_status; |
||
| 297 | array_push($responseData, (['date'=> $date, 'productName'=>$productName, |
||
| 298 | 'number' => $number, 'price' =>$price, 'status'=>$status, ])); |
||
| 299 | } |
||
| 300 | |||
| 301 | return $responseData; |
||
| 302 | } |
||
| 303 | |||
| 304 | //Get Payment Details on Invoice Page |
||
| 305 | public function getPaymentDetail($id) |
||
| 306 | { |
||
| 307 | $client = $this->user->where('id', $id)->first(); |
||
| 308 | $invoice = new Invoice(); |
||
| 309 | $invoices = $invoice->where('user_id', $id)->orderBy('created_at', 'desc')->get(); |
||
| 310 | $extraAmt = $this->getExtraAmtPaid($id); |
||
| 311 | $date = ''; |
||
| 312 | $responseData = []; |
||
| 313 | if ($invoices) { |
||
| 314 | foreach ($client->payment()->orderBy('created_at', 'desc')->get() as $payment) { |
||
| 315 | $number = $payment->invoice()->first() ? $payment->invoice()->first()->number : '--'; |
||
| 316 | $date = $payment->created_at; |
||
| 317 | $date1 = new DateTime($date); |
||
| 318 | $tz = \Auth::user()->timezone()->first()->name; |
||
| 319 | $date1->setTimezone(new DateTimeZone($tz)); |
||
| 320 | $date = $date1->format('M j, Y, g:i a '); |
||
| 321 | $pay_method = $payment->payment_method; |
||
| 322 | if ($payment->invoice_id == 0) { |
||
| 323 | $amount = $extraAmt; |
||
| 324 | } else { |
||
| 325 | $amount = $payment->amount; |
||
| 326 | } |
||
| 327 | $status = ucfirst($payment->payment_status); |
||
| 328 | array_push($responseData, (['number'=>$number, 'pay_method'=>$pay_method, 'amount'=>$amount, 'status'=>$status, 'date'=>$date])); |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | return $responseData; |
||
| 333 | } |
||
| 334 | |||
| 335 | public function getClientDetail($id) |
||
| 347 | } |
||
| 348 | |||
| 349 | public function getExtraAmt($userId) |
||
| 350 | { |
||
| 351 | try { |
||
| 352 | $amounts = Payment::where('user_id', $userId)->where('invoice_id', 0)->select('amt_to_credit')->get(); |
||
| 353 | $balance = 0; |
||
| 354 | foreach ($amounts as $amount) { |
||
| 355 | if ($amount) { |
||
| 356 | $balance = $balance + $amount->amt_to_credit ; |
||
| 357 | } |
||
| 358 | } |
||
| 359 | return $balance; |
||
| 360 | } catch (\Exception $ex) { |
||
| 361 | app('log')->info($ex->getMessage()); |
||
| 362 | Bugsnag::notifyException($ex); |
||
| 363 | |||
| 364 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 365 | } |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Get total Amount paid for a particular invoice. |
||
| 370 | */ |
||
| 371 | public function getAmountPaid($userId) |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Get total of the Invoices for a User. |
||
| 393 | */ |
||
| 394 | public function getTotalInvoice($invoices) |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Show the form for editing the specified resource. |
||
| 406 | * |
||
| 407 | * @param int $id |
||
| 408 | * |
||
| 409 | * @return \Response |
||
| 410 | */ |
||
| 411 | public function edit($id) |
||
| 412 | { |
||
| 413 | try { |
||
| 414 | $user = $this->user->where('id', $id)->first(); |
||
| 415 | $timezonesList = \App\Model\Common\Timezone::get(); |
||
| 416 | foreach ($timezonesList as $timezone) { |
||
| 417 | $location = $timezone->location; |
||
| 418 | if ($location) { |
||
| 419 | $start = strpos($location, '('); |
||
| 420 | $end = strpos($location, ')', $start + 1); |
||
| 421 | $length = $end - $start; |
||
| 422 | $result = substr($location, $start + 1, $length - 1); |
||
| 423 | $display[] = (['id'=>$timezone->id, 'name'=> '('.$result.')'.' '.$timezone->name]); |
||
| 424 | } |
||
| 425 | } |
||
| 426 | //for display |
||
| 427 | $timezones = array_column($display, 'name', 'id'); |
||
| 428 | $state = \App\Http\Controllers\Front\CartController::getStateByCode($user->state); |
||
| 429 | $managers = User::where('role', 'admin') |
||
| 430 | ->where('position', 'manager') |
||
| 431 | ->pluck('first_name', 'id')->toArray(); |
||
| 432 | $selectedCurrency = Currency::where('code', $user->currency) |
||
| 433 | ->pluck('name', 'code')->toArray(); |
||
| 434 | $selectedCompany = \DB::table('company_types')->where('short', $user->company_type) |
||
| 435 | ->pluck('name', 'short')->toArray(); |
||
| 436 | $selectedIndustry = \App\Model\Common\Bussiness::where('short', $user->bussiness) |
||
| 437 | ->pluck('name', 'short')->toArray(); |
||
| 438 | $selectedCompanySize = \DB::table('company_sizes')->where('short', $user->company_size) |
||
| 439 | ->pluck('name', 'short')->toArray(); |
||
| 440 | $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($user->country); |
||
| 441 | |||
| 442 | $bussinesses = \App\Model\Common\Bussiness::pluck('name', 'short')->toArray(); |
||
| 443 | |||
| 444 | return view( |
||
| 445 | 'themes.default1.user.client.edit', |
||
| 446 | compact( |
||
| 447 | 'bussinesses', |
||
| 448 | 'user', |
||
| 449 | 'timezones', |
||
| 450 | 'state', |
||
| 451 | 'states', |
||
| 452 | 'managers', |
||
| 453 | 'selectedCurrency', |
||
| 454 | 'selectedCompany', |
||
| 455 | 'selectedIndustry', |
||
| 456 | 'selectedCompanySize' |
||
| 457 | ) |
||
| 458 | ); |
||
| 459 | } catch (\Exception $ex) { |
||
| 460 | app('log')->error($ex->getMessage()); |
||
| 461 | |||
| 462 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 463 | } |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Update the specified resource in storage. |
||
| 468 | * |
||
| 469 | * @param int $id |
||
| 470 | * |
||
| 471 | * @return \Response |
||
| 472 | */ |
||
| 473 | public function update($id, Request $request) |
||
| 474 | { |
||
| 475 | try { |
||
| 476 | $user = $this->user->where('id', $id)->first(); |
||
| 477 | $symbol = Currency::where('code', $request->input('currency'))->pluck('symbol')->first(); |
||
| 478 | $user->currency_symbol = $symbol; |
||
| 479 | $user->fill($request->input())->save(); |
||
| 480 | |||
| 481 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 482 | } catch (\Exception $ex) { |
||
| 483 | app('log')->error($ex->getMessage()); |
||
| 484 | Bugsnag::notifyException($ex); |
||
| 485 | |||
| 486 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 487 | } |
||
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Remove the specified resource from storage. |
||
| 492 | * |
||
| 493 | * @param int $id |
||
| 494 | * |
||
| 495 | * @return \Response |
||
| 496 | */ |
||
| 497 | public function destroy(Request $request) |
||
| 532 | </div>'; |
||
| 533 | } |
||
| 534 | } |
||
| 535 | |||
| 536 | public function getUsers(Request $request) |
||
| 544 | } |
||
| 545 | |||
| 546 | public function search(Request $request) |
||
| 547 | { |
||
| 548 | try { |
||
| 549 | $term = trim($request->q); |
||
| 550 | if (empty($term)) { |
||
| 551 | return \Response::json([]); |
||
| 552 | } |
||
| 553 | $users = User::where('email', 'LIKE', '%'.$term.'%') |
||
| 554 | ->orWhere('first_name', 'LIKE', '%'.$term.'%') |
||
| 555 | ->orWhere('last_name', 'LIKE', '%'.$term.'%') |
||
| 556 | ->select('id', 'email', 'profile_pic', 'first_name', 'last_name')->get(); |
||
| 557 | $formatted_tags = []; |
||
| 558 | |||
| 559 | foreach ($users as $user) { |
||
| 560 | $formatted_users[] = ['id' => $user->id, 'text' => $user->email, 'profile_pic' => $user->profile_pic, |
||
| 561 | 'first_name' => $user->first_name, 'last_name' => $user->last_name, ]; |
||
| 562 | } |
||
| 563 | |||
| 564 | return \Response::json($formatted_users); |
||
| 565 | } catch (\Exception $e) { |
||
| 566 | // returns if try fails with exception meaagse |
||
| 567 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 568 | } |
||
| 569 | } |
||
| 570 | |||
| 571 | public function advanceSearch( |
||
| 607 | } |
||
| 608 | |||
| 609 | public function sendWelcomeMail($user) |
||
| 610 | { |
||
| 611 | $activate_model = new AccountActivate(); |
||
| 640 | } |
||
| 641 | } |
||
| 642 |