| Total Complexity | 52 |
| Total Lines | 587 |
| 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 | public function generateInvoice() |
||
| 268 | { |
||
| 269 | try { |
||
| 270 | $code = ''; |
||
| 271 | $codevalue = ''; |
||
| 272 | if(\Session::has('code')){//If coupon code is applied get it here from Session |
||
| 273 | $code = \Session::get('code'); |
||
| 274 | $codevalue = \Session::get('codevalue'); |
||
| 275 | } |
||
| 276 | $tax_rule = new \App\Model\Payment\TaxOption(); |
||
| 277 | $rule = $tax_rule->findOrFail(1); |
||
| 278 | $rounding = $rule->rounding; |
||
| 279 | $user_id = \Auth::user()->id; |
||
| 280 | if (\Auth::user()->currency == 'INR') { |
||
| 281 | $grand_total = \Cart::getSubTotal(); |
||
| 282 | } else { |
||
| 283 | foreach (\Cart::getContent() as $cart) { |
||
| 284 | $grand_total = \Cart::getSubTotal(); |
||
| 285 | } |
||
| 286 | } |
||
| 287 | $number = rand(11111111, 99999999); |
||
| 288 | $date = \Carbon\Carbon::now(); |
||
| 289 | if ($rounding == 1) { |
||
| 290 | $grand_total = round($grand_total); |
||
| 291 | } |
||
| 292 | $content = \Cart::getContent(); |
||
| 293 | $attributes = []; |
||
| 294 | foreach ($content as $key => $item) { |
||
| 295 | $attributes[] = $item->attributes; |
||
| 296 | } |
||
| 297 | |||
| 298 | $symbol = $attributes[0]['currency']['symbol']; |
||
| 299 | $invoice = $this->invoice->create(['user_id' => $user_id, 'number' => $number, |
||
| 300 | 'date' => $date, 'discount'=>$codevalue,'grand_total' => $grand_total, 'coupon_code'=>$code,'status' => 'pending', |
||
| 301 | 'currency' => \Auth::user()->currency, ]); |
||
| 302 | foreach (\Cart::getContent() as $cart) { |
||
| 303 | $this->createInvoiceItems($invoice->id, $cart,$codevalue); |
||
| 304 | } |
||
| 305 | $this->sendMail($user_id, $invoice->id); |
||
| 306 | |||
| 307 | return $invoice; |
||
| 308 | } catch (\Exception $ex) { |
||
| 309 | app('log')->error($ex->getMessage()); |
||
| 310 | Bugsnag::notifyException($ex); |
||
| 311 | |||
| 312 | throw new \Exception('Can not Generate Invoice'); |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | public function createInvoiceItems($invoiceid, $cart,$codevalue='') |
||
| 317 | { |
||
| 318 | try { |
||
| 319 | $planid = 0; |
||
| 320 | $product_name = $cart->name; |
||
| 321 | $regular_price = $cart->price; |
||
| 322 | $quantity = $cart->quantity; |
||
| 323 | $agents = $cart->attributes->agents; |
||
| 324 | $domain = $this->domain($cart->id); |
||
| 325 | $cart_cont = new \App\Http\Controllers\Front\CartController(); |
||
| 326 | if ($cart_cont->checkPlanSession() === true) { |
||
| 327 | $planid = \Session::get('plan'); |
||
| 328 | } |
||
| 329 | if ($planid == 0) { |
||
| 330 | //When Product is added from Faveo Website |
||
| 331 | $planid = Plan::where('product', $cart->id)->pluck('id')->first(); |
||
| 332 | } |
||
| 333 | $user_currency = \Auth::user()->currency; |
||
| 334 | $subtotal = $this->getSubtotal($user_currency, $cart); |
||
| 335 | |||
| 336 | $tax_name = ''; |
||
| 337 | $tax_percentage = ''; |
||
| 338 | |||
| 339 | foreach ($cart->attributes['tax'] as $tax) { |
||
| 340 | $tax_name .= $tax['name'].','; |
||
| 341 | $tax_percentage .= $tax['rate'].','; |
||
| 342 | } |
||
| 343 | $invoiceItem = $this->invoiceItem->create([ |
||
| 344 | 'invoice_id' => $invoiceid, |
||
| 345 | 'product_name' => $product_name, |
||
| 346 | 'regular_price' => $regular_price, |
||
| 347 | 'quantity' => $quantity, |
||
| 348 | 'tax_name' => $tax_name, |
||
| 349 | 'discount' => $codevalue, |
||
| 350 | 'tax_percentage' => $tax_percentage, |
||
| 351 | 'subtotal' => $subtotal, |
||
| 352 | 'domain' => $domain, |
||
| 353 | 'plan_id' => $planid, |
||
| 354 | 'agents' => $agents, |
||
| 355 | ]); |
||
| 356 | |||
| 357 | return $invoiceItem; |
||
| 358 | } catch (\Exception $ex) { |
||
| 359 | Bugsnag::notifyException($ex->getMessage()); |
||
| 360 | |||
| 361 | throw new \Exception('Can not create Invoice Items'); |
||
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | public function invoiceGenerateByForm(Request $request, $user_id = '') |
||
| 366 | { |
||
| 367 | $this->validate($request, [ |
||
| 368 | |||
| 369 | 'plan' => 'required_if:subscription,true', |
||
| 370 | 'price' => 'required', |
||
| 371 | ], [ |
||
| 372 | 'plan.required_if' => 'Select a Plan', |
||
| 373 | ]); |
||
| 374 | |||
| 375 | try { |
||
| 376 | $agents = $request->input('agents'); |
||
| 377 | $status = 'pending'; |
||
| 378 | $qty = $request->input('quantity'); |
||
| 379 | if ($user_id == '') { |
||
| 380 | $user_id = \Request::input('user'); |
||
| 381 | } |
||
| 382 | $productid = $request->input('product'); |
||
| 383 | |||
| 384 | $plan = $request->input('plan'); |
||
| 385 | $agents = $this->getAgents($agents, $productid, $plan); |
||
| 386 | $qty = $this->getQuantity($qty, $productid, $plan); |
||
| 387 | |||
| 388 | $code = $request->input('code'); |
||
| 389 | $total = $request->input('price'); |
||
| 390 | $description = $request->input('description'); |
||
| 391 | if ($request->has('domain')) { |
||
| 392 | $domain = $request->input('domain'); |
||
| 393 | $this->setDomain($productid, $domain); |
||
| 394 | } |
||
| 395 | $controller = new \App\Http\Controllers\Front\CartController(); |
||
| 396 | $userCurrency = $controller->currency($user_id); |
||
| 397 | $currency = $userCurrency['currency']; |
||
| 398 | $number = rand(11111111, 99999999); |
||
| 399 | $date = \Carbon\Carbon::now(); |
||
| 400 | $product = Product::find($productid); |
||
| 401 | $cost = $controller->cost($productid, $user_id, $plan); |
||
| 402 | // if ($cost != $total) { |
||
| 403 | // $grand_total = $total; |
||
| 404 | // } |
||
| 405 | $grand_total = $this->getGrandTotal($code, $total, $cost, $productid, $currency); |
||
| 406 | $promo = $this->promotion->where('code', $code)->first(); |
||
| 407 | $codeValue = $this->getCodeValue($promo,$code);//get coupon code value to be added to Invoice |
||
| 408 | $grand_total = $qty * $grand_total; |
||
| 409 | if ($grand_total == 0) { |
||
| 410 | $status = 'success'; |
||
| 411 | } |
||
| 412 | $tax = $this->checkTax($product->id, $user_id); |
||
| 413 | $tax_name = ''; |
||
| 414 | $tax_rate = ''; |
||
| 415 | if (!empty($tax)) { |
||
| 416 | $tax_name = $tax[0]; |
||
| 417 | $tax_rate = $tax[1]; |
||
| 418 | } |
||
| 419 | |||
| 420 | $grand_total = $this->calculateTotal($tax_rate, $grand_total); |
||
| 421 | $grand_total = \App\Http\Controllers\Front\CartController::rounding($grand_total); |
||
| 422 | |||
| 423 | $invoice = Invoice::create(['user_id' => $user_id,'number' => $number, 'date' => $date, |
||
| 424 | 'coupon_code'=>$code,'discount'=>$codeValue, |
||
| 425 | 'grand_total' => $grand_total, 'currency' => $currency, 'status' => $status, 'description' => $description, ]); |
||
| 426 | |||
| 427 | $items = $this->createInvoiceItemsByAdmin($invoice->id, $productid, |
||
| 428 | $code, $total, $currency, $qty, $agents, $plan, $user_id, $tax_name, $tax_rate); |
||
| 429 | $result = $this->getMessage($items, $user_id); |
||
| 430 | } catch (\Exception $ex) { |
||
| 431 | app('log')->info($ex->getMessage()); |
||
| 432 | Bugsnag::notifyException($ex); |
||
| 433 | $result = ['fails' => $ex->getMessage()]; |
||
| 434 | } |
||
| 435 | |||
| 436 | return response()->json(compact('result')); |
||
| 437 | } |
||
| 438 | |||
| 439 | public function createInvoiceItemsByAdmin($invoiceid, $productid, $code, $price, |
||
| 440 | $currency, $qty, $agents, $planid = '', $userid = '', $tax_name = '', $tax_rate = '') |
||
| 441 | { |
||
| 442 | try { |
||
| 443 | $discount = ''; |
||
| 444 | $mode = ''; |
||
| 445 | $product = $this->product->findOrFail($productid); |
||
| 446 | $plan = Plan::where('product', $productid)->first(); |
||
| 447 | $subtotal = $qty * intval($price); |
||
| 448 | if ($code) { |
||
| 449 | $subtotal = $this->checkCode($code, $productid, $currency); |
||
| 450 | $mode = 'coupon'; |
||
| 451 | $discount = $price - $subtotal; |
||
| 452 | } |
||
| 453 | $userid = \Auth::user()->id; |
||
| 454 | if (\Auth::user()->role == 'user') { |
||
| 455 | $tax = $this->checkTax($product->id, $userid); |
||
| 456 | $tax_name = ''; |
||
| 457 | $tax_rate = ''; |
||
| 458 | if (!empty($tax)) { |
||
| 459 | $tax_name = $tax[0]; |
||
| 460 | $tax_rate = $tax[1]; |
||
| 461 | } |
||
| 462 | } |
||
| 463 | |||
| 464 | $subtotal = $this->calculateTotal($tax_rate, $subtotal); |
||
| 465 | $domain = $this->domain($productid); |
||
| 466 | $items = $this->invoiceItem->create([ |
||
| 467 | 'invoice_id' => $invoiceid, |
||
| 468 | 'product_name' => $product->name, |
||
| 469 | 'regular_price' => $price, |
||
| 470 | 'quantity' => $qty, |
||
| 471 | 'discount' => $discount, |
||
| 472 | 'discount_mode' => $mode, |
||
| 473 | 'subtotal' => \App\Http\Controllers\Front\CartController::rounding($subtotal), |
||
| 474 | 'tax_name' => $tax_name, |
||
| 475 | 'tax_percentage' => $tax_rate, |
||
| 476 | 'domain' => $domain, |
||
| 477 | 'plan_id' => $planid, |
||
| 478 | 'agents' => $agents, |
||
| 479 | ]); |
||
| 480 | |||
| 481 | return $items; |
||
| 482 | } catch (\Exception $ex) { |
||
| 483 | Bugsnag::notifyException($ex); |
||
| 484 | |||
| 485 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 486 | } |
||
| 487 | } |
||
| 488 | |||
| 489 | public function checkTax($productid, $userid) |
||
| 490 | { |
||
| 491 | try { |
||
| 492 | $taxs = []; |
||
| 493 | $taxs[0] = ['name' => 'null', 'rate' => 0]; |
||
| 494 | $geoip_state = User::where('id', $userid)->pluck('state')->first(); |
||
| 495 | $geoip_country = User::where('id', $userid)->pluck('country')->first(); |
||
| 496 | $product = $this->product->findOrFail($productid); |
||
| 497 | $cartController = new CartController(); |
||
| 498 | if ($this->tax_option->findOrFail(1)->inclusive == 0) { |
||
| 499 | if ($this->tax_option->findOrFail(1)->tax_enable == 1) { |
||
| 500 | $taxs = $this->getTaxWhenEnable($productid, $taxs[0], $userid); |
||
| 501 | } elseif ($this->tax_option->tax_enable == 0) {//if tax_enable is 0 |
||
| 502 | |||
| 503 | $taxClassId = Tax::where('country', '')->where('state', 'Any State') |
||
| 504 | ->pluck('tax_classes_id')->first(); //In case of India when |
||
| 505 | //other tax is available and tax is not enabled |
||
| 506 | if ($taxClassId) { |
||
| 507 | $rate = $this->getTotalRate($taxClassId, $productid, $taxs); |
||
| 508 | $taxs = $rate['taxes']; |
||
| 509 | $rate = $rate['rate']; |
||
| 510 | } elseif ($geoip_country != 'IN') {//In case of other country |
||
| 511 | // when tax is available and tax is not enabled(Applicable |
||
| 512 | //when Global Tax class for any country and state is not there) |
||
| 513 | |||
| 514 | $taxClassId = Tax::where('state', $geoip_state) |
||
| 515 | ->orWhere('country', $geoip_country)->pluck('tax_classes_id')->first(); |
||
| 516 | if ($taxClassId) { //if state equals the user State |
||
| 517 | $rate = $this->getTotalRate($taxClassId, $productid, $taxs); |
||
| 518 | $taxs = $rate['taxes']; |
||
| 519 | $rate = $rate['rate']; |
||
| 520 | } |
||
| 521 | $taxs = ([$taxs[0]['name'], $taxs[0]['rate']]); |
||
| 522 | |||
| 523 | return $taxs; |
||
| 524 | } |
||
| 525 | $taxs = ([$taxs[0]['name'], $taxs[0]['rate']]); |
||
| 526 | } else { |
||
| 527 | $taxs = ([$taxs[0]['name'], $taxs[0]['rate']]); |
||
| 528 | } |
||
| 529 | } |
||
| 530 | |||
| 531 | return $taxs; |
||
| 532 | } catch (\Exception $ex) { |
||
| 533 | throw new \Exception(\Lang::get('message.check-tax-error')); |
||
| 534 | } |
||
| 535 | } |
||
| 536 | |||
| 537 | public function getRate($productid, $taxs, $userid) |
||
| 538 | { |
||
| 539 | $tax_attribute = []; |
||
| 540 | $tax_attribute[0] = ['name' => 'null', 'rate' => 0, 'tax_enable' =>0]; |
||
| 541 | $tax_value = '0'; |
||
| 542 | |||
| 543 | $geoip_state = User::where('id', $userid)->pluck('state')->first(); |
||
| 544 | $geoip_country = User::where('id', $userid)->pluck('country')->first(); |
||
| 545 | $user_state = $this->tax_by_state::where('state_code', $geoip_state)->first(); |
||
| 546 | $origin_state = $this->setting->first()->state; //Get the State of origin |
||
| 547 | $cartController = new CartController(); |
||
| 548 | $rate = 0; |
||
| 549 | $name1 = 'CGST'; |
||
| 550 | $name2 = 'SGST'; |
||
| 551 | $name3 = 'IGST'; |
||
| 552 | $name4 = 'UTGST'; |
||
| 553 | $c_gst = 0; |
||
| 554 | $s_gst = 0; |
||
| 555 | $i_gst = 0; |
||
| 556 | $ut_gst = 0; |
||
| 557 | $state_code = ''; |
||
| 558 | if ($user_state != '') {//Get the CGST,SGST,IGST,STATE_CODE of the user |
||
| 559 | $tax = $this->getTaxWhenState($user_state, $productid, $origin_state); |
||
| 560 | $taxes = $tax['taxes']; |
||
| 561 | $value = $tax['value']; |
||
| 562 | } else {//If user from other Country |
||
| 563 | $tax = $this->getTaxWhenOtherCountry($geoip_state, $geoip_country, $productid); |
||
| 564 | $taxes = $tax['taxes']; |
||
| 565 | $value = $tax['value']; |
||
| 566 | $rate = $tax['rate']; |
||
| 567 | } |
||
| 568 | |||
| 569 | foreach ($taxes as $key => $tax) { |
||
| 570 | if ($taxes[0]) { |
||
| 571 | $tax_attribute[$key] = ['name' => $tax->name, 'name1' => $name1, |
||
| 572 | 'name2' => $name2, 'name3' => $name3, 'name4' => $name4, |
||
| 573 | 'rate' => $value, 'rate1'=>$c_gst, 'rate2'=>$s_gst, |
||
| 574 | 'rate3' => $i_gst, 'rate4'=>$ut_gst, 'state'=>$state_code, |
||
| 575 | 'origin_state' => $origin_state, ]; |
||
| 576 | |||
| 577 | $rate = $tax->rate; |
||
| 578 | |||
| 579 | $tax_value = $value; |
||
| 580 | } else { |
||
| 581 | $tax_attribute[0] = ['name' => 'null', 'rate' => 0, 'tax_enable' =>0]; |
||
| 582 | $tax_value = '0%'; |
||
| 583 | } |
||
| 584 | } |
||
| 585 | |||
| 586 | return ['taxs'=>$tax_attribute, 'value'=>$tax_value]; |
||
| 587 | } |
||
| 588 | |||
| 589 | public function setDomain($productid, $domain) |
||
| 590 | { |
||
| 591 | try { |
||
| 592 | if (\Session::has('domain'.$productid)) { |
||
| 593 | \Session::forget('domain'.$productid); |
||
| 594 | } |
||
| 595 | \Session::put('domain'.$productid, $domain); |
||
| 596 | } catch (\Exception $ex) { |
||
| 597 | Bugsnag::notifyException($ex); |
||
| 598 | |||
| 599 | throw new \Exception($ex->getMessage()); |
||
| 600 | } |
||
| 601 | } |
||
| 602 | |||
| 603 | public function sendMail($userid, $invoiceid) |
||
| 615 | } |
||
| 616 | } |
||
| 617 | } |
||
| 618 |