| Total Complexity | 44 |
| Total Lines | 177 |
| 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 |
||
| 17 | class BaseInvoiceController extends Controller |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | *Tax When state is not empty. |
||
| 22 | */ |
||
| 23 | public function getTaxWhenState($user_state, $productid, $origin_state) |
||
| 24 | { |
||
| 25 | $taxes = []; |
||
| 26 | $value = []; |
||
| 27 | $cartController = new CartController(); |
||
| 28 | $c_gst = $user_state->c_gst; |
||
| 29 | $s_gst = $user_state->s_gst; |
||
| 30 | $i_gst = $user_state->i_gst; |
||
| 31 | $ut_gst = $user_state->ut_gst; |
||
| 32 | $state_code = $user_state->state_code; |
||
| 33 | if ($state_code == $origin_state) {//If user and origin state are same |
||
| 34 | $taxClassId = TaxClass::where('name', 'Intra State GST') |
||
| 35 | ->pluck('id')->toArray(); //Get the class Id of state |
||
| 36 | if ($taxClassId) { |
||
| 37 | $taxes = $cartController->getTaxByPriority($taxClassId); |
||
| 38 | $value = $cartController->getValueForSameState($productid, $c_gst, $s_gst, $taxClassId, $taxes); |
||
| 39 | } else { |
||
| 40 | $taxes = [0]; |
||
| 41 | } |
||
| 42 | } elseif ($state_code != $origin_state && $ut_gst == 'NULL') {//If user is from other state |
||
| 43 | |||
| 44 | $taxClassId = TaxClass::where('name', 'Inter State GST') |
||
| 45 | ->pluck('id')->toArray(); //Get the class Id of state |
||
| 46 | if ($taxClassId) { |
||
| 47 | $taxes = $cartController->getTaxByPriority($taxClassId); |
||
| 48 | $value = $cartController->getValueForOtherState($productid, $i_gst, $taxClassId, $taxes); |
||
| 49 | } else { |
||
| 50 | $taxes = [0]; |
||
| 51 | } |
||
| 52 | } elseif ($state_code != $origin_state && $ut_gst != 'NULL') {//if user from Union Territory |
||
| 53 | $taxClassId = TaxClass::where('name', 'Union Territory GST') |
||
| 54 | ->pluck('id')->toArray(); //Get the class Id of state |
||
| 55 | if ($taxClassId) { |
||
| 56 | $taxes = $cartController->getTaxByPriority($taxClassId); |
||
| 57 | $value = $cartController->getValueForUnionTerritory($productid, $c_gst, $ut_gst, $taxClassId, $taxes); |
||
| 58 | } else { |
||
| 59 | $taxes = [0]; |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | return ['taxes'=>$taxes, 'value'=>$value]; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | *Tax When from other Country. |
||
| 68 | */ |
||
| 69 | public function getTaxWhenOtherCountry($geoip_state, $geoip_country, $productid) |
||
| 70 | { |
||
| 71 | $cartController = new CartController(); |
||
| 72 | $taxClassId = Tax::where('state', $geoip_state) |
||
| 73 | ->orWhere('country', $geoip_country) |
||
| 74 | ->pluck('tax_classes_id')->first(); |
||
| 75 | $value = ''; |
||
| 76 | $rate = ''; |
||
| 77 | if ($taxClassId) { //if state equals the user State |
||
| 78 | |||
| 79 | $taxes = $cartController->getTaxByPriority($taxClassId); |
||
| 80 | |||
| 81 | // $taxes = $this->cartController::getTaxByPriority($taxClassId); |
||
| 82 | $value = $cartController->getValueForOthers($productid, $taxClassId, $taxes); |
||
| 83 | $rate = $value; |
||
| 84 | } else {//if Tax is selected for Any State Any Country |
||
| 85 | $taxClassId = Tax::where('country', '')->where('state', 'Any State')->pluck('tax_classes_id')->first(); |
||
| 86 | if ($taxClassId) { |
||
| 87 | $taxes = $cartController->getTaxByPriority($taxClassId); |
||
| 88 | $value = $cartController->getValueForOthers($productid, $taxClassId, $taxes); |
||
| 89 | $rate = $value; |
||
| 90 | } else { |
||
| 91 | $taxes = [0]; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | return ['taxes'=>$taxes, 'value'=>$value, 'rate'=>$rate]; |
||
| 96 | } |
||
| 97 | |||
| 98 | public function whenDateNotSet($start, $end) |
||
| 99 | { |
||
| 100 | //both not set, always true |
||
| 101 | if (($start == null || $start == '0000-00-00 00:00:00') && |
||
| 102 | ($end == null || $end == '0000-00-00 00:00:00')) { |
||
| 103 | return 'success'; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | public function whenStartDateSet($start, $end, $now) |
||
| 108 | { |
||
| 109 | //only starting date set, check the date is less or equel to today |
||
| 110 | if (($start != null || $start != '0000-00-00 00:00:00') |
||
| 111 | && ($end == null || $end == '0000-00-00 00:00:00')) { |
||
| 112 | if ($start <= $now) { |
||
| 113 | return 'success'; |
||
| 114 | } |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | public function whenEndDateSet($start, $end, $now) |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | public function whenBothSet($start, $end, $now) |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | public function postPayment($invoiceid, Request $request) |
||
| 139 | { |
||
| 140 | $this->validate($request, [ |
||
| 141 | 'payment_method' => 'required', |
||
| 142 | 'amount' => 'required|numeric', |
||
| 143 | 'payment_date' => 'required|date_format:Y-m-d', |
||
| 144 | ]); |
||
| 145 | |||
| 146 | try { |
||
| 147 | $payment_method = $request->input('payment_method'); |
||
| 148 | $payment_status = 'success'; |
||
| 149 | $payment_date = $request->input('payment_date'); |
||
| 150 | $amount = $request->input('amount'); |
||
| 151 | $payment = $this->updateInvoicePayment($invoiceid, $payment_method, |
||
| 152 | $payment_status, $payment_date, $amount); |
||
| 153 | |||
| 154 | return redirect()->back()->with('success', 'Payment Accepted Successfully'); |
||
| 155 | } catch (\Exception $ex) { |
||
| 156 | Bugsnag::notifyException($ex); |
||
| 157 | |||
| 158 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | |||
| 163 | |||
| 164 | public function domain($id) |
||
| 165 | { |
||
| 166 | try { |
||
| 167 | if (\Session::has('domain'.$id)) { |
||
| 168 | $domain = \Session::get('domain'.$id); |
||
| 169 | } else { |
||
| 170 | $domain = ''; |
||
| 171 | } |
||
| 172 | |||
| 173 | return $domain; |
||
| 174 | } catch (\Exception $ex) { |
||
| 175 | Bugsnag::notifyException($ex); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | public function checkExpiry($code = '') |
||
| 194 | } |
||
| 195 | } |
||
| 196 | } |
||
| 197 |