Completed
Push — development ( c2ff46...95cb59 )
by Ashutosh
28:29 queued 18:30
created

AdvanceSearchController::getExtraAmt()   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\User;
6
use App\Model\Order\Payment;
7
use App\Http\Controllers\Controller;
8
9
class AdvanceSearchController extends Controller
10
{
11
    /**
12
     * Serach for Mobile,Email,Country.
13
     */
14
    public function getMobEmCoun($join, $mobile, $email, $country)
15
    {
16
        if ($mobile) {
17
            $join = $join->where('mobile', $mobile);
18
        }
19
        if ($email) {
20
            $join = $join->where('email', 'LIKE', '%'.$email.'%');
21
        }
22
        if ($country) {
23
            $join = $join->where('country', $country);
24
        }
25
26
        return $join;
27
    }
28
29
    /**
30
     * Serach for industry,company_type,company_size.
31
     */
32
    public function getInCtCs($join, $industry, $company_type, $company_size)
33
    {
34
        if ($industry) {
35
            $join = $join->where('bussiness', $industry);
36
        }
37
        if ($company_type) {
38
            $join = $join->where('company_type', $company_type);
39
        }
40
        if ($company_size) {
41
            $join = $join->where('company_size', $company_size);
42
        }
43
44
        return $join;
45
    }
46
47
    /**
48
     * Serach for Role,Position.
49
     */
50
    public function getRolPos($join, $role, $position)
51
    {
52
        if ($role) {
53
            $join = $join->where('role', $role);
54
        }
55
        if ($position) {
56
            $join = $join->where('position', $position);
57
        }
58
59
        return $join;
60
    }
61
62
    /**
63
     * Serach for Registered From,tILL.
64
     */
65
    public function getregFromTill($join, $reg_from, $reg_till)
66
    {
67
        if ($reg_from) {
68
            $fromdate = date_create($reg_from);
69
70
            $from = date_format($fromdate, 'Y-m-d H:m:i');
71
            $tills = date('Y-m-d H:m:i');
72
            $cont = new \App\Http\Controllers\Order\ExtendedOrderController();
73
            $tillDate = $cont->getTillDate($from, $reg_till, $tills);
74
            $join = $join->whereBetween('created_at', [$from, $tillDate]);
75
        }
76
        if ($reg_till) {
77
            $tilldate = date_create($reg_till);
78
            $till = date_format($tilldate, 'Y-m-d H:m:i');
79
            $froms = User::first()->created_at;
80
            $cont = new \App\Http\Controllers\Order\ExtendedOrderController();
81
            $fromDate = $cont->getFromDate($reg_from, $froms);
82
            $join = $join->whereBetween('created_at', [$fromDate, $till]);
83
        }
84
85
        return $join;
86
    }
87
88
    /**
89
     * Serach for Name,UserName,Company.
90
     */
91
    public function getNamUserCom($join, $name, $username, $company)
92
    {
93
        if ($name) {
94
            $join = $join->where('first_name', 'LIKE', '%'.$name.'%')
95
                    ->orWhere('last_name', 'LIKE', '%'.$name.'%');
96
        }
97
        if ($username) {
98
            $join = $join->where('user_name', 'LIKE', '%'.$username.'%');
99
        }
100
        if ($company) {
101
            $join = $join->where('company', 'LIKE', '%'.$company.'%');
102
        }
103
104
        return $join;
105
    }
106
107
    public function advanceSearch(
108
        $name = '',
109
        $username = '',
110
        $company = '',
111
        $mobile = '',
112
        $email = '',
113
        $country = '',
114
        $industry = '',
115
        $company_type = '',
116
        $company_size = '',
117
        $role = '',
118
        $position = '',
119
        $reg_from = '',
120
        $reg_till = ''
121
    ) {
122
        $join = \DB::table('users');
123
        $join = $this->getNamUserCom($join, $name, $username, $company);
124
        $join = $this->getMobEmCoun($join, $mobile, $email, $country);
125
        $join = $this->getInCtCs($join, $industry, $company_type, $company_size);
126
        $join = $this->getRolPos($join, $role, $position);
127
        $join = $this->getregFromTill($join, $reg_from, $reg_till);
128
129
        $join = $join->orderBy('created_at', 'desc')
130
        ->select(
131
            'id',
132
            'first_name',
133
            'last_name',
134
            'email',
135
            'created_at',
136
            'active',
137
            'mobile_verified',
138
            'role',
139
            'position'
140
        );
141
142
        return $join;
143
    }
144
145
    public function search(Request $request)
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\User\Request was not found. Did you mean Request? If so, make sure to prefix the type with \.
Loading history...
146
    {
147
        try {
148
            $term = trim($request->q);
149
            if (empty($term)) {
150
                return \Response::json([]);
151
            }
152
            $users = User::where('email', 'LIKE', '%'.$term.'%')
153
             ->orWhere('first_name', 'LIKE', '%'.$term.'%')
154
             ->orWhere('last_name', 'LIKE', '%'.$term.'%')
155
             ->select('id', 'email', 'profile_pic', 'first_name', 'last_name')->get();
156
            $formatted_tags = [];
157
158
            foreach ($users as $user) {
159
                $formatted_users[] = ['id'     => $user->id, 'text' => $user->email, 'profile_pic' => $user->profile_pic,
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 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...
160
                'first_name'                   => $user->first_name, 'last_name' => $user->last_name, ];
161
            }
162
163
            return \Response::json($formatted_users);
164
        } catch (\Exception $e) {
165
            // returns if try fails with exception meaagse
166
            return redirect()->back()->with('fails', $e->getMessage());
167
        }
168
    }
169
170
    public function getUsers(Request $request)
171
    {
172
        $options = $this->user
173
                ->select('email AS text', 'id AS value')
174
                ->get();
175
176
        return response()->json(compact('options'));
177
    }
178
179
    public function getClientDetail($id)
180
    {
181
        $client = $this->user->where('id', $id)->first();
182
        $currency = $client->currency;
183
        if (array_key_exists('name', \App\Http\Controllers\Front\CartController::getStateByCode($client->state))) {
184
            $client->state = \App\Http\Controllers\Front\CartController::getStateByCode($client->state)['name'];
185
        }
186
        $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...
187
188
        $displayData = (['currency'=>$currency, 'client'=> $client]);
189
190
        return $displayData;
191
    }
192
193
    public function getExtraAmt($userId)
194
    {
195
        try {
196
            $amounts = Payment::where('user_id', $userId)->where('invoice_id', 0)->select('amt_to_credit')->get();
197
            $balance = 0;
198
            foreach ($amounts as $amount) {
199
                if ($amount) {
200
                    $balance = $balance + $amount->amt_to_credit;
201
                }
202
            }
203
204
            return $balance;
205
        } catch (\Exception $ex) {
206
            app('log')->info($ex->getMessage());
207
            Bugsnag::notifyException($ex);
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\User\Bugsnag was not found. Did you mean Bugsnag? If so, make sure to prefix the type with \.
Loading history...
208
209
            return redirect()->back()->with('fails', $ex->getMessage());
210
        }
211
    }
212
}
213