Completed
Branch feature-dynamic-fields (cdebc1)
by Ashutosh
10:20
created

InvoiceController::invoiceGenerateByForm()   B

Complexity

Conditions 6
Paths 179

Size

Total Lines 59
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 59
rs 8.0511
c 0
b 0
f 0
cc 6
nc 179
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers\Order;
4
5
use App\Http\Controllers\Front\CartController;
6
use App\Model\Common\Setting;
7
use App\Model\Common\Template;
8
use App\Model\Order\Invoice;
9
use App\Model\Order\InvoiceItem;
10
use App\Model\Order\Order;
11
use App\Model\Order\Payment;
12
use App\Model\Payment\Currency;
13
use App\Model\Payment\Plan;
14
use App\Model\Payment\Promotion;
15
use App\Model\Payment\Tax;
16
use App\Model\Payment\TaxByState;
17
use App\Model\Payment\TaxOption;
18
use App\Model\Product\Price;
19
use App\Model\Product\Product;
20
use App\Traits\CoupCodeAndInvoiceSearch;
21
use App\User;
22
use Bugsnag;
23
use Illuminate\Http\Request;
24
use Input;
25
use Log;
26
27
class InvoiceController extends TaxRatesAndCodeExpiryController
28
{
29
    use  CoupCodeAndInvoiceSearch;
0 ignored issues
show
introduced by
The trait App\Traits\CoupCodeAndInvoiceSearch requires some properties which are not provided by App\Http\Controllers\Order\InvoiceController: $grand_total, $first_name, $planPrice, $product_quantity, $id, $created_at, $planRelation, $type, $value, $no_of_agents
Loading history...
30
31
    public $invoice;
32
    public $invoiceItem;
33
    public $user;
34
    public $template;
35
    public $setting;
36
    public $payment;
37
    public $product;
38
    public $price;
39
    public $promotion;
40
    public $currency;
41
    public $tax;
42
    public $tax_option;
43
    public $order;
44
    public $cartController;
45
46
    public function __construct()
47
    {
48
        $this->middleware('auth');
49
        $this->middleware('admin', ['except' => ['pdf']]);
50
51
        $invoice = new Invoice();
52
        $this->invoice = $invoice;
53
54
        $invoiceItem = new InvoiceItem();
55
        $this->invoiceItem = $invoiceItem;
56
57
        $user = new User();
58
        $this->user = $user;
59
60
        $template = new Template();
61
        $this->template = $template;
62
63
        $seting = new Setting();
64
        $this->setting = $seting;
65
66
        $payment = new Payment();
67
        $this->payment = $payment;
68
69
        $product = new Product();
70
        $this->product = $product;
71
72
        $price = new Price();
73
        $this->price = $price;
74
75
        $promotion = new Promotion();
76
        $this->promotion = $promotion;
77
78
        $currency = new Currency();
79
        $this->currency = $currency;
80
81
        $tax = new Tax();
82
        $this->tax = $tax;
83
84
        $tax_option = new TaxOption();
85
        $this->tax_option = $tax_option;
86
87
        $order = new Order();
88
        $this->order = $order;
89
90
        $tax_by_state = new TaxByState();
91
        $this->tax_by_state = new $tax_by_state();
92
93
        $cartController = new CartController();
94
        $this->cartController = $cartController;
95
    }
96
97
    public function index(Request $request)
98
    {
99
        try {
100
            $currencies = Currency::where('status', 1)->pluck('code')->toArray();
101
            $name = $request->input('name');
102
            $invoice_no = $request->input('invoice_no');
103
            $status = $request->input('status');
104
105
            $currency_id = $request->input('currency_id');
106
            $from = $request->input('from');
107
            $till = $request->input('till');
108
109
            return view('themes.default1.invoice.index', compact('name','invoice_no','status','currencies','currency_id','from',
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 128 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...
110
111
                'till'));
112
        } catch (\Exception $ex) {
113
            Bugsnag::notifyException($ex);
114
115
            return redirect()->back()->with('fails', $ex->getMessage());
116
        }
117
    }
118
119
    public function getInvoices(Request $request)
120
    {
121
        $name = $request->input('name');
122
        $invoice_no = $request->input('invoice_no');
123
        $status = $request->input('status');
124
        $currency = $request->input('currency_id');
125
        $from = $request->input('from');
126
        $till = $request->input('till');
127
        $query = $this->advanceSearch($name, $invoice_no, $currency, $status, $from, $till);
128
129
        return \DataTables::of($query->take(100))
130
         ->setTotalRecords($query->count())
131
132
         ->addColumn('checkbox', function ($model) {
133
             return "<input type='checkbox' class='invoice_checkbox' 
134
                            value=".$model->id.' name=select[] id=check>';
135
         })
136
                        ->addColumn('user_id', function ($model) {
137
                            $first = $this->user->where('id', $model->user_id)->first()->first_name;
138
                            $last = $this->user->where('id', $model->user_id)->first()->last_name;
139
                            $id = $this->user->where('id', $model->user_id)->first()->id;
140
141
                            return '<a href='.url('clients/'.$id).'>'.ucfirst($first).' '.ucfirst($last).'</a>';
142
                        })
143
                         ->addColumn('number', function ($model) {
144
                             return ucfirst($model->number);
145
                         })
146
147
                        ->addColumn('date', function ($model) {
148
                            $date = ($model->created_at);
149
150
                            return $date;
151
                            // return "<span style='display:none'>$model->id</span>".$date->format('l, F j, Y H:m');
152
                        })
153
                         ->addColumn('grand_total', function ($model) {
154
                             return ucfirst($model->number);
155
                         })
156
                          ->addColumn('status', function ($model) {
157
                              return ucfirst($model->status);
158
                          })
159
160
                        ->addColumn('action', function ($model) {
161
                            $action = '';
162
163
                            $check = $this->checkExecution($model->id);
164
                            if ($check == false) {
165
                                $action = '<a href='.url('order/execute?invoiceid='.$model->id)
166
                                ." class='btn btn-sm btn-primary btn-xs'>
167
                                <i class='fa fa-tasks' style='color:white;'>
168
                                 </i>&nbsp;&nbsp; Execute Order</a>";
169
                            }
170
171
                            return '<a href='.url('invoices/show?invoiceid='.$model->id)
172
                            ." class='btn btn-sm btn-primary btn-xs'><i class='fa fa-eye' 
173
                            style='color:white;'> </i>&nbsp;&nbsp;View</a>"
174
                                    ."   $action";
175
                        })
176
                         ->filterColumn('user_id', function ($query, $keyword) {
177
                             $sql = 'first_name like ?';
178
                             $query->whereRaw($sql, ["%{$keyword}%"]);
179
                         })
180
181
                          ->filterColumn('status', function ($query, $keyword) {
182
                              $sql = 'status like ?';
183
                              $query->whereRaw($sql, ["%{$keyword}%"]);
184
                          })
185
186
                        ->filterColumn('number', function ($query, $keyword) {
187
                            $sql = 'number like ?';
188
                            $query->whereRaw($sql, ["%{$keyword}%"]);
189
                        })
190
                         ->filterColumn('grand_total', function ($query, $keyword) {
191
                             $sql = 'grand_total like ?';
192
                             $query->whereRaw($sql, ["%{$keyword}%"]);
193
                         })
194
                          ->filterColumn('date', function ($query, $keyword) {
195
                              $sql = 'date like ?';
196
                              $query->whereRaw($sql, ["%{$keyword}%"]);
197
                          })
198
199
                         ->rawColumns(['checkbox', 'user_id', 'number', 'date', 'grand_total', 'status', 'action'])
200
                        ->make(true);
201
    }
202
203
    /**
204
     * Shoe Invoice when view Invoice is selected from dropdown in Admin Panel.
205
     *
206
     * @param Request $request Get InvoiceId as Request
207
     */
208
    public function show(Request $request)
209
    {
210
        try {
211
            $id = $request->input('invoiceid');
212
            $invoice = $this->invoice->where('id', $id)->first();
213
            $invoiceItems = $this->invoiceItem->where('invoice_id', $id)->get();
214
            $user = $this->user->find($invoice->user_id);
215
            $currency = CartController::currency($user->id);
216
            $symbol = $currency['symbol'];
217
218
            return view('themes.default1.invoice.show', compact('invoiceItems', 'invoice', 'user', 'currency', 'symbol'));
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...
219
        } catch (\Exception $ex) {
220
            Bugsnag::notifyException($ex);
221
222
            return redirect()->back()->with('fails', $ex->getMessage());
223
        }
224
    }
225
226
    /**
227
     * not in use case.
228
     *
229
     * @param Request $request
230
     *
231
     * @return type
232
     */
233
    public function generateById(Request $request)
234
    {
235
        try {
236
            $clientid = $request->input('clientid');
237
            $user = new User();
238
            if ($clientid) {
239
                $user = $user->where('id', $clientid)->first();
240
                if (!$user) {
241
                    return redirect()->back()->with('fails', 'Invalid user');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...fails', 'Invalid user') also could return the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type App\Http\Controllers\Order\type.
Loading history...
242
                }
243
            } else {
244
                $user = '';
245
            }
246
            $products = $this->product->where('id', '!=', 1)->pluck('name', 'id')->toArray();
247
            $currency = $this->currency->pluck('name', 'code')->toArray();
248
249
            return view('themes.default1.invoice.generate', compact('user', 'products', 'currency'));
250
        } catch (\Exception $ex) {
251
            app('log')->info($ex->getMessage());
252
            Bugsnag::notifyException($ex);
253
254
            return redirect()->back()->with('fails', $ex->getMessage());
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...ls', $ex->getMessage()) also could return the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type App\Http\Controllers\Order\type.
Loading history...
255
        }
256
    }
257
258
    public function sendmailClientAgent($userid, $invoiceid)
259
    {
260
        try {
261
            $agent = \Input::get('agent');
262
            $client = \Input::get('client');
263
            if ($agent == 1) {
264
                $id = \Auth::user()->id;
265
                $this->sendMail($id, $invoiceid);
266
            }
267
            if ($client == 1) {
268
                $this->sendMail($userid, $invoiceid);
269
            }
270
        } catch (\Exception $ex) {
271
            app('log')->info($ex->getMessage());
272
            Bugsnag::notifyException($ex);
273
274
            throw new \Exception($ex->getMessage());
275
        }
276
    }
277
278
    /**
279
     * Generate invoice.
280
     *
281
     * @throws \Exception
282
     */
283
284
    //Is this Method only for cliet?? because Auth::user->id?
285
    public function generateInvoice()
286
    {
287
        try {
288
            $tax_rule = new \App\Model\Payment\TaxOption();
289
            $rule = $tax_rule->findOrFail(1);
290
            $rounding = $rule->rounding;
291
            $user_id = \Auth::user()->id;
292
            if (\Auth::user()->currency == 'INR') {
293
                $grand_total = \Cart::getSubTotal();
294
            } else {
295
                foreach (\Cart::getContent() as $cart) {
296
                    $grand_total = \Cart::getSubTotal();
297
                }
298
            }
299
            $number = rand(11111111, 99999999);
300
            $date = \Carbon\Carbon::now();
301
            if ($rounding == 1) {
302
                $grand_total = round($grand_total);
303
            }
304
            $content = \Cart::getContent();
305
            $attributes = [];
306
            foreach ($content as $key => $item) {
307
                $attributes[] = $item->attributes;
308
            }
309
            $symbol = $attributes[0]['currency']['symbol'];
310
            $invoice = $this->invoice->create(['user_id' => $user_id, 'number' => $number,
311
             'date'                                      => $date, 'grand_total' => $grand_total, 'status' => 'pending',
312
             'currency'                                  => $symbol, ]);
313
            foreach (\Cart::getContent() as $cart) {
314
                $this->createInvoiceItems($invoice->id, $cart);
315
            }
316
            $this->sendMail($user_id, $invoice->id);
317
318
            return $invoice;
319
        } catch (\Exception $ex) {
320
            app('log')->error($ex->getMessage());
321
            Bugsnag::notifyException($ex);
322
323
            throw new \Exception('Can not Generate Invoice');
324
        }
325
    }
326
327
    public function createInvoiceItems($invoiceid, $cart)
328
    {
329
        try {
330
            $planid = 0;
331
            $product_name = $cart->name;
332
            $regular_price = $cart->price;
333
            $quantity = $cart->quantity;
334
            $agents = $cart->attributes->agents;
335
            $domain = $this->domain($cart->id);
336
            $cart_cont = new \App\Http\Controllers\Front\CartController();
337
            if ($cart_cont->checkPlanSession() === true) {
338
                $planid = \Session::get('plan');
339
            }
340
            if ($planid == 0) {
341
                //When Product is added from Faveo Website
342
                $planid = Plan::where('product', $cart->id)->pluck('id')->first();
343
            }
344
            $user_currency = \Auth::user()->currency;
345
            $subtotal = $this->getSubtotal($user_currency, $cart);
346
347
            $tax_name = '';
348
            $tax_percentage = '';
349
350
            foreach ($cart->attributes['tax'] as $tax) {
351
                $tax_name .= $tax['name'].',';
352
                $tax_percentage .= $tax['rate'].',';
353
            }
354
            $invoiceItem = $this->invoiceItem->create([
355
                'invoice_id'     => $invoiceid,
356
                'product_name'   => $product_name,
357
                'regular_price'  => $regular_price,
358
                'quantity'       => $quantity,
359
                'tax_name'       => $tax_name,
360
                'tax_percentage' => $tax_percentage,
361
                'subtotal'       => $subtotal,
362
                'domain'         => $domain,
363
                'plan_id'        => $planid,
364
                'agents'         => $agents,
365
            ]);
366
367
            return $invoiceItem;
368
        } catch (\Exception $ex) {
369
            Bugsnag::notifyException($ex);
370
371
            throw new \Exception('Can not create Invoice Items');
372
        }
373
    }
374
375
    public function invoiceGenerateByForm(Request $request, $user_id = '')
376
    {
377
        try {
378
            $agents = $request->input('agents');
379
            $qty = $request->input('quantity');
380
            if ($user_id == '') {
381
                $user_id = \Request::input('user');
382
            }
383
            $productid = $request->input('product');
384
385
            $plan = $request->input('plan');
386
            $agents = $this->getAgents($agents, $productid, $plan);
387
            $qty = $this->getQuantity($qty, $productid, $plan);
388
389
            $code = $request->input('code');
390
            $total = $request->input('price');
391
            $description = $request->input('description');
392
            if ($request->has('domain')) {
393
                $domain = $request->input('domain');
394
                $this->setDomain($productid, $domain);
395
            }
396
            $controller = new \App\Http\Controllers\Front\CartController();
397
            $userCurrency = $controller->currency($user_id);
398
            $currency = $userCurrency['currency'];
399
            $number = rand(11111111, 99999999);
400
            $date = \Carbon\Carbon::now();
401
            $product = Product::find($productid);
402
            $cost = $controller->cost($productid, $user_id, $plan);
403
            if ($cost != $total) {
404
                $grand_total = $total;
405
            }
406
            $grand_total = $this->getGrandTotal($code, $total, $cost, $productid, $currency);
407
            $grand_total = $qty * $grand_total;
408
409
            $tax = $this->checkTax($product->id, $user_id);
410
            $tax_name = '';
411
            $tax_rate = '';
412
            if (!empty($tax)) {
413
                $tax_name = $tax[0];
414
                $tax_rate = $tax[1];
415
            }
416
417
            $grand_total = $this->calculateTotal($tax_rate, $grand_total);
418
            $grand_total = \App\Http\Controllers\Front\CartController::rounding($grand_total);
419
420
            $invoice = Invoice::create(['user_id' => $user_id,
421
                'number'                          => $number, 'date' => $date, 'grand_total' => $grand_total,
422
                'currency'                        => $currency, 'status' => 'pending', 'description' => $description, ]);
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...
423
424
            $items = $this->createInvoiceItemsByAdmin($invoice->id, $productid,
425
             $code, $total, $currency, $qty, $agents, $plan, $user_id, $tax_name, $tax_rate);
426
            $result = $this->getMessage($items, $user_id);
427
        } catch (\Exception $ex) {
428
            app('log')->info($ex->getMessage());
429
            Bugsnag::notifyException($ex);
430
            $result = ['fails' => $ex->getMessage()];
431
        }
432
433
        return response()->json(compact('result'));
434
    }
435
436
    public function createInvoiceItemsByAdmin($invoiceid, $productid, $code, $price,
437
        $currency, $qty, $agents, $planid = '', $userid = '', $tax_name = '', $tax_rate = '')
438
    {
439
        try {
440
            $discount = '';
441
            $mode = '';
442
            $product = $this->product->findOrFail($productid);
443
            $plan = Plan::where('product', $productid)->first();
444
            $subtotal = $qty * intval($price);
445
            if ($code) {
446
                $subtotal = $this->checkCode($code, $productid, $currency);
447
                $mode = 'coupon';
448
                $discount = $price - $subtotal;
449
            }
450
            $userid = \Auth::user()->id;
451
            if (\Auth::user()->role == 'user') {
452
                $tax = $this->checkTax($product->id, $userid);
453
                $tax_name = '';
454
                $tax_rate = '';
455
                if (!empty($tax)) {
456
                    $tax_name = $tax[0];
457
                    $tax_rate = $tax[1];
458
                }
459
            }
460
461
            $subtotal = $this->calculateTotal($tax_rate, $subtotal);
462
463
            $domain = $this->domain($productid);
464
            $items = $this->invoiceItem->create([
465
                'invoice_id'     => $invoiceid,
466
                'product_name'   => $product->name,
467
                'regular_price'  => $price,
468
                'quantity'       => $qty,
469
                'discount'       => $discount,
470
                'discount_mode'  => $mode,
471
                'subtotal'       => \App\Http\Controllers\Front\CartController::rounding($subtotal),
472
                'tax_name'       => $tax_name,
473
                'tax_percentage' => $tax_rate,
474
                'domain'         => $domain,
475
                'plan_id'        => $planid,
476
                'agents'         => $agents,
477
            ]);
478
479
            return $items;
480
        } catch (\Exception $ex) {
481
            Bugsnag::notifyException($ex);
482
483
            return redirect()->back()->with('fails', $ex->getMessage());
484
        }
485
    }
486
487
    public function checkTax($productid, $userid)
488
    {
489
        try {
490
            $taxs = [];
491
            $taxs[0] = ['name' => 'null', 'rate' => 0];
492
            $geoip_state = User::where('id', $userid)->pluck('state')->first();
493
            $geoip_country = User::where('id', $userid)->pluck('country')->first();
494
            $product = $this->product->findOrFail($productid);
495
            $cartController = new CartController();
496
            if ($this->tax_option->findOrFail(1)->inclusive == 0) {
497
                if ($this->tax_option->findOrFail(1)->tax_enable == 1) {
498
                    $taxs = $this->getTaxWhenEnable($productid, $taxs[0], $userid);
499
                } elseif ($this->tax_option->tax_enable == 0) {//if tax_enable is 0
500
501
                    $taxClassId = Tax::where('country', '')->where('state', 'Any State')
502
                     ->pluck('tax_classes_id')->first(); //In case of India when
503
                    //other tax is available and tax is not enabled
504
                    if ($taxClassId) {
505
                        $rate = $this->getTotalRate($taxClassId, $productid, $taxs);
506
                        $taxs = $rate['taxes'];
507
                        $rate = $rate['rate'];
508
                    } elseif ($geoip_country != 'IN') {//In case of other country
509
                        // when tax is available and tax is not enabled(Applicable
510
                        //when Global Tax class for any country and state is not there)
511
512
                        $taxClassId = Tax::where('state', $geoip_state)
513
                        ->orWhere('country', $geoip_country)->pluck('tax_classes_id')->first();
514
                        if ($taxClassId) { //if state equals the user State
515
                            $rate = $this->getTotalRate($taxClassId, $productid, $taxs);
516
                            $taxs = $rate['taxes'];
517
                            $rate = $rate['rate'];
518
                        }
519
                        $taxs = ([$taxs[0]['name'], $taxs[0]['rate']]);
520
521
                        return $taxs;
522
                    }
523
                    $taxs = ([$taxs[0]['name'], $taxs[0]['rate']]);
524
                } else {
525
                    $taxs = ([$taxs[0]['name'], $taxs[0]['rate']]);
526
                }
527
            }
528
529
            return $taxs;
530
        } catch (\Exception $ex) {
531
            throw new \Exception(\Lang::get('message.check-tax-error'));
532
        }
533
    }
534
535
    public function getRate($productid, $taxs, $userid)
536
    {
537
        $tax_attribute = [];
538
        $tax_attribute[0] = ['name' => 'null', 'rate' => 0, 'tax_enable' =>0];
539
        $tax_value = '0';
540
541
        $geoip_state = User::where('id', $userid)->pluck('state')->first();
542
        $geoip_country = User::where('id', $userid)->pluck('country')->first();
543
        $user_state = $this->tax_by_state::where('state_code', $geoip_state)->first();
544
        $origin_state = $this->setting->first()->state; //Get the State of origin
545
        $cartController = new CartController();
546
        $rate = 0;
547
        $name1 = 'CGST';
548
        $name2 = 'SGST';
549
        $name3 = 'IGST';
550
        $name4 = 'UTGST';
551
        $c_gst = 0;
552
        $s_gst = 0;
553
        $i_gst = 0;
554
        $ut_gst = 0;
555
        $state_code = '';
556
        if ($user_state != '') {//Get the CGST,SGST,IGST,STATE_CODE of the user
557
            $tax = $this->getTaxWhenState($user_state, $productid, $origin_state);
558
            $taxes = $tax['taxes'];
559
            $value = $tax['value'];
560
        } else {//If user from other Country
561
            $tax = $this->getTaxWhenOtherCountry($geoip_state, $geoip_country, $productid);
562
            $taxes = $tax['taxes'];
563
            $value = $tax['value'];
564
            $rate = $tax['rate'];
565
        }
566
567
        foreach ($taxes as $key => $tax) {
568
            if ($taxes[0]) {
569
                $tax_attribute[$key] = ['name' => $tax->name, 'name1' => $name1,
570
                 'name2'                       => $name2, 'name3' => $name3, 'name4' => $name4,
571
                 'rate'                        => $value, 'rate1'=>$c_gst, 'rate2'=>$s_gst,
572
                 'rate3'                       => $i_gst, 'rate4'=>$ut_gst, 'state'=>$state_code,
573
                  'origin_state'               => $origin_state, ];
574
575
                $rate = $tax->rate;
576
577
                $tax_value = $value;
578
            } else {
579
                $tax_attribute[0] = ['name' => 'null', 'rate' => 0, 'tax_enable' =>0];
580
                $tax_value = '0%';
581
            }
582
        }
583
584
        return ['taxs'=>$tax_attribute, 'value'=>$tax_value];
585
    }
586
587
    public function payment(Request $request)
588
    {
589
        try {
590
            if ($request->has('invoiceid')) {
591
                $invoice_id = $request->input('invoiceid');
592
                $invoice = $this->invoice->find($invoice_id);
593
                $userid = $invoice->user_id;
594
                //dd($invoice);
595
                $invoice_status = '';
596
                $payment_status = '';
597
                $payment_method = '';
598
                $domain = '';
599
                if ($invoice) {
600
                    $invoice_status = $invoice->status;
601
                    $items = $invoice->invoiceItem()->first();
602
                    if ($items) {
603
                        $domain = $items->domain;
604
                    }
605
                }
606
                $payment = $this->payment->where('invoice_id', $invoice_id)->first();
607
                if ($payment) {
608
                    $payment_status = $payment->payment_status;
609
                    $payment_method = $payment->payment_method;
610
                }
611
612
                return view('themes.default1.invoice.payment',
613
                 compact('invoice_status', 'payment_status',
614
                  'payment_method', 'invoice_id', 'domain', 'invoice', 'userid'));
615
            }
616
617
            return redirect()->back();
618
        } catch (\Exception $ex) {
619
            Bugsnag::notifyException($ex);
620
621
            return redirect()->back()->with('fails', $ex->getMessage());
622
        }
623
    }
624
625
    public function setDomain($productid, $domain)
626
    {
627
        try {
628
            if (\Session::has('domain'.$productid)) {
629
                \Session::forget('domain'.$productid);
630
            }
631
            \Session::put('domain'.$productid, $domain);
632
        } catch (\Exception $ex) {
633
            Bugsnag::notifyException($ex);
634
635
            throw new \Exception($ex->getMessage());
636
        }
637
    }
638
639
    public function sendMail($userid, $invoiceid)
640
    {
641
        try {
642
            $invoice = $this->invoice->find($invoiceid);
643
            $number = $invoice->number;
644
            $total = $invoice->grand_total;
645
646
            return $this->sendInvoiceMail($userid, $number, $total, $invoiceid);
647
        } catch (\Exception $ex) {
648
            Bugsnag::notifyException($ex);
649
650
            throw new \Exception($ex->getMessage());
651
        }
652
    }
653
654
    public function deletePayment(Request $request)
655
    {
656
        try {
657
            $ids = $request->input('select');
658
            if (!empty($ids)) {
659
                foreach ($ids as $id) {
660
                    $payment = $this->payment->where('id', $id)->first();
661
                    if ($payment) {
662
                        $invoice = $this->invoice->find($payment->invoice_id);
663
                        if ($invoice) {
664
                            $invoice->status = 'pending';
665
                            $invoice->save();
666
                        }
667
                        $payment->delete();
668
                    } else {
669
                        echo "<div class='alert alert-danger alert-dismissable'>
670
                    <i class='fa fa-ban'></i>
671
                    <b>"./* @scrutinizer ignore-type */ \Lang::get('message.alert').'!</b> 
672
                    './* @scrutinizer ignore-type */\Lang::get('message.failed').'
673
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
674
                        './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
675
                </div>';
676
                        //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
677
                    }
678
                }
679
                echo "<div class='alert alert-success alert-dismissable'>
680
                    <i class='fa fa-ban'></i>
681
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
682
                    /* @scrutinizer ignore-type */
683
                    \Lang::get('message.success').'
684
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
685
                        './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
686
                </div>';
687
            } else {
688
                echo "<div class='alert alert-danger alert-dismissable'>
689
                    <i class='fa fa-ban'></i>
690
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
691
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
692
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
693
                        './* @scrutinizer ignore-type */\Lang::get('message.select-a-row').'
694
                </div>';
695
                //echo \Lang::get('message.select-a-row');
696
            }
697
        } catch (\Exception $e) {
698
            Bugsnag::notifyException($e);
699
            echo "<div class='alert alert-danger alert-dismissable'>
700
                    <i class='fa fa-ban'></i>
701
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
702
                    /* @scrutinizer ignore-type */ \Lang::get('message.failed').'
703
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
704
                        '.$e->getMessage().'
705
                </div>';
706
        }
707
    }
708
}
709