| Total Complexity | 46 | 
| Total Lines | 283 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like PaymentsAndInvoices 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 PaymentsAndInvoices, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 15 | trait PaymentsAndInvoices  | 
            ||
| 16 |     { | 
            ||
| 17 | /*  | 
            ||
| 18 | *Edit payment Total.  | 
            ||
| 19 | */  | 
            ||
| 20 | public function paymentTotalChange(Request $request)  | 
            ||
| 21 |         { | 
            ||
| 22 |             try { | 
            ||
| 23 | $invoice = new Invoice();  | 
            ||
| 24 |                 $total = $request->input('total'); | 
            ||
| 25 |                 if ($total == '') { | 
            ||
| 26 | $total = 0;  | 
            ||
| 27 | }  | 
            ||
| 28 |                 $paymentid = $request->input('id'); | 
            ||
| 29 |                 $creditAmtUserId = $this->payment->where('id', $paymentid)->value('user_id'); | 
            ||
| 30 |                 $creditAmt = $this->payment->where('user_id', $creditAmtUserId) | 
            ||
| 31 |                   ->where('invoice_id', '=', 0)->value('amt_to_credit'); | 
            ||
| 32 |                 $invoices = $invoice->where('user_id', $creditAmtUserId)->orderBy('created_at', 'desc')->get(); | 
            ||
| 33 | $cltCont = new \App\Http\Controllers\User\ClientController();  | 
            ||
| 34 | $invoiceSum = $cltCont->getTotalInvoice($invoices);  | 
            ||
| 35 |                 if ($total > $invoiceSum) { | 
            ||
| 36 | $diff = $total - $invoiceSum;  | 
            ||
| 37 | $creditAmt = $creditAmt + $diff;  | 
            ||
| 38 | $total = $invoiceSum;  | 
            ||
| 39 | }  | 
            ||
| 40 |                 $payment = $this->payment->where('id', $paymentid)->update(['amount'=>$total]); | 
            ||
| 41 | |||
| 42 |                 $creditAmtInvoiceId = $this->payment->where('user_id', $creditAmtUserId) | 
            ||
| 43 |         ->where('invoice_id', '!=', 0)->first(); | 
            ||
| 44 | $invoiceId = $creditAmtInvoiceId->invoice_id;  | 
            ||
| 45 |                 $invoice = $invoice->where('id', $invoiceId)->first(); | 
            ||
| 46 | $grand_total = $invoice->grand_total;  | 
            ||
| 47 | $diffSum = $grand_total - $total;  | 
            ||
| 48 | |||
| 49 | $finalAmt = $creditAmt + $diffSum;  | 
            ||
| 50 |                 $updatedAmt = $this->payment->where('user_id', $creditAmtUserId) | 
            ||
| 51 |         ->where('invoice_id', '=', 0)->update(['amt_to_credit'=>$creditAmt]); | 
            ||
| 52 |             } catch (\Exception $ex) { | 
            ||
| 53 |                 app('log')->info($ex->getMessage()); | 
            ||
| 54 | Bugsnag::notifyException($ex);  | 
            ||
| 55 | |||
| 56 |                 return redirect()->back()->with('fails', $ex->getMessage()); | 
            ||
| 57 | }  | 
            ||
| 58 | }  | 
            ||
| 59 | |||
| 60 | public function doPayment(  | 
            ||
| 61 | $payment_method,  | 
            ||
| 62 | $invoiceid,  | 
            ||
| 63 | $amount,  | 
            ||
| 64 | $parent_id = '',  | 
            ||
| 65 | $userid = '',  | 
            ||
| 66 | $payment_status = 'pending'  | 
            ||
| 67 |     ) { | 
            ||
| 68 |             try { | 
            ||
| 69 |                 if ($amount > 0) { | 
            ||
| 70 |                     if ($userid == '') { | 
            ||
| 71 | $userid = \Auth::user()->id;  | 
            ||
| 72 | }  | 
            ||
| 73 |                     if ($amount == 0) { | 
            ||
| 74 | $payment_status = 'success';  | 
            ||
| 75 | }  | 
            ||
| 76 | $this->payment->create([  | 
            ||
| 77 | 'parent_id' => $parent_id,  | 
            ||
| 78 | 'invoice_id' => $invoiceid,  | 
            ||
| 79 | 'user_id' => $userid,  | 
            ||
| 80 | 'amount' => $amount,  | 
            ||
| 81 | 'payment_method' => $payment_method,  | 
            ||
| 82 | 'payment_status' => $payment_status,  | 
            ||
| 83 | ]);  | 
            ||
| 84 | $this->updateInvoice($invoiceid);  | 
            ||
| 85 | }  | 
            ||
| 86 |             } catch (\Exception $ex) { | 
            ||
| 87 | Bugsnag::notifyException($ex);  | 
            ||
| 88 | |||
| 89 | throw new \Exception($ex->getMessage());  | 
            ||
| 90 | }  | 
            ||
| 91 | }  | 
            ||
| 92 | |||
| 93 | public function getAgents($agents, $productid, $plan)  | 
            ||
| 107 | }  | 
            ||
| 108 | |||
| 109 | public function getQuantity($qty, $productid, $plan)  | 
            ||
| 122 | }  | 
            ||
| 123 | |||
| 124 | public function updateInvoice($invoiceid)  | 
            ||
| 125 |         { | 
            ||
| 126 |             try { | 
            ||
| 127 | $invoice = $this->invoice->findOrFail($invoiceid);  | 
            ||
| 128 |                 foreach (\Cart::getConditionsByType('fee') as $value) { | 
            ||
| 129 | $invoice->processing_fee = $value->getValue();  | 
            ||
| 130 | }  | 
            ||
| 131 |                 $payment = $this->payment->where('invoice_id', $invoiceid) | 
            ||
| 132 |             ->where('payment_status', 'success')->pluck('amount')->toArray(); | 
            ||
| 133 | $total = array_sum($payment);  | 
            ||
| 134 |                 if ($total < $invoice->grand_total) { | 
            ||
| 135 | $invoice->status = 'pending';  | 
            ||
| 136 | }  | 
            ||
| 137 |                 if ($total >= $invoice->grand_total) { | 
            ||
| 138 | $invoice->status = 'success';  | 
            ||
| 139 | }  | 
            ||
| 140 |                 if ($total > $invoice->grand_total) { | 
            ||
| 141 | $user = $invoice->user()->first();  | 
            ||
| 142 | $balance = $total - $invoice->grand_total;  | 
            ||
| 143 | $user->debit = $balance;  | 
            ||
| 144 | $user->save();  | 
            ||
| 145 | }  | 
            ||
| 146 | |||
| 147 | $invoice->save();  | 
            ||
| 148 |             } catch (\Exception $ex) { | 
            ||
| 149 | Bugsnag::notifyException($ex);  | 
            ||
| 150 | |||
| 151 | throw new \Exception($ex->getMessage());  | 
            ||
| 152 | }  | 
            ||
| 153 | }  | 
            ||
| 154 | |||
| 155 | public function postRazorpayPayment($invoice)  | 
            ||
| 177 | }  | 
            ||
| 178 | }  | 
            ||
| 179 | |||
| 180 | public function sendmailClientAgent($userid, $invoiceid)  | 
            ||
| 181 |         { | 
            ||
| 182 |             try { | 
            ||
| 183 |                 $agent = \Input::get('agent'); | 
            ||
| 184 |                 $client = \Input::get('client'); | 
            ||
| 185 |                 if ($agent == 1) { | 
            ||
| 186 | $id = \Auth::user()->id;  | 
            ||
| 187 | $this->sendMail($id, $invoiceid);  | 
            ||
| 188 | }  | 
            ||
| 189 |                 if ($client == 1) { | 
            ||
| 190 | $this->sendMail($userid, $invoiceid);  | 
            ||
| 191 | }  | 
            ||
| 192 |             } catch (\Exception $ex) { | 
            ||
| 193 |                 app('log')->info($ex->getMessage()); | 
            ||
| 194 | Bugsnag::notifyException($ex);  | 
            ||
| 195 | |||
| 196 | throw new \Exception($ex->getMessage());  | 
            ||
| 197 | }  | 
            ||
| 198 | }  | 
            ||
| 199 | |||
| 200 | public function payment(Request $request)  | 
            ||
| 201 |         { | 
            ||
| 202 |             try { | 
            ||
| 203 |                 if ($request->has('invoiceid')) { | 
            ||
| 204 |                     $invoice_id = $request->input('invoiceid'); | 
            ||
| 205 | $invoice = $this->invoice->find($invoice_id);  | 
            ||
| 206 | $userid = $invoice->user_id;  | 
            ||
| 207 | //dd($invoice);  | 
            ||
| 208 | $invoice_status = '';  | 
            ||
| 209 | $payment_status = '';  | 
            ||
| 210 | $payment_method = '';  | 
            ||
| 211 | $domain = '';  | 
            ||
| 212 |                     if ($invoice) { | 
            ||
| 213 | $invoice_status = $invoice->status;  | 
            ||
| 214 | $items = $invoice->invoiceItem()->first();  | 
            ||
| 215 |                         if ($items) { | 
            ||
| 216 | $domain = $items->domain;  | 
            ||
| 217 | }  | 
            ||
| 218 | }  | 
            ||
| 219 |                     $payment = $this->payment->where('invoice_id', $invoice_id)->first(); | 
            ||
| 220 |                     if ($payment) { | 
            ||
| 221 | $payment_status = $payment->payment_status;  | 
            ||
| 222 | $payment_method = $payment->payment_method;  | 
            ||
| 223 | }  | 
            ||
| 224 | |||
| 225 | return view(  | 
            ||
| 226 | 'themes.default1.invoice.payment',  | 
            ||
| 227 | compact(  | 
            ||
| 228 | 'invoice_status',  | 
            ||
| 229 | 'payment_status',  | 
            ||
| 230 | 'payment_method',  | 
            ||
| 231 | 'invoice_id',  | 
            ||
| 232 | 'domain',  | 
            ||
| 233 | 'invoice',  | 
            ||
| 234 | 'userid'  | 
            ||
| 235 | )  | 
            ||
| 236 | );  | 
            ||
| 237 | }  | 
            ||
| 238 | |||
| 239 | return redirect()->back();  | 
            ||
| 240 |             } catch (\Exception $ex) { | 
            ||
| 241 | Bugsnag::notifyException($ex);  | 
            ||
| 242 | |||
| 243 |                 return redirect()->back()->with('fails', $ex->getMessage()); | 
            ||
| 244 | }  | 
            ||
| 245 | }  | 
            ||
| 246 | |||
| 247 | public function getExtraAmtPaid($userId)  | 
            ||
| 248 |         { | 
            ||
| 249 |             try { | 
            ||
| 250 |                 $amounts = Payment::where('user_id', $userId)->where('invoice_id', 0)->select('amt_to_credit')->get(); | 
            ||
| 251 | $balance = 0;  | 
            ||
| 252 |                 foreach ($amounts as $amount) { | 
            ||
| 253 |                     if ($amount) { | 
            ||
| 254 | $balance = $balance + $amount->amt_to_credit;  | 
            ||
| 255 | }  | 
            ||
| 256 | }  | 
            ||
| 257 | |||
| 258 | return $balance;  | 
            ||
| 259 |             } catch (\Exception $ex) { | 
            ||
| 260 |                 app('log')->info($ex->getMessage()); | 
            ||
| 261 | Bugsnag::notifyException($ex);  | 
            ||
| 262 | |||
| 263 |                 return redirect()->back()->with('fails', $ex->getMessage()); | 
            ||
| 264 | }  | 
            ||
| 265 | }  | 
            ||
| 266 | |||
| 267 | /**  | 
            ||
| 268 | * Get total of the Invoices for a User.  | 
            ||
| 269 | */  | 
            ||
| 270 | public function getTotalInvoice($invoices)  | 
            ||
| 271 |         { | 
            ||
| 272 | $sum = 0;  | 
            ||
| 273 |             foreach ($invoices as $invoice) { | 
            ||
| 274 | $sum = $sum + $invoice->grand_total;  | 
            ||
| 275 | }  | 
            ||
| 276 | |||
| 277 | return $sum;  | 
            ||
| 278 | }  | 
            ||
| 279 | |||
| 280 | public function getAmountPaid($userId)  | 
            ||
| 298 | }  | 
            ||
| 299 | }  | 
            ||
| 300 | }  | 
            ||
| 301 | 
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.