Completed
Push — development ( a96186...0b761c )
by Ashutosh
10:54
created

ClientController::getVersionList()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 46
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 46
rs 9.0968
c 0
b 0
f 0
cc 5
nc 3
nop 3
1
<?php
2
3
namespace App\Http\Controllers\Front;
4
5
use App\Http\Controllers\Github\GithubApiController;
6
use App\Model\Github\Github;
7
use App\Model\Order\Invoice;
8
use App\Model\Order\Order;
9
use App\Model\Order\Payment;
10
use App\Model\Product\Product;
11
use App\Model\Product\ProductUpload;
12
use App\Model\Product\Subscription;
13
use App\User;
14
use Auth;
15
use Bugsnag;
16
use DateTime;
17
use DateTimeZone;
18
use Exception;
19
use GrahamCampbell\Markdown\Facades\Markdown;
20
21
class ClientController extends BaseClientController
22
{
23
    public $user;
24
    public $invoice;
25
    public $order;
26
    public $subscription;
27
    public $payment;
28
29
    public function __construct()
30
    {
31
        $this->middleware('auth');
32
33
        $user = new User();
34
        $this->user = $user;
35
36
        $invoice = new Invoice();
37
        $this->invoice = $invoice;
38
39
        $order = new Order();
40
        $this->order = $order;
41
42
        $subscription = new Subscription();
43
        $this->subscription = $subscription;
44
45
        $payment = new Payment();
46
        $this->payment = $payment;
47
48
        $product_upload = new ProductUpload();
49
        $this->product_upload = $product_upload;
50
51
        $product = new Product();
52
        $this->product = $product;
53
54
        $github_controller = new GithubApiController();
55
        $this->github_api = $github_controller;
56
57
        $model = new Github();
58
        $this->github = $model->firstOrFail();
59
60
        $this->client_id = $this->github->client_id;
61
        $this->client_secret = $this->github->client_secret;
62
    }
63
64
    public function invoices()
65
    {
66
        try {
67
            return view('themes.default1.front.clients.invoice');
68
        } catch (Exception $ex) {
69
            return redirect()->back()->with('fails', $ex->getMessage());
70
        }
71
    }
72
73
    public function getInvoices()
74
    {
75
        try {
76
            $invoices = Invoice::where('user_id', \Auth::user()->id)
77
                    ->select('number', 'created_at', 'grand_total', 'id', 'status');
78
79
            return \DataTables::of($invoices->get())
80
                            ->addColumn('number', function ($model) {
81
                                return $model->number;
82
                            })
83
                            ->addColumn('date', function ($model) {
84
                                $date = $model->created_at;
85
86
                                return $date;
87
                                // $myobject->created_at->timezone($this->auth->user()->timezone);
88
                            })
89
                            // ->showColumns('created_at')
90
                            ->addColumn('total', function ($model) {
91
                                return $model->grand_total;
92
                            })
93
                            ->addColumn('Action', function ($model) {
94
                                $status = $model->status;
95
                                $payment = '';
96
                                if ($status == 'Pending' && $model->grand_total > 0) {
97
                                    $payment = '  <a href='.url('paynow/'.$model->id).
98
                                    " class='btn btn-primary btn-xs'><i class='fa fa-credit-card'></i>&nbsp;Pay Now</a>";
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...
99
                                }
100
101
                                return '<p><a href='.url('my-invoice/'.$model->id).
102
                                " class='btn btn-primary btn-xs'><i class='fa fa-eye'></i>&nbsp;View</a>".$payment.'</p>';
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 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...
103
                            })
104
                            ->rawColumns(['number', 'created_at', 'total', 'Action'])
105
                            // ->orderColumns('number', 'created_at', 'total')
106
                            ->make(true);
107
        } catch (Exception $ex) {
108
            Bugsnag::notifyException($ex);
109
            echo $ex->getMessage();
110
        }
111
    }
112
113
    /**
114
     * Get list of all the versions from Filesystem.
115
     *
116
     * @param type $productid
117
     * @param type $clientid
118
     * @param type $invoiceid
119
     *
120
     * Get list of all the versions from Filesystem.
121
     * @param type $productid
122
     * @param type $clientid
123
     * @param type $invoiceid
124
     *
125
     * @return type
126
     */
127
    public function getVersionList($productid, $clientid, $invoiceid)
128
    {
129
        try {
130
            $versions = ProductUpload::where('product_id', $productid)
131
            ->select('id', 'product_id', 'version',
132
             'title', 'description', 'file', 'created_at')->get();
133
134
            return \DataTables::of($versions)
135
                            ->addColumn('id', function ($versions) {
136
                                return ucfirst($versions->id);
137
                            })
138
                            ->addColumn('version', function ($versions) {
139
                                return ucfirst($versions->version);
140
                            })
141
                            ->addColumn('title', function ($versions) {
142
                                return ucfirst($versions->title);
143
                            })
144
                            ->addColumn('description', function ($versions) {
145
                                return ucfirst($versions->description);
146
                            })
147
                            ->addColumn('file', function ($versions) use ($clientid, $invoiceid, $productid) {
148
                                $invoice_id = Invoice::where('number', $invoiceid)->pluck('id')->first();
149
                                $order = Order::where('invoice_id', '=', $invoice_id)->first();
150
                                $order_id = $order->id;
151
                                $getDownloadCondition = Product::where('id', $productid)->value('deny_after_subscription');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 123 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...
152
                                $endDate = Subscription::select('ends_at')
153
                                ->where('product_id', $productid)->where('order_id', $order_id)->first();
154
155
                                //if product has expiry date ie sunscriptioon is generated
156
                                if ($endDate) {
157
                                    if ($getDownloadCondition == 1){//Perpetual download till expiry selected
158
                                     $getDownload = $this->whenDownloadTillExpiry($endDate,$productid,$versions,$clientid,$invoiceid);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 134 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...
159
                                        return $getDownload;
160
                         
161
                                  }elseif($getDownloadCondition == 0){//When download retires after subscription
162
                                          
163
                                        $getDownload =  $this->whenDownloadExpiresAfterExpiry($endDate,$productid,$versions,$clientid,$invoiceid);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 146 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...
164
                                        return $getDownload;
165
                                  }
166
                                }
167
                            })
168
                            ->rawColumns(['version', 'title', 'description', 'file'])
169
                            ->make(true);
170
        } catch (Exception $ex) {
171
            Bugsnag::notifyException($ex);
172
            echo $ex->getMessage();
173
        }
174
    }
175
176
177
    public function whenDownloadTillExpiry($endDate,$productid,$versions,$clientid,$invoiceid)
178
    {
179
      if ($versions->created_at->toDateTimeString()
180
181
        < $endDate->ends_at->toDateTimeString()) {
182
            return '<p><a href='.url('download/'.$productid.'/'
183
            .$clientid.'/'.$invoiceid.'/'.$versions->id).
184
            " class='btn btn-sm btn-primary'><i class='fa fa-download'>
185
            </i>&nbsp;&nbsp;Download</a>".'&nbsp;
186
187
       </p>';
188
        } else {
189
            return '<button class="btn btn-primary 
190
        btn-sm disabled tooltip">Download <span class="tooltiptext">
191
        Please Renew!!</span></button>';
192
        }
193
    }
194
195
    public function whenDownloadExpiresAfterExpiry($endDate,$productid,$versions,$clientid,$invoiceid)
196
    {
197
        if ($versions->created_at->toDateTimeString()
198
        < $endDate->ends_at->toDateTimeString()) {
199
            return '<p><a href='.url('download/'.$productid.'/'
200
            .$clientid.'/'.$invoiceid.'/'.$versions->id).
201
            " class='btn btn-sm btn-primary'><i class='fa fa-download'>
202
            </i>&nbsp;&nbsp;Download</a>".'&nbsp;
203
204
       </p>';
205
        } else {
206
            return '<button class="btn btn-primary 
207
        btn-sm disabled tooltip">Download <span class="tooltiptext">
208
        Please Renew!!</span></button>';
209
        }
210
    }
211
212
213
    /**
214
     * Get list of all the versions from Github.
215
     *
216
     * @param type $productid
217
     * @param type $clientid
218
     * @param type $invoiceid
219
     */
220
221
222
    public function getGithubVersionList($productid, $clientid, $invoiceid)
223
    {
224
        try {
225
            $products = $this->product::where('id', $productid)
226
            ->select('name', 'version', 'github_owner', 'github_repository')->get();
227
            $owner = '';
228
            $repo = '';
229
            foreach ($products as $product) {
230
                $owner = $product->github_owner;
231
                $repo = $product->github_repository;
232
            }
233
            $url = "https://api.github.com/repos/$owner/$repo/releases";
234
            $link = $this->github_api->getCurl1($url);
235
            $link = $link['body'];
236
            $link = (array_slice($link, 0, 10, true));
237
238
            return \DataTables::of($link)
239
                            ->addColumn('version', function ($link) {
240
                                return ucfirst($link['tag_name']);
241
                            })
242
                            ->addColumn('name', function ($link) {
243
                                return ucfirst($link['name']);
244
                            })
245
                            ->addColumn('description', function ($link) {
246
                                $markdown = Markdown::convertToHtml(ucfirst($link['body']));
247
248
                                return $markdown;
249
                            })
250
                            ->addColumn('file', function ($link) use ($invoiceid, $productid) {
251
                                $order = Order::where('invoice_id', '=', $invoiceid)->first();
252
                                $order_id = $order->id;
253
                                $orderEndDate = Subscription::select('ends_at')
254
                                ->where('product_id', $productid)->where('order_id', $order_id)->first();
255
                                if ($orderEndDate) {
256
                                    $actionButton = $this->getActionButton($link, $orderEndDate, $productid);
257
258
                                    return $actionButton;
259
                                } elseif (!$orderEndDate) {
260
                                    $link = $this->github_api->getCurl1($link['zipball_url']);
261
262
                                    return '<p><a href='.$link['header']['Location']
263
                                    ." class='btn btn-sm btn-primary'>Download  </a>"
264
                                            .'&nbsp;
265
                                   </p>';
266
                                }
267
                            })
268
                            ->rawColumns(['version', 'name', 'description', 'file'])
269
                            ->make(true);
270
        } catch (Exception $ex) {
271
            Bugsnag::notifyException($ex);
272
            echo $ex->getMessage();
273
        }
274
    }
275
276
    public function orders()
277
    {
278
        try {
279
            return view('themes.default1.front.clients.order1');
280
        } catch (Exception $ex) {
281
            Bugsnag::notifyException($ex);
282
283
            return redirect()->back()->with('fails', $ex->getMessage());
284
        }
285
    }
286
287
    /*
288
     * Show all the orders for User
289
     */
290
291
    public function getOrders()
292
    {
293
        try {
294
            $orders = Order::where('client', \Auth::user()->id);
295
296
            return \DataTables::of($orders->get())
297
                            ->addColumn('id', function ($model) {
298
                                return $model->id;
299
                            })
300
                            ->addColumn('product_name', function ($model) {
301
                                return $model->product()->first()->name;
302
                            })
303
                            ->addColumn('expiry', function ($model) {
304
                                $tz = \Auth::user()->timezone()->first()->name;
305
                                $end = $this->getExpiryDate($model);
306
307
                                return $end;
308
                            })
309
310
                            ->addColumn('Action', function ($model) {
311
                                $sub = $model->subscription()->first();
312
                                $order = Order::where('id', $model->id)->select('product')->first();
313
                                $productid = $order->product;
314
                                $order_cont = new \App\Http\Controllers\Order\OrderController();
315
                                $status = $order_cont->checkInvoiceStatusByOrderId($model->id);
316
                                $url = '';
317
                                if ($status == 'success') {
318
                                    if ($sub) {
319
                                        $url = $this->renewPopup($sub->id, $productid);
320
                                    }
321
                                }
322
323
                                $listUrl = $this->getPopup($model, $productid);
324
325
                                return '<a href='.url('my-order/'.$model->id)." 
326
                                class='btn  btn-primary btn-xs' style='margin-right:5px;'>
327
                                <i class='fa fa-eye' title='Details of order'></i>&nbsp;View $listUrl $url </a>";
328
                            })
329
                            ->rawColumns(['id', 'created_at', 'ends_at', 'product', 'Action'])
330
                            ->make(true);
331
        } catch (Exception $ex) {
332
            Bugsnag::notifyException($ex);
333
            echo $ex->getMessage();
334
        }
335
    }
336
337
    public function subscriptions()
338
    {
339
        try {
340
            return view('themes.default1.front.clients.subscription');
341
        } catch (Exception $ex) {
342
            Bugsnag::notifyException($ex);
343
344
            return redirect()->back()->with('fails', $ex->getMessage());
345
        }
346
    }
347
348
    public function profile()
349
    {
350
        try {
351
            $user = $this->user->where('id', \Auth::user()->id)->first();
352
            //dd($user);
353
            $timezones = new \App\Model\Common\Timezone();
354
            $timezones = $timezones->pluck('name', 'id')->toArray();
355
            $state = \App\Http\Controllers\Front\CartController::getStateByCode($user->state);
356
            $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($user->country);
357
            $bussinesses = \App\Model\Common\Bussiness::pluck('name', 'short')->toArray();
358
359
            return view('themes.default1.front.clients.profile',
360
             compact('user', 'timezones', 'state', 'states', 'bussinesses'));
361
        } catch (Exception $ex) {
362
            Bugsnag::notifyException($ex);
363
364
            return redirect()->back()->with('fails', $ex->getMessage());
365
        }
366
    }
367
368
    public function getInvoice($id)
369
    {
370
        try {
371
            $invoice = $this->invoice->findOrFail($id);
372
            $items = $invoice->invoiceItem()->get();
373
            $user = \Auth::user();
374
375
            return view('themes.default1.front.clients.show-invoice', compact('invoice', 'items', 'user'));
376
        } catch (Exception $ex) {
377
            Bugsnag::notifyException($ex);
378
379
            return redirect()->back()->with('fails', $ex->getMessage());
380
        }
381
    }
382
383
    public function getOrder($id)
384
    {
385
        try {
386
            $order = $this->order->findOrFail($id);
387
            $invoice = $order->invoice()->first();
388
            $items = $order->invoice()->first()->invoiceItem()->get();
389
            $subscription = '';
390
            $plan = '';
391
            if ($order->subscription) {
392
                $subscription = $order->subscription;
393
394
                $plan = $subscription->plan()->first();
395
            }
396
            $product = $order->product()->first();
397
            $price = $product->price()->first();
398
            $user = \Auth::user();
399
400
            return view('themes.default1.front.clients.show-order',
401
                compact('invoice', 'order', 'user', 'plan', 'product', 'subscription'));
402
        } catch (Exception $ex) {
403
            Bugsnag::notifyException($ex);
404
405
            return redirect('/')->with('fails', $ex->getMessage());
406
        }
407
    }
408
409
    public function getPaymentByOrderId($orderid, $userid)
410
    {
411
        try {
412
            // dd($orderid);
413
            $order = $this->order->where('id', $orderid)->where('client', $userid)->first();
414
            // dd($order);
415
            $relation = $order->invoiceRelation()->pluck('invoice_id')->toArray();
416
            if (count($relation) > 0) {
417
                $invoices = $relation;
418
            } else {
419
                $invoices = $order->invoice()->pluck('id')->toArray();
420
            }
421
            $payments = $this->payment->whereIn('invoice_id', $invoices)
422
                    ->select('id', 'invoice_id', 'user_id', 'amount', 'payment_method', 'payment_status', 'created_at');
423
424
            return \DataTables::of($payments->get())
425
                            ->addColumn('checkbox', function ($model) {
426
                                if (\Input::get('client') != 'true') {
427
                                    return "<input type='checkbox' class='payment_checkbox' 
428
                                    value=".$model->id.' name=select[] id=check>';
429
                                }
430
                            })
431
                            ->addColumn('number', function ($model) {
432
                                return $model->invoice()->first()->number;
433
                            })
434
                            ->addColumn('amount', 'payment_method', 'payment_status', 'created_at')
435
                            ->addColumn('total', function ($model) {
436
                                return $model->grand_total;
437
                            })
438
                            ->rawColumns(['checkbox', 'number', 'total',
439
                             'payment_method', 'payment_status', 'created_at', ])
440
                            ->make(true);
441
        } catch (Exception $ex) {
442
            Bugsnag::notifyException($ex);
443
444
            return redirect()->back()->with('fails', $ex->getMessage());
445
        }
446
    }
447
448
    public function getPaymentByOrderIdClient($orderid, $userid)
449
    {
450
        try {
451
            $order = $this->order->where('id', $orderid)->where('client', $userid)->first();
452
            $relation = $order->invoiceRelation()->pluck('invoice_id')->toArray();
453
            if (count($relation) > 0) {
454
                $invoices = $relation;
455
            } else {
456
                $invoices = $order->invoice()->pluck('id')->toArray();
457
            }
458
            $payments = $this->payment->whereIn('invoice_id', $invoices)
459
                    ->select('id', 'invoice_id', 'user_id', 'payment_method', 'payment_status', 'created_at', 'amount');
460
            //dd(\Input::all());
461
            return \DataTables::of($payments->get())
462
                            ->addColumn('number', function ($model) {
463
                                return $model->invoice()->first()->number;
464
                            })
465
                              ->addColumn('total', function ($model) {
466
                                  return $model->amount;
467
                              })
468
                               ->addColumn('created_at', function ($model) {
469
                                   $date1 = new DateTime($model->created_at);
470
                                   $tz = \Auth::user()->timezone()->first()->name;
471
                                   $date1->setTimezone(new DateTimeZone($tz));
472
                                   $date = $date1->format('M j, Y, g:i a');
473
474
                                   return $date;
475
                               })
476
477
                            ->addColumn('payment_method', 'payment_status', 'created_at')
478
479
                            ->rawColumns(['number', 'total', 'payment_method', 'payment_status', 'created_at'])
480
                            ->make(true);
481
        } catch (Exception $ex) {
482
            Bugsnag::notifyException($ex);
483
484
            return redirect()->back()->with('fails', $ex->getMessage());
485
        }
486
    }
487
}
488