| Total Complexity | 51 | 
| Total Lines | 579 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like InvoiceController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use InvoiceController, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 28 | class InvoiceController extends TaxRatesAndCodeExpiryController  | 
            ||
| 29 | { | 
            ||
| 30 | use CoupCodeAndInvoiceSearch;  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 31 | use PaymentsAndInvoices;  | 
            ||
| 32 | |||
| 33 | public $invoice;  | 
            ||
| 34 | public $invoiceItem;  | 
            ||
| 35 | public $user;  | 
            ||
| 36 | public $template;  | 
            ||
| 37 | public $setting;  | 
            ||
| 38 | public $payment;  | 
            ||
| 39 | public $product;  | 
            ||
| 40 | public $price;  | 
            ||
| 41 | public $promotion;  | 
            ||
| 42 | public $currency;  | 
            ||
| 43 | public $tax;  | 
            ||
| 44 | public $tax_option;  | 
            ||
| 45 | public $order;  | 
            ||
| 46 | public $cartController;  | 
            ||
| 47 | |||
| 48 | public function __construct()  | 
            ||
| 97 | }  | 
            ||
| 98 | |||
| 99 | public function index(Request $request)  | 
            ||
| 100 |     { | 
            ||
| 101 |         try { | 
            ||
| 102 |             $currencies = Currency::where('status', 1)->pluck('code')->toArray(); | 
            ||
| 103 |             $name = $request->input('name'); | 
            ||
| 104 |             $invoice_no = $request->input('invoice_no'); | 
            ||
| 105 |             $status = $request->input('status'); | 
            ||
| 106 | |||
| 107 |             $currency_id = $request->input('currency_id'); | 
            ||
| 108 |             $from = $request->input('from'); | 
            ||
| 109 |             $till = $request->input('till'); | 
            ||
| 110 | |||
| 111 |             return view('themes.default1.invoice.index', compact('name','invoice_no','status','currencies','currency_id','from', | 
            ||
| 112 | |||
| 113 | 'till'));  | 
            ||
| 114 |         } catch (\Exception $ex) { | 
            ||
| 115 | Bugsnag::notifyException($ex);  | 
            ||
| 116 | |||
| 117 |             return redirect()->back()->with('fails', $ex->getMessage()); | 
            ||
| 118 | }  | 
            ||
| 119 | }  | 
            ||
| 120 | |||
| 121 | public function getInvoices(Request $request)  | 
            ||
| 203 | }  | 
            ||
| 204 | |||
| 205 | /**  | 
            ||
| 206 | * Shoe Invoice when view Invoice is selected from dropdown in Admin Panel.  | 
            ||
| 207 | *  | 
            ||
| 208 | * @param Request $request Get InvoiceId as Request  | 
            ||
| 209 | */  | 
            ||
| 210 | public function show(Request $request)  | 
            ||
| 226 | }  | 
            ||
| 227 | }  | 
            ||
| 228 | |||
| 229 | /**  | 
            ||
| 230 | * not in use case.  | 
            ||
| 231 | *  | 
            ||
| 232 | * @param Request $request  | 
            ||
| 233 | *  | 
            ||
| 234 | * @return type  | 
            ||
| 235 | */  | 
            ||
| 236 | public function generateById(Request $request)  | 
            ||
| 258 | }  | 
            ||
| 259 | }  | 
            ||
| 260 | |||
| 261 | /**  | 
            ||
| 262 | * Generate invoice.  | 
            ||
| 263 | *  | 
            ||
| 264 | * @throws \Exception  | 
            ||
| 265 | */  | 
            ||
| 266 | |||
| 267 | //Is this Method only for cliet?? because Auth::user->id?  | 
            ||
| 268 | public function generateInvoice()  | 
            ||
| 269 |     { | 
            ||
| 270 |         try { | 
            ||
| 271 | $tax_rule = new \App\Model\Payment\TaxOption();  | 
            ||
| 272 | $rule = $tax_rule->findOrFail(1);  | 
            ||
| 273 | $rounding = $rule->rounding;  | 
            ||
| 274 | $user_id = \Auth::user()->id;  | 
            ||
| 275 |             if (\Auth::user()->currency == 'INR') { | 
            ||
| 276 | $grand_total = \Cart::getSubTotal();  | 
            ||
| 277 |             } else { | 
            ||
| 278 |                 foreach (\Cart::getContent() as $cart) { | 
            ||
| 279 | $grand_total = \Cart::getSubTotal();  | 
            ||
| 280 | }  | 
            ||
| 281 | }  | 
            ||
| 282 | $number = rand(11111111, 99999999);  | 
            ||
| 283 | $date = \Carbon\Carbon::now();  | 
            ||
| 284 |             if ($rounding == 1) { | 
            ||
| 285 | $grand_total = round($grand_total);  | 
            ||
| 286 | }  | 
            ||
| 287 | $content = \Cart::getContent();  | 
            ||
| 288 | $attributes = [];  | 
            ||
| 289 |             foreach ($content as $key => $item) { | 
            ||
| 290 | $attributes[] = $item->attributes;  | 
            ||
| 291 | }  | 
            ||
| 292 | |||
| 293 | $symbol = $attributes[0]['currency']['symbol'];  | 
            ||
| 294 | $invoice = $this->invoice->create(['user_id' => $user_id, 'number' => $number,  | 
            ||
| 295 | 'date' => $date, 'grand_total' => $grand_total, 'status' => 'pending',  | 
            ||
| 296 | 'currency' => \Auth::user()->currency, ]);  | 
            ||
| 297 |             foreach (\Cart::getContent() as $cart) { | 
            ||
| 298 | $this->createInvoiceItems($invoice->id, $cart);  | 
            ||
| 299 | }  | 
            ||
| 300 | $this->sendMail($user_id, $invoice->id);  | 
            ||
| 301 | |||
| 302 | return $invoice;  | 
            ||
| 303 |         } catch (\Exception $ex) { | 
            ||
| 304 |             app('log')->error($ex->getMessage()); | 
            ||
| 305 | Bugsnag::notifyException($ex);  | 
            ||
| 306 | |||
| 307 |             throw new \Exception('Can not Generate Invoice'); | 
            ||
| 308 | }  | 
            ||
| 309 | }  | 
            ||
| 310 | |||
| 311 | public function createInvoiceItems($invoiceid, $cart)  | 
            ||
| 357 | }  | 
            ||
| 358 | }  | 
            ||
| 359 | |||
| 360 | public function invoiceGenerateByForm(Request $request, $user_id = '')  | 
            ||
| 361 |     { | 
            ||
| 362 | $this->validate($request,[  | 
            ||
| 363 | |||
| 364 | 'plan' => 'required_if:subscription,true',  | 
            ||
| 365 | 'price' => 'required',  | 
            ||
| 366 | ],[  | 
            ||
| 367 | 'plan.required_if' =>'Select a Plan',  | 
            ||
| 368 | ]);  | 
            ||
| 369 |         try { | 
            ||
| 370 |             $agents = $request->input('agents'); | 
            ||
| 371 | $status = 'pending';  | 
            ||
| 372 |             $qty = $request->input('quantity'); | 
            ||
| 373 |             if ($user_id == '') { | 
            ||
| 374 |                 $user_id = \Request::input('user'); | 
            ||
| 375 | }  | 
            ||
| 376 |             $productid = $request->input('product'); | 
            ||
| 377 | |||
| 378 |             $plan = $request->input('plan'); | 
            ||
| 379 | $agents = $this->getAgents($agents, $productid, $plan);  | 
            ||
| 380 | $qty = $this->getQuantity($qty, $productid, $plan);  | 
            ||
| 381 | |||
| 382 |             $code = $request->input('code'); | 
            ||
| 383 |             $total = $request->input('price'); | 
            ||
| 384 |             $description = $request->input('description'); | 
            ||
| 385 |             if ($request->has('domain')) { | 
            ||
| 386 |                 $domain = $request->input('domain'); | 
            ||
| 387 | $this->setDomain($productid, $domain);  | 
            ||
| 388 | }  | 
            ||
| 389 | $controller = new \App\Http\Controllers\Front\CartController();  | 
            ||
| 390 | $userCurrency = $controller->currency($user_id);  | 
            ||
| 391 | $currency = $userCurrency['currency'];  | 
            ||
| 392 | $number = rand(11111111, 99999999);  | 
            ||
| 393 | $date = \Carbon\Carbon::now();  | 
            ||
| 394 | $product = Product::find($productid);  | 
            ||
| 395 | $cost = $controller->cost($productid, $user_id, $plan);  | 
            ||
| 396 |             // if ($cost != $total) { | 
            ||
| 397 | // $grand_total = $total;  | 
            ||
| 398 | // }  | 
            ||
| 399 | $grand_total = $this->getGrandTotal($code, $total, $cost, $productid, $currency);  | 
            ||
| 400 | $grand_total = $qty * $grand_total;  | 
            ||
| 401 |             if ($grand_total == 0) { | 
            ||
| 402 | $status = 'success';  | 
            ||
| 403 | }  | 
            ||
| 404 | $tax = $this->checkTax($product->id, $user_id);  | 
            ||
| 405 | $tax_name = '';  | 
            ||
| 406 | $tax_rate = '';  | 
            ||
| 407 |             if (!empty($tax)) { | 
            ||
| 408 | $tax_name = $tax[0];  | 
            ||
| 409 | $tax_rate = $tax[1];  | 
            ||
| 410 | }  | 
            ||
| 411 | |||
| 412 | $grand_total = $this->calculateTotal($tax_rate, $grand_total);  | 
            ||
| 413 | $grand_total = \App\Http\Controllers\Front\CartController::rounding($grand_total);  | 
            ||
| 414 | |||
| 415 | $invoice = Invoice::create(['user_id' => $user_id,  | 
            ||
| 416 | 'number' => $number, 'date' => $date, 'grand_total' => $grand_total,  | 
            ||
| 417 | 'currency' => $currency, 'status' => $status, 'description' => $description, ]);  | 
            ||
| 418 | |||
| 419 | $items = $this->createInvoiceItemsByAdmin($invoice->id, $productid,  | 
            ||
| 420 | $code, $total, $currency, $qty, $agents, $plan, $user_id, $tax_name, $tax_rate);  | 
            ||
| 421 | $result = $this->getMessage($items, $user_id);  | 
            ||
| 422 |         } catch (\Exception $ex) { | 
            ||
| 423 |             app('log')->info($ex->getMessage()); | 
            ||
| 424 | Bugsnag::notifyException($ex);  | 
            ||
| 425 | $result = ['fails' => $ex->getMessage()];  | 
            ||
| 426 | }  | 
            ||
| 427 | |||
| 428 |         return response()->json(compact('result')); | 
            ||
| 429 | }  | 
            ||
| 430 | |||
| 431 | public function createInvoiceItemsByAdmin($invoiceid, $productid, $code, $price,  | 
            ||
| 432 | $currency, $qty, $agents, $planid = '', $userid = '', $tax_name = '', $tax_rate = '')  | 
            ||
| 433 |     { | 
            ||
| 434 |         try { | 
            ||
| 435 | $discount = '';  | 
            ||
| 436 | $mode = '';  | 
            ||
| 437 | $product = $this->product->findOrFail($productid);  | 
            ||
| 438 |             $plan = Plan::where('product', $productid)->first(); | 
            ||
| 439 | $subtotal = $qty * intval($price);  | 
            ||
| 440 |             if ($code) { | 
            ||
| 441 | $subtotal = $this->checkCode($code, $productid, $currency);  | 
            ||
| 442 | $mode = 'coupon';  | 
            ||
| 443 | $discount = $price - $subtotal;  | 
            ||
| 444 | }  | 
            ||
| 445 | $userid = \Auth::user()->id;  | 
            ||
| 446 |             if (\Auth::user()->role == 'user') { | 
            ||
| 447 | $tax = $this->checkTax($product->id, $userid);  | 
            ||
| 448 | $tax_name = '';  | 
            ||
| 449 | $tax_rate = '';  | 
            ||
| 450 |                 if (!empty($tax)) { | 
            ||
| 451 | $tax_name = $tax[0];  | 
            ||
| 452 | $tax_rate = $tax[1];  | 
            ||
| 453 | }  | 
            ||
| 454 | }  | 
            ||
| 455 | |||
| 456 | $subtotal = $this->calculateTotal($tax_rate, $subtotal);  | 
            ||
| 457 | $domain = $this->domain($productid);  | 
            ||
| 458 | $items = $this->invoiceItem->create([  | 
            ||
| 459 | 'invoice_id' => $invoiceid,  | 
            ||
| 460 | 'product_name' => $product->name,  | 
            ||
| 461 | 'regular_price' => $price,  | 
            ||
| 462 | 'quantity' => $qty,  | 
            ||
| 463 | 'discount' => $discount,  | 
            ||
| 464 | 'discount_mode' => $mode,  | 
            ||
| 465 | 'subtotal' => \App\Http\Controllers\Front\CartController::rounding($subtotal),  | 
            ||
| 466 | 'tax_name' => $tax_name,  | 
            ||
| 467 | 'tax_percentage' => $tax_rate,  | 
            ||
| 468 | 'domain' => $domain,  | 
            ||
| 469 | 'plan_id' => $planid,  | 
            ||
| 470 | 'agents' => $agents,  | 
            ||
| 471 | ]);  | 
            ||
| 472 | |||
| 473 | return $items;  | 
            ||
| 474 |         } catch (\Exception $ex) { | 
            ||
| 475 | Bugsnag::notifyException($ex);  | 
            ||
| 476 | |||
| 477 |             return redirect()->back()->with('fails', $ex->getMessage()); | 
            ||
| 478 | }  | 
            ||
| 479 | }  | 
            ||
| 480 | |||
| 481 | public function checkTax($productid, $userid)  | 
            ||
| 482 |     { | 
            ||
| 483 |         try { | 
            ||
| 484 | $taxs = [];  | 
            ||
| 485 | $taxs[0] = ['name' => 'null', 'rate' => 0];  | 
            ||
| 486 |             $geoip_state = User::where('id', $userid)->pluck('state')->first(); | 
            ||
| 487 |             $geoip_country = User::where('id', $userid)->pluck('country')->first(); | 
            ||
| 488 | $product = $this->product->findOrFail($productid);  | 
            ||
| 489 | $cartController = new CartController();  | 
            ||
| 490 |             if ($this->tax_option->findOrFail(1)->inclusive == 0) { | 
            ||
| 491 |                 if ($this->tax_option->findOrFail(1)->tax_enable == 1) { | 
            ||
| 492 | $taxs = $this->getTaxWhenEnable($productid, $taxs[0], $userid);  | 
            ||
| 493 |                 } elseif ($this->tax_option->tax_enable == 0) {//if tax_enable is 0 | 
            ||
| 494 | |||
| 495 |                     $taxClassId = Tax::where('country', '')->where('state', 'Any State') | 
            ||
| 496 |                      ->pluck('tax_classes_id')->first(); //In case of India when | 
            ||
| 497 | //other tax is available and tax is not enabled  | 
            ||
| 498 |                     if ($taxClassId) { | 
            ||
| 499 | $rate = $this->getTotalRate($taxClassId, $productid, $taxs);  | 
            ||
| 500 | $taxs = $rate['taxes'];  | 
            ||
| 501 | $rate = $rate['rate'];  | 
            ||
| 502 |                     } elseif ($geoip_country != 'IN') {//In case of other country | 
            ||
| 503 | // when tax is available and tax is not enabled(Applicable  | 
            ||
| 504 | //when Global Tax class for any country and state is not there)  | 
            ||
| 505 | |||
| 506 |                         $taxClassId = Tax::where('state', $geoip_state) | 
            ||
| 507 |                         ->orWhere('country', $geoip_country)->pluck('tax_classes_id')->first(); | 
            ||
| 508 |                         if ($taxClassId) { //if state equals the user State | 
            ||
| 509 | $rate = $this->getTotalRate($taxClassId, $productid, $taxs);  | 
            ||
| 510 | $taxs = $rate['taxes'];  | 
            ||
| 511 | $rate = $rate['rate'];  | 
            ||
| 512 | }  | 
            ||
| 513 | $taxs = ([$taxs[0]['name'], $taxs[0]['rate']]);  | 
            ||
| 514 | |||
| 515 | return $taxs;  | 
            ||
| 516 | }  | 
            ||
| 517 | $taxs = ([$taxs[0]['name'], $taxs[0]['rate']]);  | 
            ||
| 518 |                 } else { | 
            ||
| 519 | $taxs = ([$taxs[0]['name'], $taxs[0]['rate']]);  | 
            ||
| 520 | }  | 
            ||
| 521 | }  | 
            ||
| 522 | |||
| 523 | return $taxs;  | 
            ||
| 524 |         } catch (\Exception $ex) { | 
            ||
| 525 |             throw new \Exception(\Lang::get('message.check-tax-error')); | 
            ||
| 526 | }  | 
            ||
| 527 | }  | 
            ||
| 528 | |||
| 529 | public function getRate($productid, $taxs, $userid)  | 
            ||
| 530 |     { | 
            ||
| 531 | $tax_attribute = [];  | 
            ||
| 532 | $tax_attribute[0] = ['name' => 'null', 'rate' => 0, 'tax_enable' =>0];  | 
            ||
| 533 | $tax_value = '0';  | 
            ||
| 534 | |||
| 535 |         $geoip_state = User::where('id', $userid)->pluck('state')->first(); | 
            ||
| 536 |         $geoip_country = User::where('id', $userid)->pluck('country')->first(); | 
            ||
| 537 |         $user_state = $this->tax_by_state::where('state_code', $geoip_state)->first(); | 
            ||
| 538 | $origin_state = $this->setting->first()->state; //Get the State of origin  | 
            ||
| 539 | $cartController = new CartController();  | 
            ||
| 540 | $rate = 0;  | 
            ||
| 541 | $name1 = 'CGST';  | 
            ||
| 542 | $name2 = 'SGST';  | 
            ||
| 543 | $name3 = 'IGST';  | 
            ||
| 544 | $name4 = 'UTGST';  | 
            ||
| 545 | $c_gst = 0;  | 
            ||
| 546 | $s_gst = 0;  | 
            ||
| 547 | $i_gst = 0;  | 
            ||
| 548 | $ut_gst = 0;  | 
            ||
| 549 | $state_code = '';  | 
            ||
| 550 |         if ($user_state != '') {//Get the CGST,SGST,IGST,STATE_CODE of the user | 
            ||
| 551 | $tax = $this->getTaxWhenState($user_state, $productid, $origin_state);  | 
            ||
| 552 | $taxes = $tax['taxes'];  | 
            ||
| 553 | $value = $tax['value'];  | 
            ||
| 554 |         } else {//If user from other Country | 
            ||
| 555 | $tax = $this->getTaxWhenOtherCountry($geoip_state, $geoip_country, $productid);  | 
            ||
| 556 | $taxes = $tax['taxes'];  | 
            ||
| 557 | $value = $tax['value'];  | 
            ||
| 558 | $rate = $tax['rate'];  | 
            ||
| 559 | }  | 
            ||
| 560 | |||
| 561 |         foreach ($taxes as $key => $tax) { | 
            ||
| 562 |             if ($taxes[0]) { | 
            ||
| 563 | $tax_attribute[$key] = ['name' => $tax->name, 'name1' => $name1,  | 
            ||
| 564 | 'name2' => $name2, 'name3' => $name3, 'name4' => $name4,  | 
            ||
| 565 | 'rate' => $value, 'rate1'=>$c_gst, 'rate2'=>$s_gst,  | 
            ||
| 566 | 'rate3' => $i_gst, 'rate4'=>$ut_gst, 'state'=>$state_code,  | 
            ||
| 567 | 'origin_state' => $origin_state, ];  | 
            ||
| 568 | |||
| 569 | $rate = $tax->rate;  | 
            ||
| 570 | |||
| 571 | $tax_value = $value;  | 
            ||
| 572 |             } else { | 
            ||
| 573 | $tax_attribute[0] = ['name' => 'null', 'rate' => 0, 'tax_enable' =>0];  | 
            ||
| 574 | $tax_value = '0%';  | 
            ||
| 575 | }  | 
            ||
| 576 | }  | 
            ||
| 577 | |||
| 578 | return ['taxs'=>$tax_attribute, 'value'=>$tax_value];  | 
            ||
| 579 | }  | 
            ||
| 580 | |||
| 581 | public function setDomain($productid, $domain)  | 
            ||
| 582 |     { | 
            ||
| 583 |         try { | 
            ||
| 584 |             if (\Session::has('domain'.$productid)) { | 
            ||
| 585 |                 \Session::forget('domain'.$productid); | 
            ||
| 586 | }  | 
            ||
| 587 |             \Session::put('domain'.$productid, $domain); | 
            ||
| 588 |         } catch (\Exception $ex) { | 
            ||
| 589 | Bugsnag::notifyException($ex);  | 
            ||
| 590 | |||
| 591 | throw new \Exception($ex->getMessage());  | 
            ||
| 592 | }  | 
            ||
| 593 | }  | 
            ||
| 594 | |||
| 595 | public function sendMail($userid, $invoiceid)  | 
            ||
| 607 | }  | 
            ||
| 608 | }  | 
            ||
| 609 | }  | 
            ||
| 610 |