| Total Complexity | 45 |
| Total Lines | 300 |
| 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 = '') |
||
| 121 | } |
||
| 122 | |||
| 123 | public function name($name, $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) |
||
| 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) |
||
| 226 | } |
||
| 227 | |||
| 228 | public function updateInvoicePayment($invoiceid, $payment_method, $payment_status, $payment_date, $amount) |
||
| 229 | { |
||
| 230 | try { |
||
| 231 | $invoice = Invoice::find($invoiceid); |
||
| 232 | $invoice_status = 'pending'; |
||
| 233 | |||
| 234 | $payment = $this->payment->create([ |
||
| 235 | 'invoice_id' => $invoiceid, |
||
| 236 | 'user_id' => $invoice->user_id, |
||
| 237 | 'amount' => $amount, |
||
| 238 | 'payment_method' => $payment_method, |
||
| 239 | 'payment_status' => $payment_status, |
||
| 240 | 'created_at' => $payment_date, |
||
| 241 | ]); |
||
| 242 | $all_payments = $this->payment |
||
| 243 | ->where('invoice_id', $invoiceid) |
||
| 244 | ->where('payment_status', 'success') |
||
| 245 | ->pluck('amount')->toArray(); |
||
| 246 | $total_paid = array_sum($all_payments); |
||
| 247 | if ($total_paid >= $invoice->grand_total) { |
||
| 248 | $invoice_status = 'success'; |
||
| 249 | } |
||
| 250 | if ($invoice) { |
||
| 251 | $invoice->status = $invoice_status; |
||
| 252 | $invoice->save(); |
||
| 253 | } |
||
| 254 | |||
| 255 | return $payment; |
||
| 256 | } catch (\Exception $ex) { |
||
| 257 | Bugsnag::notifyException($ex); |
||
| 258 | |||
| 259 | throw new \Exception($ex->getMessage()); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Remove the specified resource from storage. |
||
| 265 | * |
||
| 266 | * @param int $id |
||
| 267 | * |
||
| 268 | * @return \Response |
||
| 269 | */ |
||
| 270 | public function destroy(Request $request) |
||
| 316 | </div>'; |
||
| 317 | } |
||
| 320 |