| Total Complexity | 54 |
| Total Lines | 366 |
| 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 |
||
| 17 | trait CoupCodeAndInvoiceSearch |
||
| 18 | { |
||
| 19 | public function checkCode($code, $productid, $currency) |
||
| 20 | { |
||
| 21 | try { |
||
| 22 | if ($code != '') { |
||
| 23 | $promo = $this->promotion->where('code', $code)->first(); |
||
| 24 | //check promotion code is valid |
||
| 25 | if (!$promo) { |
||
| 26 | throw new \Exception(\Lang::get('message.no-such-code')); |
||
| 27 | } |
||
| 28 | $relation = $promo->relation()->get(); |
||
| 29 | //check the relation between code and product |
||
| 30 | if (count($relation) == 0) { |
||
| 31 | throw new \Exception(\Lang::get('message.no-product-related-to-this-code')); |
||
| 32 | } |
||
| 33 | //check the usess |
||
| 34 | $cont = new \App\Http\Controllers\Payment\PromotionController(); |
||
| 35 | $uses = $cont->checkNumberOfUses($code); |
||
| 36 | if ($uses != 'success') { |
||
| 37 | throw new \Exception(\Lang::get('message.usage-of-code-completed')); |
||
| 38 | } |
||
| 39 | //check for the expiry date |
||
| 40 | $expiry = $this->checkExpiry($code); |
||
|
|
|||
| 41 | if ($expiry != 'success') { |
||
| 42 | throw new \Exception(\Lang::get('message.usage-of-code-expired')); |
||
| 43 | } |
||
| 44 | $value = $this->findCostAfterDiscount($promo->id, $productid, $currency); |
||
| 45 | |||
| 46 | return $value; |
||
| 47 | } else { |
||
| 48 | $product = $this->product->find($productid); |
||
| 49 | $plans = Plan::where('product', $product)->pluck('id')->first(); |
||
| 50 | $price = PlanPrice::where('currency', $currency)->where('plan_id', $plans)->pluck('add_price')->first(); |
||
| 51 | |||
| 52 | return $price; |
||
| 53 | } |
||
| 54 | } catch (\Exception $ex) { |
||
| 55 | throw new \Exception($ex->getMessage()); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | public function findCostAfterDiscount($promoid, $productid, $currency) |
||
| 60 | { |
||
| 61 | try { |
||
| 62 | $promotion = Promotion::findOrFail($promoid); |
||
| 63 | $product = Product::findOrFail($productid); |
||
| 64 | $promotion_type = $promotion->type; |
||
| 65 | $promotion_value = $promotion->value; |
||
| 66 | $planId = Plan::where('product', $productid)->pluck('id')->first(); |
||
| 67 | // dd($planId); |
||
| 68 | $product_price = PlanPrice::where('plan_id', $planId) |
||
| 69 | ->where('currency', $currency)->pluck('add_price')->first(); |
||
| 70 | $updated_price = $this->findCost($promotion_type, $promotion_value, $product_price, $productid); |
||
| 71 | |||
| 72 | return $updated_price; |
||
| 73 | } catch (\Exception $ex) { |
||
| 74 | Bugsnag::notifyException($ex); |
||
| 75 | |||
| 76 | throw new \Exception(\Lang::get('message.find-discount-error')); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | public function findCost($type, $value, $price, $productid) |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | public function advanceSearch($name = '', $invoice_no = '', $currency = '', $status = '', $from = '', $till = '') |
||
| 98 | { |
||
| 99 | $join = Invoice::leftJoin('users', 'invoices.user_id', '=', 'users.id'); |
||
| 100 | $this->name($name, $join); |
||
| 101 | $this->invoice_no($invoice_no, $join); |
||
| 102 | $this->status($status, $join); |
||
| 103 | $this->searchcurrency($currency, $join); |
||
| 104 | |||
| 105 | $this->invoice_from($from, $till, $join); |
||
| 106 | $this->till_date($till, $from, $join); |
||
| 107 | |||
| 108 | $join = $join->select('id', 'user_id', 'number', 'date', 'grand_total', 'currency', 'status', 'created_at'); |
||
| 109 | |||
| 110 | $join = $join->orderBy('created_at', 'desc') |
||
| 111 | ->select( |
||
| 112 | 'invoices.id', |
||
| 113 | 'first_name', |
||
| 114 | 'invoices.created_at', |
||
| 115 | 'invoices.currency', |
||
| 116 | 'user_id', |
||
| 117 | 'invoices.grand_total', |
||
| 118 | 'number', |
||
| 119 | 'status' |
||
| 120 | ); |
||
| 121 | |||
| 122 | return $join; |
||
| 123 | } |
||
| 124 | |||
| 125 | public function name($name, $join) |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | public function invoice_no($invoice_no, $join) |
||
| 135 | { |
||
| 136 | if ($invoice_no) { |
||
| 137 | $join = $join->where('number', $invoice_no); |
||
| 138 | |||
| 139 | return $join; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | public function status($status, $join) |
||
| 144 | { |
||
| 145 | if ($status) { |
||
| 146 | $join = $join->where('status', $status); |
||
| 147 | |||
| 148 | return $join; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | public function searchcurrency($currency, $join) |
||
| 153 | { |
||
| 154 | if ($currency) { |
||
| 155 | $join = $join->where('invoices.currency', $currency); |
||
| 156 | |||
| 157 | return $join; |
||
| 158 | } |
||
| 159 | $return; |
||
| 160 | } |
||
| 161 | |||
| 162 | public function invoice_from($from, $till, $join) |
||
| 163 | { |
||
| 164 | if ($from) { |
||
| 165 | $fromdate = date_create($from); |
||
| 166 | $from = date_format($fromdate, 'Y-m-d H:m:i'); |
||
| 167 | $tills = date('Y-m-d H:m:i'); |
||
| 168 | $tillDate = $this->getTillDate($from, $till, $tills); |
||
| 169 | $join = $join->whereBetween('invoices.created_at', [$from, $tillDate]); |
||
| 170 | |||
| 171 | return $join; |
||
| 172 | } |
||
| 173 | |||
| 174 | return $join; |
||
| 175 | } |
||
| 176 | |||
| 177 | public function till_date($till, $from, $join) |
||
| 178 | { |
||
| 179 | if ($till) { |
||
| 180 | $tilldate = date_create($till); |
||
| 181 | $till = date_format($tilldate, 'Y-m-d H:m:i'); |
||
| 182 | $froms = Invoice::first()->created_at; |
||
| 183 | $fromDate = $this->getFromDate($from, $froms); |
||
| 184 | $join = $join->whereBetween('invoices.created_at', [$fromDate, $till]); |
||
| 185 | |||
| 186 | return $join; |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | public function getExpiryStatus($start, $end, $now) |
||
| 191 | { |
||
| 192 | $whenDateNotSet = $this->whenDateNotSet($start, $end); |
||
| 193 | if ($whenDateNotSet) { |
||
| 194 | return $whenDateNotSet; |
||
| 195 | } |
||
| 196 | $whenStartDateSet = $this->whenStartDateSet($start, $end, $now); |
||
| 197 | if ($whenStartDateSet) { |
||
| 198 | return $whenStartDateSet; |
||
| 199 | } |
||
| 200 | $whenEndDateSet = $this->whenEndDateSet($start, $end, $now); |
||
| 201 | if ($whenEndDateSet) { |
||
| 202 | return $whenEndDateSet; |
||
| 203 | } |
||
| 204 | $whenBothAreSet = $this->whenBothSet($start, $end, $now); |
||
| 205 | if ($whenBothAreSet) { |
||
| 206 | return $whenBothAreSet; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | public function getTillDate($from, $till, $tills) |
||
| 211 | { |
||
| 212 | if ($till) { |
||
| 213 | $todate = date_create($till); |
||
| 214 | $tills = date_format($todate, 'Y-m-d H:m:i'); |
||
| 215 | } |
||
| 216 | |||
| 217 | return $tills; |
||
| 218 | } |
||
| 219 | |||
| 220 | public function getFromDate($from, $froms) |
||
| 221 | { |
||
| 222 | if ($from) { |
||
| 223 | $fromdate = date_create($from); |
||
| 224 | $froms = date_format($fromdate, 'Y-m-d H:m:i'); |
||
| 225 | } |
||
| 226 | |||
| 227 | return $froms; |
||
| 228 | } |
||
| 229 | |||
| 230 | public function updateInvoicePayment($invoiceid, $payment_method, $payment_status, $payment_date, $amount) |
||
| 231 | { |
||
| 232 | try { |
||
| 233 | $invoice = Invoice::find($invoiceid); |
||
| 234 | $invoice_status = 'pending'; |
||
| 235 | |||
| 236 | $payment = $this->payment->create([ |
||
| 237 | 'invoice_id' => $invoiceid, |
||
| 238 | 'user_id' => $invoice->user_id, |
||
| 239 | 'amount' => $amount, |
||
| 240 | 'payment_method' => $payment_method, |
||
| 241 | 'payment_status' => $payment_status, |
||
| 242 | 'created_at' => $payment_date, |
||
| 243 | ]); |
||
| 244 | $all_payments = $this->payment |
||
| 245 | ->where('invoice_id', $invoiceid) |
||
| 246 | ->where('payment_status', 'success') |
||
| 247 | ->pluck('amount')->toArray(); |
||
| 248 | $total_paid = array_sum($all_payments); |
||
| 249 | if ($total_paid >= $invoice->grand_total) { |
||
| 250 | $invoice_status = 'success'; |
||
| 251 | } |
||
| 252 | if ($invoice) { |
||
| 253 | $invoice->status = $invoice_status; |
||
| 254 | $invoice->save(); |
||
| 255 | } |
||
| 256 | |||
| 257 | return $payment; |
||
| 258 | } catch (\Exception $ex) { |
||
| 259 | Bugsnag::notifyException($ex); |
||
| 260 | |||
| 261 | throw new \Exception($ex->getMessage()); |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Remove the specified resource from storage. |
||
| 267 | * |
||
| 268 | * @param int $id |
||
| 269 | * |
||
| 270 | * @return \Response |
||
| 271 | */ |
||
| 272 | public function destroy(Request $request) |
||
| 318 | </div>'; |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | public function deletePayment(Request $request) |
||
| 373 | </div>'; |
||
| 374 | } |
||
| 375 | } |
||
| 376 | |||
| 377 | public function getCodeValue($promo,$code) |
||
| 378 | { |
||
| 379 | if($promo && $code) { |
||
| 385 |