Completed
Pull Request — development (#635)
by Ashutosh
09:42
created

ClientController::getAmountPaid()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 17
rs 9.9
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace App\Http\Controllers\User;
4
5
use App\Http\Controllers\License\LicenseController;
6
use App\Http\Requests\User\ClientRequest;
7
use App\Model\Common\StatusSetting;
8
use App\Model\Order\Invoice;
9
use App\Model\Order\Order;
10
use App\Model\Order\Payment;
11
use App\Model\Payment\Currency;
12
use App\Model\User\AccountActivate;
13
use App\User;
14
use Bugsnag;
15
use DateTime;
16
use DateTimeZone;
17
use Illuminate\Http\Request;
18
use Log;
19
20
class ClientController extends AdvanceSearchController
21
{
22
    public $user;
23
    public $activate;
24
    public $product;
25
26
    public function __construct()
27
    {
28
        $this->middleware('auth');
29
        // $this->middleware('admin');
30
        $user = new User();
31
        $this->user = $user;
32
        $activate = new AccountActivate();
33
        $this->activate = $activate;
34
        $product = new \App\Model\Product\Product();
35
        $this->product = $product;
36
        $license = new LicenseController();
37
        $this->licensing = $license;
38
    }
39
40
    /**
41
     * Display a listing of the resource.
42
     *
43
     * @return \Response
44
     */
45
    public function index(Request $request)
46
    {
47
        $name = $request->input('name');
48
        $username = $request->input('username');
49
        $company = $request->input('company');
50
        $mobile = $request->input('mobile');
51
        $email = $request->input('email');
52
        $country = $request->input('country');
53
        $industry = $request->input('industry');
54
        $company_type = $request->input('company_type');
55
        $company_size = $request->input('company_size');
56
        $role = $request->input('role');
57
        $position = $request->input('position');
58
        $reg_from = $request->input('reg_from');
59
        $reg_till = $request->input('reg_till');
60
61
        return view('themes.default1.user.client.index',
62
            compact('name', 'username', 'company', 'mobile', 'email',
63
                'country', 'industry', 'company_type', 'company_size', 'role', 'position', 'reg_from', 'reg_till'));
64
    }
65
66
    /**
67
     * Get Clients for yajra datatable.
68
     */
69
    public function getClients(Request $request)
70
    {
71
        $name = $request->input('name');
72
        $username = $request->input('username');
73
        $company = $request->input('company');
74
        $mobile = $request->input('mobile');
75
        $email = $request->input('email');
76
        $country = $request->input('country');
77
        $industry = $request->input('industry');
78
        $company_type = $request->input('company_type');
79
        $company_size = $request->input('company_size');
80
        $role = $request->input('role');
81
        $position = $request->input('position');
82
        $reg_from = $request->input('reg_from');
83
        $reg_till = $request->input('reg_till');
84
        $user = $this->advanceSearch($name, $username, $company,
85
         $mobile, $email, $country, $industry, $company_type, $company_size, $role, $position, $reg_from, $reg_till);
86
87
        return\ DataTables::of($user->get())
88
                         ->addColumn('checkbox', function ($model) {
89
                             return "<input type='checkbox' class='user_checkbox' 
90
                            value=".$model->id.' name=select[] id=check>';
91
                         })
92
                        ->addColumn('first_name', function ($model) {
93
                            return '<a href='.url('clients/'.$model->id).'>'
94
                            .ucfirst($model->first_name).' '.ucfirst($model->last_name).'</a>';
95
                        })
96
                         ->addColumn('email', function ($model) {
97
                             return $model->email;
98
                         })
99
                          ->addColumn('created_at', function ($model) {
100
                              $ends = $model->created_at;
101
                              if ($ends) {
102
                                  $date1 = new DateTime($ends);
103
                                  $tz = \Auth::user()->timezone()->first()->name;
104
                                  $date1->setTimezone(new DateTimeZone($tz));
105
                                  $end = $date1->format('M j, Y, g:i a ');
106
                              }
107
108
                              return $end;
109
                          })
110
                        // ->showColumns('email', 'created_at')
111
                        ->addColumn('active', function ($model) {
112
                            if ($model->active == 1) {
113
                                $email = "<span class='glyphicon glyphicon-envelope'
114
                                 style='color:green' title='verified email'></span>";
115
                            } else {
116
                                $email = "<span class='glyphicon glyphicon-envelope'
117
                                 style='color:red' title='unverified email'></span>";
118
                            }
119
                            if ($model->mobile_verified == 1) {
120
                                $mobile = "<span class='glyphicon glyphicon-phone' 
121
                                style='color:green' title='verified mobile'></span>";
122
                            } else {
123
                                $mobile = "<span class='glyphicon glyphicon-phone'
124
                                 style='color:red' title='unverified mobile'></span>";
125
                            }
126
127
                            return $email.'&nbsp;&nbsp;'.$mobile;
128
                        })
129
                        ->addColumn('action', function ($model) {
130
                            return '<a href='.url('clients/'.$model->id.'/edit')
131
                            ." class='btn btn-sm btn-primary btn-xs'>
132
                            <i class='fa fa-edit' style='color:white;'> </i>&nbsp;&nbsp;Edit</a>"
133
                                    .'  <a href='.url('clients/'.$model->id)
134
                                    ." class='btn btn-sm btn-primary btn-xs'>
135
                                    <i class='fa fa-eye' style='color:white;'> </i>&nbsp;&nbsp;View</a>";
136
                            // return 'hhhh';
137
                        })
138
                        ->rawColumns(['checkbox', 'first_name', 'email',  'created_at', 'active', 'action'])
139
                        ->make(true);
140
141
        // ->searchColumns('email', 'first_name')
142
                        // ->orderColumns('email', 'first_name', 'created_at')
143
                        // ->make();
144
    }
145
146
    /**
147
     * Show the form for creating a new resource.
148
     *
149
     * @return \Response
150
     */
151
    public function create()
152
    {
153
        $timezones = new \App\Model\Common\Timezone();
154
        $timezones = $timezones->pluck('name', 'id')->toArray();
155
        $bussinesses = \App\Model\Common\Bussiness::pluck('name', 'short')->toArray();
156
        $managers = User::where('role', 'admin')->where('position', 'manager')
157
        ->pluck('first_name', 'id')->toArray();
158
        $timezonesList = \App\Model\Common\Timezone::get();
159
        foreach ($timezonesList as $timezone) {
160
            $location = $timezone->location;
161
            if ($location) {
162
                $start = strpos($location, '(');
163
                $end = strpos($location, ')', $start + 1);
164
                $length = $end - $start;
165
                $result = substr($location, $start + 1, $length - 1);
166
                $display[] = (['id'=>$timezone->id, 'name'=> '('.$result.')'.' '.$timezone->name]);
167
            }
168
        }
169
        $timezones = array_column($display, 'name', 'id');
170
171
        return view('themes.default1.user.client.create', compact('timezones', 'bussinesses', 'managers'));
172
    }
173
174
    /**
175
     * Store a newly created resource in storage.
176
     *
177
     * @return \Response
178
     */
179
    public function store(ClientRequest $request)
180
    {
181
        try {
182
            $user = $this->user;
183
            $str = 'demopass';
184
            $password = \Hash::make($str);
185
            $user->password = $password;
186
            $cont = new \App\Http\Controllers\Front\PageController();
187
            $location = $cont->getLocation();
188
            $user->ip = $location['ip'];
189
            $user->fill($request->input())->save();
190
            // $this->sendWelcomeMail($user);
191
            // $mailchimp = new \App\Http\Controllers\Common\MailChimpController();
192
            // $r = $mailchimp->addSubscriber($user->email);
193
            $licenseStatus = StatusSetting::pluck('license_status')->first();
194
            if ($licenseStatus == 1) {
195
                $addUserToLicensing = $this->licensing->addNewUser($request->first_name, $request->last_name, $request->email);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $addUserToLicensing is correct as $this->licensing->addNew..._name, $request->email) targeting App\Http\Controllers\Lic...ontroller::addNewUser() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
196
            }
197
198
            return redirect()->back()->with('success', \Lang::get('message.saved-successfully'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...e.saved-successfully')) also could return the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type Response.
Loading history...
199
        } catch (\Swift_TransportException $e) {
200
            return redirect()->back()->with('warning', 'User has been created successfully
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...ion has some problem!') also could return the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type Response.
Loading history...
201
             But email configuration has some problem!');
202
        } catch (\Exception $e) {
203
            return redirect()->back()->with('fails', $e->getMessage());
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...ils', $e->getMessage()) also could return the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type Response.
Loading history...
204
        }
205
    }
206
207
    /**
208
     * Display the specified resource.
209
     *
210
     * @param int $id
211
     *
212
     * @return \Response
213
     */
214
    public function show($id)
215
    {
216
        try {
217
            $invoice = new Invoice();
218
            $order = new Order();
219
            $invoices = $invoice->where('user_id', $id)->orderBy('created_at', 'desc')->get();
220
            $invoiceSum = $this->getTotalInvoice($invoices);
221
            $amountReceived = $this->getAmountPaid($id);
222
            $pendingAmount = $invoiceSum - $amountReceived;
223
            if ($pendingAmount < 0) {
224
                $pendingAmount = 0;
225
            }
226
            $extraAmt = $this->getExtraAmt($id);
227
            $client = $this->user->where('id', $id)->first();
228
229
            // $client = "";
230
            $currency = $client->currency;
231
            $orders = $order->where('client', $id)->get();
232
            $comments = $client->comments()->where('user_id', $client->id)->get();
233
234
            return view('themes.default1.user.client.show',
235
                compact('id', 'client', 'invoices', 'model_popup', 'orders',
236
                 'payments', 'invoiceSum', 'amountReceived', 'pendingAmount', 'currency', 'extraAmt', 'comments'));
237
        } catch (\Exception $ex) {
238
            app('log')->info($ex->getMessage());
239
            Bugsnag::notifyException($ex);
240
241
            return redirect()->back()->with('fails', $ex->getMessage());
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...ls', $ex->getMessage()) also could return the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type Response.
Loading history...
242
        }
243
    }
244
245
    public function getOrderDetail($id)
246
    {
247
        $client = $this->user->where('id', $id)->first();
248
        $responseData = [];
249
        foreach ($client->order()->orderBy('created_at', 'desc')->get() as $order) {
250
            $date = $order->created_at;
251
            $productName = $order->product()->first() && $order->product()->first()->name ?
252
            $order->product()->first()->name : 'Unknown';
253
            $number = $order->number;
254
            $price = $order->price_override;
255
            $status = $order->order_status;
256
            array_push($responseData, (['date'=> $date, 'productName'=>$productName,
257
                'number'                      => $number, 'price' =>$price, 'status'=>$status, ]));
258
        }
259
260
        return $responseData;
261
    }
262
263
    //Get Paymetn Details on Invoice Page
264
    public function getPaymentDetail($id)
265
    {
266
        $client = $this->user->where('id', $id)->first();
267
        $invoice = new Invoice();
268
        $invoices = $invoice->where('user_id', $id)->orderBy('created_at', 'desc')->get();
269
        $extraAmt = $this->getExtraAmt($id);
270
        $date = '';
271
        $responseData = [];
272
        if ($invoices) {
273
            foreach ($client->payment()->orderBy('created_at', 'desc')->get() as $payment) {
274
                $number = $payment->invoice()->first() ? $payment->invoice()->first()->number : '--';
275
                $date = $payment->created_at;
276
                $date1 = new DateTime($date);
277
                $tz = \Auth::user()->timezone()->first()->name;
278
                $date1->setTimezone(new DateTimeZone($tz));
279
                $date = $date1->format('M j, Y, g:i a ');
280
                $pay_method = $payment->payment_method;
281
                if ($payment->invoice_id == 0) {
282
                    $amount = $extraAmt;
283
                } else {
284
                    $amount = $payment->amount;
285
                }
286
                $status = ucfirst($payment->payment_status);
287
                array_push($responseData, (['number'=>$number, 'pay_method'=>$pay_method, 'amount'=>$amount, 'status'=>$status, 'date'=>$date]));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 145 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
288
            }
289
        }
290
291
        return $responseData;
292
    }
293
294
    public function getClientDetail($id)
295
    {
296
        $client = $this->user->where('id', $id)->first();
297
        $currency = $client->currency;
298
        if (array_key_exists('name', \App\Http\Controllers\Front\CartController::getStateByCode($client->state))) {
299
            $client->state = \App\Http\Controllers\Front\CartController::getStateByCode($client->state)['name'];
300
        }
301
        $client->country = ucwords(strtolower(\App\Http\Controllers\Front\CartController::getCountryByCode($client->country)));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
302
303
        $displayData = (['currency'=>$currency, 'client'=> $client]);
304
305
        return $displayData;
306
    }
307
308
    public function getExtraAmt($userId)
309
    {
310
        try {
311
            $amounts = Payment::where('user_id', $userId)
312
            ->where('invoice_id', 0)
313
            ->select('amt_to_credit')->get();
314
            $paidSum = 0;
315
            foreach ($amounts as $amount) {
316
                $paidSum = $paidSum + $amount->amt_to_credit;
317
            }
318
319
            return $paidSum;
320
        } catch (\Exception $ex) {
321
            app('log')->useDailyFiles(storage_path().'/logs/laravel.log');
322
            app('log')->info($ex->getMessage());
323
            Bugsnag::notifyException($ex);
324
325
            return redirect()->back()->with('fails', $ex->getMessage());
326
        }
327
    }
328
329
    /**
330
     * Get total Amount paid for a particular invoice.
331
     */
332
    public function getAmountPaid($userId)
333
    {
334
        try {
335
            $amounts = Payment::where('user_id', $userId)->select('amount')->get();
336
            $paidSum = 0;
337
            foreach ($amounts as $amount) {
338
                if ($amount) {
339
                    $paidSum = $paidSum + $amount->amount;
340
                }
341
            }
342
343
            return $paidSum;
344
        } catch (\Exception $ex) {
345
            app('log')->info($ex->getMessage());
346
            Bugsnag::notifyException($ex);
347
348
            return redirect()->back()->with('fails', $ex->getMessage());
349
        }
350
    }
351
352
    /**
353
     * Get total of the Invoices for a User.
354
     */
355
    public function getTotalInvoice($invoices)
356
    {
357
        $sum = 0;
358
        foreach ($invoices as $invoice) {
359
            $sum = $sum + $invoice->grand_total;
360
        }
361
362
        return $sum;
363
    }
364
365
    /**
366
     * Show the form for editing the specified resource.
367
     *
368
     * @param int $id
369
     *
370
     * @return \Response
371
     */
372
    public function edit($id)
373
    {
374
        try {
375
            $user = $this->user->where('id', $id)->first();
376
            $timezonesList = \App\Model\Common\Timezone::get();
377
            foreach ($timezonesList as $timezone) {
378
                $location = $timezone->location;
379
                if ($location) {
380
                    $start = strpos($location, '(');
381
                    $end = strpos($location, ')', $start + 1);
382
                    $length = $end - $start;
383
                    $result = substr($location, $start + 1, $length - 1);
384
                    $display[] = (['id'=>$timezone->id, 'name'=> '('.$result.')'.' '.$timezone->name]);
385
                }
386
            }
387
            //for display
388
            $timezones = array_column($display, 'name', 'id');
389
            $state = \App\Http\Controllers\Front\CartController::getStateByCode($user->state);
390
            $managers = User::where('role', 'admin')
391
            ->where('position', 'manager')
392
            ->pluck('first_name', 'id')->toArray();
393
            $selectedCurrency = Currency::where('code', $user->currency)
394
            ->pluck('name', 'code')->toArray();
395
            $selectedCompany = \DB::table('company_types')->where('short', $user->company_type)
396
            ->pluck('name', 'short')->toArray();
397
            $selectedIndustry = \App\Model\Common\Bussiness::where('short', $user->bussiness)
398
            ->pluck('name', 'short')->toArray();
399
            $selectedCompanySize = \DB::table('company_sizes')->where('short', $user->company_size)
400
            ->pluck('name', 'short')->toArray();
401
            $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($user->country);
402
403
            $bussinesses = \App\Model\Common\Bussiness::pluck('name', 'short')->toArray();
404
405
            return view('themes.default1.user.client.edit',
406
                compact('bussinesses', 'user', 'timezones', 'state',
407
                    'states', 'managers', 'selectedCurrency', 'selectedCompany',
408
                     'selectedIndustry', 'selectedCompanySize'));
409
        } catch (\Exception $ex) {
410
            app('log')->error($ex->getMessage());
411
412
            return redirect()->back()->with('fails', $ex->getMessage());
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...ls', $ex->getMessage()) also could return the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type Response.
Loading history...
413
        }
414
    }
415
416
    /**
417
     * Update the specified resource in storage.
418
     *
419
     * @param int $id
420
     *
421
     * @return \Response
422
     */
423
    public function update($id, Request $request)
424
    {
425
        try {
426
            $user = $this->user->where('id', $id)->first();
427
            $symbol = Currency::where('code', $request->input('currency'))->pluck('symbol')->first();
428
            $user->currency_symbol = $symbol;
429
            $user->fill($request->input())->save();
430
            $licenseStatus = StatusSetting::pluck('license_status')->first();
431
            if ($licenseStatus == 1) {
432
                $editUserInLicensing = $this->licensing->editUserInLicensing($user->first_name, $user->last_name, $user->email);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $editUserInLicensing is correct as $this->licensing->editUs...ast_name, $user->email) targeting App\Http\Controllers\Lic...::editUserInLicensing() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 128 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
433
            }
434
435
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...updated-successfully')) also could return the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type Response.
Loading history...
436
        } catch (\Exception $ex) {
437
            app('log')->error($ex->getMessage());
438
            Bugsnag::notifyException($ex);
439
440
            return redirect()->back()->with('fails', $ex->getMessage());
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...ls', $ex->getMessage()) also could return the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type Response.
Loading history...
441
        }
442
    }
443
444
    /**
445
     * Remove the specified resource from storage.
446
     *
447
     * @param int $id
448
     *
449
     * @return \Response
450
     */
451
    public function destroy(Request $request)
452
    {
453
        $ids = $request->input('select');
454
        if (!empty($ids)) {
455
            foreach ($ids as $id) {
456
                $user = $this->user->where('id', $id)->first();
457
                if ($user) {
458
                    $user->delete();
459
                } else {
460
                    echo "<div class='alert alert-success alert-dismissable'>
461
                    <i class='fa fa-ban'></i>
462
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
463
                    /* @scrutinizer ignore-type */
464
                    \Lang::get('message.success').'
465
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
466
                        './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
467
                </div>';
468
                    //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
469
                }
470
            }
471
            echo "<div class='alert alert-success alert-dismissable'>
472
                    <i class='fa fa-ban'></i>
473
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert')
474
                    .'!</b> './* @scrutinizer ignore-type */
475
                    \Lang::get('message.success').'
476
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
477
                        './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
478
                </div>';
479
        } else {
480
            echo "<div class='alert alert-success alert-dismissable'>
481
                    <i class='fa fa-ban'></i>
482
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '
483
                    ./* @scrutinizer ignore-type */\Lang::get('message.success').'
484
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
485
                        './* @scrutinizer ignore-type */\Lang::get('message.select-a-row').'
486
                </div>';
487
        }
488
    }
489
490
    public function getUsers(Request $request)
491
    {
492
        $options = $this->user
493
//->where('email','LIKE','%'.$s.'%')
494
                ->select('email AS text', 'id AS value')
495
                ->get();
496
497
        return response()->json(compact('options'));
498
    }
499
500
    public function search(Request $request)
501
    {
502
        try {
503
            $term = trim($request->q);
504
            if (empty($term)) {
505
                return \Response::json([]);
506
            }
507
            $users = User::where('email', 'LIKE', '%'.$term.'%')
508
             ->orWhere('first_name', 'LIKE', '%'.$term.'%')
509
             ->orWhere('last_name', 'LIKE', '%'.$term.'%')
510
             ->select('id', 'email', 'profile_pic', 'first_name', 'last_name')->get();
511
            $formatted_tags = [];
512
513
            foreach ($users as $user) {
514
                $formatted_users[] = ['id' => $user->id, 'text' => $user->email, 'profile_pic' => $user->profile_pic,
515
            'first_name'                   => $user->first_name, 'last_name' => $user->last_name, ];
516
            }
517
518
            return \Response::json($formatted_users);
519
        } catch (\Exception $e) {
520
            // returns if try fails with exception meaagse
521
            return redirect()->back()->with('fails', $e->getMessage());
522
        }
523
    }
524
525
    public function advanceSearch($name = '', $username = '', $company = '',
526
     $mobile = '', $email = '', $country = '', $industry = '',
527
      $company_type = '', $company_size = '', $role = '', $position = '', $reg_from = '', $reg_till = '')
528
    {
529
        $join = \DB::table('users');
530
        $join = $this->getNamUserCom($join, $name, $username, $company);
531
        $join = $this->getMobEmCoun($join, $mobile, $email, $country);
532
        $join = $this->getInCtCs($join, $industry, $company_type, $company_size);
533
        $join = $this->getRolPos($join, $role, $position);
534
        $join = $this->getregFromTill($join, $reg_from, $reg_till);
535
536
        $join = $join->orderBy('created_at', 'desc')
537
        ->select('id', 'first_name', 'last_name', 'email', 'created_at',
538
         'active', 'mobile_verified', 'role', 'position');
539
540
        return $join;
541
    }
542
543
    public function sendWelcomeMail($user)
544
    {
545
        $activate_model = new AccountActivate();
546
        $str = str_random(40);
547
        $activate = $activate_model->create(['email' => $user->email, 'token' => $str]);
548
        $token = $activate->token;
549
        $url = url("activate/$token");
550
        //check in the settings
551
        $settings = new \App\Model\Common\Setting();
552
        $setting = $settings->where('id', 1)->first();
553
        //template
554
        $templates = new \App\Model\Common\Template();
555
        $temp_id = $setting->welcome_mail;
556
        $template = $templates->where('id', $temp_id)->first();
557
        $from = $setting->email;
558
        $to = $user->email;
559
        $subject = $template->name;
560
        $data = $template->data;
561
        $replace = ['name' => $user->first_name.' '.$user->last_name,
562
        'username'         => $user->email, 'password' => $str, 'url' => $url, ];
563
        $type = '';
564
        if ($template) {
565
            $type_id = $template->type;
566
            $temp_type = new \App\Model\Common\TemplateType();
567
            $type = $temp_type->where('id', $type_id)->first()->name;
568
        }
569
        //dd($type);
570
        $templateController = new \App\Http\Controllers\Common\TemplateController();
571
        $mail = $templateController->mailing($from, $to, $data, $subject, $replace, $type);
572
573
        return $mail;
574
    }
575
}
576