Completed
Push — development ( c87275...b28f77 )
by Ashutosh
11:48
created

ProductController::adminDownload()   A

Complexity

Conditions 5
Paths 22

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 24
rs 9.3554
c 0
b 0
f 0
cc 5
nc 22
nop 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A ProductController::getSubscriptionCheckScript() 0 15 1
1
<?php
2
3
namespace App\Http\Controllers\Product;
4
5
// use Illuminate\Http\Request;
6
    use App\Model\Order\Order;
7
    use App\Model\Payment\Currency;
8
    use App\Model\Payment\Period;
9
    use App\Model\Payment\Plan;
10
    use App\Model\Payment\Tax;
11
    use App\Model\Payment\TaxClass;
12
    use App\Model\Payment\TaxProductRelation;
13
    use App\Model\Product\Price;
14
    use App\Model\Product\Product;
15
    use App\Model\Product\ProductGroup;
16
    use App\Model\Product\ProductUpload;
17
    use App\Model\Product\Subscription;
18
    use App\Model\Product\Type;
19
    use Bugsnag\BugsnagLaravel\Facades\Bugsnag;
20
    use Illuminate\Http\Request;
21
    use Illuminate\Support\Facades\Input;
22
    use Spatie\Activitylog\Models\Activity;
23
24
    // use Input;
25
26
    class ProductController extends BaseProductController
27
    {
28
        public $product;
29
        public $price;
30
        public $type;
31
        public $subscription;
32
        public $currency;
33
        public $group;
34
        public $plan;
35
        public $tax;
36
        public $tax_relation;
37
        public $tax_class;
38
        public $product_upload;
39
40
        public function __construct()
41
        {
42
            $this->middleware('auth');
43
            $this->middleware('admin', ['except' => ['adminDownload', 'userDownload']]);
44
45
            $product = new Product();
46
            $this->product = $product;
47
48
            $price = new Price();
49
            $this->price = $price;
50
51
            $type = new Type();
52
            $this->type = $type;
53
54
            $subscription = new Subscription();
55
            $this->subscription = $subscription;
56
57
            $currency = new Currency();
58
            $this->currency = $currency;
59
60
            $group = new ProductGroup();
61
            $this->group = $group;
62
63
            $plan = new Plan();
64
            $this->plan = $plan;
65
66
            $tax = new Tax();
67
            $this->tax = $tax;
68
69
            $period = new Period();
70
            $this->period = $period;
71
72
            $tax_relation = new TaxProductRelation();
73
            $this->tax_relation = $tax_relation;
74
75
            $tax_class = new TaxClass();
76
            $this->tax_class = $tax_class;
77
78
            $product_upload = new ProductUpload();
79
            $this->product_upload = $product_upload;
80
        }
81
82
        /**
83
         * Display a listing of the resource.
84
         *
85
         * @return \Response
86
         */
87
        public function index()
88
        {
89
            try {
90
                return view('themes.default1.product.product.index');
91
            } catch (\Exception $e) {
92
                Bugsnag::notifyException($e);
93
94
                return redirect('/')->with('fails', $e->getMessage());
95
            }
96
        }
97
98
        /**
99
         * Display a listing of the resource.
100
         *
101
         * @return \Response
102
         */
103
        public function getProducts()
104
        {
105
            try {
106
                $new_product = Product::select('id', 'name', 'type', 'group')->get();
107
108
                return\ DataTables::of($new_product)
109
110
                            ->addColumn('checkbox', function ($model) {
111
                                return "<input type='checkbox' class='product_checkbox' 
112
                                value=".$model->id.' name=select[] id=check>';
113
                            })
114
                            ->addColumn('name', function ($model) {
115
                                return ucfirst($model->name);
116
                            })
117
                            ->addColumn('type', function ($model) {
118
                                if ($this->type->where('id', $model->type)->first()) {
119
                                    return $this->type->where('id', $model->type)->first()->name;
120
                                } else {
121
                                    return 'Not available';
122
                                }
123
                            })
124
                            ->addColumn('group', function ($model) {
125
                                if ($this->group->where('id', $model->group)->first()) {
126
                                    return $this->group->where('id', $model->group)->first()->name;
127
                                } else {
128
                                    return 'Not available';
129
                                }
130
                            })
131
                            ->addColumn('price', function ($model) {
132
                                if ($this->price->where('product_id', $model->id)->first()) {
133
                                    return $this->price->where('product_id', $model->id)->first()->price;
134
                                } else {
135
                                    return 'Not available';
136
                                }
137
                            })
138
                            ->addColumn('currency', function ($model) {
139
                                if ($this->price->where('product_id', $model->id)->first()) {
140
                                    return $this->price->where('product_id', $model->id)->first()->currency;
141
                                } else {
142
                                    return 'Not available';
143
                                }
144
                            })
145
                            ->addColumn('Action', function ($model) {
146
                                $url = '';
147
                                if ($model->type == 2) {
148
                                    $url = '<a href='.url('product/download/'.$model->id).
149
                                    " class='btn btn-sm btn-primary btn-xs'><i class='fa fa-download' 
150
                                    style='color:white;'> </i>&nbsp;&nbsp;Download</a>";
151
                                }
152
153
                                return '<p><a href='.url('products/'.$model->id.'/edit').
154
                                " class='btn btn-sm btn-primary btn-xs'><i class='fa fa-edit'
155
                                 style='color:white;'> </i>&nbsp;&nbsp;Edit</a>&nbsp;$url</p>";
156
                            })
157
158
                            ->rawColumns(['checkbox', 'name', 'type', 'group', 'price', 'currency', 'Action'])
159
                            ->make(true);
160
            } catch (\Exception $e) {
161
                Bugsnag::notifyException($e);
162
163
                return redirect()->back()->with('fails', $e->getMessage());
164
            }
165
        }
166
167
        // Save file Info in Modal popup
168
        public function save(Request $request)
169
        {
170
            try {
171
                $product_id = Product::where('name', '=', $request->input('product'))->select('id')->first();
172
173
                $this->product_upload->product_id = $product_id->id;
174
                $this->product_upload->title = $request->input('title');
175
                $this->product_upload->description = $request->input('description');
176
                $this->product_upload->version = $request->input('version');
177
178
                if ($request->file) {
179
                    $file = $request->file('file')->getClientOriginalName();
180
181
                    $destination = storage_path().'/products';
182
                    $request->file('file')->move($destination, $file);
183
                    $this->product_upload->file = $file;
184
                }
185
                $this->product_upload->save();
186
187
                return redirect()->back()->with('success', \Lang::get('message.saved-successfully'));
188
            } catch (\Exception $e) {
189
                Bugsnag::notifyException($e);
190
191
                return redirect()->with('fails', $e->getMessage());
192
            }
193
        }
194
195
        /**
196
         * Show the form for creating a new resource.
197
         *
198
         * @return \Response
199
         */
200
        public function create()
201
        {
202
            try {
203
                /*
204
                 * server url
205
                 */
206
                $url = $this->getMyUrl();
207
                $i = $this->product->orderBy('created_at', 'desc')->first()->id + 1;
208
                $cartUrl = $url.'/pricing?id='.$i;
209
                $type = $this->type->pluck('name', 'id')->toArray();
210
                $subscription = $this->plan->pluck('name', 'id')->toArray();
211
                $currency = $this->currency->pluck('name', 'code')->toArray();
212
                $group = $this->group->pluck('name', 'id')->toArray();
213
                $products = $this->product->pluck('name', 'id')->toArray();
214
                $periods = $this->period->pluck('name', 'days')->toArray();
215
                $taxes = $this->tax_class->pluck('name', 'id')->toArray();
216
217
                return view('themes.default1.product.product.create',
218
                    compact('subscription', 'type', 'periods', 'currency',
219
                        'group', 'cartUrl', 'products', 'taxes'));
220
            } catch (\Exception $e) {
221
                Bugsnag::notifyException($e);
222
223
                return redirect()->back()->with('fails', $e->getMessage());
224
            }
225
        }
226
227
        /**
228
         * Store a newly created resource in storage.
229
         *
230
         * @return \Response
231
         */
232
        public function store(Request $request)
233
        {
234
            $input = $request->all();
235
            // dd($input);
236
            $v = \Validator::make($input, [
237
                        'name'       => 'required|unique:products,name',
238
                        'type'       => 'required',
239
                        'group'      => 'required',
240
                        'description'=> 'required',
241
                        'image'      => 'sometimes | mimes:jpeg,jpg,png,gif | max:1000',
242
                        // 'version' => 'required',
243
            ]);
244
            if ($v->fails()) {
245
                //     $currency = $input['currency'];
246
247
                return redirect()->back()
248
                        ->withErrors($v)
249
                        ->withInput()
250
                        ->with('currency');
251
            }
252
253
            try {
254
                if ($request->hasFile('image')) {
255
                    $image = $request->file('image')->getClientOriginalName();
256
                    $imagedestinationPath = 'dist/product/images';
257
                    $request->file('image')->move($imagedestinationPath, $image);
258
                    $this->product->image = $image;
259
                }
260
261
                $product = $this->product;
262
                $product->fill($request->except('image', 'file'))->save();
263
                // Product::where('id',$product->id)->update(['name'=>$request->names]);
264
265
                $product_id = $product->id;
266
                $subscription = $request->input('subscription');
267
268
                $price = $request->input('price');
269
                // $price=
270
271
                $sales_price = $request->input('sales_price');
272
                $currencies = $request->input('currency');
273
                $taxes = $request->input('tax');
274
                if ($taxes) {
275
                    foreach ($taxes as $key=>$value) {
276
                        $newtax = new TaxProductRelation();
277
                        $newtax->product_id = $product_id;
278
                        $newtax->tax_class_id = $value;
279
                        $newtax->save();
280
                    }
281
                }
282
283
                return redirect()->back()->with('success', \Lang::get('message.saved-successfully'));
284
            } catch (\Exception $e) {
285
                Bugsnag::notifyException($e);
286
287
                return redirect()->with('fails', $e->getMessage());
288
            }
289
        }
290
291
        /**
292
         * Show the form for editing the specified resource.
293
         *
294
         * @param int $id
295
         *
296
         * @return \Response
297
         */
298
        public function edit($id)
299
        {
300
            try {
301
                $type = $this->type->pluck('name', 'id')->toArray();
302
303
                $subscription = $this->plan->pluck('name', 'id')->toArray();
304
                $currency = $this->currency->pluck('name', 'code')->toArray();
305
                $group = $this->group->pluck('name', 'id')->toArray();
306
                $products = $this->product->pluck('name', 'id')->toArray();
307
                $periods = $this->period->pluck('name', 'days')->toArray();
308
                $url = $this->GetMyUrl();
309
                $cartUrl = $url.'/cart?id='.$id;
310
                $product = $this->product->where('id', $id)->first();
311
                $taxes = $this->tax_class->pluck('name', 'id')->toArray();
312
                // dd($taxes);
313
                $saved_taxes = $this->tax_relation->where('product_id', $id)->get();
314
                $savedTaxes = $this->tax_relation->where('product_id', $id)->pluck('tax_class_id')->toArray();
315
316
                return view('themes.default1.product.product.edit',
317
                    compact('product', 'periods', 'type', 'subscription',
318
                        'currency', 'group', 'price', 'cartUrl', 'products',
319
                        'regular', 'sales', 'taxes', 'saved_taxes', 'savedTaxes'));
320
            } catch (\Exception $e) {
321
                Bugsnag::notifyException($e);
322
323
                return redirect()->back()->with('fails', $e->getMessage());
324
            }
325
        }
326
327
        /**
328
         * Update the specified resource in storage.
329
         *
330
         * @param int $id
331
         *
332
         * @return \Response
333
         */
334
        public function update($id, Request $request)
335
        {
336
            $input = $request->all();
337
            $v = \Validator::make($input, [
338
                        'name'    => 'required',
339
                        'type'    => 'required',
340
                        'group'   => 'required',
341
                        'image'   => 'sometimes | mimes:jpeg,jpg,png,gif | max:1000',
342
      ]);
343
344
            if ($v->fails()) {
345
                return redirect()->back()->with('errors', $v->errors());
346
            }
347
348
            try {
349
                $product = $this->product->where('id', $id)->first();
350
                if ($request->hasFile('image')) {
351
                    $image = $request->file('image')->getClientOriginalName();
352
                    $imagedestinationPath = 'dist/product/images';
353
                    $request->file('image')->move($imagedestinationPath, $image);
354
                    $product->image = $image;
355
                }
356
                if ($request->hasFile('file')) {
357
                    $file = $request->file('file')->getClientOriginalName();
358
                    $filedestinationPath = storage_path().'/products';
359
                    $request->file('file')->move($filedestinationPath, $file);
360
                    $product->file = $file;
361
                }
362
                $product->fill($request->except('image', 'file'))->save();
363
                $this->updateVersionFromGithub($product->id);
364
365
                $product_id = $product->id;
366
                $subscription = $request->input('subscription');
367
                $cost = $request->input('price');
368
                $sales_price = $request->input('sales_price');
369
                $currencies = $request->input('currency');
370
371
                //add tax class to tax_product_relation table
372
                $taxes = $request->input('tax');
373
                $newTax = $this->saveTax($taxes, $product_id);
374
375
                return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
376
            } catch (\Exception $e) {
377
                Bugsnag::notifyException($e);
378
379
                return redirect()->back()->with('fails', $e->getMessage());
380
            }
381
        }
382
383
        /**
384
         * Remove the specified resource from storage.
385
         *
386
         * @param int $id
387
         *
388
         * @return \Response
389
         */
390
        public function destroy(Request $request)
391
        {
392
            try {
393
                $ids = $request->input('select');
394
                if (!empty($ids)) {
395
                    foreach ($ids as $id) {
396
                        if ($id != 1) {
397
                            $product = $this->product->where('id', $id)->first();
398
                            if ($product) {
399
                                $product->delete();
400
                            } else {
401
                                echo "<div class='alert alert-danger alert-dismissable'>
402
                    <i class='fa fa-ban'></i>
403
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
404
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
405
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
406
                        './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
407
                </div>';
408
                                //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
409
                            }
410
                            echo "<div class='alert alert-success alert-dismissable'>
411
                    <i class='fa fa-ban'></i>
412
                    <b>"./* @scrutinizer ignore-type */
413
                    \Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */ \Lang::get('message.success').'
414
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
415
                        './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
416
                </div>';
417
                        } else {
418
                            echo "<div class='alert alert-danger alert-dismissable'>
419
                    <i class='fa fa-ban'></i>
420
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
421
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
422
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
423
                        './* @scrutinizer ignore-type */ \Lang::get('message.can-not-delete-default').'
424
                </div>';
425
                        }
426
                    }
427
                } else {
428
                    echo "<div class='alert alert-danger alert-dismissable'>
429
                    <i class='fa fa-ban'></i>
430
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
431
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
432
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
433
                        './* @scrutinizer ignore-type */\Lang::get('message.select-a-row').'
434
                </div>';
435
                    //echo \Lang::get('message.select-a-row');
436
                }
437
                $lastActivity = Activity::all()->last();
438
            } catch (\Exception $e) {
439
                echo "<div class='alert alert-danger alert-dismissable'>
440
                    <i class='fa fa-ban'></i>
441
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
442
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
443
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
444
                        '.$e->getMessage().'
445
                </div>';
446
            }
447
        }
448
449
        /**
450
         * Remove the specified resource from storage.
451
         *
452
         * @param int $id
453
         *
454
         * @return \Response
455
         */
456
        public function fileDestroy(Request $request)
457
        {
458
            try {
459
                $ids = $request->input('select');
460
                if (!empty($ids)) {
461
                    foreach ($ids as $id) {
462
                        if ($id != 1) {
463
                            $product = $this->product_upload->where('id', $id)->first();
464
                            if ($product) {
465
                                $product->delete();
466
                            } else {
467
                                echo "<div class='alert alert-danger alert-dismissable'>
468
                    <i class='fa fa-ban'></i>
469
                    <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').'
470
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
471
                        '.\Lang::get('message.no-record').'
472
                </div>';
473
                                //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
474
                            }
475
                            echo "<div class='alert alert-success alert-dismissable'>
476
                    <i class='fa fa-ban'></i>
477
                    <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.success').'
478
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
479
                        '.\Lang::get('message.deleted-successfully').'
480
                </div>';
481
                        } else {
482
                            echo "<div class='alert alert-danger alert-dismissable'>
483
                    <i class='fa fa-ban'></i>
484
                    <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').'
485
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
486
                        '.\Lang::get('message.can-not-delete-default').'
487
                </div>';
488
                        }
489
                    }
490
                } else {
491
                    echo "<div class='alert alert-danger alert-dismissable'>
492
                    <i class='fa fa-ban'></i>
493
                    <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').'
494
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
495
                        '.\Lang::get('message.select-a-row').'
496
                </div>';
497
                    //echo \Lang::get('message.select-a-row');
498
                }
499
            } catch (\Exception $e) {
500
                echo "<div class='alert alert-danger alert-dismissable'>
501
                    <i class='fa fa-ban'></i>
502
                    <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').'
503
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
504
                        '.$e->getMessage().'
505
                </div>';
506
            }
507
        }
508
509
        /*
510
        *  Download Files from Filesystem/Github
511
        */
512
        public function downloadProduct($uploadid, $id, $invoice_id, $version_id = '')
513
        {
514
            try {
515
                $product = $this->product->findOrFail($uploadid);
516
                $type = $product->type;
517
                $owner = $product->github_owner;
518
                $repository = $product->github_repository;
519
                $file = $this->product_upload
520
                ->where('product_id', '=', $uploadid)
521
                ->where('id', $version_id)->select('file')->first();
522
                $order = Order::where('invoice_id', '=', $invoice_id)->first();
523
                $order_id = $order->id;
524
                if ($type == 2) {
525
                    $relese = $this->getRelease($owner, $repository, $order_id, $file);
526
527
                    return $relese;
528
                }
529
            } catch (\Exception $e) {
530
                Bugsnag::notifyException($e);
531
532
                return redirect()->back()->with('fails', $e->getMessage());
533
            }
534
        }
535
536
537
538
        public function getSubscriptionCheckScript()
539
        {
540
            $response = "<script>
541
        function getPrice(val) {
542
            var user = document.getElementsByName('user')[0].value;
543
            var plan = '';
544
            if ($('#plan').length > 0) {
545
                var plan = document.getElementsByName('plan')[0].value;
546
            }
547
            //var plan = document.getElementsByName('plan')[0].value;
548
            //alert(user);
549
550
            $.ajax({
551
                type: 'POST',
552
                url: ".url('get-price').",
553
                data: {'product': val, 'user': user,'plan':plan},
554
                //data: 'product=' + val+'user='+user,
555
                success: function (data) {
556
                    var price = data['price'];
557
                    var field = data['field'];
558
                    $('#price').val(price);
559
                    $('#fields').append(field);
560
                }
561
            });
562
        }
563
564
    </script>";
565
        }
566
    }
567