OrderController::show()   B
last analyzed

Complexity

Conditions 9
Paths 169

Size

Total Lines 47
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
dl 0
loc 47
rs 7.4805
c 1
b 0
f 0
cc 9
nc 169
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Order;
4
5
use App\Http\Requests\Order\OrderRequest;
6
use App\Model\Common\StatusSetting;
7
use App\Model\Order\Invoice;
8
use App\Model\Order\InvoiceItem;
9
use App\Model\Order\Order;
10
use App\Model\Payment\Plan;
11
use App\Model\Payment\Promotion;
12
use App\Model\Product\Price;
13
use App\Model\Product\Product;
14
use App\Model\Product\ProductUpload;
15
use App\Model\Product\Subscription;
16
use App\User;
17
use Bugsnag;
18
use Illuminate\Http\Request;
19
20
class OrderController extends BaseOrderController
21
{
22
    // NOTE FROM AVINASH: utha le re deva
23
    // NOTE: don't lose hope.
24
    public $order;
25
    public $user;
26
    public $promotion;
27
    public $product;
28
    public $subscription;
29
    public $invoice;
30
    public $invoice_items;
31
    public $price;
32
    public $plan;
33
34
    public function __construct()
35
    {
36
        $this->middleware('auth');
37
        $this->middleware('admin');
38
39
        $order = new Order();
40
        $this->order = $order;
41
42
        $user = new User();
43
        $this->user = $user;
44
45
        $promotion = new Promotion();
46
        $this->promotion = $promotion;
47
48
        $product = new Product();
49
        $this->product = $product;
50
51
        $subscription = new Subscription();
52
        $this->subscription = $subscription;
53
54
        $invoice = new Invoice();
55
        $this->invoice = $invoice;
56
57
        $invoice_items = new InvoiceItem();
58
        $this->invoice_items = $invoice_items;
59
60
        $plan = new Plan();
61
        $this->plan = $plan;
62
63
        $price = new Price();
64
        $this->price = $price;
65
66
        $product_upload = new ProductUpload();
67
        $this->product_upload = $product_upload;
68
    }
69
70
    /**
71
     * Display a listing of the resource.
72
     * @param Request $request
73
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
74
     */
75
    public function index(Request $request)
76
    {
77
        $validator = \Validator::make($request->all(), [
78
            'sub_from'     => 'nullable',
79
            'sub_till'     => 'nullable|after:sub_from',
80
            'expiry'       => 'nullable',
81
            'expiryTill'     => 'nullable|after:expiry',
82
            'from'          => 'nullable',
83
            'till'          => 'nullable|after:from',
84
85
        ]);
86
        if ($validator->fails()) {
87
            $request->sub_from = '';
0 ignored issues
show
Bug introduced by
The property sub_from does not seem to exist on Illuminate\Http\Request.
Loading history...
88
            $request->sub_till = '';
0 ignored issues
show
Bug introduced by
The property sub_till does not seem to exist on Illuminate\Http\Request.
Loading history...
89
            $request->expiry = '';
0 ignored issues
show
Bug introduced by
The property expiry does not seem to exist on Illuminate\Http\Request.
Loading history...
90
            $request->expiryTill = '';
0 ignored issues
show
Bug introduced by
The property expiryTill does not seem to exist on Illuminate\Http\Request.
Loading history...
91
            $request->from = '';
0 ignored issues
show
Bug introduced by
The property from does not seem to exist on Illuminate\Http\Request.
Loading history...
92
            $request->till = '';
0 ignored issues
show
Bug introduced by
The property till does not seem to exist on Illuminate\Http\Request.
Loading history...
93
94
            return redirect('orders')->with('fails', 'Start date should be before end date');
95
        }
96
        try {
97
            $products = $this->product->where('id', '!=', 1)->pluck('name', 'id')->toArray();
98
99
            $paidUnpaidOptions = ['paid'=>'Paid Products', 'unpaid'=>'Unpaid Products'];
100
            $insNotIns = ['installed'=>'Yes (Installed atleast once)', 'not_installed'=>'No (Not Installed)'];
101
            $activeInstallationOptions = ['paid_ins'=>'Active installation'];
102
            $inactiveInstallationOptions = ['paid_inactive_ins'=>'Inactive installation'];
103
            $renewal = ['expired_subscription'=>'Expired Subscriptions', 'active_subscription'=> 'Active Subscriptions'];
104
            $allVersions = Subscription::where('version', '!=', '')->whereNotNull('version')
105
                ->orderBy('version', 'desc')->groupBy('version')
106
                ->pluck('version')->toArray();
107
108
            return view('themes.default1.order.index',
109
                compact('request', 'products', 'allVersions', 'activeInstallationOptions', 'paidUnpaidOptions', 'inactiveInstallationOptions', 'renewal', 'insNotIns'));
110
        } catch (\Exception $e) {
111
            Bugsnag::notifyExeption($e);
0 ignored issues
show
Bug introduced by
The method notifyExeption() does not exist on Bugsnag. Did you maybe mean notify()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
            Bugsnag::/** @scrutinizer ignore-call */ 
112
                     notifyExeption($e);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
112
113
            return redirect('orders')->with('fails', $e->getMessage());
114
        }
115
    }
116
117
    public function getOrders(Request $request)
118
    {
119
        $orderSearch = new OrderSearchController();
120
        $query = $orderSearch->advanceOrderSearch($request);
121
122
        return \DataTables::of($query)
123
            ->setTotalRecords($query->count())
124
            ->addColumn('checkbox', function ($model) {
125
                return "<input type='checkbox' class='order_checkbox' value=".$model->id.' name=select[] id=check>';
126
            })
127
            ->addColumn('client', function ($model) {
128
                return '<a href='.url('clients/'.$model->client_id).'>'.ucfirst($model->client_name).'<a>';
129
            })
130
            ->addColumn('product_name', function ($model) {
131
                return $model->product_name;
132
            })
133
            ->addColumn('version', function ($model) {
134
                return getVersionAndLabel($model->product_version, $model->product);
135
            })
136
            ->addColumn('number', function ($model) {
137
                $orderLink = '<a href='.url('orders/'.$model->id).'>'.$model->number.'</a>';
138
                if ($model->subscription_updated_at) {//For few older clients subscription was not generated, so no updated_at column exists
139
                    $orderLink = '<a href='.url('orders/'.$model->id).'>'.$model->number.'</a>'.installationStatusLabel($model->subscription_updated_at, $model->subscription_created_at);
140
                }
141
142
                return $orderLink;
143
            })
144
            ->addColumn('order_status', function ($model) {
145
                return ucfirst($model->order_status);
146
            })
147
            ->addColumn('order_date', function ($model) {
148
                return getDateHtml($model->created_at);
149
            })
150
            ->addColumn('update_ends_at', function ($model) {
151
                $ends_at = strtotime($model->subscription_ends_at) > 1 ? $model->subscription_ends_at : '--';
152
153
                return getExpiryLabel($ends_at);
154
            })
155
            ->addColumn('action', function ($model) {
156
                $status = $this->checkInvoiceStatusByOrderId($model->id);
157
158
                return $this->getUrl($model, $status, $model->subscription_id);
159
            })
160
161
            ->filterColumn('client', function ($query, $keyword) {
162
                $query->whereRaw("concat(first_name, ' ', last_name) like ?", ["%$keyword%"]);
163
            })
164
            ->filterColumn('product_name', function ($query, $keyword) {
165
                $query->whereRaw('products.name like ?', ["%$keyword%"]);
166
            })
167
            ->filterColumn('version', function ($query, $keyword) {
168
                $query->whereRaw('subscriptions.version like ?', ["%$keyword%"]);
169
            })
170
            ->filterColumn('number', function ($query, $keyword) {
171
                $query->whereRaw('number like ?', ["%{$keyword}%"]);
172
            })
173
            ->filterColumn('price_override', function ($query, $keyword) {
174
                $query->whereRaw('price_override like ?', ["%{$keyword}%"]);
175
            })
176
            ->filterColumn('order_status', function ($query, $keyword) {
177
                $query->whereRaw('order_status like ?', ["%{$keyword}%"]);
178
            })
179
180
            ->orderColumn('order_date', 'orders.created_at $1')
181
            ->orderColumn('client', 'client_name $1')
182
            ->orderColumn('product_name', 'product_name $1')
183
            ->orderColumn('version', 'product_version $1')
184
            ->orderColumn('number', 'number $1')
185
            ->orderColumn('price_override', 'price_override $1')
186
            ->orderColumn('order_status', 'order_status $1')
187
            ->orderColumn('update_ends_at', 'update_ends_at $1')
188
189
            ->rawColumns(['checkbox', 'date', 'client', 'version', 'number', 'order_status', 'order_date', 'update_ends_at', 'action'])
190
            ->make(true);
191
    }
192
193
    /**
194
     * Show the form for creating a new resource.
195
     *
196
     * @return \Response
197
     */
198
    public function create()
199
    {
200
        try {
201
            $clients = $this->user->pluck('first_name', 'id')->toArray();
202
            $product = $this->product->pluck('name', 'id')->toArray();
203
            $subscription = $this->subscription->pluck('name', 'id')->toArray();
204
            $promotion = $this->promotion->pluck('code', 'id')->toArray();
205
206
            return view('themes.default1.order.create', compact('clients', 'product', 'subscription', 'promotion'));
207
        } catch (\Exception $e) {
208
            Bugsnag::notifyExeption($e);
209
210
            return redirect()->back()->with('fails', $e->getMessage());
211
        }
212
    }
213
214
    /**
215
     * Store a newly created resource in storage.
216
     *
217
     * @return \Response
218
     */
219
    public function show($id)
220
    {
221
        try {
222
            $order = $this->order->findOrFail($id);
223
            if (User::onlyTrashed()->find($order->client)) {//If User is soft deleted for this order
224
                throw new \Exception('The user for this order is suspended from the system. Restore the user to view order details.');
225
            }
226
            $subscription = $order->subscription()->first();
227
228
            $date = '--';
229
            $licdate = '--';
230
            $supdate = '--';
231
            $connectionLabel = '--';
232
            $lastActivity = '--';
233
            $versionLabel = '--';
234
            if ($subscription) {
235
                $date = strtotime($subscription->update_ends_at) > 1 ? getExpiryLabel($subscription->update_ends_at) : '--';
236
                $licdate = strtotime($subscription->ends_at) > 1 ? getExpiryLabel($subscription->ends_at) : '--';
237
                $supdate = strtotime($subscription->support_ends_at) > 1 ? getExpiryLabel($subscription->support_ends_at) : '--';
238
                $lastActivity = getDateHtml($subscription->updated_at).'&nbsp;'.installationStatusLabel($subscription->updated_at, $subscription->created_at);
239
                $versionLabel = getVersionAndLabel($subscription->version, $order->product);
240
            }
241
            $invoice = $this->invoice->where('id', $order->invoice_id)->first();
242
243
            if (! $invoice) {
244
                return redirect()->back()->with('fails', 'no orders');
245
            }
246
            $user = $this->user->find($invoice->user_id);
247
            $licenseStatus = StatusSetting::pluck('license_status')->first();
248
            $installationDetails = [];
249
            $noOfAllowedInstallation = '';
250
            $getInstallPreference = '';
251
            if ($licenseStatus == 1) {
252
                $cont = new \App\Http\Controllers\License\LicenseController();
253
                $installationDetails = $cont->searchInstallationPath($order->serial_key, $order->product);
254
                $noOfAllowedInstallation = $cont->getNoOfAllowedInstallation($order->serial_key, $order->product);
255
                $getInstallPreference = $cont->getInstallPreference($order->serial_key, $order->product);
256
            }
257
258
            $allowDomainStatus = StatusSetting::pluck('domain_check')->first();
259
260
            return view('themes.default1.order.show',
261
                compact('user', 'order', 'subscription', 'licenseStatus', 'installationDetails', 'allowDomainStatus', 'noOfAllowedInstallation', 'getInstallPreference', 'lastActivity', 'versionLabel', 'date', 'licdate', 'supdate'));
262
        } catch (\Exception $ex) {
263
            Bugsnag::notifyException($ex);
264
265
            return redirect()->back()->with('fails', $ex->getMessage());
266
        }
267
    }
268
269
    /**
270
     * Show the form for editing the specified resource.
271
     *
272
     * @param int $id
273
     *
274
     * @return \Response
275
     */
276
    public function edit($id)
277
    {
278
        try {
279
            $order = $this->order->where('id', $id)->first();
280
            $clients = $this->user->pluck('first_name', 'id')->toArray();
281
            $product = $this->product->pluck('name', 'id')->toArray();
282
            $subscription = $this->subscription->pluck('name', 'id')->toArray();
283
            $promotion = $this->promotion->pluck('code', 'id')->toArray();
284
285
            return view('themes.default1.order.edit',
286
                compact('clients', 'product', 'subscription', 'promotion', 'order'));
287
        } catch (\Exception $e) {
288
            Bugsnag::notifyExeption($e);
289
290
            return redirect()->back()->with('fails', $e->getMessage());
291
        }
292
    }
293
294
    /**
295
     * Update the specified resource in storage.
296
     *
297
     * @param int $id
298
     *
299
     * @return \Response
300
     */
301
    public function update($id, OrderRequest $request)
302
    {
303
        try {
304
            $order = $this->order->where('id', $id)->first();
305
            $order->fill($request->input())->save();
306
307
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
308
        } catch (\Exception $e) {
309
            Bugsnag::notifyExeption($e);
310
311
            return redirect()->back()->with('fails', $e->getMessage());
312
        }
313
    }
314
315
    /**
316
     * Remove the specified resource from storage.
317
     *
318
     * @param int $id
319
     *
320
     * @return \Response
321
     */
322
    public function destroy(Request $request)
323
    {
324
        try {
325
            // dd('df');
326
            $ids = $request->input('select');
327
            if (! empty($ids)) {
328
                foreach ($ids as $id) {
329
                    $order = $this->order->where('id', $id)->first();
330
                    if ($order) {
331
                        $order->delete();
332
                    } else {
333
                        echo "<div class='alert alert-danger alert-dismissable'>
334
                    <i class='fa fa-ban'></i>
335
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
336
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
337
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
338
                        './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
339
                </div>';
340
                        //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
341
                    }
342
                }
343
                echo "<div class='alert alert-success alert-dismissable'>
344
                    <i class='fa fa-ban'></i>
345
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
346
                    /* @scrutinizer ignore-type */\Lang::get('message.success').'
347
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
348
                        './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
349
                </div>';
350
            } else {
351
                echo "<div class='alert alert-danger alert-dismissable'>
352
                    <i class='fa fa-ban'></i>
353
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
354
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
355
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
356
                        './* @scrutinizer ignore-type */ \Lang::get('message.select-a-row').'
357
                </div>';
358
                //echo \Lang::get('message.select-a-row');
359
            }
360
        } catch (\Exception $e) {
361
            echo "<div class='alert alert-danger alert-dismissable'>
362
                    <i class='fa fa-ban'></i>
363
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
364
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
365
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
366
                        '.$e->getMessage().'
367
                </div>';
368
        }
369
    }
370
371
    public function plan($invoice_item_id)
372
    {
373
        try {
374
            $planid = 0;
375
            $item = $this->invoice_items->find($invoice_item_id);
376
            if ($item) {
377
                $planid = $item->plan_id;
378
            }
379
380
            return $planid;
381
        } catch (\Exception $ex) {
382
            Bugsnag::notifyException($ex);
383
384
            throw new \Exception($ex->getMessage());
385
        }
386
    }
387
388
    public function checkInvoiceStatusByOrderId($orderid)
389
    {
390
        try {
391
            $status = 'pending';
392
            $order = $this->order->find($orderid);
393
            if ($order) {
394
                $invoiceid = $order->invoice_id;
395
                $invoice = $this->invoice->find($invoiceid);
396
                if ($invoice) {
397
                    if ($invoice->status == 'Success') {
398
                        $status = 'success';
399
                    }
400
                }
401
            }
402
403
            return $status;
404
        } catch (\Exception $ex) {
405
            Bugsnag::notifyException($ex);
406
407
            throw new \Exception($ex->getMessage());
408
        }
409
    }
410
411
    public function product($itemid)
412
    {
413
        $invoice_items = new InvoiceItem();
414
        $invoice_item = $invoice_items->find($itemid);
415
        $product = $invoice_item->product_name;
416
417
        return $product;
418
    }
419
420
    public function subscription($orderid)
421
    {
422
        $sub = $this->subscription->where('order_id', $orderid)->first();
423
424
        return $sub;
425
    }
426
427
    public function expiry($orderid)
428
    {
429
        $sub = $this->subscription($orderid);
430
        if ($sub) {
431
            return $sub->update_ends_at;
432
        }
433
434
        return '';
435
    }
436
437
    public function renew($orderid)
438
    {
439
        //$sub = $this->subscription($orderid);
440
        return url('my-orders');
441
    }
442
}
443