Completed
Push — development ( 5717ca...90583d )
by Ashutosh
08:29
created

OrderController::destroy()   A

Complexity

Conditions 5
Paths 14

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 45
rs 9.1928
c 0
b 0
f 0
cc 5
nc 14
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
            $from = $request->input('from');
80
            $till = $request->input('till');
81
            $domain = $request->input('domain');
82
83
            return view('themes.default1.order.index',
84
                compact('products', 'order_no', 'product_id',
85
                    'expiry', 'from', 'till', 'domain'));
86
        } catch (\Exception $e) {
87
            Bugsnag::notifyExeption($e);
88
89
            return redirect('orders')->with('fails', $e->getMessage());
90
        }
91
    }
92
93
    public function getOrders(Request $request)
94
    {
95
        $order_no = $request->input('order_no');
96
        $product_id = $request->input('product_id');
97
        $expiry = $request->input('expiry');
98
        $from = $request->input('from');
99
        $till = $request->input('till');
100
        $domain = $request->input('domain');
101
        $query = $this->advanceSearch($order_no, $product_id, $expiry, $from, $till, $domain);
102
103
        return \DataTables::of($query->take(100))
104
                        ->setTotalRecords($query->count())
105
                        ->addColumn('checkbox', function ($model) {
106
                            return "<input type='checkbox' class='order_checkbox' value=".
107
                            $model->id.' name=select[] id=check>';
108
                        })
109
                        ->addColumn('date', function ($model) {
110
                            $date = $model->created_at;
111
112
                            return "<span style='display:none'>$model->id</span>".$date;
113
                        })
114
                        ->addColumn('client', function ($model) {
115
                            $user = $this->user->where('id', $model->client)->first();
116
                            $first = $user->first_name;
117
                            $last = $user->last_name;
118
                            $id = $user->id;
119
120
                            return '<a href='.url('clients/'.$id).'>'.ucfirst($first).' '.ucfirst($last).'<a>';
121
                        })
122
                        ->addColumn('number', function ($model) {
123
                            return ucfirst($model->number);
124
                        })
125
                        ->addColumn('price_override', function ($model) {
126
                            return ucfirst($model->price_override);
127
                        })
128
                        ->addColumn('order_status', function ($model) {
129
                            return ucfirst($model->order_status);
130
                        })
131
                        // ->showColumns('number', 'price_override', 'order_status')
132
                        ->addColumn('ends_at', function ($model) {
133
                            $end = $this->getEndDate($model);
134
135
                            return $end;
136
                        })
137
                        ->addColumn('action', function ($model) {
138
                            $sub = $model->subscription()->first();
139
                            $status = $this->checkInvoiceStatusByOrderId($model->id);
140
                            $url = $this->getUrl($model, $status, $sub);
141
142
                            return $url;
143
                        })
144
145
                         ->filterColumn('created_at', function($query,$keyword) {
146
                            $sql = 'created_at like ?';
147
                            $query->whereRaw($sql, ["%{$keyword}%"]);
148
                        })
149
150
                         
151
                          ->filterColumn('client', function($query,$keyword) {
152
                            $sql = 'client like ?';
153
                            $query->whereRaw($sql, ["%{$keyword}%"]);
154
                        })
155
156
157
                           ->filterColumn('number', function($query,$keyword) {
158
                            $sql = 'number like ?';
159
                            $query->whereRaw($sql, ["%{$keyword}%"]);
160
                        })
161
                            ->filterColumn('price_override', function($query,$keyword) {
162
                            $sql = 'price_override like ?';
163
                            $query->whereRaw($sql, ["%{$keyword}%"]);
164
                        })
165
                             ->filterColumn('order_status', function($query,$keyword) {
166
                            $sql = 'order_status like ?';
167
                            $query->whereRaw($sql, ["%{$keyword}%"]);
168
                        })
169
170
                              ->filterColumn('ends_at', function($query,$keyword) {
171
                            $sql = 'ends_at like ?';
172
                            $query->whereRaw($sql, ["%{$keyword}%"]);
173
                        })
174
175
                         ->rawColumns(['checkbox', 'date', 'client', 'number',
176
                          'price_override', 'order_status', 'ends_at', 'action', ])
177
                        ->make(true);
178
    }
179
180
    /**
181
     * Show the form for creating a new resource.
182
     *
183
     * @return \Response
184
     */
185
    public function create()
186
    {
187
        try {
188
            $clients = $this->user->pluck('first_name', 'id')->toArray();
189
            $product = $this->product->pluck('name', 'id')->toArray();
190
            $subscription = $this->subscription->pluck('name', 'id')->toArray();
191
            $promotion = $this->promotion->pluck('code', 'id')->toArray();
192
193
            return view('themes.default1.order.create', compact('clients', 'product', 'subscription', 'promotion'));
194
        } catch (\Exception $e) {
195
            Bugsnag::notifyExeption($e);
196
197
            return redirect()->back()->with('fails', $e->getMessage());
198
        }
199
    }
200
201
    /**
202
     * Store a newly created resource in storage.
203
     *
204
     * @return \Response
205
     */
206
    public function show($id)
207
    {
208
        try {
209
            $order = $this->order->findOrFail($id);
210
            $subscription = $order->subscription()->first();
211
            $invoiceid = $order->invoice_id;
212
            $invoice = $this->invoice->where('id', $invoiceid)->first();
213
            if (!$invoice) {
214
                return redirect()->back()->with('fails', 'no orders');
215
            }
216
            $invoiceItems = $this->invoice_items->where('invoice_id', $invoiceid)->get();
217
            $user = $this->user->find($invoice->user_id);
218
219
            return view('themes.default1.order.show',
220
                compact('invoiceItems', 'invoice', 'user', 'order', 'subscription'));
221
        } catch (\Exception $ex) {
222
            Bugsnag::notifyException($ex);
223
224
            return redirect()->back()->with('fails', $ex->getMessage());
225
        }
226
    }
227
228
    /**
229
     * Show the form for editing the specified resource.
230
     *
231
     * @param int $id
232
     *
233
     * @return \Response
234
     */
235
    public function edit($id)
236
    {
237
        try {
238
            $order = $this->order->where('id', $id)->first();
239
            $clients = $this->user->pluck('first_name', 'id')->toArray();
240
            $product = $this->product->pluck('name', 'id')->toArray();
241
            $subscription = $this->subscription->pluck('name', 'id')->toArray();
242
            $promotion = $this->promotion->pluck('code', 'id')->toArray();
243
244
            return view('themes.default1.order.edit',
245
                compact('clients', 'product', 'subscription', 'promotion', 'order'));
246
        } catch (\Exception $e) {
247
            Bugsnag::notifyExeption($e);
248
249
            return redirect()->back()->with('fails', $e->getMessage());
250
        }
251
    }
252
253
    /**
254
     * Update the specified resource in storage.
255
     *
256
     * @param int $id
257
     *
258
     * @return \Response
259
     */
260
    public function update($id, OrderRequest $request)
261
    {
262
        try {
263
            $order = $this->order->where('id', $id)->first();
264
            $order->fill($request->input())->save();
265
266
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
267
        } catch (\Exception $e) {
268
            Bugsnag::notifyExeption($e);
269
270
            return redirect()->back()->with('fails', $e->getMessage());
271
        }
272
    }
273
274
    /**
275
     * Remove the specified resource from storage.
276
     *
277
     * @param int $id
278
     *
279
     * @return \Response
280
     */
281
    public function destroy(Request $request)
282
    {
283
        try {
284
            // dd('df');
285
            $ids = $request->input('select');
286
            if (!empty($ids)) {
287
                foreach ($ids as $id) {
288
                    $order = $this->order->where('id', $id)->first();
289
                    if ($order) {
290
                        $order->delete();
291
                    } else {
292
                        echo "<div class='alert alert-danger alert-dismissable'>
293
                    <i class='fa fa-ban'></i>
294
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
295
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
296
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
297
                        './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
298
                </div>';
299
                        //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
300
                    }
301
                }
302
                echo "<div class='alert alert-success alert-dismissable'>
303
                    <i class='fa fa-ban'></i>
304
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
305
                    /* @scrutinizer ignore-type */\Lang::get('message.success').'
306
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
307
                        './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
308
                </div>';
309
            } else {
310
                echo "<div class='alert alert-danger alert-dismissable'>
311
                    <i class='fa fa-ban'></i>
312
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
313
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
314
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
315
                        './* @scrutinizer ignore-type */ \Lang::get('message.select-a-row').'
316
                </div>';
317
                //echo \Lang::get('message.select-a-row');
318
            }
319
        } catch (\Exception $e) {
320
            echo "<div class='alert alert-danger alert-dismissable'>
321
                    <i class='fa fa-ban'></i>
322
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
323
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
324
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
325
                        '.$e->getMessage().'
326
                </div>';
327
        }
328
    }
329
330
    public function deleleById($id)
331
    {
332
        try {
333
            $order = $this->order->find($id);
334
            if ($order) {
335
                $order->delete();
336
            } else {
337
                return redirect()->back()->with('fails', 'Can not delete');
338
            }
339
340
            return redirect()->back()->with('success', "Order $order->number has Deleted Successfully");
341
        } catch (\Exception $e) {
342
            Bugsnag::notifyException($e);
343
344
            return redirect()->back()->with('fails', $e->getMessage());
345
        }
346
    }
347
348
    public function plan($invoice_item_id)
349
    {
350
        try {
351
            $planid = 0;
352
            $item = $this->invoice_items->find($invoice_item_id);
353
            if ($item) {
354
                $planid = $item->plan_id;
355
            }
356
357
            return $planid;
358
        } catch (\Exception $ex) {
359
            Bugsnag::notifyException($ex);
360
361
            throw new \Exception($ex->getMessage());
362
        }
363
    }
364
365
    public function checkInvoiceStatusByOrderId($orderid)
366
    {
367
        try {
368
            $status = 'pending';
369
            $order = $this->order->find($orderid);
370
            if ($order) {
371
                $invoiceid = $order->invoice_id;
372
                $invoice = $this->invoice->find($invoiceid);
373
                if ($invoice) {
374
                    if ($invoice->status == 'Success') {
375
                        $status = 'success';
376
                    }
377
                }
378
            }
379
380
            return $status;
381
        } catch (\Exception $ex) {
382
            Bugsnag::notifyException($ex);
383
384
            throw new \Exception($ex->getMessage());
385
        }
386
    }
387
388
    public function product($itemid)
389
    {
390
        $invoice_items = new InvoiceItem();
391
        $invoice_item = $invoice_items->find($itemid);
392
        $product = $invoice_item->product_name;
393
394
        return $product;
395
    }
396
397
    public function subscription($orderid)
398
    {
399
        $sub = $this->subscription->where('order_id', $orderid)->first();
400
401
        return $sub;
402
    }
403
404
    public function expiry($orderid)
405
    {
406
        $sub = $this->subscription($orderid);
407
        if ($sub) {
408
            return $sub->ends_at;
409
        }
410
411
        return '';
412
    }
413
414
    public function renew($orderid)
415
    {
416
        //$sub = $this->subscription($orderid);
417
        return url('my-orders');
418
    }
419
}
420