| Total Complexity | 67 | 
| Total Lines | 678 | 
| 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  | 
            ||
| 27 | class InvoiceController extends TaxRatesAndCodeExpiryController  | 
            ||
| 28 | { | 
            ||
| 29 | use CoupCodeAndInvoiceSearch;  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 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()  | 
            ||
| 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', | 
            ||
| 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>   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>  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')); | 
            ||
| 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'); | 
            ||
| 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()); | 
            ||
| 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)  | 
            ||
| 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, ]);  | 
            ||
| 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)  | 
            ||
| 651 | }  | 
            ||
| 652 | }  | 
            ||
| 653 | |||
| 654 | public function deletePayment(Request $request)  | 
            ||
| 705 | </div>';  | 
            ||
| 706 | }  | 
            ||
| 707 | }  | 
            ||
| 708 | }  | 
            ||
| 709 |