| Total Complexity | 43 |
| Total Lines | 311 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TaxRatesAndCodeExpiryController 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 TaxRatesAndCodeExpiryController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class TaxRatesAndCodeExpiryController extends BaseInvoiceController |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Get tax when enabled. |
||
| 20 | */ |
||
| 21 | public function getTaxWhenEnable($productid, $taxs, $userid) |
||
| 22 | { |
||
| 23 | $rate = $this->getRate($productid, $taxs, $userid); |
||
| 24 | $taxs = ([$rate['taxs']['0']['name'], $rate['taxs']['0']['rate']]); |
||
| 25 | |||
| 26 | return $taxs; |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * GeT Total Rate. |
||
| 31 | */ |
||
| 32 | public function getTotalRate($taxClassId, $productid, $taxs) |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Get Grandtotal. |
||
| 47 | **/ |
||
| 48 | public function getGrandTotal($code, $total, $cost, $productid, $currency) |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Get Message on Invoice Generation. |
||
| 65 | **/ |
||
| 66 | public function getMessage($items, $user_id) |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * get Subtotal. |
||
| 80 | */ |
||
| 81 | public function getSubtotal($user_currency, $cart) |
||
| 90 | } |
||
| 91 | |||
| 92 | public function calculateTotal($rate, $total) |
||
| 93 | { |
||
| 94 | try { |
||
| 95 | $total = intval($total); |
||
| 96 | $rates = explode(',', $rate); |
||
| 97 | $rule = new TaxOption(); |
||
| 98 | $rule = $rule->findOrFail(1); |
||
| 99 | if ($rule->inclusive == 0) { |
||
| 100 | foreach ($rates as $rate1) { |
||
| 101 | if ($rate1 != '') { |
||
| 102 | $rateTotal = str_replace('%', '', $rate1); |
||
| 103 | $total += $total * ($rateTotal / 100); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | return intval(round($total)); |
||
| 109 | } catch (\Exception $ex) { |
||
| 110 | app('log')->warning($ex->getMessage()); |
||
| 111 | Bugsnag::notifyException($ex); |
||
| 112 | |||
| 113 | throw new \Exception($ex->getMessage()); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | public function checkExecution($invoiceid) |
||
| 118 | { |
||
| 119 | try { |
||
| 120 | $response = false; |
||
| 121 | $invoice = Invoice::find($invoiceid); |
||
| 122 | |||
| 123 | $order = Order::where('invoice_id', $invoiceid); |
||
| 124 | $order_invoice_relation = $invoice->orderRelation()->first(); |
||
| 125 | |||
| 126 | if ($order_invoice_relation) { |
||
| 127 | $response = true; |
||
| 128 | } elseif ($order->get()->count() > 0) { |
||
| 129 | $response = true; |
||
| 130 | } |
||
| 131 | |||
| 132 | return $response; |
||
| 133 | } catch (\Exception $e) { |
||
| 134 | Bugsnag::notifyException($e); |
||
| 135 | |||
| 136 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | public function invoiceContent($invoiceid) |
||
| 163 | } |
||
| 164 | |||
| 165 | public function currency($invoiceid) |
||
| 166 | { |
||
| 167 | $invoice = Invoice::find($invoiceid); |
||
| 168 | $currency_code = $invoice->currency; |
||
| 169 | $cur = ' '; |
||
| 170 | if ($invoice->grand_total == 0) { |
||
| 171 | return $cur; |
||
| 172 | } |
||
| 173 | $currency = Currency::where('code', $currency_code)->first(); |
||
| 174 | if ($currency) { |
||
| 175 | $cur = $currency->symbol; |
||
| 176 | if (!$cur) { |
||
| 177 | $cur = $currency->code; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | return $cur; |
||
| 182 | } |
||
| 183 | |||
| 184 | public function sendInvoiceMail($userid, $number, $total, $invoiceid) |
||
| 185 | { |
||
| 186 | |||
| 187 | //user |
||
| 188 | $users = new User(); |
||
| 189 | $user = $users->find($userid); |
||
| 190 | //check in the settings |
||
| 191 | $settings = new \App\Model\Common\Setting(); |
||
| 192 | $setting = $settings->where('id', 1)->first(); |
||
| 193 | $invoiceurl = $this->invoiceUrl($invoiceid); |
||
| 194 | //template |
||
| 195 | $templates = new \App\Model\Common\Template(); |
||
| 196 | $temp_id = $setting->invoice; |
||
| 197 | $template = $templates->where('id', $temp_id)->first(); |
||
| 198 | $from = $setting->email; |
||
| 199 | $to = $user->email; |
||
| 200 | $subject = $template->name; |
||
| 201 | $data = $template->data; |
||
| 202 | $replace = [ |
||
| 203 | 'name' => $user->first_name.' '.$user->last_name, |
||
| 204 | 'number' => $number, |
||
| 205 | 'address' => $user->address, |
||
| 206 | 'invoiceurl' => $invoiceurl, |
||
| 207 | 'content' => $this->invoiceContent($invoiceid), |
||
| 208 | 'currency' => $this->currency($invoiceid), |
||
| 209 | ]; |
||
| 210 | $type = ''; |
||
| 211 | if ($template) { |
||
| 212 | $type_id = $template->type; |
||
| 213 | $temp_type = new \App\Model\Common\TemplateType(); |
||
| 214 | $type = $temp_type->where('id', $type_id)->first()->name; |
||
| 215 | } |
||
| 216 | //dd($type); |
||
| 217 | $templateController = new \App\Http\Controllers\Common\TemplateController(); |
||
| 218 | $mail = $templateController->mailing($from, $to, $data, $subject, $replace, $type); |
||
| 219 | |||
| 220 | return $mail; |
||
| 221 | } |
||
| 222 | |||
| 223 | public function invoiceUrl($invoiceid) |
||
| 224 | { |
||
| 225 | $url = url('my-invoice/'.$invoiceid); |
||
| 226 | |||
| 227 | return $url; |
||
| 228 | } |
||
| 229 | |||
| 230 | public function paymentDeleleById($id) |
||
| 231 | { |
||
| 232 | try { |
||
| 233 | $invoice_no = ''; |
||
| 234 | $payment = Payment::find($id); |
||
| 235 | if ($payment) { |
||
| 236 | $invoice_id = $payment->invoice_id; |
||
| 237 | $invoice = Invoice::find($invoice_id); |
||
| 238 | if ($invoice) { |
||
| 239 | $invoice_no = $invoice->number; |
||
| 240 | } |
||
| 241 | $payment->delete(); |
||
| 242 | } else { |
||
| 243 | return redirect()->back()->with('fails', 'Can not delete'); |
||
| 244 | } |
||
| 245 | |||
| 246 | return redirect()->back()->with('success', "Payment for invoice no: $invoice_no has Deleted Successfully"); |
||
| 247 | } catch (\Exception $e) { |
||
| 248 | Bugsnag::notifyException($e); |
||
| 249 | |||
| 250 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | public function paymentEditById($id) |
||
| 255 | { |
||
| 256 | try { |
||
| 257 | $cltCont = new \App\Http\Controllers\User\ClientController(); |
||
| 258 | $payment = Payment::find($id); |
||
| 259 | $clientid = $payment->user_id; |
||
| 260 | $invoice = new Invoice(); |
||
| 261 | $order = new Order(); |
||
| 262 | $invoices = $invoice->where('user_id', $clientid)->where('status', '=', 'pending') |
||
| 263 | ->orderBy('created_at', 'desc')->get(); |
||
| 264 | $cltCont = new \App\Http\Controllers\User\ClientController(); |
||
| 265 | $invoiceSum = $cltCont->getTotalInvoice($invoices); |
||
| 266 | $amountReceived = $cltCont->getExtraAmt($clientid); |
||
| 267 | $pendingAmount = $invoiceSum - $amountReceived; |
||
| 268 | $client = $this->user->where('id', $clientid)->first(); |
||
| 269 | $currency = $client->currency; |
||
| 270 | $symbol = Currency::where('code', $currency)->pluck('symbol')->first(); |
||
| 271 | $orders = $order->where('client', $clientid)->get(); |
||
| 272 | |||
| 273 | return view('themes.default1.invoice.editPayment', |
||
| 274 | compact('amountReceived','clientid', 'client', 'invoices', 'orders', |
||
| 275 | 'invoiceSum', 'amountReceived', 'pendingAmount', 'currency', 'symbol')); |
||
| 276 | } catch (\Exception $e) { |
||
| 277 | Bugsnag::notifyException($e); |
||
| 278 | |||
| 279 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | public function deleleById($id) |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | public function getPromotionDetails($code) |
||
| 327 | } |
||
| 328 | } |
||
| 329 |