| Total Complexity | 75 |
| Total Lines | 479 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CoupCodeAndInvoiceSearch 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 CoupCodeAndInvoiceSearch, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | trait CoupCodeAndInvoiceSearch |
||
| 17 | { |
||
| 18 | public function checkCode($code, $productid, $currency) |
||
| 19 | { |
||
| 20 | try { |
||
| 21 | if ($code != '') { |
||
| 22 | $promo = $this->promotion->where('code', $code)->first(); |
||
| 23 | //check promotion code is valid |
||
| 24 | if (!$promo) { |
||
| 25 | throw new \Exception(\Lang::get('message.no-such-code')); |
||
| 26 | } |
||
| 27 | $relation = $promo->relation()->get(); |
||
| 28 | //check the relation between code and product |
||
| 29 | if (count($relation) == 0) { |
||
| 30 | throw new \Exception(\Lang::get('message.no-product-related-to-this-code')); |
||
| 31 | } |
||
| 32 | //check the usess |
||
| 33 | $cont = new \App\Http\Controllers\Payment\PromotionController(); |
||
| 34 | $uses = $cont->checkNumberOfUses($code); |
||
| 35 | if ($uses != 'success') { |
||
| 36 | throw new \Exception(\Lang::get('message.usage-of-code-completed')); |
||
| 37 | } |
||
| 38 | //check for the expiry date |
||
| 39 | $expiry = $this->checkExpiry($code); |
||
|
|
|||
| 40 | if ($expiry != 'success') { |
||
| 41 | throw new \Exception(\Lang::get('message.usage-of-code-expired')); |
||
| 42 | } |
||
| 43 | $value = $this->findCostAfterDiscount($promo->id, $productid, $currency); |
||
| 44 | |||
| 45 | return $value; |
||
| 46 | } else { |
||
| 47 | $product = $this->product->find($productid); |
||
| 48 | $plans = Plan::where('product', $product)->pluck('id')->first(); |
||
| 49 | $price = PlanPrice::where('currency', $currency)->where('plan_id', $plans)->pluck('add_price')->first(); |
||
| 50 | |||
| 51 | return $price; |
||
| 52 | } |
||
| 53 | } catch (\Exception $ex) { |
||
| 54 | throw new \Exception($ex->getMessage()); |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | public function findCostAfterDiscount($promoid, $productid, $currency) |
||
| 59 | { |
||
| 60 | try { |
||
| 61 | $promotion = Promotion::findOrFail($promoid); |
||
| 62 | $product = Product::findOrFail($productid); |
||
| 63 | $promotion_type = $promotion->type; |
||
| 64 | $promotion_value = $promotion->value; |
||
| 65 | $planId = Plan::where('product', $productid)->pluck('id')->first(); |
||
| 66 | // dd($planId); |
||
| 67 | $product_price = PlanPrice::where('plan_id', $planId) |
||
| 68 | ->where('currency', $currency)->pluck('add_price')->first(); |
||
| 69 | $updated_price = $this->findCost($promotion_type, $promotion_value, $product_price, $productid); |
||
| 70 | |||
| 71 | return $updated_price; |
||
| 72 | } catch (\Exception $ex) { |
||
| 73 | Bugsnag::notifyException($ex); |
||
| 74 | |||
| 75 | throw new \Exception(\Lang::get('message.find-discount-error')); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | public function findCost($type, $value, $price, $productid) |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | public function advanceSearch($name = '', $invoice_no = '', $currency = '', $status = '', $from = '', $till = '') |
||
| 97 | { |
||
| 98 | $join = Invoice::leftJoin('users', 'invoices.user_id', '=', 'users.id'); |
||
| 99 | $this->name($name, $join); |
||
| 100 | $this->invoice_no($invoice_no, $join); |
||
| 101 | $this->status($status, $join); |
||
| 102 | $this->currency($currency, $join); |
||
| 103 | |||
| 104 | $this->invoice_from($from, $till, $join); |
||
| 105 | $this->till_date($till, $from, $join); |
||
| 106 | |||
| 107 | $join = $join->select('id', 'user_id', 'number', 'date', 'grand_total', 'currency', 'status', 'created_at'); |
||
| 108 | |||
| 109 | $join = $join->orderBy('created_at', 'desc') |
||
| 110 | ->select( |
||
| 111 | 'invoices.id', |
||
| 112 | 'first_name', |
||
| 113 | 'invoices.created_at', |
||
| 114 | 'invoices.currency', |
||
| 115 | 'user_id', |
||
| 116 | 'number', |
||
| 117 | 'status' |
||
| 118 | ); |
||
| 119 | |||
| 120 | return $join; |
||
| 121 | } |
||
| 122 | |||
| 123 | public function name($name, $join) |
||
| 124 | { |
||
| 125 | if ($name) { |
||
| 126 | $join = $join->where('first_name', $name); |
||
| 127 | |||
| 128 | return $join; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | public function invoice_no($invoice_no, $join) |
||
| 133 | { |
||
| 134 | if ($invoice_no) { |
||
| 135 | $join = $join->where('number', $invoice_no); |
||
| 136 | |||
| 137 | return $join; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | public function status($status, $join) |
||
| 142 | { |
||
| 143 | if ($status) { |
||
| 144 | $join = $join->where('status', $status); |
||
| 145 | |||
| 146 | return $join; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | public function currency($currency, $join) |
||
| 151 | { |
||
| 152 | if ($currency) { |
||
| 153 | $join = $join->where('invoices.currency', $currency); |
||
| 154 | |||
| 155 | return $join; |
||
| 156 | } |
||
| 157 | $return; |
||
| 158 | } |
||
| 159 | |||
| 160 | public function invoice_from($from, $till, $join) |
||
| 161 | { |
||
| 162 | if ($from) { |
||
| 163 | $fromdate = date_create($from); |
||
| 164 | $from = date_format($fromdate, 'Y-m-d H:m:i'); |
||
| 165 | $tills = date('Y-m-d H:m:i'); |
||
| 166 | $tillDate = $this->getTillDate($from, $till, $tills); |
||
| 167 | $join = $join->whereBetween('invoices.created_at', [$from, $tillDate]); |
||
| 168 | |||
| 169 | return $join; |
||
| 170 | } |
||
| 171 | |||
| 172 | return $join; |
||
| 173 | } |
||
| 174 | |||
| 175 | public function till_date($till, $from, $join) |
||
| 176 | { |
||
| 177 | if ($till) { |
||
| 178 | $tilldate = date_create($till); |
||
| 179 | $till = date_format($tilldate, 'Y-m-d H:m:i'); |
||
| 180 | $froms = Invoice::first()->created_at; |
||
| 181 | $fromDate = $this->getFromDate($from, $froms); |
||
| 182 | $join = $join->whereBetween('invoices.created_at', [$fromDate, $till]); |
||
| 183 | |||
| 184 | return $join; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | public function getExpiryStatus($start, $end, $now) |
||
| 189 | { |
||
| 190 | $whenDateNotSet = $this->whenDateNotSet($start, $end); |
||
| 191 | if ($whenDateNotSet) { |
||
| 192 | return $whenDateNotSet; |
||
| 193 | } |
||
| 194 | $whenStartDateSet = $this->whenStartDateSet($start, $end, $now); |
||
| 195 | if ($whenStartDateSet) { |
||
| 196 | return $whenStartDateSet; |
||
| 197 | } |
||
| 198 | $whenEndDateSet = $this->whenEndDateSet($start, $end, $now); |
||
| 199 | if ($whenEndDateSet) { |
||
| 200 | return $whenEndDateSet; |
||
| 201 | } |
||
| 202 | $whenBothAreSet = $this->whenBothSet($start, $end, $now); |
||
| 203 | if ($whenBothAreSet) { |
||
| 204 | return $whenBothAreSet; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | public function getTillDate($from, $till, $tills) |
||
| 209 | { |
||
| 210 | if ($till) { |
||
| 211 | $todate = date_create($till); |
||
| 212 | $tills = date_format($todate, 'Y-m-d H:m:i'); |
||
| 213 | } |
||
| 214 | |||
| 215 | return $tills; |
||
| 216 | } |
||
| 217 | |||
| 218 | public function getFromDate($from, $froms) |
||
| 219 | { |
||
| 220 | if ($from) { |
||
| 221 | $fromdate = date_create($from); |
||
| 222 | $froms = date_format($fromdate, 'Y-m-d H:m:i'); |
||
| 223 | } |
||
| 224 | |||
| 225 | return $froms; |
||
| 226 | } |
||
| 227 | |||
| 228 | public function postRazorpayPayment($invoiceid, $grand_total) |
||
| 229 | { |
||
| 230 | try { |
||
| 231 | $payment_method = \Session::get('payment_method'); |
||
| 232 | $payment_status = 'success'; |
||
| 233 | $payment_date = \Carbon\Carbon::now()->toDateTimeString(); |
||
| 234 | $amount = $grand_total; |
||
| 235 | $paymentRenewal = $this->updateInvoicePayment($invoiceid, $payment_method, |
||
| 236 | $payment_status, $payment_date, $amount); |
||
| 237 | |||
| 238 | return redirect()->back()->with('success', 'Payment Accepted Successfully'); |
||
| 239 | } catch (\Exception $ex) { |
||
| 240 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | public function updateInvoicePayment($invoiceid, $payment_method, $payment_status, $payment_date, $amount) |
||
| 245 | { |
||
| 246 | try { |
||
| 247 | $invoice = Invoice::find($invoiceid); |
||
| 248 | $invoice_status = 'pending'; |
||
| 249 | |||
| 250 | $payment = $this->payment->create([ |
||
| 251 | 'invoice_id' => $invoiceid, |
||
| 252 | 'user_id' => $invoice->user_id, |
||
| 253 | 'amount' => $amount, |
||
| 254 | 'payment_method' => $payment_method, |
||
| 255 | 'payment_status' => $payment_status, |
||
| 256 | 'created_at' => $payment_date, |
||
| 257 | ]); |
||
| 258 | $all_payments = $this->payment |
||
| 259 | ->where('invoice_id', $invoiceid) |
||
| 260 | ->where('payment_status', 'success') |
||
| 261 | ->pluck('amount')->toArray(); |
||
| 262 | $total_paid = array_sum($all_payments); |
||
| 263 | if ($total_paid >= $invoice->grand_total) { |
||
| 264 | $invoice_status = 'success'; |
||
| 265 | } |
||
| 266 | if ($invoice) { |
||
| 267 | $invoice->status = $invoice_status; |
||
| 268 | $invoice->save(); |
||
| 269 | } |
||
| 270 | |||
| 271 | return $payment; |
||
| 272 | } catch (\Exception $ex) { |
||
| 273 | Bugsnag::notifyException($ex); |
||
| 274 | |||
| 275 | throw new \Exception($ex->getMessage()); |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | public function updateInvoice($invoiceid) |
||
| 304 | } |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Remove the specified resource from storage. |
||
| 309 | * |
||
| 310 | * @param int $id |
||
| 311 | * |
||
| 312 | * @return \Response |
||
| 313 | */ |
||
| 314 | public function destroy(Request $request) |
||
| 360 | </div>'; |
||
| 361 | } |
||
| 362 | } |
||
| 363 | |||
| 364 | /* |
||
| 365 | *Edit payment Total. |
||
| 366 | */ |
||
| 367 | public function paymentTotalChange(Request $request) |
||
| 368 | { |
||
| 369 | try { |
||
| 370 | $invoice = new Invoice(); |
||
| 371 | $total = $request->input('total'); |
||
| 372 | if ($total == '') { |
||
| 373 | $total = 0; |
||
| 374 | } |
||
| 375 | $paymentid = $request->input('id'); |
||
| 376 | $creditAmtUserId = $this->payment->where('id', $paymentid)->value('user_id'); |
||
| 377 | $creditAmt = $this->payment->where('user_id', $creditAmtUserId) |
||
| 378 | ->where('invoice_id', '=', 0)->value('amt_to_credit'); |
||
| 379 | $invoices = $invoice->where('user_id', $creditAmtUserId)->orderBy('created_at', 'desc')->get(); |
||
| 380 | $cltCont = new \App\Http\Controllers\User\ClientController(); |
||
| 381 | $invoiceSum = $cltCont->getTotalInvoice($invoices); |
||
| 382 | if ($total > $invoiceSum) { |
||
| 383 | $diff = $total - $invoiceSum; |
||
| 384 | $creditAmt = $creditAmt + $diff; |
||
| 385 | $total = $invoiceSum; |
||
| 386 | } |
||
| 387 | $payment = $this->payment->where('id', $paymentid)->update(['amount'=>$total]); |
||
| 388 | |||
| 389 | $creditAmtInvoiceId = $this->payment->where('user_id', $creditAmtUserId) |
||
| 390 | ->where('invoice_id', '!=', 0)->first(); |
||
| 391 | $invoiceId = $creditAmtInvoiceId->invoice_id; |
||
| 392 | $invoice = $invoice->where('id', $invoiceId)->first(); |
||
| 393 | $grand_total = $invoice->grand_total; |
||
| 394 | $diffSum = $grand_total - $total; |
||
| 395 | |||
| 396 | $finalAmt = $creditAmt + $diffSum; |
||
| 397 | $updatedAmt = $this->payment->where('user_id', $creditAmtUserId) |
||
| 398 | ->where('invoice_id', '=', 0)->update(['amt_to_credit'=>$creditAmt]); |
||
| 399 | } catch (\Exception $ex) { |
||
| 400 | app('log')->info($ex->getMessage()); |
||
| 401 | Bugsnag::notifyException($ex); |
||
| 402 | |||
| 403 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 404 | } |
||
| 405 | } |
||
| 406 | |||
| 407 | public function getAgents($agents, $productid, $plan) |
||
| 408 | { |
||
| 409 | if (!$agents) {//If agents is not received in the request in the case when |
||
| 410 | // 'modify agent' is not allowed for the Product,get the no of Agents from the Plan Table. |
||
| 411 | $planForAgent = Product::find($productid)->planRelation->find($plan); |
||
| 412 | if ($planForAgent) {//If Plan Exists For the Product ie not a Product without Plan |
||
| 413 | $noOfAgents = $planForAgent->planPrice->first()->no_of_agents; |
||
| 414 | $agents = $noOfAgents ? $noOfAgents : 0; //If no. of Agents is specified then that,else 0(Unlimited Agents) |
||
| 415 | } else { |
||
| 416 | $agents = 0; |
||
| 417 | } |
||
| 418 | } |
||
| 419 | |||
| 420 | return $agents; |
||
| 421 | } |
||
| 422 | |||
| 423 | public function getQuantity($qty, $productid, $plan) |
||
| 436 | } |
||
| 437 | |||
| 438 | public function pdf(Request $request) |
||
| 439 | { |
||
| 440 | try { |
||
| 441 | $id = $request->input('invoiceid'); |
||
| 442 | if (!$id) { |
||
| 443 | return redirect()->back()->with('fails', \Lang::get('message.no-invoice-id')); |
||
| 444 | } |
||
| 445 | $invoice = $this->invoice->where('id', $id)->first(); |
||
| 446 | if (!$invoice) { |
||
| 467 | } |
||
| 468 | } |
||
| 469 | |||
| 470 | public function doPayment($payment_method, $invoiceid, $amount, |
||
| 495 | } |
||
| 496 | } |
||
| 497 | } |
||
| 498 |