| Total Complexity | 56 |
| Total Lines | 387 |
| 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 | $this->invoice_from($from, $till, $join); |
||
| 104 | $this->till_date($till, $from, $join); |
||
| 105 | |||
| 106 | $join = $join->select('id', 'user_id', 'number', 'date', 'grand_total', 'currency', 'status', 'created_at'); |
||
| 107 | |||
| 108 | $join = $join->orderBy('created_at', 'desc') |
||
| 109 | ->select( |
||
| 110 | 'invoices.id', |
||
| 111 | 'first_name', |
||
| 112 | 'invoices.created_at', |
||
| 113 | 'invoices.currency', |
||
| 114 | 'user_id', |
||
| 115 | 'number', |
||
| 116 | 'status' |
||
| 117 | ); |
||
| 118 | |||
| 119 | return $join; |
||
| 120 | } |
||
| 121 | |||
| 122 | public function name($name, $join) |
||
| 123 | { |
||
| 124 | if ($name) { |
||
| 125 | $join = $join->where('first_name', $name); |
||
| 126 | |||
| 127 | return $join; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | public function invoice_no($invoice_no, $join) |
||
| 132 | { |
||
| 133 | if ($invoice_no) { |
||
| 134 | $join = $join->where('number', $invoice_no); |
||
| 135 | |||
| 136 | return $join; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | public function status($status, $join) |
||
| 141 | { |
||
| 142 | if ($status) { |
||
| 143 | $join = $join->where('status', $status); |
||
| 144 | |||
| 145 | return $join; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | public function currency($currency, $join) |
||
| 150 | { |
||
| 151 | if ($currency) { |
||
| 152 | $join = $join->where('invoices.currency', $currency); |
||
| 153 | |||
| 154 | return $join; |
||
| 155 | } |
||
| 156 | $return; |
||
| 157 | } |
||
| 158 | |||
| 159 | public function invoice_from($from, $till, $join) |
||
| 160 | { |
||
| 161 | if ($from) { |
||
| 162 | $fromdate = date_create($from); |
||
| 163 | $from = date_format($fromdate, 'Y-m-d H:m:i'); |
||
| 164 | $tills = date('Y-m-d H:m:i'); |
||
| 165 | $tillDate = $this->getTillDate($from, $till, $tills); |
||
| 166 | $join = $join->whereBetween('invoices.created_at', [$from, $tillDate]); |
||
| 167 | |||
| 168 | return $join; |
||
| 169 | } |
||
| 170 | |||
| 171 | return $join; |
||
| 172 | } |
||
| 173 | |||
| 174 | public function till_date($till, $from, $join) |
||
| 175 | { |
||
| 176 | if ($till) { |
||
| 177 | $tilldate = date_create($till); |
||
| 178 | $till = date_format($tilldate, 'Y-m-d H:m:i'); |
||
| 179 | $froms = Invoice::first()->created_at; |
||
| 180 | $fromDate = $this->getFromDate($from, $froms); |
||
| 181 | $join = $join->whereBetween('invoices.created_at', [$fromDate, $till]); |
||
| 182 | |||
| 183 | return $join; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | public function getExpiryStatus($start, $end, $now) |
||
| 188 | { |
||
| 189 | $whenDateNotSet = $this->whenDateNotSet($start, $end); |
||
| 190 | if ($whenDateNotSet) { |
||
| 191 | return $whenDateNotSet; |
||
| 192 | } |
||
| 193 | $whenStartDateSet = $this->whenStartDateSet($start, $end, $now); |
||
| 194 | if ($whenStartDateSet) { |
||
| 195 | return $whenStartDateSet; |
||
| 196 | } |
||
| 197 | $whenEndDateSet = $this->whenEndDateSet($start, $end, $now); |
||
| 198 | if ($whenEndDateSet) { |
||
| 199 | return $whenEndDateSet; |
||
| 200 | } |
||
| 201 | $whenBothAreSet = $this->whenBothSet($start, $end, $now); |
||
| 202 | if ($whenBothAreSet) { |
||
| 203 | return $whenBothAreSet; |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | public function getTillDate($from, $till, $tills) |
||
| 208 | { |
||
| 209 | if ($till) { |
||
| 210 | $todate = date_create($till); |
||
| 211 | $tills = date_format($todate, 'Y-m-d H:m:i'); |
||
| 212 | } |
||
| 213 | |||
| 214 | return $tills; |
||
| 215 | } |
||
| 216 | |||
| 217 | public function getFromDate($from, $froms) |
||
| 218 | { |
||
| 219 | if ($from) { |
||
| 220 | $fromdate = date_create($from); |
||
| 221 | $froms = date_format($fromdate, 'Y-m-d H:m:i'); |
||
| 222 | } |
||
| 223 | |||
| 224 | return $froms; |
||
| 225 | } |
||
| 226 | |||
| 227 | public function postRazorpayPayment($invoiceid, $grand_total) |
||
| 228 | { |
||
| 229 | try { |
||
| 230 | $payment_method = \Session::get('payment_method'); |
||
| 231 | $payment_status = 'success'; |
||
| 232 | $payment_date = \Carbon\Carbon::now()->toDateTimeString(); |
||
| 233 | $amount = $grand_total; |
||
| 234 | $paymentRenewal = $this->updateInvoicePayment($invoiceid, $payment_method, |
||
| 235 | $payment_status, $payment_date, $amount); |
||
| 236 | |||
| 237 | return redirect()->back()->with('success', 'Payment Accepted Successfully'); |
||
| 238 | } catch (\Exception $ex) { |
||
| 239 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | public function updateInvoicePayment($invoiceid, $payment_method, $payment_status, $payment_date, $amount) |
||
| 244 | { |
||
| 245 | try { |
||
| 246 | $invoice = Invoice::find($invoiceid); |
||
| 247 | $invoice_status = 'pending'; |
||
| 248 | |||
| 249 | $payment = $this->payment->create([ |
||
| 250 | 'invoice_id' => $invoiceid, |
||
| 251 | 'user_id' => $invoice->user_id, |
||
| 252 | 'amount' => $amount, |
||
| 253 | 'payment_method' => $payment_method, |
||
| 254 | 'payment_status' => $payment_status, |
||
| 255 | 'created_at' => $payment_date, |
||
| 256 | ]); |
||
| 257 | $all_payments = $this->payment |
||
| 258 | ->where('invoice_id', $invoiceid) |
||
| 259 | ->where('payment_status', 'success') |
||
| 260 | ->pluck('amount')->toArray(); |
||
| 261 | $total_paid = array_sum($all_payments); |
||
| 262 | if ($total_paid >= $invoice->grand_total) { |
||
| 263 | $invoice_status = 'success'; |
||
| 264 | } |
||
| 265 | if ($invoice) { |
||
| 266 | $invoice->status = $invoice_status; |
||
| 267 | $invoice->save(); |
||
| 268 | } |
||
| 269 | |||
| 270 | return $payment; |
||
| 271 | } catch (\Exception $ex) { |
||
| 272 | Bugsnag::notifyException($ex); |
||
| 273 | |||
| 274 | throw new \Exception($ex->getMessage()); |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | public function updateInvoice($invoiceid) |
||
| 303 | } |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Remove the specified resource from storage. |
||
| 308 | * |
||
| 309 | * @param int $id |
||
| 310 | * |
||
| 311 | * @return \Response |
||
| 312 | */ |
||
| 313 | public function destroy(Request $request) |
||
| 359 | </div>'; |
||
| 360 | } |
||
| 361 | } |
||
| 362 | |||
| 363 | /* |
||
| 364 | *Edit payment Total. |
||
| 365 | */ |
||
| 366 | public function paymentTotalChange(Request $request) |
||
| 403 | } |
||
| 404 | } |
||
| 405 | } |
||
| 406 |