| Total Complexity | 55 |
| Total Lines | 213 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like BaseInvoiceController 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 BaseInvoiceController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class BaseInvoiceController extends Controller |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | *Tax When state is not empty. |
||
| 17 | */ |
||
| 18 | public function getTaxWhenState($user_state, $productid, $origin_state) |
||
| 19 | { |
||
| 20 | $cartController = new CartController(); |
||
| 21 | $c_gst = $user_state->c_gst; |
||
| 22 | $s_gst = $user_state->s_gst; |
||
| 23 | $i_gst = $user_state->i_gst; |
||
| 24 | $ut_gst = $user_state->ut_gst; |
||
| 25 | $state_code = $user_state->state_code; |
||
| 26 | if ($state_code == $origin_state) {//If user and origin state are same |
||
| 27 | $taxClassId = TaxClass::where('name', 'Intra State GST')->pluck('id')->toArray(); //Get the class Id of state |
||
| 28 | if ($taxClassId) { |
||
| 29 | $taxes = $cartController->getTaxByPriority($taxClassId); |
||
| 30 | $value = $cartController->getValueForSameState($productid, $c_gst, $s_gst, $taxClassId, $taxes); |
||
| 31 | } else { |
||
| 32 | $taxes = [0]; |
||
| 33 | } |
||
| 34 | } elseif ($state_code != $origin_state && $ut_gst == 'NULL') {//If user is from other state |
||
| 35 | |||
| 36 | $taxClassId = TaxClass::where('name', 'Inter State GST')->pluck('id')->toArray(); //Get the class Id of state |
||
| 37 | if ($taxClassId) { |
||
| 38 | $taxes = $cartController->getTaxByPriority($taxClassId); |
||
| 39 | $value = $cartController->getValueForOtherState($productid, $i_gst, $taxClassId, $taxes); |
||
| 40 | } else { |
||
| 41 | $taxes = [0]; |
||
| 42 | } |
||
| 43 | } elseif ($state_code != $origin_state && $ut_gst != 'NULL') {//if user from Union Territory |
||
| 44 | $taxClassId = TaxClass::where('name', 'Union Territory GST')->pluck('id')->toArray(); //Get the class Id of state |
||
| 45 | if ($taxClassId) { |
||
| 46 | $taxes = $cartController->getTaxByPriority($taxClassId); |
||
| 47 | $value = $cartController->getValueForUnionTerritory($productid, $c_gst, $ut_gst, $taxClassId, $taxes); |
||
| 48 | } else { |
||
| 49 | $taxes = [0]; |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | return ['taxes'=>$taxes, 'value'=>$value]; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | *Tax When from other Country. |
||
| 58 | */ |
||
| 59 | public function getTaxWhenOtherCountry($geoip_state, $geoip_country, $productid) |
||
| 60 | { |
||
| 61 | $cartController = new CartController(); |
||
| 62 | $taxClassId = Tax::where('state', $geoip_state)->orWhere('country', $geoip_country)->pluck('tax_classes_id')->first(); |
||
| 63 | $value = ''; |
||
| 64 | $rate = ''; |
||
| 65 | if ($taxClassId) { //if state equals the user State |
||
| 66 | |||
| 67 | $taxes = $cartController->getTaxByPriority($taxClassId); |
||
| 68 | |||
| 69 | // $taxes = $this->cartController::getTaxByPriority($taxClassId); |
||
| 70 | $value = $cartController->getValueForOthers($productid, $taxClassId, $taxes); |
||
| 71 | $rate = $value; |
||
| 72 | } else {//if Tax is selected for Any State Any Country |
||
| 73 | $taxClassId = Tax::where('country', '')->where('state', 'Any State')->pluck('tax_classes_id')->first(); |
||
| 74 | if ($taxClassId) { |
||
| 75 | $taxes = $cartController->getTaxByPriority($taxClassId); |
||
| 76 | $value = $cartController->getValueForOthers($productid, $taxClassId, $taxes); |
||
| 77 | $rate = $value; |
||
| 78 | } else { |
||
| 79 | $taxes = [0]; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | return ['taxes'=>$taxes, 'value'=>$value, 'rate'=>$rate]; |
||
| 84 | } |
||
| 85 | |||
| 86 | public function getExpiryStatus($start, $end, $now) |
||
| 87 | { |
||
| 88 | $whenDateNotSet = $this->whenDateNotSet($start, $end); |
||
| 89 | if ($whenDateNotSet) { |
||
| 90 | return $whenDateNotSet; |
||
| 91 | } |
||
| 92 | $whenStartDateSet = $this->whenStartDateSet($start, $end, $now); |
||
| 93 | if ($whenStartDateSet) { |
||
| 94 | return $whenStartDateSet; |
||
| 95 | } |
||
| 96 | $whenEndDateSet = $this->whenEndDateSet($start, $end, $now); |
||
| 97 | if ($whenEndDateSet) { |
||
| 98 | return $whenEndDateSet; |
||
| 99 | } |
||
| 100 | $whenBothAreSet = $this->whenBothSet($start, $end, $now); |
||
| 101 | if ($whenBothAreSet) { |
||
| 102 | return $whenBothAreSet; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | public function whenDateNotSet($start, $end) |
||
| 107 | { |
||
| 108 | //both not set, always true |
||
| 109 | if (($start == null || $start == '0000-00-00 00:00:00') && |
||
| 110 | ($end == null || $end == '0000-00-00 00:00:00')) { |
||
| 111 | return 'success'; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | public function whenStartDateSet($start, $end, $now) |
||
| 116 | { |
||
| 117 | //only starting date set, check the date is less or equel to today |
||
| 118 | if (($start != null || $start != '0000-00-00 00:00:00') |
||
| 119 | && ($end == null || $end == '0000-00-00 00:00:00')) { |
||
| 120 | if ($start <= $now) { |
||
| 121 | return 'success'; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | public function whenEndDateSet($start, $end, $now) |
||
| 132 | } |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | public function whenBothSet($start, $end, $now) |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | public function postPayment($invoiceid, Request $request) |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | |||
| 170 | public function pdf(Request $request) |
||
| 171 | { |
||
| 172 | try { |
||
| 173 | $id = $request->input('invoiceid'); |
||
| 174 | if (!$id) { |
||
| 175 | return redirect()->back()->with('fails', \Lang::get('message.no-invoice-id')); |
||
| 176 | } |
||
| 177 | $invoice = $this->invoice->where('id', $id)->first(); |
||
| 178 | if (!$invoice) { |
||
| 179 | return redirect()->back()->with('fails', \Lang::get('message.invalid-invoice-id')); |
||
| 180 | } |
||
| 181 | $invoiceItems = $this->invoiceItem->where('invoice_id', $id)->get(); |
||
| 182 | if ($invoiceItems->count() == 0) { |
||
| 183 | return redirect()->back()->with('fails', \Lang::get('message.invalid-invoice-id')); |
||
| 184 | } |
||
| 185 | $user = $this->user->find($invoice->user_id); |
||
| 186 | if (!$user) { |
||
| 187 | return redirect()->back()->with('fails', 'No User'); |
||
| 188 | } |
||
| 189 | $pdf = \PDF::loadView('themes.default1.invoice.newpdf', compact('invoiceItems', 'invoice', 'user')); |
||
| 190 | // $pdf = \PDF::loadView('themes.default1.invoice.newpdf', compact('invoiceItems', 'invoice', 'user')); |
||
| 191 | |||
| 192 | return $pdf->download($user->first_name.'-invoice.pdf'); |
||
| 193 | } catch (\Exception $ex) { |
||
| 194 | Bugsnag::notifyException($ex); |
||
| 195 | |||
| 196 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | public function setDomain($productid, $domain) |
||
| 201 | { |
||
| 202 | try { |
||
| 203 | if (\Session::has('domain'.$productid)) { |
||
| 204 | \Session::forget('domain'.$productid); |
||
| 205 | } |
||
| 206 | \Session::put('domain'.$productid, $domain); |
||
| 207 | } catch (\Exception $ex) { |
||
| 208 | Bugsnag::notifyException($ex); |
||
| 209 | |||
| 210 | throw new \Exception($ex->getMessage()); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | public function domain($id) |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 232 |