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