Completed
Push — development ( 50bb76...10afda )
by Ashutosh
08:23
created

OrderController   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 367
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 34
eloc 187
dl 0
loc 367
rs 9.68
c 0
b 0
f 0

15 Methods

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