Completed
Push — development ( 3e307e...0dc251 )
by Ashutosh
11:44
created

ClientController::getGithubVersionList()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 46
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 46
rs 9.0008
c 0
b 0
f 0
cc 5
nc 6
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'>Pay Now</a>";
99
                                }
100
101
                                return '<p><a href='.url('my-invoice/'.$model->id).
102
                                " class='btn btn-primary btn-xs'>View</a>".$payment.'</p>';
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
                                $endDate = Subscription::select('ends_at')
152
                                ->where('product_id', $productid)->where('order_id', $order_id)->first();
153
                                if ($versions->created_at->toDateTimeString()
154
                                    < $endDate->ends_at->toDateTimeString()) {
155
                                    return '<p><a href='.url('download/'.$productid.'/'
156
                                        .$clientid.'/'.$invoiceid.'/'.$versions->id).
157
                                " class='btn btn-sm btn-primary'><i class='fa fa-download'>
158
                                </i>&nbsp;&nbsp;Download</a>".'&nbsp;
159
160
                                   </p>';
161
                                } else {
162
                                    return '<button class="btn btn-primary 
163
                                    btn-sm disabled tooltip">Download <span class="tooltiptext">
164
                                    Please Renew!!</span></button>';
165
                                }
166
                            })
167
                            ->rawColumns(['version', 'title', 'description', 'file'])
168
                            ->make(true);
169
        } catch (Exception $ex) {
170
            Bugsnag::notifyException($ex);
171
            echo $ex->getMessage();
172
        }
173
    }
174
175
    /**
176
     * Get list of all the versions from Github.
177
     *
178
     * @param type $productid
179
     * @param type $clientid
180
     * @param type $invoiceid
181
     */
182
    public function getGithubVersionList($productid, $clientid, $invoiceid)
183
    {
184
        try {
185
            $products = $this->product::where('id', $productid)
186
            ->select('name', 'version', 'github_owner', 'github_repository')->get();
187
            $owner = '';  $repo = '';
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
188
           foreach ($products as $product) {
189
                $owner = $product->github_owner;
190
                $repo = $product->github_repository;
191
            }
192
            $url = "https://api.github.com/repos/$owner/$repo/releases";
193
            $link = $this->github_api->getCurl1($url);
194
            $link = $link['body'];
195
            $link = (array_slice($link, 0, 10, true));
196
            return \DataTables::of($link)
197
                            ->addColumn('version', function ($link) {
198
                                return ucfirst($link['tag_name']);
199
                            })
200
                            ->addColumn('name', function ($link) {
201
                                return ucfirst($link['name']);
202
                            })
203
                            ->addColumn('description', function ($link) {
204
                                $markdown = Markdown::convertToHtml(ucfirst($link['body']));
205
                                 return $markdown;
206
                            })
207
                            ->addColumn('file', function ($link) use ($invoiceid, $productid) {
208
                                $order = Order::where('invoice_id', '=', $invoiceid)->first();
209
                                $order_id = $order->id;
210
                                $orderEndDate = Subscription::select('ends_at')
211
                                ->where('product_id', $productid)->where('order_id', $order_id)->first();
212
                                if ($orderEndDate) {
213
                                    $actionButton = $this->getActionButton($link, $orderEndDate);
214
                                    return $actionButton;
215
                                } elseif (!$orderEndDate) {
216
                                    $link = $this->github_api->getCurl1($link['zipball_url']);
217
                                    return '<p><a href='.$link['header']['Location']
218
                                    ." class='btn btn-sm btn-primary'>Download  </a>"
219
                                            .'&nbsp;
220
                                   </p>';
221
                                }
222
                            })
223
                            ->rawColumns(['version', 'name', 'description', 'file'])
224
                            ->make(true);
225
        } catch (Exception $ex) {
226
            Bugsnag::notifyException($ex);
227
            echo $ex->getMessage();
228
        }
229
    }
230
231
    public function orders()
232
    {
233
        try {
234
            return view('themes.default1.front.clients.order1');
235
        } catch (Exception $ex) {
236
            Bugsnag::notifyException($ex);
237
238
            return redirect()->back()->with('fails', $ex->getMessage());
239
        }
240
    }
241
242
    /*
243
     * Show all the orders for User
244
     */
245
246
    public function getOrders()
247
    {
248
        try {
249
            $orders = Order::where('client', \Auth::user()->id);
250
251
            return \DataTables::of($orders->get())
252
                            ->addColumn('id', function ($model) {
253
                                return $model->id;
254
                            })
255
                            ->addColumn('product_name', function ($model) {
256
                                return $model->product()->first()->name;
257
                            })
258
                            ->addColumn('expiry', function ($model) {
259
                                $tz = \Auth::user()->timezone()->first()->name;
260
                                $end = $this->getExpiryDate($model);
261
262
                                return $end;
263
                            })
264
265
                            ->addColumn('Action', function ($model) {
266
                                $sub = $model->subscription()->first();
267
                                $order = Order::where('id', $model->id)->select('product')->first();
268
                                $productid = $order->product;
269
                                $order_cont = new \App\Http\Controllers\Order\OrderController();
270
                                $status = $order_cont->checkInvoiceStatusByOrderId($model->id);
271
                                $url = '';
272
                                if ($status == 'success') {
273
                                    if ($sub) {
274
                                        $url = $this->renewPopup($sub->id, $productid);
275
                                    }
276
                                }
277
278
                                $listUrl = $this->getPopup($model, $productid);
279
280
                                return '<a href='.url('my-order/'.$model->id)." 
281
                                class='btn  btn-primary btn-xs' style='margin-right:5px;'>
282
                                <i class='fa fa-eye' title='Details of order'></i> $listUrl $url </a>";
283
                            })
284
                            ->rawColumns(['id', 'created_at', 'ends_at', 'product', 'Action'])
285
                            ->make(true);
286
        } catch (Exception $ex) {
287
            Bugsnag::notifyException($ex);
288
            echo $ex->getMessage();
289
        }
290
    }
291
292
    public function subscriptions()
293
    {
294
        try {
295
            return view('themes.default1.front.clients.subscription');
296
        } catch (Exception $ex) {
297
            Bugsnag::notifyException($ex);
298
299
            return redirect()->back()->with('fails', $ex->getMessage());
300
        }
301
    }
302
303
    public function profile()
304
    {
305
        try {
306
            $user = $this->user->where('id', \Auth::user()->id)->first();
307
            //dd($user);
308
            $timezones = new \App\Model\Common\Timezone();
309
            $timezones = $timezones->pluck('name', 'id')->toArray();
310
            $state = \App\Http\Controllers\Front\CartController::getStateByCode($user->state);
311
            $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($user->country);
312
            $bussinesses = \App\Model\Common\Bussiness::pluck('name', 'short')->toArray();
313
314
            return view('themes.default1.front.clients.profile',
315
             compact('user', 'timezones', 'state', 'states', 'bussinesses'));
316
        } catch (Exception $ex) {
317
            Bugsnag::notifyException($ex);
318
319
            return redirect()->back()->with('fails', $ex->getMessage());
320
        }
321
    }
322
323
    public function getInvoice($id)
324
    {
325
        try {
326
            $invoice = $this->invoice->findOrFail($id);
327
            $items = $invoice->invoiceItem()->get();
328
            $user = \Auth::user();
329
330
            return view('themes.default1.front.clients.show-invoice', compact('invoice', 'items', 'user'));
331
        } catch (Exception $ex) {
332
            Bugsnag::notifyException($ex);
333
334
            return redirect()->back()->with('fails', $ex->getMessage());
335
        }
336
    }
337
338
    public function getOrder($id)
339
    {
340
        try {
341
            $order = $this->order->findOrFail($id);
342
            $invoice = $order->invoice()->first();
343
            $items = $order->invoice()->first()->invoiceItem()->get();
344
            $subscription = '';
345
            $plan = '';
346
            if ($order->subscription) {
347
                $subscription = $order->subscription;
348
349
                $plan = $subscription->plan()->first();
350
            }
351
            $product = $order->product()->first();
352
            $price = $product->price()->first();
353
            $user = \Auth::user();
354
355
            return view('themes.default1.front.clients.show-order',
356
                compact('invoice', 'order', 'user', 'plan', 'product', 'subscription'));
357
        } catch (Exception $ex) {
358
            Bugsnag::notifyException($ex);
359
360
            return redirect('/')->with('fails', $ex->getMessage());
361
        }
362
    }
363
364
    public function getPaymentByOrderId($orderid, $userid)
365
    {
366
        try {
367
            // dd($orderid);
368
            $order = $this->order->where('id', $orderid)->where('client', $userid)->first();
369
            // dd($order);
370
            $relation = $order->invoiceRelation()->pluck('invoice_id')->toArray();
371
            if (count($relation) > 0) {
372
                $invoices = $relation;
373
            } else {
374
                $invoices = $order->invoice()->pluck('id')->toArray();
375
            }
376
            $payments = $this->payment->whereIn('invoice_id', $invoices)
377
                    ->select('id', 'invoice_id', 'user_id', 'amount', 'payment_method', 'payment_status', 'created_at');
378
379
            return \DataTables::of($payments->get())
380
                            ->addColumn('checkbox', function ($model) {
381
                                if (\Input::get('client') != 'true') {
382
                                    return "<input type='checkbox' class='payment_checkbox' 
383
                                    value=".$model->id.' name=select[] id=check>';
384
                                }
385
                            })
386
                            ->addColumn('number', function ($model) {
387
                                return $model->invoice()->first()->number;
388
                            })
389
                            ->addColumn('amount', 'payment_method', 'payment_status', 'created_at')
390
                            ->addColumn('total', function ($model) {
391
                                return $model->grand_total;
392
                            })
393
                            ->rawColumns(['checkbox', 'number', 'total',
394
                             'payment_method', 'payment_status', 'created_at', ])
395
                            ->make(true);
396
        } catch (Exception $ex) {
397
            Bugsnag::notifyException($ex);
398
399
            return redirect()->back()->with('fails', $ex->getMessage());
400
        }
401
    }
402
403
    public function getPaymentByOrderIdClient($orderid, $userid)
404
    {
405
        try {
406
            $order = $this->order->where('id', $orderid)->where('client', $userid)->first();
407
            $relation = $order->invoiceRelation()->pluck('invoice_id')->toArray();
408
            if (count($relation) > 0) {
409
                $invoices = $relation;
410
            } else {
411
                $invoices = $order->invoice()->pluck('id')->toArray();
412
            }
413
            $payments = $this->payment->whereIn('invoice_id', $invoices)
414
                    ->select('id', 'invoice_id', 'user_id', 'payment_method', 'payment_status', 'created_at', 'amount');
415
            //dd(\Input::all());
416
            return \DataTables::of($payments->get())
417
                            ->addColumn('number', function ($model) {
418
                                return $model->invoice()->first()->number;
419
                            })
420
                              ->addColumn('total', function ($model) {
421
                                  return $model->amount;
422
                              })
423
                               ->addColumn('created_at', function ($model) {
424
                                   $date1 = new DateTime($model->created_at);
425
                                   $tz = \Auth::user()->timezone()->first()->name;
426
                                   $date1->setTimezone(new DateTimeZone($tz));
427
                                   $date = $date1->format('M j, Y, g:i a');
428
429
                                   return $date;
430
                               })
431
432
                            ->addColumn('payment_method', 'payment_status', 'created_at')
433
434
                            ->rawColumns(['number', 'total', 'payment_method', 'payment_status', 'created_at'])
435
                            ->make(true);
436
        } catch (Exception $ex) {
437
            Bugsnag::notifyException($ex);
438
439
            return redirect()->back()->with('fails', $ex->getMessage());
440
        }
441
    }
442
}
443