Completed
Push — development ( 877883...280d38 )
by Ashutosh
10:04
created

OrderController::index()   A

Complexity

Conditions 2
Paths 10

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 19
rs 9.7666
c 0
b 0
f 0
cc 2
nc 10
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Order;
4
5
use App\Http\Requests\Order\OrderRequest;
6
use App\Model\Order\Invoice;
7
use App\Model\Order\InvoiceItem;
8
use App\Model\Order\Order;
9
use App\Model\Payment\Plan;
10
use App\Model\Payment\Promotion;
11
use App\Model\Product\Price;
12
use App\Model\Product\Product;
13
use App\Model\Product\ProductUpload;
14
use App\Model\Product\Subscription;
15
use App\User;
16
use Bugsnag;
17
use Illuminate\Http\Request;
18
19
class OrderController extends BaseOrderController
20
{
21
    public $order;
22
    public $user;
23
    public $promotion;
24
    public $product;
25
    public $subscription;
26
    public $invoice;
27
    public $invoice_items;
28
    public $price;
29
    public $plan;
30
31
    public function __construct()
32
    {
33
        $this->middleware('auth');
34
        $this->middleware('admin');
35
36
        $order = new Order();
37
        $this->order = $order;
38
39
        $user = new User();
40
        $this->user = $user;
41
42
        $promotion = new Promotion();
43
        $this->promotion = $promotion;
44
45
        $product = new Product();
46
        $this->product = $product;
47
48
        $subscription = new Subscription();
49
        $this->subscription = $subscription;
50
51
        $invoice = new Invoice();
52
        $this->invoice = $invoice;
53
54
        $invoice_items = new InvoiceItem();
55
        $this->invoice_items = $invoice_items;
56
57
        $plan = new Plan();
58
        $this->plan = $plan;
59
60
        $price = new Price();
61
        $this->price = $price;
62
63
        $product_upload = new ProductUpload();
64
        $this->product_upload = $product_upload;
65
    }
66
67
    /**
68
     * Display a listing of the resource.
69
     *
70
     * @return \Response
71
     */
72
    public function index(Request $request)
73
    {
74
        try {
75
            $products = $this->product->where('id', '!=', 1)->pluck('name', 'id')->toArray();
76
            $order_no = $request->input('order_no');
77
            $product_id = $request->input('product_id');
78
            $expiry = $request->input('expiry');
79
            $expiryTill = $request->input('expiryTill');
80
            $from = $request->input('from');
81
            $till = $request->input('till');
82
            $domain = $request->input('domain');
83
84
            return view('themes.default1.order.index',
85
                compact('products', 'order_no', 'product_id',
86
                    'expiry', 'from', 'till', 'domain', 'expiryTill'));
87
        } catch (\Exception $e) {
88
            Bugsnag::notifyExeption($e);
89
90
            return redirect('orders')->with('fails', $e->getMessage());
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect('orders'...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...
91
        }
92
    }
93
94
    public function getOrders(Request $request)
95
    {
96
        $order_no = $request->input('order_no');
97
        $product_id = $request->input('product_id');
98
        $expiry = $request->input('expiry');
99
        $expiryTill = $request->input('expiryTill');
100
        $from = $request->input('from');
101
        $till = $request->input('till');
102
        $domain = $request->input('domain');
103
        $query = $this->advanceSearch($order_no, $product_id, $expiry, $expiryTill, $from, $till, $domain);
104
105
        return \DataTables::of($query->take(50))
106
                        ->setTotalRecords($query->count())
107
                        ->addColumn('checkbox', function ($model) {
108
                            return "<input type='checkbox' class='order_checkbox' value=".
109
                            $model->id.' name=select[] id=check>';
110
                        })
111
                        ->addColumn('date', function ($model) {
112
                            $date = $model->created_at;
113
114
                            return "<span style='display:none'>$model->id</span>".$date;
115
                        })
116
                        ->addColumn('client', function ($model) {
117
                            $user = $this->user->where('id', $model->client)->first();
118
                            $first = $user->first_name;
119
                            $last = $user->last_name;
120
                            $id = $user->id;
121
122
                            return '<a href='.url('clients/'.$id).'>'.ucfirst($first).' '.ucfirst($last).'<a>';
123
                        })
124
                        ->addColumn('productname', function ($model) {
125
                            $productid = ($model->product);
126
                            $productName = Product::where('id', $productid)->pluck('name')->first();
127
128
                            return $productName;
129
                        })
130
                        ->addColumn('number', function ($model) {
131
                            return ucfirst($model->number);
132
                        })
133
                        ->addColumn('price_override', function ($model) {
134
                            return ucfirst($model->price_override);
135
                        })
136
                        ->addColumn('order_status', function ($model) {
137
                            return ucfirst($model->order_status);
138
                        })
139
                        // ->showColumns('number', 'price_override', 'order_status')
140
                        ->addColumn('update_ends_at', function ($model) {
141
                            $end = $this->getEndDate($model);
142
143
                            return $end;
144
                        })
145
                        ->addColumn('action', function ($model) {
146
                            $sub = $model->subscription()->first();
147
                            $status = $this->checkInvoiceStatusByOrderId($model->id);
148
                            $url = $this->getUrl($model, $status, $sub);
149
150
                            return $url;
151
                        })
152
153
                         ->filterColumn('created_at', function ($query, $keyword) {
154
                             $sql = 'created_at like ?';
155
                             $query->whereRaw($sql, ["%{$keyword}%"]);
156
                         })
157
158
                          ->filterColumn('client', function ($query, $keyword) {
159
                              $sql = 'client like ?';
160
                              $query->whereRaw($sql, ["%{$keyword}%"]);
161
                          })
162
163
                           ->filterColumn('number', function ($query, $keyword) {
164
                               $sql = 'number like ?';
165
                               $query->whereRaw($sql, ["%{$keyword}%"]);
166
                           })
167
                            ->filterColumn('price_override', function ($query, $keyword) {
168
                                $sql = 'price_override like ?';
169
                                $query->whereRaw($sql, ["%{$keyword}%"]);
170
                            })
171
                             ->filterColumn('order_status', function ($query, $keyword) {
172
                                 $sql = 'order_status like ?';
173
                                 $query->whereRaw($sql, ["%{$keyword}%"]);
174
                             })
175
176
                              ->filterColumn('update_ends_at', function ($query, $keyword) {
177
                                  $sql = 'update_ends_at like ?';
178
                                  $query->whereRaw($sql, ["%{$keyword}%"]);
179
                              })
180
181
                         ->rawColumns(['checkbox', 'date', 'client', 'number',
182
                          'price_override', 'order_status', 'productname', 'update_ends_at', 'action', ])
183
                        ->make(true);
184
    }
185
186
    /**
187
     * Show the form for creating a new resource.
188
     *
189
     * @return \Response
190
     */
191
    public function create()
192
    {
193
        try {
194
            $clients = $this->user->pluck('first_name', 'id')->toArray();
195
            $product = $this->product->pluck('name', 'id')->toArray();
196
            $subscription = $this->subscription->pluck('name', 'id')->toArray();
197
            $promotion = $this->promotion->pluck('code', 'id')->toArray();
198
199
            return view('themes.default1.order.create', compact('clients', 'product', 'subscription', 'promotion'));
200
        } catch (\Exception $e) {
201
            Bugsnag::notifyExeption($e);
202
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
     * Store a newly created resource in storage.
209
     *
210
     * @return \Response
211
     */
212
    public function show($id)
213
    {
214
        try {
215
            $order = $this->order->findOrFail($id);
216
            $subscription = $order->subscription()->first();
217
            $invoiceid = $order->invoice_id;
218
            $invoice = $this->invoice->where('id', $invoiceid)->first();
219
            if (!$invoice) {
220
                return redirect()->back()->with('fails', 'no orders');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...h('fails', 'no orders') also could return the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type Response.
Loading history...
221
            }
222
            $invoiceItems = $this->invoice_items->where('invoice_id', $invoiceid)->get();
223
            $user = $this->user->find($invoice->user_id);
224
225
            return view('themes.default1.order.show',
226
                compact('invoiceItems', 'invoice', 'user', 'order', 'subscription'));
227
        } catch (\Exception $ex) {
228
            Bugsnag::notifyException($ex);
229
230
            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...
231
        }
232
    }
233
234
    /**
235
     * Show the form for editing the specified resource.
236
     *
237
     * @param int $id
238
     *
239
     * @return \Response
240
     */
241
    public function edit($id)
242
    {
243
        try {
244
            $order = $this->order->where('id', $id)->first();
245
            $clients = $this->user->pluck('first_name', 'id')->toArray();
246
            $product = $this->product->pluck('name', 'id')->toArray();
247
            $subscription = $this->subscription->pluck('name', 'id')->toArray();
248
            $promotion = $this->promotion->pluck('code', 'id')->toArray();
249
250
            return view('themes.default1.order.edit',
251
                compact('clients', 'product', 'subscription', 'promotion', 'order'));
252
        } catch (\Exception $e) {
253
            Bugsnag::notifyExeption($e);
254
255
            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...
256
        }
257
    }
258
259
    /**
260
     * Update the specified resource in storage.
261
     *
262
     * @param int $id
263
     *
264
     * @return \Response
265
     */
266
    public function update($id, OrderRequest $request)
267
    {
268
        try {
269
            $order = $this->order->where('id', $id)->first();
270
            $order->fill($request->input())->save();
271
272
            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...
273
        } catch (\Exception $e) {
274
            Bugsnag::notifyExeption($e);
275
276
            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...
277
        }
278
    }
279
280
    /**
281
     * Remove the specified resource from storage.
282
     *
283
     * @param int $id
284
     *
285
     * @return \Response
286
     */
287
    public function destroy(Request $request)
288
    {
289
        try {
290
            // dd('df');
291
            $ids = $request->input('select');
292
            if (!empty($ids)) {
293
                foreach ($ids as $id) {
294
                    $order = $this->order->where('id', $id)->first();
295
                    if ($order) {
296
                        $order->delete();
297
                    } else {
298
                        echo "<div class='alert alert-danger alert-dismissable'>
299
                    <i class='fa fa-ban'></i>
300
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
301
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
302
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
303
                        './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
304
                </div>';
305
                        //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
306
                    }
307
                }
308
                echo "<div class='alert alert-success alert-dismissable'>
309
                    <i class='fa fa-ban'></i>
310
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
311
                    /* @scrutinizer ignore-type */\Lang::get('message.success').'
312
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
313
                        './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
314
                </div>';
315
            } else {
316
                echo "<div class='alert alert-danger alert-dismissable'>
317
                    <i class='fa fa-ban'></i>
318
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
319
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
320
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
321
                        './* @scrutinizer ignore-type */ \Lang::get('message.select-a-row').'
322
                </div>';
323
                //echo \Lang::get('message.select-a-row');
324
            }
325
        } catch (\Exception $e) {
326
            echo "<div class='alert alert-danger alert-dismissable'>
327
                    <i class='fa fa-ban'></i>
328
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
329
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
330
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
331
                        '.$e->getMessage().'
332
                </div>';
333
        }
334
    }
335
336
    public function plan($invoice_item_id)
337
    {
338
        try {
339
            $planid = 0;
340
            $item = $this->invoice_items->find($invoice_item_id);
341
            if ($item) {
342
                $planid = $item->plan_id;
343
            }
344
345
            return $planid;
346
        } catch (\Exception $ex) {
347
            Bugsnag::notifyException($ex);
348
349
            throw new \Exception($ex->getMessage());
350
        }
351
    }
352
353
    public function checkInvoiceStatusByOrderId($orderid)
354
    {
355
        try {
356
            $status = 'pending';
357
            $order = $this->order->find($orderid);
358
            if ($order) {
359
                $invoiceid = $order->invoice_id;
360
                $invoice = $this->invoice->find($invoiceid);
361
                if ($invoice) {
362
                    if ($invoice->status == 'Success') {
363
                        $status = 'success';
364
                    }
365
                }
366
            }
367
368
            return $status;
369
        } catch (\Exception $ex) {
370
            Bugsnag::notifyException($ex);
371
372
            throw new \Exception($ex->getMessage());
373
        }
374
    }
375
376
    public function product($itemid)
377
    {
378
        $invoice_items = new InvoiceItem();
379
        $invoice_item = $invoice_items->find($itemid);
380
        $product = $invoice_item->product_name;
381
382
        return $product;
383
    }
384
385
    public function subscription($orderid)
386
    {
387
        $sub = $this->subscription->where('order_id', $orderid)->first();
388
389
        return $sub;
390
    }
391
392
    public function expiry($orderid)
393
    {
394
        $sub = $this->subscription($orderid);
395
        if ($sub) {
396
            return $sub->ends_at;
397
        }
398
399
        return '';
400
    }
401
402
    public function renew($orderid)
403
    {
404
        //$sub = $this->subscription($orderid);
405
        return url('my-orders');
406
    }
407
}
408