| Total Complexity | 40 |
| Total Lines | 292 |
| 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) |
||
| 49 | { |
||
| 50 | $grand_total = $total; |
||
| 51 | if ($code) { |
||
| 52 | $grand_total = $this->checkCode($code, $productid, $currency); |
||
| 53 | } else { |
||
| 54 | if ($total != 0) { |
||
| 55 | $grand_total = $cost; |
||
| 56 | } |
||
| 57 | } |
||
| 58 | return $grand_total; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get Message on Invoice Generation. |
||
| 63 | **/ |
||
| 64 | public function getMessage($items, $user_id) |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * get Subtotal. |
||
| 78 | */ |
||
| 79 | public function getSubtotal($user_currency, $cart) |
||
| 88 | } |
||
| 89 | |||
| 90 | public function calculateTotal($rate, $total) |
||
| 91 | { |
||
| 92 | try { |
||
| 93 | $total = intval($total); |
||
| 94 | $rates = explode(',', $rate); |
||
| 95 | $rule = new TaxOption(); |
||
| 96 | $rule = $rule->findOrFail(1); |
||
| 97 | if ($rule->inclusive == 0) { |
||
| 98 | foreach ($rates as $rate1) { |
||
| 99 | if ($rate1 != '') { |
||
| 100 | $rateTotal = str_replace('%', '', $rate1); |
||
| 101 | $total += $total * ($rateTotal / 100); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | return intval(round($total)); |
||
| 107 | } catch (\Exception $ex) { |
||
| 108 | app('log')->warning($ex->getMessage()); |
||
| 109 | Bugsnag::notifyException($ex); |
||
| 110 | |||
| 111 | throw new \Exception($ex->getMessage()); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | public function checkExecution($invoiceid) |
||
| 116 | { |
||
| 117 | try { |
||
| 118 | $response = false; |
||
| 119 | $invoice = Invoice::find($invoiceid); |
||
| 120 | |||
| 121 | $order = Order::where('invoice_id', $invoiceid); |
||
| 122 | $order_invoice_relation = $invoice->orderRelation()->first(); |
||
| 123 | |||
| 124 | if ($order_invoice_relation) { |
||
| 125 | $response = true; |
||
| 126 | } elseif ($order->get()->count() > 0) { |
||
| 127 | $response = true; |
||
| 128 | } |
||
| 129 | |||
| 130 | return $response; |
||
| 131 | } catch (\Exception $e) { |
||
| 132 | Bugsnag::notifyException($e); |
||
| 133 | |||
| 134 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | public function invoiceContent($invoiceid) |
||
| 139 | { |
||
| 140 | $invoice = $this->invoice->find($invoiceid); |
||
| 141 | $items = $invoice->invoiceItem()->get(); |
||
| 142 | $content = ''; |
||
| 143 | if ($items->count() > 0) { |
||
| 144 | foreach ($items as $item) { |
||
| 145 | $content .= '<tr>'. |
||
| 146 | '<td style="border-bottom: 1px solid#ccc; color: #333; |
||
| 147 | font-family: Arial,sans-serif; font-size: 14px; line-height: 20px; |
||
| 148 | padding: 15px 8px;" valign="top">'.$invoice->number.'</td>'. |
||
| 149 | '<td style="border-bottom: 1px solid#ccc; color: #333; |
||
| 150 | font-family: Arial,sans-serif; font-size: 14px; line-height: 20px; |
||
| 151 | padding: 15px 8px;" valign="top">'.$item->product_name.'</td>'. |
||
| 152 | '<td style="border-bottom: 1px solid#ccc; color: #333; |
||
| 153 | font-family: Arial,sans-serif; font-size: 14px; line-height: 20px; |
||
| 154 | padding: 15px 8px;" valign="top">'.$this->currency($invoiceid).' ' |
||
| 155 | .$item->subtotal.'</td>'. |
||
| 156 | '</tr>'; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | return $content; |
||
| 161 | } |
||
| 162 | |||
| 163 | public function currency($invoiceid) |
||
| 164 | { |
||
| 165 | $invoice = Invoice::find($invoiceid); |
||
| 166 | $currency_code = $invoice->currency; |
||
| 167 | $cur = ' '; |
||
| 168 | if ($invoice->grand_total == 0) { |
||
| 169 | return $cur; |
||
| 170 | } |
||
| 171 | $currency = Currency::where('code', $currency_code)->first(); |
||
| 172 | if ($currency) { |
||
| 173 | $cur = $currency->symbol; |
||
| 174 | if (!$cur) { |
||
| 175 | $cur = $currency->code; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | return $cur; |
||
| 180 | } |
||
| 181 | |||
| 182 | public function sendInvoiceMail($userid, $number, $total, $invoiceid) |
||
| 183 | { |
||
| 184 | |||
| 185 | //user |
||
| 186 | $users = new User(); |
||
| 187 | $user = $users->find($userid); |
||
| 188 | //check in the settings |
||
| 189 | $settings = new \App\Model\Common\Setting(); |
||
| 190 | $setting = $settings->where('id', 1)->first(); |
||
| 191 | $invoiceurl = $this->invoiceUrl($invoiceid); |
||
| 192 | //template |
||
| 193 | $templates = new \App\Model\Common\Template(); |
||
| 194 | $temp_id = $setting->invoice; |
||
| 195 | $template = $templates->where('id', $temp_id)->first(); |
||
| 196 | $from = $setting->email; |
||
| 197 | $to = $user->email; |
||
| 198 | $subject = $template->name; |
||
| 199 | $data = $template->data; |
||
| 200 | $replace = [ |
||
| 201 | 'name' => $user->first_name.' '.$user->last_name, |
||
| 202 | 'number' => $number, |
||
| 203 | 'address' => $user->address, |
||
| 204 | 'invoiceurl' => $invoiceurl, |
||
| 205 | 'content' => $this->invoiceContent($invoiceid), |
||
| 206 | 'currency' => $this->currency($invoiceid), |
||
| 207 | ]; |
||
| 208 | $type = ''; |
||
| 209 | if ($template) { |
||
| 210 | $type_id = $template->type; |
||
| 211 | $temp_type = new \App\Model\Common\TemplateType(); |
||
| 212 | $type = $temp_type->where('id', $type_id)->first()->name; |
||
| 213 | } |
||
| 214 | //dd($type); |
||
| 215 | $templateController = new \App\Http\Controllers\Common\TemplateController(); |
||
| 216 | $mail = $templateController->mailing($from, $to, $data, $subject, $replace, $type); |
||
| 217 | |||
| 218 | return $mail; |
||
| 219 | } |
||
| 220 | |||
| 221 | public function invoiceUrl($invoiceid) |
||
| 222 | { |
||
| 223 | $url = url('my-invoice/'.$invoiceid); |
||
| 224 | |||
| 225 | return $url; |
||
| 226 | } |
||
| 227 | |||
| 228 | public function paymentDeleleById($id) |
||
| 229 | { |
||
| 230 | try { |
||
| 231 | $invoice_no = ''; |
||
| 232 | $payment = Payment::find($id); |
||
| 233 | if ($payment) { |
||
| 234 | $invoice_id = $payment->invoice_id; |
||
| 235 | $invoice = Invoice::find($invoice_id); |
||
| 236 | if ($invoice) { |
||
| 237 | $invoice_no = $invoice->number; |
||
| 238 | } |
||
| 239 | $payment->delete(); |
||
| 240 | } else { |
||
| 241 | return redirect()->back()->with('fails', 'Can not delete'); |
||
| 242 | } |
||
| 243 | |||
| 244 | return redirect()->back()->with('success', "Payment for invoice no: $invoice_no has Deleted Successfully"); |
||
| 245 | } catch (\Exception $e) { |
||
| 246 | Bugsnag::notifyException($e); |
||
| 247 | |||
| 248 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | public function paymentEditById($id) |
||
| 253 | { |
||
| 254 | try { |
||
| 255 | $cltCont = new \App\Http\Controllers\User\ClientController(); |
||
| 256 | $payment = Payment::find($id); |
||
| 257 | $clientid = $payment->user_id; |
||
| 258 | $invoice = new Invoice(); |
||
| 259 | $order = new Order(); |
||
| 260 | $invoices = $invoice->where('user_id', $clientid)->where('status', '=', 'pending') |
||
| 261 | ->orderBy('created_at', 'desc')->get(); |
||
| 262 | $cltCont = new \App\Http\Controllers\User\ClientController(); |
||
| 263 | $invoiceSum = $cltCont->getTotalInvoice($invoices); |
||
| 264 | $amountReceived = $cltCont->getExtraAmt($clientid); |
||
| 265 | $pendingAmount = $invoiceSum - $amountReceived; |
||
| 266 | $client = $this->user->where('id', $clientid)->first(); |
||
| 267 | $currency = $client->currency; |
||
| 268 | $symbol = Currency::where('code', $currency)->pluck('symbol')->first(); |
||
| 269 | $orders = $order->where('client', $clientid)->get(); |
||
| 270 | |||
| 271 | return view('themes.default1.invoice.editPayment', |
||
| 272 | compact('amountReceived','clientid', 'client', 'invoices', 'orders', |
||
| 273 | 'invoiceSum', 'amountReceived', 'pendingAmount', 'currency', 'symbol')); |
||
| 274 | } catch (\Exception $e) { |
||
| 275 | Bugsnag::notifyException($e); |
||
| 276 | |||
| 277 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | |||
| 282 | public function getPromotionDetails($code) |
||
| 308 | } |
||
| 309 | } |
||
| 310 |