| Total Complexity | 60 |
| Total Lines | 270 |
| 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 |
||
| 18 | class BaseInvoiceController extends Controller |
||
| 19 | { |
||
| 20 | public function invoiceGenerateByForm(Request $request, $user_id = '') |
||
| 21 | { |
||
| 22 | $qty = 1; |
||
| 23 | |||
| 24 | try { |
||
| 25 | if ($user_id == '') { |
||
| 26 | $user_id = \Request::input('user'); |
||
| 27 | } |
||
| 28 | $productid = $request->input('product'); |
||
| 29 | $code = $request->input('code'); |
||
| 30 | $total = $request->input('price'); |
||
| 31 | $plan = $request->input('plan'); |
||
| 32 | $description = $request->input('description'); |
||
| 33 | if ($request->has('domain')) { |
||
| 34 | $domain = $request->input('domain'); |
||
| 35 | $this->setDomain($productid, $domain); |
||
| 36 | } |
||
| 37 | $controller = new \App\Http\Controllers\Front\CartController(); |
||
| 38 | $currency = $controller->currency($user_id); |
||
| 39 | $number = rand(11111111, 99999999); |
||
| 40 | $date = \Carbon\Carbon::now(); |
||
| 41 | $product = Product::find($productid); |
||
| 42 | $cost = $controller->cost($productid, $user_id, $plan); |
||
| 43 | if ($cost != $total) { |
||
| 44 | $grand_total = $total; |
||
| 45 | } |
||
| 46 | $grand_total = $this->getGrandTotal($code, $total, $cost, $productid, $currency); |
||
| 47 | $grand_total = $qty * $grand_total; |
||
| 48 | |||
| 49 | $tax = $this->checkTax($product->id, $user_id); |
||
| 50 | $tax_name = ''; |
||
| 51 | $tax_rate = ''; |
||
| 52 | if (!empty($tax)) { |
||
| 53 | $tax_name = $tax[0]; |
||
| 54 | $tax_rate = $tax[1]; |
||
| 55 | } |
||
| 56 | |||
| 57 | $grand_total = $this->calculateTotal($tax_rate, $grand_total); |
||
| 58 | $grand_total = \App\Http\Controllers\Front\CartController::rounding($grand_total); |
||
| 59 | |||
| 60 | $invoice = Invoice::create(['user_id' => $user_id, 'number' => $number, 'date' => $date, 'grand_total' => $grand_total, 'currency' => $currency, 'status' => 'pending', 'description' => $description]); |
||
| 61 | |||
| 62 | $items = $this->createInvoiceItemsByAdmin($invoice->id, $productid, $code, $total, $currency, $qty, $plan, $user_id, $tax_name, $tax_rate); |
||
| 63 | $result = $this->getMessage($items, $user_id); |
||
| 64 | } catch (\Exception $ex) { |
||
| 65 | app('log')->useDailyFiles(storage_path().'/laravel.log'); |
||
| 66 | app('log')->info($ex->getMessage()); |
||
| 67 | Bugsnag::notifyException($ex); |
||
| 68 | $result = ['fails' => $ex->getMessage()]; |
||
| 69 | } |
||
| 70 | |||
| 71 | return response()->json(compact('result')); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | *Tax When state is not empty. |
||
| 76 | */ |
||
| 77 | public function getTaxWhenState($user_state, $productid, $origin_state) |
||
| 78 | { |
||
| 79 | $cartController = new CartController(); |
||
| 80 | $c_gst = $user_state->c_gst; |
||
| 81 | $s_gst = $user_state->s_gst; |
||
| 82 | $i_gst = $user_state->i_gst; |
||
| 83 | $ut_gst = $user_state->ut_gst; |
||
| 84 | $state_code = $user_state->state_code; |
||
| 85 | if ($state_code == $origin_state) {//If user and origin state are same |
||
| 86 | $taxClassId = TaxClass::where('name', 'Intra State GST')->pluck('id')->toArray(); //Get the class Id of state |
||
| 87 | if ($taxClassId) { |
||
| 88 | $taxes = $cartController->getTaxByPriority($taxClassId); |
||
| 89 | $value = $cartController->getValueForSameState($productid, $c_gst, $s_gst, $taxClassId, $taxes); |
||
| 90 | } else { |
||
| 91 | $taxes = [0]; |
||
| 92 | } |
||
| 93 | } elseif ($state_code != $origin_state && $ut_gst == 'NULL') {//If user is from other state |
||
| 94 | |||
| 95 | $taxClassId = TaxClass::where('name', 'Inter State GST')->pluck('id')->toArray(); //Get the class Id of state |
||
| 96 | if ($taxClassId) { |
||
| 97 | $taxes = $cartController->getTaxByPriority($taxClassId); |
||
| 98 | $value = $cartController->getValueForOtherState($productid, $i_gst, $taxClassId, $taxes); |
||
| 99 | } else { |
||
| 100 | $taxes = [0]; |
||
| 101 | } |
||
| 102 | } elseif ($state_code != $origin_state && $ut_gst != 'NULL') {//if user from Union Territory |
||
| 103 | $taxClassId = TaxClass::where('name', 'Union Territory GST')->pluck('id')->toArray(); //Get the class Id of state |
||
| 104 | if ($taxClassId) { |
||
| 105 | $taxes = $cartController->getTaxByPriority($taxClassId); |
||
| 106 | $value = $cartController->getValueForUnionTerritory($productid, $c_gst, $ut_gst, $taxClassId, $taxes); |
||
| 107 | } else { |
||
| 108 | $taxes = [0]; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | return ['taxes'=>$taxes, 'value'=>$value]; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | *Tax When from other Country. |
||
| 117 | */ |
||
| 118 | public function getTaxWhenOtherCountry($geoip_state, $geoip_country, $productid) |
||
| 119 | { |
||
| 120 | $cartController = new CartController(); |
||
| 121 | $taxClassId = Tax::where('state', $geoip_state)->orWhere('country', $geoip_country)->pluck('tax_classes_id')->first(); |
||
| 122 | $value = ''; |
||
| 123 | $rate = ''; |
||
| 124 | if ($taxClassId) { //if state equals the user State |
||
| 125 | |||
| 126 | $taxes = $cartController->getTaxByPriority($taxClassId); |
||
| 127 | |||
| 128 | // $taxes = $this->cartController::getTaxByPriority($taxClassId); |
||
| 129 | $value = $cartController->getValueForOthers($productid, $taxClassId, $taxes); |
||
| 130 | $rate = $value; |
||
| 131 | } else {//if Tax is selected for Any State Any Country |
||
| 132 | $taxClassId = Tax::where('country', '')->where('state', 'Any State')->pluck('tax_classes_id')->first(); |
||
| 133 | if ($taxClassId) { |
||
| 134 | $taxes = $cartController->getTaxByPriority($taxClassId); |
||
| 135 | $value = $cartController->getValueForOthers($productid, $taxClassId, $taxes); |
||
| 136 | $rate = $value; |
||
| 137 | } else { |
||
| 138 | $taxes = [0]; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | return ['taxes'=>$taxes, 'value'=>$value, 'rate'=>$rate]; |
||
| 143 | } |
||
| 144 | |||
| 145 | public function whenDateNotSet($start, $end) |
||
| 146 | { |
||
| 147 | //both not set, always true |
||
| 148 | if (($start == null || $start == '0000-00-00 00:00:00') && |
||
| 149 | ($end == null || $end == '0000-00-00 00:00:00')) { |
||
| 150 | return 'success'; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | public function whenStartDateSet($start, $end, $now) |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | public function whenEndDateSet($start, $end, $now) |
||
| 171 | } |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | public function whenBothSet($start, $end, $now) |
||
| 176 | { |
||
| 177 | //both set |
||
| 178 | if (($end != null || $start != '0000-00-00 00:00:00') && ($start != null || $start != '0000-00-00 00:00:00')) { |
||
| 179 | if ($end >= $now && $start <= $now) { |
||
| 180 | return 'success'; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | public function postPayment($invoiceid, Request $request) |
||
| 186 | { |
||
| 187 | $this->validate($request, [ |
||
| 188 | 'payment_method' => 'required', |
||
| 189 | 'amount' => 'required|numeric', |
||
| 190 | 'payment_date' => 'required|date_format:Y-m-d', |
||
| 191 | ]); |
||
| 192 | |||
| 193 | try { |
||
| 194 | $payment_method = $request->input('payment_method'); |
||
| 195 | $payment_status = 'success'; |
||
| 196 | $payment_date = $request->input('payment_date'); |
||
| 197 | $amount = $request->input('amount'); |
||
| 198 | $payment = $this->updateInvoicePayment($invoiceid, $payment_method, $payment_status, $payment_date, $amount); |
||
| 199 | |||
| 200 | return redirect()->back()->with('success', 'Payment Accepted Successfully'); |
||
| 201 | } catch (\Exception $ex) { |
||
| 202 | Bugsnag::notifyException($ex); |
||
| 203 | |||
| 204 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | public function setDomain($productid, $domain) |
||
| 209 | { |
||
| 210 | try { |
||
| 211 | if (\Session::has('domain'.$productid)) { |
||
| 212 | \Session::forget('domain'.$productid); |
||
| 213 | } |
||
| 214 | \Session::put('domain'.$productid, $domain); |
||
| 215 | } catch (\Exception $ex) { |
||
| 216 | Bugsnag::notifyException($ex); |
||
| 217 | |||
| 218 | throw new \Exception($ex->getMessage()); |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | public function domain($id) |
||
| 223 | { |
||
| 224 | try { |
||
| 225 | if (\Session::has('domain'.$id)) { |
||
| 226 | $domain = \Session::get('domain'.$id); |
||
| 227 | } else { |
||
| 228 | $domain = ''; |
||
| 229 | } |
||
| 230 | |||
| 231 | return $domain; |
||
| 232 | } catch (\Exception $ex) { |
||
| 233 | Bugsnag::notifyException($ex); |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | public function checkExpiry($code = '') |
||
| 238 | { |
||
| 239 | try { |
||
| 240 | if ($code != '') { |
||
| 241 | $promotion =Promotin::where('code', $code)->first(); |
||
| 242 | $start = $promotion->start; |
||
| 243 | $end = $promotion->expiry; |
||
| 244 | $now = \Carbon\Carbon::now(); |
||
| 245 | $getExpiryStatus = $this->getExpiryStatus($start, $end, $now); |
||
| 246 | |||
| 247 | return $getExpiryStatus; |
||
| 248 | } else { |
||
| 249 | } |
||
| 250 | } catch (\Exception $ex) { |
||
| 251 | throw new \Exception(\Lang::get('message.check-expiry')); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | public function findCostAfterDiscount($promoid, $productid, $currency) |
||
| 256 | { |
||
| 257 | try { |
||
| 258 | $promotion = Promotion::findOrFail($promoid); |
||
| 259 | $product = Product::findOrFail($productid); |
||
| 260 | $promotion_type = $promotion->type; |
||
| 261 | $promotion_value = $promotion->value; |
||
| 262 | $planId = Plan::where('product', $productid)->pluck('id')->first(); |
||
| 263 | // dd($planId); |
||
| 264 | $product_price = PlanPrice::where('plan_id', $planId)->where('currency', $currency)->pluck('add_price')->first(); |
||
| 265 | $updated_price = $this->findCost($promotion_type, $promotion_value, $product_price, $productid); |
||
| 266 | |||
| 267 | return $updated_price; |
||
| 268 | } catch (\Exception $ex) { |
||
| 269 | Bugsnag::notifyException($ex); |
||
| 270 | |||
| 271 | throw new \Exception(\Lang::get('message.find-discount-error')); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | public function findCost($type, $value, $price, $productid) |
||
| 288 | } |
||
| 289 | } |
||
| 290 | } |
||
| 291 |