| Total Complexity | 43 |
| Total Lines | 155 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like BaseInvoiceController 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 BaseInvoiceController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class BaseInvoiceController extends Controller |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | *Tax When state is not empty. |
||
| 20 | */ |
||
| 21 | public function getTaxWhenState($user_state, $productid, $origin_state) |
||
| 22 | { |
||
| 23 | $cartController = new CartController(); |
||
| 24 | $c_gst = $user_state->c_gst; |
||
| 25 | $s_gst = $user_state->s_gst; |
||
| 26 | $i_gst = $user_state->i_gst; |
||
| 27 | $ut_gst = $user_state->ut_gst; |
||
| 28 | $state_code = $user_state->state_code; |
||
| 29 | if ($state_code == $origin_state) {//If user and origin state are same |
||
| 30 | $taxClassId = TaxClass::where('name', 'Intra State GST')->pluck('id')->toArray(); //Get the class Id of state |
||
| 31 | if ($taxClassId) { |
||
| 32 | $taxes = $cartController->getTaxByPriority($taxClassId); |
||
| 33 | $value = $cartController->getValueForSameState($productid, $c_gst, $s_gst, $taxClassId, $taxes); |
||
| 34 | } else { |
||
| 35 | $taxes = [0]; |
||
| 36 | } |
||
| 37 | } elseif ($state_code != $origin_state && $ut_gst == 'NULL') {//If user is from other state |
||
| 38 | |||
| 39 | $taxClassId = TaxClass::where('name', 'Inter State GST')->pluck('id')->toArray(); //Get the class Id of state |
||
| 40 | if ($taxClassId) { |
||
| 41 | $taxes = $cartController->getTaxByPriority($taxClassId); |
||
| 42 | $value = $cartController->getValueForOtherState($productid, $i_gst, $taxClassId, $taxes); |
||
| 43 | } else { |
||
| 44 | $taxes = [0]; |
||
| 45 | } |
||
| 46 | } elseif ($state_code != $origin_state && $ut_gst != 'NULL') {//if user from Union Territory |
||
| 47 | $taxClassId = TaxClass::where('name', 'Union Territory GST')->pluck('id')->toArray(); //Get the class Id of state |
||
| 48 | if ($taxClassId) { |
||
| 49 | $taxes = $cartController->getTaxByPriority($taxClassId); |
||
| 50 | $value = $cartController->getValueForUnionTerritory($productid, $c_gst, $ut_gst, $taxClassId, $taxes); |
||
| 51 | } else { |
||
| 52 | $taxes = [0]; |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | return ['taxes'=>$taxes, 'value'=>$value]; |
||
| 57 | } |
||
| 58 | |||
| 59 | |||
| 60 | /** |
||
| 61 | *Tax When from other Country. |
||
| 62 | */ |
||
| 63 | public function getTaxWhenOtherCountry($geoip_state, $geoip_country, $productid) |
||
| 64 | { |
||
| 65 | $cartController = new CartController(); |
||
| 66 | $taxClassId = Tax::where('state', $geoip_state)->orWhere('country', $geoip_country)->pluck('tax_classes_id')->first(); |
||
| 67 | $value = ''; |
||
| 68 | $rate = ''; |
||
| 69 | if ($taxClassId) { //if state equals the user State |
||
| 70 | |||
| 71 | $taxes = $cartController->getTaxByPriority($taxClassId); |
||
| 72 | |||
| 73 | // $taxes = $this->cartController::getTaxByPriority($taxClassId); |
||
| 74 | $value = $cartController->getValueForOthers($productid, $taxClassId, $taxes); |
||
| 75 | $rate = $value; |
||
| 76 | } else {//if Tax is selected for Any State Any Country |
||
| 77 | $taxClassId = Tax::where('country', '')->where('state', 'Any State')->pluck('tax_classes_id')->first(); |
||
| 78 | if ($taxClassId) { |
||
| 79 | $taxes = $cartController->getTaxByPriority($taxClassId); |
||
| 80 | $value = $cartController->getValueForOthers($productid, $taxClassId, $taxes); |
||
| 81 | $rate = $value; |
||
| 82 | } else { |
||
| 83 | $taxes = [0]; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | return ['taxes'=>$taxes, 'value'=>$value, 'rate'=>$rate]; |
||
| 88 | } |
||
| 89 | |||
| 90 | |||
| 91 | public function getExpiryStatus($start, $end, $now) |
||
| 92 | { |
||
| 93 | $whenDateNotSet = $this->whenDateNotSet($start, $end); |
||
| 94 | if ($whenDateNotSet) { |
||
| 95 | return $whenDateNotSet; |
||
| 96 | } |
||
| 97 | $whenStartDateSet = $this->whenStartDateSet($start, $end, $now); |
||
| 98 | if ($whenStartDateSet) { |
||
| 99 | return $whenStartDateSet; |
||
| 100 | } |
||
| 101 | $whenEndDateSet = $this->whenEndDateSet($start, $end, $now); |
||
| 102 | if ($whenEndDateSet) { |
||
| 103 | return $whenEndDateSet; |
||
| 104 | } |
||
| 105 | $whenBothAreSet = $this->whenBothSet($start, $end, $now); |
||
| 106 | if ($whenBothAreSet) { |
||
| 107 | return $whenBothAreSet; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | public function whenDateNotSet($start, $end) |
||
| 112 | { |
||
| 113 | //both not set, always true |
||
| 114 | if (($start == null || $start == '0000-00-00 00:00:00') && |
||
| 115 | ($end == null || $end == '0000-00-00 00:00:00')) { |
||
| 116 | return 'success'; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | public function whenStartDateSet($start, $end, $now) |
||
| 121 | { |
||
| 122 | //only starting date set, check the date is less or equel to today |
||
| 123 | if (($start != null || $start != '0000-00-00 00:00:00') |
||
| 124 | && ($end == null || $end == '0000-00-00 00:00:00')) { |
||
| 125 | if ($start <= $now) { |
||
| 126 | return 'success'; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | public function whenEndDateSet($start, $end, $now) |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | public function whenBothSet($start, $end, $now) |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | public function postPayment($invoiceid, Request $request) |
||
| 176 |